This document provides guidelines and instructions for testing the Envoy XDS Controller.
The Envoy XDS Controller uses a comprehensive testing strategy to ensure code quality and functionality. The testing approach includes:
Unit tests focus on testing individual functions and methods in isolation. They are located alongside the code they test and follow the Go convention of *_test.go files.
Key packages with unit tests:
internal/xds/: Tests for xDS server implementationinternal/cache/: Tests for cache implementationinternal/updater/: Tests for configuration updatersapi/: Tests for API types and methodsIntegration tests verify that different components work together correctly. These tests typically involve multiple packages and may use mocks for external dependencies.
End-to-end (e2e) tests validate the entire system in a real or simulated environment. The e2e tests for Envoy XDS Controller:
E2e tests are located in the test/e2e/ directory.
Linting ensures code quality and consistency. The project uses golangci-lint with a configuration defined in .golangci.yml.
To run all unit tests:
make test
To run tests for a specific package:
go test ./path/to/package
To run tests with verbose output:
go test -v ./path/to/package
Before running e2e tests, ensure you have a Kind cluster running:
kind create cluster
Then run the e2e tests:
make test-e2e
To run linters:
make lint
To automatically fix linting issues where possible:
make lint-fix
*_test.go suffix for test files.Test<FunctionName> or Test<Behavior>.Example unit test:
// TestResourceBuilder_BuildListener tests the BuildListener function
func TestResourceBuilder_BuildListener(t *testing.T) {
// Setup test cases
testCases := []struct {
name string
input *v1alpha1.Listener
expected *envoy_config_listener_v3.Listener
wantErr bool
}{
// Test cases...
}
// Run test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
builder := NewResourceBuilder()
result, err := builder.BuildListener(tc.input)
if tc.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.expected, result)
})
}
}
Example e2e test structure:
// Ginkgo test for Envoy XDS Controller
var _ = Describe("Envoy XDS Controller", func() {
Context("When creating a VirtualService", func() {
It("Should create corresponding Envoy configuration", func() {
// Setup
vs := createVirtualService()
// Execution
err := k8sClient.Create(context.Background(), vs)
Expect(err).NotTo(HaveOccurred())
// Verification
Eventually(func() bool {
// Check if configuration was created correctly
return checkEnvoyConfiguration(vs)
}, timeout, interval).Should(BeTrue())
// Cleanup
err = k8sClient.Delete(context.Background(), vs)
Expect(err).NotTo(HaveOccurred())
})
})
})
The project uses GitHub Actions for continuous integration. The CI pipeline:
To generate a test coverage report:
make test
go tool cover -html=cover.out
The project aims to maintain high test coverage, especially for critical components like the xDS server, cache, and API handlers.