You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/book/src/cronjob-tutorial/writing-tests.md
+32-3Lines changed: 32 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,11 +23,40 @@ This Status update example above demonstrates a general testing strategy for a c
23
23
* Writing stubs for creating test objects
24
24
* Isolating changes to an object to test specific controller behavior
25
25
26
+
# Writing controller tests
27
+
28
+
Testing Kubernetes controllers is a big subject, and the boilerplate testing
29
+
files generated for you by kubebuilder are fairly minimal.
30
+
31
+
To walk you through integration testing patterns for Kubebuilder-generated controllers, we will revisit the CronJob we built in our first tutorial and write a simple test for it.
32
+
33
+
The basic approach is that, in your generated `suite_test.go` file, you will use envtest to create a local Kubernetes API server, instantiate and run your controllers, and then write additional `*_test.go` files to test it using [Ginkgo](http://onsi.github.io/ginkgo).
34
+
35
+
If you want to tinker with how your envtest cluster is configured, see section [Configuring envtest for integration tests](../reference/envtest.md) as well as the [`envtest docs`](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest?tab=doc).
36
+
26
37
<asideclass="note">
27
-
<h1>Examples</h1>
38
+
<h1>Other Examples</h1>
28
39
29
-
You can use the plugin [DeployImage](../plugins/available/deploy-image-plugin-v1-alpha.md) to check examples. This plugin allows users to scaffold API/Controllers to deploy and manage an Operand (image) on the cluster following the guidelines and best practices. It abstracts the complexities of achieving this goal while allowing users to customize the generated code.
40
+
If you would like to see additional examples of controller tests generated by Kubebuilder, take a look at the <ahref="../plugins/available/deploy-image-plugin-v1-alpha.md">Deploy Image plugin (deploy-image/v1-alpha)</a>.
41
+
This plugin scaffolds APIs and controllers for managing an Operand (container image) on a cluster, following recommended patterns.
30
42
31
-
Therefore, you can check that a test using ENV TEST will be generated for the controller which has the purpose to ensure that the Deployment is created successfully. You can see an example of its code implementation under the `testdata` directory with the [DeployImage](../plugins/available/deploy-image-plugin-v1-alpha.md) samples [here](https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-plugins/internal/controller/busybox_controller_test.go).
43
+
The plugin abstracts much of the complexity of managing Deployments while still allowing you to customize the generated code.
44
+
When enabled, it also generates a controller test that uses <ahref="../reference/envtest.md">ENV TEST</a> to verify that the expected Deployment is created.
32
45
46
+
An example of this test can be found under the `testdata` directory in the Kubebuilder repository:
This Status update example above demonstrates a general testing strategy for a custom Kind with downstream objects. By this point, you hopefully have learned the following methods for testing your controller behavior:
59
+
60
+
* Setting up your controller to run on an envtest cluster
61
+
* Writing stubs for creating test objects
62
+
* Isolating changes to an object to test specific controller behavior
0 commit comments