Skip to content

Commit 5a51775

Browse files
authored
Merge pull request #5230 from camilamacedo86/fix-note-writing-test
📖 (doc): Improve note about other examples for the tests
2 parents 18ae126 + bbf5c93 commit 5a51775

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

docs/book/src/cronjob-tutorial/writing-tests.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,40 @@ This Status update example above demonstrates a general testing strategy for a c
2323
* Writing stubs for creating test objects
2424
* Isolating changes to an object to test specific controller behavior
2525

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+
2637
<aside class="note">
27-
<h1>Examples</h1>
38+
<h1>Other Examples</h1>
2839

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 <a href="../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.
3042

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 <a href="../reference/envtest.md">ENV TEST</a> to verify that the expected Deployment is created.
3245

46+
An example of this test can be found under the `testdata` directory in the Kubebuilder repository:
47+
<a href="https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-plugins/internal/controller/busybox_controller_test.go">testdata/project-v4-with-plugins/internal/controller/busybox_controller_test.go</a>.
3348
</aside>
49+
50+
## Test Environment Setup
51+
52+
{{#literatego ../cronjob-tutorial/testdata/project/internal/controller/suite_test.go}}
53+
54+
## Testing your Controller's Behavior
55+
56+
{{#literatego ../cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go}}
57+
58+
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

Comments
 (0)