Skip to content

Commit efbff31

Browse files
authored
Add support for multiple output formats for live e2e tests (#2555)
1 parent 6bccc37 commit efbff31

File tree

10 files changed

+113
-46
lines changed

10 files changed

+113
-46
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ test-fn-eval: build
8989

9090
# target to run e2e tests for "kpt fn eval" command
9191
test-live-apply: build
92-
PATH=$(GOBIN):$(PATH) go test -v --tags=kind --run=TestLiveApply/testdata/live-apply/$(T) ./e2e/
92+
PATH=$(GOBIN):$(PATH) go test -v -timeout=20m --tags=kind --run=TestLiveApply/testdata/live-apply/$(T) ./e2e/
9393

9494
vet:
9595
go vet ./...

e2e/live_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package e2e
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"path/filepath"
2223
"testing"
@@ -33,12 +34,16 @@ func runTests(t *testing.T, path string) {
3334

3435
for p := range testCases {
3536
c := testCases[p]
36-
t.Run(p, func(t *testing.T) {
37-
(&livetest.Runner{
38-
Config: c,
39-
Path: p,
40-
}).Run(t)
41-
})
37+
for format := range c.Output {
38+
testName := fmt.Sprintf("%s#%s", p, format)
39+
t.Run(testName, func(t *testing.T) {
40+
(&livetest.Runner{
41+
Config: c,
42+
Path: p,
43+
Format: format,
44+
}).Run(t)
45+
})
46+
}
4247
}
4348
}
4449

e2e/testdata/live-apply/apply-depends-on/config.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
preinstallResourceGroup: true
2-
stdOut: |
3-
deployment.apps/first-nginx created
4-
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
5-
deployment.apps/second-nginx created
6-
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
2+
output:
3+
events:
4+
stdOut: |
5+
deployment.apps/first-nginx created
6+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
7+
deployment.apps/second-nginx created
8+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
9+
json:
10+
stdOut: |
11+
{"eventType":"resourceApplied","group":"apps","kind":"Deployment","name":"first-nginx","namespace":"apply-depends-on","operation":"Created","timestamp":"<TIMESTAMP>","type":"apply"}
12+
{"configuredCount":0,"count":1,"createdCount":1,"eventType":"completed","failedCount":0,"serverSideCount":0,"timestamp":"<TIMESTAMP>","type":"apply","unchangedCount":0}
13+
{"eventType":"resourceApplied","group":"apps","kind":"Deployment","name":"second-nginx","namespace":"apply-depends-on","operation":"Created","timestamp":"<TIMESTAMP>","type":"apply"}
14+
{"configuredCount":0,"count":1,"createdCount":1,"eventType":"completed","failedCount":0,"serverSideCount":0,"timestamp":"<TIMESTAMP>","type":"apply","unchangedCount":0}
715
inventory:
816
- group: apps
917
kind: Deployment

e2e/testdata/live-apply/crd-and-cr/config.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
requiresCleanCluster: true
22
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
3+
output:
4+
events:
5+
stdOut: |
6+
customresourcedefinition.apiextensions.k8s.io/customs.kpt.dev created
7+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
8+
custom.kpt.dev/cr created
9+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
10+
json:
11+
stdOut: |
12+
{"eventType":"resourceApplied","group":"apiextensions.k8s.io","kind":"CustomResourceDefinition","name":"customs.kpt.dev","namespace":"","operation":"Created","timestamp":"<TIMESTAMP>","type":"apply"}
13+
{"configuredCount":0,"count":1,"createdCount":1,"eventType":"completed","failedCount":0,"serverSideCount":0,"timestamp":"<TIMESTAMP>","type":"apply","unchangedCount":0}
14+
{"eventType":"resourceApplied","group":"kpt.dev","kind":"Custom","name":"cr","namespace":"","operation":"Created","timestamp":"<TIMESTAMP>","type":"apply"}
15+
{"configuredCount":0,"count":1,"createdCount":1,"eventType":"completed","failedCount":0,"serverSideCount":0,"timestamp":"<TIMESTAMP>","type":"apply","unchangedCount":0}
816
inventory:
917
- group: apiextensions.k8s.io
1018
kind: CustomResourceDefinition

e2e/testdata/live-apply/dry-run-with-install-rg/config.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ requiresCleanCluster: true
22
kptArgs:
33
- "--dry-run"
44
- "--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.
5+
output:
6+
events:
7+
stdOut: |
8+
deployment.apps/nginx-deployment created (dry-run)
9+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed (dry-run)
10+
stdErr: |
11+
installing inventory ResourceGroup CRD.
12+
json:
13+
stdOut: |
14+
{"eventType":"resourceApplied","group":"apps","kind":"Deployment","name":"nginx-deployment","namespace":"dry-run-with-install-rg","operation":"Created","timestamp":"<TIMESTAMP>","type":"apply"}
15+
{"configuredCount":0,"count":1,"createdCount":1,"eventType":"completed","failedCount":0,"serverSideCount":0,"timestamp":"<TIMESTAMP>","type":"apply","unchangedCount":0}
16+
stdErr: |
17+
installing inventory ResourceGroup CRD.
1018
exitCode: 0
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
requiresCleanCluster: true
22
kptArgs:
33
- "--dry-run"
4-
stdErr: |
5-
Error: The ResourceGroup CRD was not found in the cluster. Please install it either by using the '--install-resource-group' flag or the 'kpt live install-resource-group' command.
4+
output:
5+
events:
6+
stdErr: |
7+
Error: The ResourceGroup CRD was not found in the cluster. Please install it either by using the '--install-resource-group' flag or the 'kpt live install-resource-group' command.
68
exitCode: 1

e2e/testdata/live-apply/install-rg-on-apply/config.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
requiresCleanCluster: true
2-
stdErr: |
3-
installing inventory ResourceGroup CRD.
4-
stdOut: |
5-
deployment.apps/nginx-deployment created
6-
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
2+
output:
3+
events:
4+
stdOut: |
5+
deployment.apps/nginx-deployment created
6+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
7+
stdErr: |
8+
installing inventory ResourceGroup CRD.
9+
json:
10+
stdOut: |
11+
{"eventType":"resourceApplied","group":"apps","kind":"Deployment","name":"nginx-deployment","namespace":"install-rg-on-apply","operation":"Created","timestamp":"<TIMESTAMP>","type":"apply"}
12+
{"configuredCount":0,"count":1,"createdCount":1,"eventType":"completed","failedCount":0,"serverSideCount":0,"timestamp":"<TIMESTAMP>","type":"apply","unchangedCount":0}
13+
stdErr: |
14+
installing inventory ResourceGroup CRD.
715
inventory:
816
- group: apps
917
kind: Deployment

e2e/testdata/live-apply/prune-depends-on/config.yaml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
preinstallResourceGroup: true
2-
stdOut: |
3-
configmap/cm created
4-
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
5-
deployment.apps/second-nginx pruned
6-
1 resource(s) pruned, 0 skipped, 0 failed
7-
deployment.apps/first-nginx pruned
8-
1 resource(s) pruned, 0 skipped, 0 failed
2+
output:
3+
events:
4+
stdOut: |
5+
configmap/cm created
6+
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
7+
deployment.apps/second-nginx pruned
8+
1 resource(s) pruned, 0 skipped, 0 failed
9+
deployment.apps/first-nginx pruned
10+
1 resource(s) pruned, 0 skipped, 0 failed
11+
json:
12+
stdOut: |
13+
{"eventType":"resourceApplied","group":"","kind":"ConfigMap","name":"cm","namespace":"prune-depends-on","operation":"Created","timestamp":"<TIMESTAMP>","type":"apply"}
14+
{"configuredCount":0,"count":1,"createdCount":1,"eventType":"completed","failedCount":0,"serverSideCount":0,"timestamp":"<TIMESTAMP>","type":"apply","unchangedCount":0}
15+
{"eventType":"resourcePruned","group":"apps","kind":"Deployment","name":"second-nginx","namespace":"prune-depends-on","operation":"Pruned","timestamp":"<TIMESTAMP>","type":"prune"}
16+
{"eventType":"completed","pruned":1,"skipped":0,"timestamp":"<TIMESTAMP>","type":"prune"}
17+
{"eventType":"resourcePruned","group":"apps","kind":"Deployment","name":"first-nginx","namespace":"prune-depends-on","operation":"Pruned","timestamp":"<TIMESTAMP>","type":"prune"}
18+
{"eventType":"completed","pruned":1,"skipped":0,"timestamp":"<TIMESTAMP>","type":"prune"}
919
inventory:
1020
- kind: ConfigMap
1121
name: cm

pkg/test/live/config.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ type TestCaseConfig struct {
2626
// ExitCode is the expected exit code from the kpt commands. Default: 0
2727
ExitCode int `yaml:"exitCode,omitempty"`
2828

29-
// StdErr is the expected standard error output. Default: ""
30-
StdErr string `yaml:"stdErr,omitempty"`
31-
32-
// StdOut is the expected standard output from running the command.
33-
// Default: ""
34-
StdOut string `yaml:"stdOut,omitempty"`
29+
Output map[string]Output `yaml:"output,omitempty"`
3530

3631
// Inventory is the expected list of resource present in the inventory.
3732
Inventory []InventoryEntry `yaml:"inventory,omitempty"`
@@ -49,6 +44,15 @@ type TestCaseConfig struct {
4944
KptArgs []string `yaml:"kptArgs,omitempty"`
5045
}
5146

47+
type Output struct {
48+
// StdErr is the expected standard error output. Default: ""
49+
StdErr string `yaml:"stdErr,omitempty"`
50+
51+
// StdOut is the expected standard output from running the command.
52+
// Default: ""
53+
StdOut string `yaml:"stdOut,omitempty"`
54+
}
55+
5256
// InventoryEntry defines an entry in an inventory list.
5357
type InventoryEntry struct {
5458
Group string `yaml:"group,omitempty"`

pkg/test/live/runner.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"os/exec"
2424
"path/filepath"
25+
"regexp"
2526
"sort"
2627
"strings"
2728
"testing"
@@ -43,6 +44,9 @@ type Runner struct {
4344

4445
// Path provides the path to the test files.
4546
Path string
47+
48+
// Format provides the output format that should be used.
49+
Format string
4650
}
4751

4852
// Run executes the test.
@@ -77,7 +81,7 @@ func (r *Runner) Run(t *testing.T) {
7781

7882
r.RunPreApply(t)
7983

80-
stdout, stderr, err := r.RunApply()
84+
stdout, stderr, err := r.RunApply(t)
8185
r.VerifyExitCode(t, err)
8286
r.VerifyStdout(t, stdout)
8387
r.VerifyStderr(t, stderr)
@@ -102,8 +106,12 @@ func (r *Runner) RunPreApply(t *testing.T) {
102106
}
103107
}
104108

105-
func (r *Runner) RunApply() (string, string, error) {
109+
func (r *Runner) RunApply(t *testing.T) (string, string, error) {
106110
args := append([]string{"live", "apply"}, r.Config.KptArgs...)
111+
if r.Format != "" {
112+
args = append(args, "--output", r.Format)
113+
}
114+
t.Logf("Running command: kpt %s", strings.Join(args, " "))
107115
cmd := exec.Command("kpt", args...)
108116
cmd.Dir = filepath.Join(r.Path, "resources")
109117

@@ -220,11 +228,11 @@ func (r *Runner) VerifyExitCode(t *testing.T, err error) {
220228
}
221229

222230
func (r *Runner) VerifyStdout(t *testing.T, stdout string) {
223-
assert.Equal(t, strings.TrimSpace(r.Config.StdOut), strings.TrimSpace(stdout))
231+
assert.Equal(t, strings.TrimSpace(r.Config.Output[r.Format].StdOut), strings.TrimSpace(substituteTimestamps(stdout)))
224232
}
225233

226234
func (r *Runner) VerifyStderr(t *testing.T, stderr string) {
227-
assert.Equal(t, strings.TrimSpace(r.Config.StdErr), strings.TrimSpace(stderr))
235+
assert.Equal(t, strings.TrimSpace(r.Config.Output[r.Format].StdErr), strings.TrimSpace(substituteTimestamps(stderr)))
228236
}
229237

230238
func (r *Runner) VerifyInventory(t *testing.T, name, namespace string) {
@@ -291,3 +299,9 @@ func inventorySortFunc(inv []InventoryEntry) func(i, j int) bool {
291299
return iInv.Namespace < jInv.Namespace
292300
}
293301
}
302+
303+
var timestampRegexp = regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z`)
304+
305+
func substituteTimestamps(text string) string {
306+
return timestampRegexp.ReplaceAllString(text, "<TIMESTAMP>")
307+
}

0 commit comments

Comments
 (0)