Unit Test and Integration Test

Unit Testing

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

make test

Test Specific Package(s) Only

# Single package
make test WHAT=./pkg/api
# Multiple packages
make test WHAT=./pkg/{api,kubelet}

Or, you can directly use go test

go test -v k8s.io/kubernetes/pkg/kubelet

Test a Specific Test Case in a Given Package Only

# Runs TestValidatePod in pkg/api/validation with the verbose flag set
make test WHAT=./pkg/api/validation KUBE_GOFLAGS="-v" KUBE_TEST_ARGS='-run ^TestValidatePod$'

# Runs tests that match the regex ValidatePod|ValidateConfigMap in pkg/api/validation
make test WHAT=./pkg/api/validation KUBE_GOFLAGS="-v" KUBE_TEST_ARGS="-run ValidatePod\\|ValidateConfigMap$"

Or simply use go test

go test -v k8s.io/kubernetes/pkg/api/validation -run ^TestValidatePod$

Parallel Testing

Parallel testing is an effective way to root out flakes:

# Have 2 workers run all tests 5 times each (10 total iterations).
make test PARALLEL=2 ITERATION=5

Generate Test Reports

make test KUBE_COVER=y

Benchmark Testing

go test ./pkg/apiserver -benchmem -run=XXX -bench=BenchmarkWatch

Integration Testing

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/etcd
echo export PATH="\$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

make test-integration  # Run all integration tests.

Specify Integration Test Cases

# Run integration test TestPodUpdateActiveDeadlineSeconds with the verbose flag set.
make test-integration KUBE_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 down
go run hack/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\]$'

Skip Test Cases

go run hack/e2e.go -- -v --test --test_args="--ginkgo.skip=Pods.*env

Parallel Testing

# Run tests in parallel, skip any that must be run serially
GINKGO_PARALLEL=y go run hack/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 failed
GINKGO_PARALLEL=y go run hack/e2e.go --v --test --test_args="--ginkgo.skip=\[Serial\] --delete-namespace-on-failure=false"

Clean Up Testing Resources

go run hack/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.
go run hack/e2e.go -- -v -ctl='get events'
go run hack/e2e.go -- -v -ctl='delete pod foobar'

Federation E2E Testing

export FEDERATION=true
export 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 images
KUBE_RELEASE_RUN_TESTS=n KUBE_FASTBUILD=true go run hack/e2e.go -- -v -build

# push the federation container images
build/push-federation-images.sh

# Deploy federation control plane
go run hack/e2e.go -- -v --up

# Finally, run the tests
go run hack/e2e.go -- -v --test --test_args="--ginkgo.focus=\[Feature:Federation\]"

# Don't forget to teardown everything down
go run hack/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.

export KUBERNETES_PROVIDER=local
make test-e2e-node FOCUS="InitContainer"
make test_e2e_node TEST_ARGS="--experimental-cgroups-per-qos=true"

Additional Explanation

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:

kubectl get pods nginx-4263166205-ggst4 -o template '--template={{if (exists ."status""containerStatuses")}}{{range .status.containerStatuses}}{{if eq .name "nginx"}}{{.image}}{{end}}{{end}}{{end}}'

Referenced Documents

最后更新于