diff --git a/testdata/go/memcached-operator/bin/manager b/testdata/go/memcached-operator/bin/manager new file mode 100755 index 0000000000..fb231a3f24 Binary files /dev/null and b/testdata/go/memcached-operator/bin/manager differ diff --git a/website/content/en/docs/building-operators/golang/advanced-topics.md b/website/content/en/docs/building-operators/golang/advanced-topics.md index 64f0c21a6a..0e9528e1d7 100644 --- a/website/content/en/docs/building-operators/golang/advanced-topics.md +++ b/website/content/en/docs/building-operators/golang/advanced-topics.md @@ -1,7 +1,7 @@ --- title: Advanced Topics linkTitle: Advanced Topics -weight: 70 +weight: 80 --- ### Manage CR status conditions diff --git a/website/content/en/docs/building-operators/golang/migration.md b/website/content/en/docs/building-operators/golang/migration.md index f7d64d9e37..f09dbfad07 100644 --- a/website/content/en/docs/building-operators/golang/migration.md +++ b/website/content/en/docs/building-operators/golang/migration.md @@ -230,11 +230,11 @@ test: generate fmt vet manifests ## Migrate your tests -For the new layout, you will see that `controllers/suite_test.go` is created. This file contains boilerplate for executing integration tests using [envtest][envtest] with [ginkgo](https://onsi.github.io/ginkgo/) and [gomega](https://onsi.github.io/gomega/). +For the new layout, you will see that `controllers/suite_test.go` is created when a controller is scaffolded by the tool. This file contains boilerplate for executing integration tests using [envtest][envtest] with [ginkgo](https://onsi.github.io/ginkgo/) and [gomega][gomega]. Operator SDK 1.0.0+ removes support for the legacy test framework and no longer supports the `operator-sdk test` subcommand. All affected tests should be migrated to use `envtest`. -The Operator SDK project recommends controller-runtime's [envtest][envtest] because it has a more active contributor community, it has become more mature than Operator SDK's test framework, and it does not require an actual cluster to run tests, which can be a huge benefit in CI scenarios. +The Operator SDK project recommends using controller-runtime's [envtest][envtest] to write tests for your Operators projects. Envtest has a more active contributor community, it is more mature than Operator SDK's test framework, and it does not require an actual cluster to run tests which can be a huge benefit in CI scenarios. To learn more about how you can test your controllers, see the documentation about [writing controller tests][writing-controller-tests]. @@ -371,4 +371,5 @@ E.g `kubectl logs deployment.apps/memcached-operator-controller-manager -n memca [migration-guide-main-section]: /docs/building-operators/golang/migration/#migrate-maingo [kustomize]: https://github.com/kubernetes-sigs/kustomize [ctrl-options]: https://godoc.org/sigs.k8s.io/controller-runtime/pkg/manager#Options -[envtest]: https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest \ No newline at end of file +[envtest]: https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest +[gomega]: https://onsi.github.io/gomega/ \ No newline at end of file diff --git a/website/content/en/docs/building-operators/golang/references/envtest-setup.md b/website/content/en/docs/building-operators/golang/references/envtest-setup.md index 731180ccf2..a1a0c4dec0 100644 --- a/website/content/en/docs/building-operators/golang/references/envtest-setup.md +++ b/website/content/en/docs/building-operators/golang/references/envtest-setup.md @@ -19,5 +19,5 @@ test: generate fmt vet manifests If using `git`, it is recommended to add `testbin/*` to your `.gitignore` file to avoid committing these binaries. [envtest]: https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest -[controller-test]: https://book.kubebuilder.io/reference/writing-tests.html +[controller-test]: https://book.kubebuilder.io/cronjob-tutorial/writing-tests.html [script]: https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh diff --git a/website/content/en/docs/building-operators/golang/testing.md b/website/content/en/docs/building-operators/golang/testing.md new file mode 100644 index 0000000000..6c8cd1bdf3 --- /dev/null +++ b/website/content/en/docs/building-operators/golang/testing.md @@ -0,0 +1,40 @@ +--- +title: Testing your Operator project +linkTitle: Testing with EnvTest +description: Learn how to ensure the quality of your Operator project +weight: 70 +--- + +## Overview + +The Operator SDK project recommends using controller-runtime's [envtest][envtest] to write tests for your Operators projects. Envtest has a more active contributor community, it is more mature than Operator SDK's test framework, and it does not require an actual cluster to run tests which can be a huge benefit in CI scenarios. + +## Using EnvTest + +You will see that `controllers/suite_test.go` is created when a controller is scaffolded by the tool. This file contains boilerplate for executing integration tests using [envtest][envtest] with [ginkgo](https://onsi.github.io/ginkgo/) and [gomega][gomega]. + + +It means that you will implement your controller's tests in go using this stack and it will be called by the `suite_test.go` via go tool such as: + +```shell +go test controllers/ -v -ginkgo.v +``` + +The projects generated by using the SDK tool have a Makefile which contains the target tests which executes when you run `make test`. Note that this target will also execute when you run `make docker-build IMG=/:`. + +Operator SDK adopted this stack to write tests for its operators. It might be useful to check [writing controller tests][writing-controller-tests] documentation and examples to learn how to better write tests for your operator. See, for example, that [controller-runtime](https://github.com/kubernetes-sigs/controller-runtime) is covered by tests using the same stack as well. + +## Other Options + +Also you can write tests for your operator in a declarative using the [kuttl][kuttl]. Via kuttl, you can define YAML manifests that specify the expected before and after statues of a cluster when your operator is used. For more info see [Writing Kuttl Scorecard Tests][writing-kuttl-scorecard-tests]. + +To implement application-specific tests, the SDK's test harness, [scorecard][scorecard], provides the ability to ship custom code in container images as well, which can be referenced in the test suite. Because this test suite definition metadata travels with the Operator Bundle, it allows for functional testing of the Operator without the source code or the project layout being available. See [Writing Custom Scorecard Tests][writing-custom-scorecard-tests]. + +[envtest]: https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest +[writing-controller-tests]: https://book.kubebuilder.io/cronjob-tutorial/writing-tests.html +[envtest-setup]: /docs/building-operators/golang/references/envtest-setup +[writing-kuttl-scorecard-tests]: /docs/advanced-topics/scorecard/kuttl-tests +[writing-custom-scorecard-tests]: /docs/advanced-topics/scorecard/custom-tests +[scorecard]: /docs/advanced-topics/scorecard/scorecard +[gomega]: https://onsi.github.io/gomega/ +[kuttl]: https://kuttl.dev/ \ No newline at end of file