This directory contains integration tests for kgrep that require a real Kubernetes cluster to run. These tests complement the unit tests by testing scenarios that cannot be properly tested with mocked Kubernetes clients.
The integration tests cover scenarios that require interaction with a real Kubernetes cluster.
The integration tests create a dedicated test namespace (kgrep-integration-test) and:
- Set up test resources (ConfigMaps, Secrets, Pods, ServiceAccounts)
- For custom resource tests: Apply a test CRD and create custom resources
- Run kgrep commands against these resources
- Verify the expected output
- Clean up resources after each test
The custom resource tests use a simple test CRD (TestApplication) that mimics real-world custom resources:
- CRD:
testapplications.test.kgrep.io - Resource Names:
- Plural:
testapplications - Short Name:
tapp - Kind:
TestApplication - Full Name:
testapplications.test.kgrep.io
- Plural:
- Test Content: Contains searchable text in spec, status, labels, and metadata
This setup allows testing of the exact scenarios from GitHub issue #125:
testapplications(plural name)tapp(short name)testapplications.test.kgrep.io(resource.group format)TestApplication(Kind name)
- Go 1.24+
- A running Kubernetes cluster accessible via
kubectl kubectlconfigured to connect to your cluster
If you have a running Kubernetes cluster (local or remote):
# Run integration tests using the current kubectl context
make test-integration
# Or directly with go test
cd test/integration
KGREP_INTEGRATION_TESTS=true go test -v -timeout=10m ./...To run tests with a temporary kind cluster:
# This will create a kind cluster, run tests, and clean up
make test-integration-kindPrerequisites for kind:
If you want more control over the kind cluster:
# Create kind cluster
kind create cluster --name kgrep-integration-test --image kindest/node:v1.31.0
# Wait for cluster to be ready
kubectl wait --for=condition=Ready nodes --all --timeout=300s
# Run tests
cd test/integration
KGREP_INTEGRATION_TESTS=true go test -v -timeout=10m ./...
# Clean up
kind delete cluster --name kgrep-integration-testKGREP_INTEGRATION_TESTS: Set to"true"to enable integration tests (required)KUBECONFIG: Path to kubeconfig file (optional, defaults to~/.kube/config)
The integration tests are designed to be safe:
- Isolated Namespace: All test resources are created in a dedicated namespace
- Resource Cleanup: Resources are cleaned up after each test
- CRD Cleanup: Test CRDs are automatically removed after custom resource tests
- Non-Destructive: Tests only create test resources and don't modify existing ones
- Opt-In: Tests only run when explicitly enabled via environment variable
The integration tests run automatically in CI/CD via GitHub Actions:
- Trigger: On pushes to
mainbranch and pull requests - Environment: Ubuntu with kind-created Kubernetes cluster
- Isolation: Each CI run gets a fresh kind cluster
- Coverage: Includes both core resource tests and custom resource tests
When adding new integration tests:
- Follow the existing pattern of setup/test/cleanup
- Use the dedicated test namespace
- Include proper cleanup in
deferstatements - For custom resource tests, use the provided test CRD
- Add meaningful assertions and logging
- Test both success and failure scenarios where applicable
Make sure to set the environment variable:
export KGREP_INTEGRATION_TESTS=trueVerify your kubectl configuration:
kubectl cluster-info
kubectl get nodesIf tests fail to clean up resources:
kubectl delete namespace kgrep-integration-test
kubectl delete crd testapplications.test.kgrep.ioIf kind cluster creation fails:
kind delete cluster --name kgrep-integration-test
docker system prune -fIf custom resource tests fail:
# Check if CRD was applied
kubectl get crd testapplications.test.kgrep.io
# Check custom resources
kubectl get testapplications --all-namespaces
# Clean up manually if needed
kubectl delete crd testapplications.test.kgrep.io