Skip to content

Commit 7818fd7

Browse files
authored
Update Docs With Incluster Testing Support (#526)
* doc/test-framework: update test framework walkthrough * doc/sdk-cli-reference.md: update for incluster testing support
1 parent 4e49fba commit 7818fd7

File tree

3 files changed

+105
-27
lines changed

3 files changed

+105
-27
lines changed

commands/operator-sdk/cmd/test/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func NewTestClusterCmd() *cobra.Command {
5656
testCmd.Flags().StringVar(&tcConfig.kubeconfig, "kubeconfig", defaultKubeConfig, "Kubeconfig path")
5757
testCmd.Flags().StringVar(&tcConfig.imagePullPolicy, "image-pull-policy", "Always", "Set test pod image pull policy. Allowed values: Always, Never")
5858
testCmd.Flags().StringVar(&tcConfig.serviceAccount, "service-account", "default", "Service account to run tests on")
59-
testCmd.Flags().IntVar(&tcConfig.pendingTimeout, "pending-timeout", 60, "Timeout for testing pod in pending state")
59+
testCmd.Flags().IntVar(&tcConfig.pendingTimeout, "pending-timeout", 60, "Timeout in seconds for testing pod to stay in pending state (default 60s)")
6060

6161
return testCmd
6262
}

doc/sdk-cli-reference.md

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ Usage:
99

1010
### Args
1111

12-
* image - is the container image to be built, e.g. "quay.io/example/operator:v0.0.1". This image will be automatically set in the deployment manifests.
12+
* image - is the container image to be built, e.g. "quay.io/example/operator:v0.0.1".
1313

1414
### Flags
15-
15+
* `--enable-tests` bool - enable in-cluster testing by adding test binary to the image
16+
* `--namespaced-manifest` string - path of namespaced resources manifest for tests (default "deploy/operator.yaml")
17+
* `--test-location` string - location of tests (default "./test/e2e")
1618
* `-h, --help` - help for build
1719

20+
1821
### Use
1922

20-
The operator-sdk build command compiles the code, builds the executables,
21-
and generates Kubernetes manifests. After build completes, the image would be built locally in docker. Then it needs to be pushed to remote registry.
23+
The operator-sdk build command compiles the code and builds the executables. After build completes, the image is built locally in docker. Then it needs to be pushed to a remote registry.
24+
25+
If `--enable-tests` is set, the build command will also build the testing binary, add it to the docker image, and generate
26+
a `deploy/test-pod.yaml` file that allows a user to run the tests as a pod on a cluster.
2227

2328
### Example:
2429

@@ -158,30 +163,61 @@ Create app-operator/.gitignore
158163

159164
## test
160165

161-
### Flags
166+
### Available Commands
162167

163-
* `-t, --test-location` **(required)** string - location of e2e test files
164-
* `-k, --kubeconfig` string - location of kubeconfig for kubernetes cluster
165-
* `-g, --global-init` string - location of global resource manifest yaml file
166-
* `-n, --namespaced-init` string - location of namespaced resource manifest yaml file
167-
* `-f, --go-test-flags` string - extra arguments to pass to `go test` (e.g. -f "-v -parallel=2")
168-
* `-h, --help` - help for test
168+
#### local
169+
Runs the tests locally
169170

170-
### Use
171+
##### Args
172+
* <test-location> string - location of e2e test files (e.g. "./test/e2e/")
171173

172-
The operator-sdk test command runs go tests built using the Operator SDK's test framework.
174+
##### Flags
175+
* `--kubeconfig` string - location of kubeconfig for kubernetes cluster (default "~/.kube/config")
176+
* `--global-manifest` string - path to manifest for global resources (default "deploy/crd.yaml)
177+
* `--namespaced-manifest` string - path to manifest for per-test, namespaced resources (default: combines deploy/sa.yaml, deploy/rbac.yaml, and deploy/operator.yaml)
178+
* `--go-test-flags` string - extra arguments to pass to `go test` (e.g. -f "-v -parallel=2")
179+
* `-h, --help` - help for local
173180

174-
### Example:
181+
##### Use
182+
183+
The operator-sdk test command runs go tests built using the Operator SDK's test framework.
175184

176-
#### Test
185+
##### Example:
177186

178187
```bash
179-
operator-sdk test --test-location ./test/e2e/
188+
$ operator-sdk test local ./test/e2e/
180189

181190
# Output:
182191
ok github.com/operator-framework/operator-sdk-samples/memcached-operator/test/e2e 20.410s
183192
```
184193

194+
#### cluster
195+
Runs the e2e tests packaged in an operator image as a pod in the cluster
196+
197+
##### Args
198+
* <image-name> string - the operator image that is used to run the tests in a pod (e.g. "quay.io/example/memcached-operator:v0.0.1")
199+
200+
##### Flags
201+
* `--kubeconfig` string - location of kubeconfig for kubernetes cluster (default "~/.kube/config")
202+
* `--image-pull-policy` string - set test pod image pull policy. Allowed values: Always, Never (default "Always")
203+
* `--namespace` string - namespace to run tests in (default "default")
204+
* `--pending-timeout` int - timeout in seconds for testing pod to stay in pending state (default 60s)
205+
* `--service-account` string - service account to run tests on (default "default")
206+
* `--help` - help for cluster
207+
208+
##### Use
209+
210+
The operator-sdk test command runs go tests embedded in an operator image built using the Operator SDK.
211+
212+
##### Example:
213+
214+
```bash
215+
$ operator-sdk test cluster quay.io/example/memcached-operator:v0.0.1
216+
217+
# Output:
218+
Test Successfully Completed
219+
```
220+
185221
## up
186222

187223
### Available Commands

doc/test-framework/writing-e2e-tests.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,31 +194,73 @@ functions will automatically be run since they were deferred when the TestCtx wa
194194

195195
## Running the Tests
196196

197-
To make running the tests simpler, the `operator-sdk` CLI tool has a `test` subcommand that configures some
198-
default test settings, such as locations of the manifest files for your global resource manifest file (by default `deploy/crd.yaml`) and your namespaced manifest file (by defualt `deploy/rbac.yaml` concatenated with `deploy/operator.yaml`), and allows the user to configure these runtime options. To use it, run the
199-
`operator-sdk test` command in your project root and pass the location of the tests using the
200-
`--test-location` flag. You can use `--help` to view the other configuration options and use
201-
`--go-test-flags` to pass in arguments to `go test`. Here is an example command:
197+
To make running the tests simpler, the `operator-sdk` CLI tool has a `test` subcommand that can configure
198+
default test settings, such as locations of your global resource manifest file (by default
199+
`deploy/crd.yaml`) and your namespaced resource manifest file (by default `deploy/sa.yaml` concatenated with
200+
`deploy/rbac.yaml` and `deploy/operator.yaml`), and allows the user to configure runtime options. There are 2 ways to use the
201+
subcommand: local and cluster.
202+
### Local
203+
To run the tests locally, run the `operator-sdk test local` command in your project root and pass the location of the tests
204+
as an argument. You can use `--help` to view the other configuration options and use `--go-test-flags` to pass in arguments to `go test`. Here is an example command:
202205

203206
```shell
204-
$ operator-sdk test --test-location ./test/e2e --go-test-flags "-v -parallel=2"
207+
$ operator-sdk test local ./test/e2e --go-test-flags "-v -parallel=2"
205208
```
206209

207-
For more documentation on the `operator-sdk test` command, see the [SDK CLI Reference][sdk-cli-ref] doc.
210+
For more documentation on the `operator-sdk test local` command, see the [SDK CLI Reference][sdk-cli-ref] doc.
208211

209212
For advanced use cases, it is possible to run the tests via `go test` directly. As long as all flags defined
210213
in [MainEntry][main-entry-link] are declared, the tests will run correctly. Running the tests directly with missing flags
211-
will result in undefined behavior. This is an example `go test` equivalent to the `operator-sdk test` example above:
214+
will result in undefined behavior. This is an example `go test` equivalent to the `operator-sdk test local` example above:
212215

213216
```shell
214-
# Combine rbac and operator manifest into namespaced manifest
215-
$ cp deploy/rbac.yaml deploy/namespace-init.yaml
217+
# Combine sa, rbac, operator manifest into namespaced manifest
218+
$ cp deploy/sa.yaml deploy/namespace-init.yaml
219+
$ echo -e "\n---\n" >> deploy/namespace-init.yaml
220+
$ cat deploy/rbac.yaml >> deploy/namespace-init.yaml
216221
$ echo -e "\n---\n" >> deploy/namespace-init.yaml
217222
$ cat deploy/operator.yaml >> deploy/namespace-init.yaml
218223
# Run tests
219224
$ go test ./test/e2e/... -root=$(pwd) -kubeconfig=$HOME/.kube/config -globalMan deploy/crd.yaml -namespacedMan deploy/namespace-init.yaml -v -parallel=2
220225
```
221226

227+
### Cluster
228+
229+
Another way to run the tests is from within a kubernetes cluster. To do this, you first need to build an image with
230+
the testing binary embedded by using the `operator-sdk build` command and using the `--enable-tests` flag to enable tests:
231+
232+
```shell
233+
$ operator-sdk build quay.io/example/memcached-operator:v0.0.1 --enable-tests
234+
```
235+
236+
Note that the namespaced yaml must be up to date before running this command. The `build` subcommand will warn you
237+
if it finds a deployment in the namespaced manifest with an image that doesn't match the argument you provided. The
238+
`operator-sdk build` command has other flags for configuring the tests that can be viewed with the `--help` flag
239+
or at the [SDK CLI Reference][sdk-cli-ref].
240+
241+
Once the image is ready, the tests are ready to be run. To run the tests, make sure you have all global resources
242+
and a namespace with proper rbac configured:
243+
244+
```shell
245+
$ kubectl create -f deploy/crd.yaml
246+
$ kubectl create namespace memcached-test
247+
$ kubectl create -f deploy/sa.yaml -n memcached-test
248+
$ kubectl create -f deploy/rbac.yaml -n memcached-test
249+
```
250+
251+
Once you have your environment properly configured, you can start the tests using the `operator-sdk test cluster` command:
252+
253+
```shell
254+
$ operator-sdk test cluster quay.io/example/memcached-operator:v0.0.1 --namespace memcached-test
255+
256+
Example Output:
257+
Test Successfully Completed
258+
```
259+
260+
The `test cluster` command will deploy a test pod in the given namespace that will run the e2e tests packaged in the image.
261+
The command will wait until the tests succeed (pod phase=`Succeeded`) or fail (pod phase=`Failed`).
262+
If the tests fail, the command will output the test pod logs which should be the standard go test error logs.
263+
222264
## Manual Cleanup
223265

224266
While the test framework provides utilities that allow the test to automatically be cleaned up when done,

0 commit comments

Comments
 (0)