Skip to content

Commit 1737e47

Browse files
authored
New e2e tests for kpt live (#2390)
* New e2e tests for kpt live * Updated
1 parent eb8c41e commit 1737e47

File tree

18 files changed

+654
-0
lines changed

18 files changed

+654
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ test-fn-render: build
8484
test-fn-eval: build
8585
PATH=$(GOBIN):$(PATH) go test -v --tags=docker --run=TestFnEval/testdata/fn-eval/$(T) ./e2e/
8686

87+
# target to run e2e tests for "kpt fn eval" command
88+
test-live-apply: build
89+
PATH=$(GOBIN):$(PATH) go test -v --tags=kind --run=TestLiveApply/testdata/live-apply/$(T) ./e2e/
90+
8791
vet:
8892
go vet ./...
8993

e2e/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# End-to-end testing of kpt
2+
3+
## kpt live e2e tests
4+
5+
We currently have two different solutions for running e2e tests for the kpt
6+
live functionality. We are working on reconciling this into one approach that
7+
we can use consistently.
8+
9+
All e2e tests for live requires that kind is available.
10+
11+
### Testing with go
12+
13+
We have a framework for running e2e tests based on specifying test cases
14+
under testdata/live-apply folder (tests for other kpt live commands will be
15+
added). The entry point for the test framework is in
16+
the `live_test.go` file.
17+
18+
In order to run all the tests for live apply, there is a make target
19+
```sh
20+
make test-live-apply
21+
```
22+
23+
It is possible to run a single test by specifying the name of the test case:
24+
```sh
25+
make test-live-apply T=crd-and-cr
26+
```
27+
28+
#### Structure of a test case
29+
30+
Each test case is a folder directly under `testdata/live-apply`. In the root
31+
of each test case folder there must be a `config.yaml` file that provides the
32+
configuration of the test case (like whether a clean cluster is required and
33+
the expected output). The package that will be applied with `kpt live apply` is
34+
provided in the `resources` folder.
35+
36+
#### Configuration options
37+
38+
These are the configuration options available in the `config.yaml` file:
39+
* `exitCode`: Defines the expected exit code after running the kpt live command. Defaults to 0.
40+
* `stdErr`: Defines the expected output to stdErr after running the command. Defaults to "".
41+
* `stdOut`: Defines the expected output to stdOut after running the command. Defaults to "".
42+
* `inventory`: Defines the expected inventory after running the command.
43+
* `requiresCleanCluster`: Defines whether a new kind cluster should be created prior to running the test.
44+
* `preinstallResourceGroup`: Defines whether the framework should make sure the RG CRD is available before running the test.
45+
* `kptArgs`: Defines the arguments that will be used with executing the kpt live command.
46+
47+
## Testing with bash
48+
49+
This approach uses a bash script that runs through several scenarios for
50+
kpt live in sequence. Run it by running
51+
```sh
52+
./live/end-to-end-test.sh
53+
```

e2e/live_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// +build kind
2+
3+
// Copyright 2021 Google LLC
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package e2e
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
livetest "github.com/GoogleContainerTools/kpt/pkg/test/live"
25+
)
26+
27+
func TestLiveApply(t *testing.T) {
28+
runTests(t, filepath.Join(".", "testdata", "live-apply"))
29+
}
30+
31+
func runTests(t *testing.T, path string) {
32+
testCases := scanTestCases(t, path)
33+
34+
for p := range testCases {
35+
c := testCases[p]
36+
t.Run(p, func(t *testing.T) {
37+
(&livetest.Runner{
38+
Config: c,
39+
Path: p,
40+
}).Run(t)
41+
})
42+
}
43+
}
44+
45+
func scanTestCases(t *testing.T, path string) map[string]livetest.TestCaseConfig {
46+
testCases := make(map[string]livetest.TestCaseConfig)
47+
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
48+
if err != nil {
49+
return err
50+
}
51+
52+
if !info.IsDir() {
53+
return nil
54+
}
55+
if path == p {
56+
return nil
57+
}
58+
59+
testCases[p] = livetest.ReadTestCaseConfig(t, p)
60+
return filepath.SkipDir
61+
})
62+
if err != nil {
63+
t.Fatalf("failed to scan for test cases in %s", path)
64+
}
65+
return testCases
66+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
requiresCleanCluster: true
2+
preinstallResourceGroup: true
3+
stdOut: |
4+
customresourcedefinition.apiextensions.k8s.io/customs.kpt.dev created
5+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
6+
custom.kpt.dev/cr created
7+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
8+
inventory:
9+
- group: apiextensions.k8s.io
10+
kind: CustomResourceDefinition
11+
name: customs.kpt.dev
12+
- group: kpt.dev
13+
kind: Custom
14+
name: cr
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: kpt.dev/v1
2+
kind: Kptfile
3+
metadata:
4+
name: crd-and-cr
5+
inventory:
6+
namespace: crd-and-cr
7+
name: crd-and-cr
8+
inventoryID: crd-and-cr
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: kpt.dev/v1
2+
kind: Custom
3+
metadata:
4+
name: cr
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: customs.kpt.dev
5+
spec:
6+
conversion:
7+
strategy: None
8+
group: kpt.dev
9+
names:
10+
kind: Custom
11+
listKind: CustomList
12+
plural: customs
13+
singular: custom
14+
scope: Cluster
15+
versions:
16+
- name: v1
17+
schema:
18+
openAPIV3Schema:
19+
properties:
20+
apiVersion:
21+
type: string
22+
kind:
23+
type: string
24+
metadata:
25+
type: object
26+
type: object
27+
served: true
28+
storage: true
29+
subresources: {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
requiresCleanCluster: true
2+
kptArgs:
3+
- "--dry-run"
4+
- "--install-resource-group"
5+
stdOut: |
6+
deployment.apps/nginx-deployment created (dry-run)
7+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed (dry-run)
8+
stdErr: |
9+
installing inventory ResourceGroup CRD.
10+
exitCode: 0
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: kpt.dev/v1
2+
kind: Kptfile
3+
metadata:
4+
name: dry-run-with-install-rg
5+
inventory:
6+
namespace: dry-run-with-install-rg
7+
name: dry-run-with-install-rg
8+
inventoryID: dry-run-with-install-rg
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment
5+
namespace: dry-run-with-install-rg
6+
labels:
7+
app: nginx-deployment
8+
spec:
9+
strategy:
10+
rollingUpdate:
11+
maxUnavailable: 1
12+
maxSurge: 0
13+
replicas: 2
14+
selector:
15+
matchLabels:
16+
app: nginx-deployment
17+
template:
18+
metadata:
19+
labels:
20+
app: nginx-deployment
21+
pdb: mypdb
22+
spec:
23+
containers:
24+
- name: nginx
25+
image: nginx:1.7.10
26+
ports:
27+
- containerPort: 80
28+
protocol: TCP

0 commit comments

Comments
 (0)