2016-03-07 03:07:34 +00:00
|
|
|
# Testing guide
|
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
Updated: 5/21/2016
|
2016-05-03 17:23:25 +00:00
|
|
|
|
|
|
|
**Table of Contents**
|
|
|
|
<!-- BEGIN MUNGE: GENERATED_TOC -->
|
|
|
|
|
|
|
|
- [Testing guide](#testing-guide)
|
|
|
|
- [Unit tests](#unit-tests)
|
|
|
|
- [Run all unit tests](#run-all-unit-tests)
|
2016-06-13 19:10:46 +00:00
|
|
|
- [Set go flags during unit tests](#set-go-flags-during-unit-tests)
|
|
|
|
- [Run unit tests from certain packages](#run-unit-tests-from-certain-packages)
|
|
|
|
- [Run specific unit test cases in a package](#run-specific-unit-test-cases-in-a-package)
|
2016-05-03 17:23:25 +00:00
|
|
|
- [Stress running unit tests](#stress-running-unit-tests)
|
|
|
|
- [Unit test coverage](#unit-test-coverage)
|
|
|
|
- [Benchmark unit tests](#benchmark-unit-tests)
|
|
|
|
- [Integration tests](#integration-tests)
|
|
|
|
- [Install etcd dependency](#install-etcd-dependency)
|
2016-06-15 03:41:47 +00:00
|
|
|
- [Etcd test data](#etcd-test-data)
|
2016-05-03 17:23:25 +00:00
|
|
|
- [Run integration tests](#run-integration-tests)
|
2016-06-13 19:10:46 +00:00
|
|
|
- [Run a specific integration test](#run-a-specific-integration-test)
|
2016-05-03 17:23:25 +00:00
|
|
|
- [End-to-End tests](#end-to-end-tests)
|
|
|
|
|
|
|
|
<!-- END MUNGE: GENERATED_TOC -->
|
|
|
|
|
2016-03-07 03:07:34 +00:00
|
|
|
This assumes you already read the [development guide](development.md) to
|
2016-06-13 19:10:46 +00:00
|
|
|
install go, godeps, and configure your git client. All command examples are
|
|
|
|
relative to the `kubernetes` root directory.
|
2016-03-07 03:07:34 +00:00
|
|
|
|
2016-04-01 21:48:14 +00:00
|
|
|
Before sending pull requests you should at least make sure your changes have
|
|
|
|
passed both unit and integration tests.
|
2016-03-07 03:07:34 +00:00
|
|
|
|
2016-04-01 21:48:14 +00:00
|
|
|
Kubernetes only merges pull requests when unit, integration, and e2e tests are
|
|
|
|
passing, so it is often a good idea to make sure the e2e tests work as well.
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
## Unit tests
|
|
|
|
|
2016-03-07 03:50:31 +00:00
|
|
|
* Unit tests should be fully hermetic
|
|
|
|
- Only access resources in the test binary.
|
|
|
|
* All packages and any significant files require unit tests.
|
2016-06-13 19:10:46 +00:00
|
|
|
* The preferred method of testing multiple scenarios or input is
|
|
|
|
[table driven testing](https://github.com/golang/go/wiki/TableDrivenTests)
|
2016-06-08 21:49:33 +00:00
|
|
|
- Example: [TestNamespaceAuthorization](../../test/integration/auth/auth_test.go)
|
2016-03-07 03:50:31 +00:00
|
|
|
* Unit tests must pass on OS X and Windows platforms.
|
|
|
|
- Tests using linux-specific features must be skipped or compiled out.
|
|
|
|
- Skipped is better, compiled out is required when it won't compile.
|
|
|
|
* Concurrent unit test runs must pass.
|
|
|
|
* See [coding conventions](coding-conventions.md).
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
### Run all unit tests
|
|
|
|
|
2016-05-24 15:40:44 +00:00
|
|
|
`make test` is the entrypoint for running the unit tests that ensures that
|
|
|
|
`GOPATH` is set up correctly. If you have `GOPATH` set up correctly, you can
|
|
|
|
also just use `go test` directly.
|
2016-06-13 19:10:46 +00:00
|
|
|
|
2016-03-07 03:07:34 +00:00
|
|
|
```sh
|
|
|
|
cd kubernetes
|
2016-05-24 15:40:44 +00:00
|
|
|
make test # Run all unit tests.
|
2016-03-07 03:07:34 +00:00
|
|
|
```
|
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
### Set go flags during unit tests
|
|
|
|
|
|
|
|
You can set [go flags](https://golang.org/cmd/go/) by setting the
|
|
|
|
`KUBE_GOFLAGS` environment variable.
|
|
|
|
|
|
|
|
### Run unit tests from certain packages
|
|
|
|
|
2016-05-24 15:40:44 +00:00
|
|
|
`make test` accepts packages as arguments; the `k8s.io/kubernetes` prefix is
|
|
|
|
added automatically to these:
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
```sh
|
2016-05-24 15:40:44 +00:00
|
|
|
make test WHAT=pkg/api # run tests for pkg/api
|
|
|
|
```
|
|
|
|
|
|
|
|
To run multiple targets you need quotes:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
make test WHAT="pkg/api pkg/kubelet" # run tests for pkg/api and pkg/kubelet
|
2016-06-13 19:10:46 +00:00
|
|
|
```
|
2016-03-07 03:07:34 +00:00
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
In a shell, it's often handy to use brace expansion:
|
2016-03-07 03:07:34 +00:00
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
```sh
|
2016-05-24 15:40:44 +00:00
|
|
|
make test WHAT=pkg/{api,kubelet} # run tests for pkg/api and pkg/kubelet
|
2016-03-07 03:07:34 +00:00
|
|
|
```
|
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
### Run specific unit test cases in a package
|
|
|
|
|
|
|
|
You can set the test args using the `KUBE_TEST_ARGS` environment variable.
|
|
|
|
You can use this to pass the `-run` argument to `go test`, which accepts a
|
|
|
|
regular expression for the name of the test that should be run.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
# Runs TestValidatePod in pkg/api/validation with the verbose flag set
|
2016-05-24 15:40:44 +00:00
|
|
|
make test WHAT=pkg/api/validation KUBE_GOFLAGS="-v" KUBE_TEST_ARGS='-run ^TestValidatePod$'
|
2016-06-13 19:10:46 +00:00
|
|
|
|
|
|
|
# Runs tests that match the regex ValidatePod|ValidateConfigMap in pkg/api/validation
|
2016-05-24 15:40:44 +00:00
|
|
|
make test WHAT=pkg/api/validation KUBE_GOFLAGS="-v" KUBE_TEST_ARGS="-run ValidatePod\|ValidateConfigMap$"
|
2016-06-13 19:10:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
For other supported test flags, see the [golang
|
|
|
|
documentation](https://golang.org/cmd/go/#hdr-Description_of_testing_flags).
|
|
|
|
|
2016-03-07 03:07:34 +00:00
|
|
|
### Stress running unit tests
|
|
|
|
|
|
|
|
Running the same tests repeatedly is one way to root out flakes.
|
|
|
|
You can do this efficiently.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
# Have 2 workers run all tests 5 times each (10 total iterations).
|
2016-05-24 15:40:44 +00:00
|
|
|
make test PARALLEL=2 ITERATION=5
|
2016-03-07 03:07:34 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
For more advanced ideas please see [flaky-tests.md](flaky-tests.md).
|
|
|
|
|
|
|
|
### Unit test coverage
|
|
|
|
|
|
|
|
Currently, collecting coverage is only supported for the Go unit tests.
|
|
|
|
|
|
|
|
To run all unit tests and generate an HTML coverage report, run the following:
|
|
|
|
|
|
|
|
```sh
|
2016-05-24 15:40:44 +00:00
|
|
|
make test KUBE_COVER=y
|
2016-03-07 03:07:34 +00:00
|
|
|
```
|
|
|
|
|
2016-06-13 19:11:02 +00:00
|
|
|
At the end of the run, an HTML report will be generated with the path
|
2016-06-13 19:10:46 +00:00
|
|
|
printed to stdout.
|
2016-03-07 03:07:34 +00:00
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
To run tests and collect coverage in only one package, pass its relative path
|
|
|
|
under the `kubernetes` directory as an argument, for example:
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
```sh
|
2016-05-24 15:40:44 +00:00
|
|
|
make test WHAT=pkg/kubectl KUBE_COVER=y
|
2016-03-07 03:07:34 +00:00
|
|
|
```
|
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
Multiple arguments can be passed, in which case the coverage results will be
|
|
|
|
combined for all tests run.
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
### Benchmark unit tests
|
|
|
|
|
|
|
|
To run benchmark tests, you'll typically use something like:
|
|
|
|
|
|
|
|
```sh
|
2016-04-14 06:30:15 +00:00
|
|
|
go test ./pkg/apiserver -benchmem -run=XXX -bench=BenchmarkWatch
|
2016-03-07 03:07:34 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
This will do the following:
|
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
1. `-run=XXX` is a regular expression filter on the name of test cases to run
|
2016-03-07 03:07:34 +00:00
|
|
|
2. `-bench=BenchmarkWatch` will run test methods with BenchmarkWatch in the name
|
|
|
|
* See `grep -nr BenchmarkWatch .` for examples
|
|
|
|
3. `-benchmem` enables memory allocation stats
|
|
|
|
|
|
|
|
See `go help test` and `go help testflag` for additional info.
|
|
|
|
|
|
|
|
## Integration tests
|
|
|
|
|
2016-03-07 03:50:31 +00:00
|
|
|
* Integration tests should only access other resources on the local machine
|
|
|
|
- Most commonly etcd or a service listening on localhost.
|
|
|
|
* All significant features require integration tests.
|
|
|
|
- This includes kubectl commands
|
|
|
|
* The preferred method of testing multiple scenarios or inputs
|
|
|
|
is [table driven testing](https://github.com/golang/go/wiki/TableDrivenTests)
|
2016-06-08 21:49:33 +00:00
|
|
|
- Example: [TestNamespaceAuthorization](../../test/integration/auth/auth_test.go)
|
2016-06-13 19:10:46 +00:00
|
|
|
* Each test should create its own master, httpserver and config.
|
2016-06-08 21:49:33 +00:00
|
|
|
- Example: [TestPodUpdateActiveDeadlineSeconds](../../test/integration/pods/pods_test.go)
|
2016-03-07 03:50:31 +00:00
|
|
|
* See [coding conventions](coding-conventions.md).
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
### Install etcd dependency
|
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
Kubernetes integration tests require your `PATH` to include an
|
|
|
|
[etcd](https://github.com/coreos/etcd/releases) installation. Kubernetes
|
|
|
|
includes a script to help install etcd on your machine.
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
```sh
|
|
|
|
# Install etcd and add to PATH
|
|
|
|
|
|
|
|
# Option a) install inside kubernetes root
|
|
|
|
hack/install-etcd.sh # Installs in ./third_party/etcd
|
2016-11-18 08:21:28 +00:00
|
|
|
echo export PATH="\$PATH:$(pwd)/third_party/etcd" >> ~/.profile # Add to PATH
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
# Option b) install manually
|
|
|
|
grep -E "image.*etcd" cluster/saltbase/etcd/etcd.manifest # Find version
|
|
|
|
# Install that version using yum/apt-get/etc
|
2016-11-18 08:21:28 +00:00
|
|
|
echo export PATH="\$PATH:<LOCATION>" >> ~/.profile # Add to PATH
|
2016-03-07 03:07:34 +00:00
|
|
|
```
|
|
|
|
|
2016-06-15 03:41:47 +00:00
|
|
|
### Etcd test data
|
|
|
|
|
|
|
|
Many tests start an etcd server internally, storing test data in the operating system's temporary directory.
|
|
|
|
|
|
|
|
If you see test failures because the temporary directory does not have sufficient space,
|
|
|
|
or is on a volume with unpredictable write latency, you can override the test data directory
|
|
|
|
for those internal etcd instances with the `TEST_ETCD_DIR` environment variable.
|
|
|
|
|
2016-03-07 03:07:34 +00:00
|
|
|
### Run integration tests
|
|
|
|
|
2016-05-24 15:40:44 +00:00
|
|
|
The integration tests are run using `make test-integration`.
|
2016-06-13 19:10:46 +00:00
|
|
|
The Kubernetes integration tests are writting using the normal golang testing
|
|
|
|
package but expect to have a running etcd instance to connect to. The `test-
|
2016-05-24 15:40:44 +00:00
|
|
|
integration.sh` script wraps `make test` and sets up an etcd instance
|
2016-06-13 19:10:46 +00:00
|
|
|
for the integration tests to use.
|
|
|
|
|
2016-03-07 03:07:34 +00:00
|
|
|
```sh
|
2016-05-24 15:40:44 +00:00
|
|
|
make test-integration # Run all integration tests.
|
2016-03-07 03:07:34 +00:00
|
|
|
```
|
|
|
|
|
2016-06-13 19:10:46 +00:00
|
|
|
This script runs the golang tests in package
|
2016-07-04 10:22:07 +00:00
|
|
|
[`test/integration`](../../test/integration/).
|
2016-06-13 19:10:46 +00:00
|
|
|
|
|
|
|
### Run a specific integration test
|
|
|
|
|
|
|
|
You can use also use the `KUBE_TEST_ARGS` environment variable with the `hack
|
|
|
|
/test-integration.sh` script to run a specific integration test case:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
# Run integration test TestPodUpdateActiveDeadlineSeconds with the verbose flag set.
|
2016-05-24 15:40:44 +00:00
|
|
|
make test-integration KUBE_GOFLAGS="-v" KUBE_TEST_ARGS="-run ^TestPodUpdateActiveDeadlineSeconds$"
|
2016-06-13 19:10:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
If you set `KUBE_TEST_ARGS`, the test case will be run with only the `v1` API
|
|
|
|
version and the watch cache test is skipped.
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
## End-to-End tests
|
|
|
|
|
2016-03-22 20:09:31 +00:00
|
|
|
Please refer to [End-to-End Testing in Kubernetes](e2e-tests.md).
|
2016-03-07 03:07:34 +00:00
|
|
|
|
|
|
|
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
|
|
|
|
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/devel/testing.md?pixel)]()
|
|
|
|
<!-- END MUNGE: GENERATED_ANALYTICS -->
|