Skip to main content

Kubebuilder Testing

Kubebuilder Testing

Testing Kubernetes controllers with in-memory API server

The Control Plane uses Kubebuilder's testing framework to validate controller behavior using an in-memory Kubernetes API server.

Controller testing

Testing Kubernetes controllers requires a special approach since they interact with the Kubernetes API server. Kubebuilder provides tools to set up an in-memory API server for efficient and reliable testing.

Overviewโ€‹

Kubebuilder's testing framework, built on the controller-runtime library's envtest package, allows you to test controllers without a real Kubernetes cluster. This approach offers several advantages:

Kubebuilder Testing Logo

Why Kubebuilder Testing?โ€‹

๐Ÿš€ Fast Execution

Tests run quickly without the overhead of a full Kubernetes cluster.

๐Ÿ”„ Isolation

Each test runs in a clean environment without side effects from other tests.

๐Ÿ’ป Local Development

No need for a real cluster during development and CI pipelines.

๐Ÿ“Š Comprehensive Testing

Test edge cases and error conditions that are hard to reproduce in real clusters.

๐Ÿ› ๏ธ Integration Testing

True integration tests with a real API server implementation.

โš™๏ธ CRD Support

Register and test Custom Resource Definitions (CRDs) used by your controllers.

Configurationโ€‹

Test environment setup

The test environment includes an API server, etcd, and controller-runtime components that closely simulate a real Kubernetes cluster.

In-Memory Testing Architectureโ€‹

Best Practices for Controller Testingโ€‹

Testing guidelines

The Control Plane follows these best practices for effective controller testing.

๐Ÿ” Test Isolation

Use unique namespaces for each test to prevent test interference.

โฑ๏ธ Use Eventually

Use Eventually assertions for asynchronous operations with appropriate timeouts.

๐Ÿงช Test All Resource Operations

Cover create, update, delete, and error scenarios in your tests.

๐Ÿงฉ Mock External Dependencies

Use interfaces and mocks for external services to ensure tests are self-contained.

๐Ÿ“Š Verify Status Updates

Check that status fields are properly updated by controllers.

๐Ÿ”„ Test Finalizers

Verify that finalizers are added/removed and cleanup logic runs correctly.

โš ๏ธ Test Error Handling

Simulate error conditions to test controller resilience and error propagation.

๐Ÿงน Clean Up Resources

Ensure all resources are cleaned up after tests to prevent interference.

Debugging Controller Testsโ€‹

Troubleshooting

When controller tests fail, these debugging techniques can help identify the issue.

Enable Verbose Logging

Increase log verbosity to see what's happening

Use Ginkgo Debug Capabilities

Use Ginkgo's debugging features

Inspect API Objects

Dump API object contents for inspection

Check Controller Logs

Set up a custom logger to capture controller logs

Performance Considerationsโ€‹

Test efficiency

The in-memory test environment can be resource-intensive. These strategies help optimize test performance.

๐Ÿš€ Single Test Environment

Start the test environment once for the whole test suite instead of per-test.

๐Ÿ“Š Selective Testing

Group and organize tests to minimize setup/teardown overhead.

๐Ÿ”„ Resource Reuse

Reuse test environment resources when possible instead of recreating them.

โฑ๏ธ Appropriate Timeouts

Use reasonable timeouts for Eventually assertions to avoid long waits.

๐Ÿงช Parallelization

Run independent tests in parallel with Ginkgo's parallel testing features.

๐Ÿ” Focused Testing

Use Ginkgo's focus features to run only relevant tests during development.

Kubebuilder

Learn about the framework used to build Kubernetes operators.

Ginkgo

BDD-style testing framework used with Kubebuilder.

Gomega

Matcher/assertion library used for controller testing.

Controller Runtime

The library that powers Kubebuilder-based controllers and their tests.