Add Kubernetes Controller Tests with `envtest` from `controller-runtime`
Created by: janiskemper
/kind feature
Description:
We propose the introduction of controller tests using the envtest
package provided by controller-runtime
for our Kubernetes operator. This testing environment will facilitate the simulation of a real Kubernetes API server and etcd, enabling comprehensive and realistic tests for our controller's behavior.
envtest
:
Some arguments for using -
Realistic Environment Simulation: Unlike mock-based testing,
envtest
sets up an actual Kubernetes API server and etcd, which ensures that our tests mimic real-world scenarios more closely. -
CRD Integration: With
envtest
, we can easily test the installation of our Custom Resource Definitions (CRDs) and ensure that they function as expected with the controller. This is especially helpful for our operator as it introduces and manages custom resources. -
Event-Driven Behavior Verification: Our operator reacts to Kubernetes events. With
envtest
, we can create, update, or delete resources and ensure that our controller responds correctly to these events. -
Ease of Setup:
envtest
provides utilities for starting and stopping the Kubernetes API server and etcd, making the setup and teardown of tests straightforward. -
Easier Debugging and Error Tracing: Since
envtest
provides a near-real environment, any issues or errors in our controller logic will manifest similarly to how they would in a real Kubernetes cluster, making debugging more intuitive.
Acceptance Criteria:
-
Add envtest in Go code -
Implement tests that use envtest. -
Have Make target to easily run the tests. -
The test setup is encapsulated and doesn't need external dependencies. -
Run the tests in the CI with Github Action.