Unit testing is solely dependent on the source code, serving as the simplest method to test if the code logic aligns with expectations.
Run all Unit Tests
maketest
Test Specific Package(s) Only
# Single packagemaketestWHAT=./pkg/api# Multiple packagesmaketestWHAT=./pkg/{api,kubelet}
Or, you can directly use go test
gotest-vk8s.io/kubernetes/pkg/kubelet
Test a Specific Test Case in a Given Package Only
# Runs TestValidatePod in pkg/api/validation with the verbose flag setmaketestWHAT=./pkg/api/validationKUBE_GOFLAGS="-v"KUBE_TEST_ARGS='-run ^TestValidatePod$'# Runs tests that match the regex ValidatePod|ValidateConfigMap in pkg/api/validationmaketestWHAT=./pkg/api/validationKUBE_GOFLAGS="-v"KUBE_TEST_ARGS="-run ValidatePod\\|ValidateConfigMap$"
Kubernetes integration tests require the installation of etcd (just the installation, no need to start it up), like:
hack/install-etcd.sh# Installs in ./third_party/etcdechoexportPATH="\$PATH:$(pwd)/third_party/etcd">>~/.profile# Add to PATH
Integration tests will automatically start etcd and Kubernetes services when needed and run the tests within test/integration.
Run All Integration Tests
maketest-integration# Run all integration tests.
Specify Integration Test Cases
# Run integration test TestPodUpdateActiveDeadlineSeconds with the verbose flag set.maketest-integrationKUBE_GOFLAGS="-v"KUBE_TEST_ARGS="-run ^TestPodUpdateActiveDeadlineSeconds$"
End-to-End (e2e) Testing
End-to-End (e2e) testing simulates user actions on Kubernetes, ensuring that the behavior of the Kubernetes services or clusters is fully in line with the design expectations.
Before starting e2e testing, you need to compile the test files and set KUBERNETES_PROVIDER (default is gce):
make WHAT='test/e2e/e2e.test'
make ginkgo
export KUBERNETES_PROVIDER=local
Start Cluster, Test, and Stop Cluster Eventually
# Builds Kubernetes, puts up a cluster, runs tests, and tears everything downgorunhack/e2e.go---v--build--up--test--down
Test Specific Case Only
go run hack/e2e.go -v -test --test_args='--ginkgo.focus=Kubectl\sclient\s\[k8s\.io\]\sKubectl\srolling\-update\sshould\ssupport\srolling\-update\sto\ssame\simage\s\[Conformance\]$'
# Run tests in parallel, skip any that must be run seriallyGINKGO_PARALLEL=ygorunhack/e2e.go--v--test--test_args="--ginkgo.skip=\[Serial\]"# Run tests in parallel, skip any that must be run serially and keep the test namespace if test failedGINKGO_PARALLEL=y go run hack/e2e.go --v --test --test_args="--ginkgo.skip=\[Serial\] --delete-namespace-on-failure=false"
Clean Up Testing Resources
gorunhack/e2e.go---v--down
Useful -ctl
# -ctl can be used to quickly call kubectl against your e2e cluster. Useful for# cleaning up after a failed test or viewing logs. Use -v to avoid suppressing# kubectl output.gorunhack/e2e.go---v-ctl='get events'gorunhack/e2e.go---v-ctl='delete pod foobar'
Federation E2E Testing
export FEDERATION=trueexport E2E_ZONES="us-central1-a us-central1-b us-central1-f"# or export FEDERATION_PUSH_REPO_BASE="quay.io/colin_hom"export FEDERATION_PUSH_REPO_BASE="gcr.io/${GCE_PROJECT_NAME}"# build container imagesKUBE_RELEASE_RUN_TESTS=n KUBE_FASTBUILD=truegorunhack/e2e.go---v-build# push the federation container imagesbuild/push-federation-images.sh# Deploy federation control planegorunhack/e2e.go---v--up# Finally, run the testsgorunhack/e2e.go---v--test--test_args="--ginkgo.focus=\[Feature:Federation\]"# Don't forget to teardown everything downgorunhack/e2e.go---v--down
You can use cluster/log-dump.sh <directory> to download related logs conveniently, which can help troubleshoot issues encountered during testing.
Node E2E Testing
Node e2e only tests Kubelet-related functionalities and can be conducted either locally or inside clusters.
You can conveniently obtain desired data with the assistance of the kubectl template. For example, the method to query the image of a certain container is: