单元测试和集成测试

单元测试

单元测试仅依赖于源代码,是测试代码逻辑是否符合预期的最简单方法。

运行所有的单元测试

1
make test
Copied!

仅测试指定的 package

1
# 单个 package
2
make test WHAT=./pkg/api
3
# 多个 packages
4
make test WHAT=./pkg/{api,kubelet}
Copied!
或者,也可以直接用 go test
1
go test -v k8s.io/kubernetes/pkg/kubelet
Copied!

仅测试指定 package 的某个测试 case

1
# Runs TestValidatePod in pkg/api/validation with the verbose flag set
2
make test WHAT=./pkg/api/validation KUBE_GOFLAGS="-v" KUBE_TEST_ARGS='-run ^TestValidatePod#x27;
3
4
# Runs tests that match the regex ValidatePod|ValidateConfigMap in pkg/api/validation
5
make test WHAT=./pkg/api/validation KUBE_GOFLAGS="-v" KUBE_TEST_ARGS="-run ValidatePod\|ValidateConfigMapquot;
Copied!
或者直接用 go test
1
go test -v k8s.io/kubernetes/pkg/api/validation -run ^TestValidatePod$
Copied!

并行测试

并行测试是 root out flakes 的一种有效方法:
1
# Have 2 workers run all tests 5 times each (10 total iterations).
2
make test PARALLEL=2 ITERATION=5
Copied!

生成测试报告

1
make test KUBE_COVER=y
Copied!

Benchmark 测试

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

集成测试

Kubernetes 集成测试需要安装 etcd(只要按照即可,不需要启动),比如
1
hack/install-etcd.sh # Installs in ./third_party/etcd
2
echo export PATH="\$PATH:$(pwd)/third_party/etcd" >> ~/.profile # Add to PATH
Copied!
集成测试会在需要的时候自动启动 etcd 和 kubernetes 服务,并运行 test/integration 里面的测试。

运行所有集成测试

1
make test-integration # Run all integration tests.
Copied!

指定集成测试用例

1
# Run integration test TestPodUpdateActiveDeadlineSeconds with the verbose flag set.
2
make test-integration KUBE_GOFLAGS="-v" KUBE_TEST_ARGS="-run ^TestPodUpdateActiveDeadlineSecondsquot;
Copied!

End to end (e2e) 测试

End to end (e2e) 测试模拟用户行为操作 Kubernetes,用来保证 Kubernetes 服务或集群的行为完全符合设计预期。
在开启 e2e 测试之前,需要先编译测试文件,并设置 KUBERNETES_PROVIDER(默认为 gce):
1
make WHAT='test/e2e/e2e.test'
2
make ginkgo
3
export KUBERNETES_PROVIDER=local
Copied!

启动 cluster,测试,最后停止 cluster

1
# build Kubernetes, up a cluster, run tests, and tear everything down
2
go run hack/e2e.go -- -v --build --up --test --down
Copied!

仅测试指定的用例

1
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\]#x27;
Copied!

跳过测试用例

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

并行测试

1
# Run tests in parallel, skip any that must be run serially
2
GINKGO_PARALLEL=y go run hack/e2e.go --v --test --test_args="--ginkgo.skip=\[Serial\]"
3
4
# Run tests in parallel, skip any that must be run serially and keep the test namespace if test failed
5
GINKGO_PARALLEL=y go run hack/e2e.go --v --test --test_args="--ginkgo.skip=\[Serial\] --delete-namespace-on-failure=false"
Copied!

清理测试资源

1
go run hack/e2e.go -- -v --down
Copied!

有用的 -ctl

1
# -ctl can be used to quickly call kubectl against your e2e cluster. Useful for
2
# cleaning up after a failed test or viewing logs. Use -v to avoid suppressing
3
# kubectl output.
4
go run hack/e2e.go -- -v -ctl='get events'
5
go run hack/e2e.go -- -v -ctl='delete pod foobar'
Copied!

Fedaration e2e 测试

1
export FEDERATION=true
2
export E2E_ZONES="us-central1-a us-central1-b us-central1-f"
3
# or export FEDERATION_PUSH_REPO_BASE="quay.io/colin_hom"
4
export FEDERATION_PUSH_REPO_BASE="gcr.io/${GCE_PROJECT_NAME}"
5
6
# build container images
7
KUBE_RELEASE_RUN_TESTS=n KUBE_FASTBUILD=true go run hack/e2e.go -- -v -build
8
9
# push the federation container images
10
build/push-federation-images.sh
11
12
# Deploy federation control plane
13
go run hack/e2e.go -- -v --up
14
15
# Finally, run the tests
16
go run hack/e2e.go -- -v --test --test_args="--ginkgo.focus=\[Feature:Federation\]"
17
18
# Don't forget to teardown everything down
19
go run hack/e2e.go -- -v --down
Copied!
可以用 cluster/log-dump.sh <directory> 方便的下载相关日志,帮助排查测试中碰到的问题。

Node e2e 测试

Node e2e 仅测试 Kubelet 的相关功能,可以在本地或者集群中测试
1
export KUBERNETES_PROVIDER=local
2
make test-e2e-node FOCUS="InitContainer"
3
make test_e2e_node TEST_ARGS="--experimental-cgroups-per-qos=true"
Copied!

补充说明

借助 kubectl 的模版可以方便获取想要的数据,比如查询某个 container 的镜像的方法为
1
kubectl get pods nginx-4263166205-ggst4 -o template '--template={{if (exists ."status""containerStatuses")}}{{range .status.containerStatuses}}{{if eq .name "nginx"}}{{.image}}{{end}}{{end}}{{end}}'
Copied!

参考文档

最近更新 2yr ago