Skip to content

Commit

Permalink
šŸ› (go/v4) ensure that schemas are added before start EnvTest-based suā€¦
Browse files Browse the repository at this point in the history
ā€¦ite tests for webhooks and controllers (kubernetes-sigs#4466)

fix: add schemas prior to envtest start

Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 authored Jan 2, 2025
1 parent 9c36b61 commit f051464
Show file tree
Hide file tree
Showing 30 changed files with 270 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
/*
The CronJob Kind is added to the runtime scheme used by the test environment.
This ensures that the CronJob API is registered with the scheme, allowing the test controller to recognize and interact with CronJob resources.
*/
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
/*
After the schemas, you will see the following marker.
This marker is what allows new schemas to be added here automatically when a new API is added to the project.
*/

// +kubebuilder:scaffold:scheme

/*
The envtest environment is configured to load Custom Resource Definitions (CRDs) from the specified directory.
This setup enables the test environment to recognize and interact with the custom resources defined by these CRDs.
Expand All @@ -92,25 +106,11 @@ var _ = BeforeSuite(func() {
Then, we start the envtest cluster.
*/

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

/*
The CronJob Kind is added to the runtime scheme used by the test environment.
This ensures that the CronJob API is registered with the scheme, allowing the test controller to recognize and interact with CronJob resources.
*/
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
/*
After the schemas, you will see the following marker.
This marker is what allows new schemas to be added here automatically when a new API is added to the project.
*/

// +kubebuilder:scaffold:scheme

/*
A client is created for our test CRUD operations.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
scheme := apimachineryruntime.NewScheme()
err = batchv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
Expand All @@ -81,21 +91,11 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := apimachineryruntime.NewScheme()
err = batchv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
err = cachev1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
Expand All @@ -69,17 +75,11 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = cachev1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
/*
The CronJob Kind is added to the runtime scheme used by the test environment.
This ensures that the CronJob API is registered with the scheme, allowing the test controller to recognize and interact with CronJob resources.
*/
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
/*
After the schemas, you will see the following marker.
This marker is what allows new schemas to be added here automatically when a new API is added to the project.
*/

// +kubebuilder:scaffold:scheme

/*
The envtest environment is configured to load Custom Resource Definitions (CRDs) from the specified directory.
This setup enables the test environment to recognize and interact with the custom resources defined by these CRDs.
Expand All @@ -92,25 +106,11 @@ var _ = BeforeSuite(func() {
Then, we start the envtest cluster.
*/

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

/*
The CronJob Kind is added to the runtime scheme used by the test environment.
This ensures that the CronJob API is registered with the scheme, allowing the test controller to recognize and interact with CronJob resources.
*/
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
/*
After the schemas, you will see the following marker.
This marker is what allows new schemas to be added here automatically when a new API is added to the project.
*/

// +kubebuilder:scaffold:scheme

/*
A client is created for our test CRUD operations.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
scheme := apimachineryruntime.NewScheme()
err = batchv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
Expand All @@ -81,21 +91,11 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := apimachineryruntime.NewScheme()
err = batchv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
scheme := apimachineryruntime.NewScheme()
err = batchv2.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
Expand All @@ -81,21 +91,11 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := apimachineryruntime.NewScheme()
err = batchv2.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
23 changes: 13 additions & 10 deletions hack/docs/internal/cronjob-tutorial/generate_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,15 @@ var (
`, suiteTestEnv)
hackutils.CheckError("updating suite_test.go to add more variables", err)

err = pluginutil.InsertCode(
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`ctx, cancel = context.WithCancel(context.TODO())
`, suiteTestReadCRD)
hackutils.CheckError("updating suite_test.go to add text about CRD", err)
`
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
// +kubebuilder:scaffold:scheme
`, suiteTestAddSchema)
hackutils.CheckError("updating suite_test.go to add schema", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
Expand All @@ -555,15 +559,14 @@ var (
*/`)
hackutils.CheckError("updating suite_test.go to add text to show where envtest cluster start", err)

err = pluginutil.ReplaceInFile(
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
err = batchv1.AddToScheme(scheme.Scheme)
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
// +kubebuilder:scaffold:scheme
`, suiteTestAddSchema)
hackutils.CheckError("updating suite_test.go to add schema", err)
Expect(cfg).NotTo(BeNil())
`, suiteTestClient)
hackutils.CheckError("updating suite_test.go to add text about test client", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
Expand Down
12 changes: 6 additions & 6 deletions hack/docs/internal/cronjob-tutorial/writing_tests_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ var (
)
`

const suiteTestReadCRD = `
/*
The envtest environment is configured to load Custom Resource Definitions (CRDs) from the specified directory.
This setup enables the test environment to recognize and interact with the custom resources defined by these CRDs.
*/`

const suiteTestAddSchema = `
/*
The CronJob Kind is added to the runtime scheme used by the test environment.
Expand All @@ -63,6 +57,12 @@ const suiteTestAddSchema = `
// +kubebuilder:scaffold:scheme
/*
The envtest environment is configured to load Custom Resource Definitions (CRDs) from the specified directory.
This setup enables the test environment to recognize and interact with the custom resources defined by these CRDs.
*/`

const suiteTestClient = `
/*
A client is created for our test CRUD operations.
*/`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ var _ = BeforeSuite(func() {
ctx, cancel = context.WithCancel(context.TODO())
var err error
%s
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")},
Expand All @@ -182,14 +185,11 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}
var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
%s
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ var _ = BeforeSuite(func() {
ctx, cancel = context.WithCancel(context.TODO())
var err error
scheme := apimachineryruntime.NewScheme()
err = %s.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
err = %s.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
%s
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "crd", "bases")},
Expand All @@ -245,21 +255,11 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}
var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
scheme := apimachineryruntime.NewScheme()
err = %s.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
err = %s.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
%s
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
Loading

0 comments on commit f051464

Please sign in to comment.