Kubernetes指南
Linux性能优化实战eBPF 核心技术与实战SDN指南个人博客
EN
EN
  • Overview
  • Introduction
    • Kubernetes Introduction
    • Kubernetes Concepts
    • Kubernetes 101
    • Kubernetes 201
    • Kubernetes Cluster
  • Concepts
    • Concepts
    • Architecture
    • Design Principles
    • Components
      • etcd
      • kube-apiserver
      • kube-scheduler
      • kube-controller-manager
      • kubelet
      • kube-proxy
      • kube-dns
      • Federation
      • kubeadm
      • hyperkube
      • kubectl
    • Objects
      • Autoscaling
      • ConfigMap
      • CronJob
      • CustomResourceDefinition
      • DaemonSet
      • Deployment
      • Ingress
      • Job
      • LocalVolume
      • Namespace
      • NetworkPolicy
      • Node
      • PersistentVolume
      • Pod
      • PodPreset
      • ReplicaSet
      • Resource Quota
      • Secret
      • SecurityContext
      • Service
      • ServiceAccount
      • StatefulSet
      • Volume
  • Setup
    • Setup Guidance
    • kubectl Install
    • Single Machine
    • Feature Gates
    • Best Practice
    • Version Support
    • Setup Cluster
      • kubeadm
      • kops
      • Kubespray
      • Azure
      • Windows
      • LinuxKit
      • kubeasz
    • Setup Addons
      • Addon-manager
      • DNS
      • Dashboard
      • Monitoring
      • Logging
      • Metrics
      • GPU
      • Cluster Autoscaler
      • ip-masq-agent
  • Extension
    • API Extension
      • Aggregation
      • CustomResourceDefinition
    • Access Control
      • Authentication
      • RBAC Authz
      • Admission
    • Scheduler Extension
    • Network Plugin
      • CNI
      • Flannel
      • Calico
      • Weave
      • Cilium
      • OVN
      • Contiv
      • SR-IOV
      • Romana
      • OpenContrail
      • Kuryr
    • Container Runtime
      • CRI-tools
      • Frakti
    • Storage Driver
      • CSI
      • FlexVolume
      • glusterfs
    • Network Policy
    • Ingress Controller
      • Ingress + Letsencrypt
      • minikube Ingress
      • Traefik Ingress
      • Keepalived-VIP
    • Cloud Provider
    • Device Plugin
  • Cloud Native Apps
    • Apps Management
      • Patterns
      • Rolling Update
      • Helm
      • Operator
      • Service Mesh
      • Linkerd
      • Linkerd2
    • Istio
      • Deploy
      • Traffic Management
      • Security
      • Policy
      • Metrics
      • Troubleshooting
      • Community
    • Devops
      • Draft
      • Jenkins X
      • Spinnaker
      • Kompose
      • Skaffold
      • Argo
      • Flux GitOps
  • Practices
    • Overview
    • Resource Management
    • Cluster HA
    • Workload HA
    • Debugging
    • Portmap
    • Portforward
    • User Management
    • GPU
    • HugePage
    • Security
    • Audit
    • Backup
    • Cert Rotation
    • Large Cluster
    • Big Data
      • Spark
      • Tensorflow
    • Serverless
  • Troubleshooting
    • Overview
    • Cluster Troubleshooting
    • Pod Troubleshooting
    • Network Troubleshooting
    • PV Troubleshooting
      • AzureDisk
      • AzureFile
    • Windows Troubleshooting
    • Cloud Platform Troubleshooting
      • Azure
    • Troubleshooting Tools
  • Community
    • Development Guide
    • Unit Test and Integration Test
    • Community Contribution
  • Appendix
    • Ecosystem
    • Learning Resources
    • Domestic Mirrors
    • How to Contribute
    • Reference Documents
由 GitBook 提供支持
在本页
  • Unit Testing
  • Run all Unit Tests
  • Test Specific Package(s) Only
  • Test a Specific Test Case in a Given Package Only
  • Parallel Testing
  • Generate Test Reports
  • Benchmark Testing
  • Integration Testing
  • Run All Integration Tests
  • Specify Integration Test Cases
  • End-to-End (e2e) Testing
  • Start Cluster, Test, and Stop Cluster Eventually
  • Test Specific Case Only
  • Skip Test Cases
  • Parallel Testing
  • Clean Up Testing Resources
  • Useful -ctl
  • Federation E2E Testing
  • Node E2E Testing
  • Additional Explanation
  • Referenced Documents
  1. Community

Unit Test and Integration Test

上一页Development Guide下一页Community Contribution

最后更新于1年前

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

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

Integration tests will automatically start etcd and Kubernetes services when needed and run the tests within .

Current Test Status
Aggregated Failures
Test Grid
test/integration
Kubernetes testing
End-to-End Testing
Node e2e test
How to write e2e test
Coding Conventions