Skip to content

Commit 4764bdc

Browse files
committed
Setup e2e tests for domain
1 parent 32c5b15 commit 4764bdc

3 files changed

Lines changed: 160 additions & 0 deletions

File tree

Taskfile.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ tasks:
5656
cmds:
5757
- go test -race -coverprofile=coverage.txt -covermode=atomic -v ./cmd/... {{ .CLI_ARGS }}
5858

59+
test-e2e:
60+
desc: Run Integration tests
61+
dir: "{{.SRC_DIR}}"
62+
cmds:
63+
- go test -race -v ./e2e/... {{ .CLI_ARGS }}
64+
5965
update-opslevel-go:
6066
desc: Update opslevel-go version to latest release
6167
dir: "{{.SRC_DIR}}"

src/e2e/domain_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package e2e
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestDomainHappyPath(t *testing.T) {
9+
createInput := `
10+
name: "Integration Test Domain"
11+
description: "Created by integration test"
12+
`
13+
updateInput1 := `
14+
name: "Integration Test Domain Updated"
15+
description: "Updated by integration test"
16+
`
17+
updateInput2 := `
18+
name: "Integration Test Domain Updated Again"
19+
description: null
20+
`
21+
domainName := "Integration Test Domain"
22+
updatedDomainName := "Integration Test Domain Updated"
23+
updatedAgainDomainName := "Integration Test Domain Updated Again"
24+
25+
test := CLITest{
26+
Create: Create("create domain -f -", createInput),
27+
Get: Get("get domain"),
28+
Delete: Delete("delete domain"),
29+
Steps: []Step{
30+
func(u *Utility) {
31+
out, err := u.Run("list domain")
32+
if err != nil || !strings.Contains(out, domainName) {
33+
u.Fatalf("list failed: %v\nout: %s", err, out)
34+
}
35+
},
36+
func(u *Utility) {
37+
out, err := u.Run("update domain "+u.ID+" -f -", updateInput1)
38+
if err != nil {
39+
u.Fatalf("update1 failed: %v\nout: %s", err, out)
40+
}
41+
out, err = u.Run("get domain " + u.ID)
42+
if err != nil || !strings.Contains(out, updatedDomainName) || !strings.Contains(out, "Updated by integration test") {
43+
u.Fatalf("get after update1 failed: %v\nout: %s", err, out)
44+
}
45+
},
46+
func(u *Utility) {
47+
out, err := u.Run("update domain "+u.ID+" -f -", updateInput2)
48+
if err != nil {
49+
u.Fatalf("update2 (unset) failed: %v\nout: %s", err, out)
50+
}
51+
out, err = u.Run("get domain " + u.ID)
52+
if err != nil || !strings.Contains(out, updatedAgainDomainName) || strings.Contains(out, "Updated by integration test") {
53+
u.Fatalf("get after update2 failed (description should be unset): %v\nout: %s", err, out)
54+
}
55+
},
56+
},
57+
}
58+
test.Run(t)
59+
}

src/e2e/helpers.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
)
10+
11+
type Utility struct {
12+
*testing.T
13+
ID string // The resource ID created by Create
14+
}
15+
16+
// Run executes the CLI using 'go run main.go' from the ./src directory with the given arguments and optional stdin, returning combined output and error.
17+
func (u *Utility) Run(args string, stdin ...string) (string, error) {
18+
cmd := exec.Command("go", append([]string{"run", "main.go"}, strings.Split(args, " ")...)...)
19+
cmd.Dir = ".."
20+
cmd.Env = os.Environ()
21+
var out bytes.Buffer
22+
var errBuf bytes.Buffer
23+
cmd.Stdout = &out
24+
cmd.Stderr = &errBuf
25+
if len(stdin) > 0 {
26+
cmd.Stdin = strings.NewReader(stdin[0])
27+
}
28+
err := cmd.Run()
29+
return out.String() + errBuf.String(), err
30+
}
31+
32+
type Step func(*Utility)
33+
34+
type CLITest struct {
35+
Create Step
36+
Get [2]Step
37+
Delete Step
38+
Steps []Step
39+
}
40+
41+
func (ct *CLITest) Run(t *testing.T) {
42+
util := &Utility{T: t}
43+
44+
defer func() {
45+
if util.ID != "" {
46+
ct.Delete(util)
47+
}
48+
}()
49+
50+
ct.Create(util)
51+
ct.Get[0](util) // Should exist after create
52+
for _, step := range ct.Steps {
53+
step(util)
54+
}
55+
ct.Delete(util)
56+
ct.Get[1](util) // Should not exist after delete
57+
util.ID = "" // Mark as deleted so defer doesn't try again
58+
}
59+
60+
func Create(cmd string, input string) Step {
61+
return func(u *Utility) {
62+
out, err := u.Run(cmd, input)
63+
if err != nil {
64+
u.Fatalf("create failed: %v\nout: %s", err, out)
65+
}
66+
u.ID = strings.TrimSpace(out)
67+
if u.ID == "" {
68+
u.Fatalf("expected ID, got: %q", out)
69+
}
70+
}
71+
}
72+
73+
func Get(cmd string) [2]Step {
74+
return [2]Step{func(u *Utility) {
75+
out, err := u.Run(cmd + " " + u.ID)
76+
if err != nil {
77+
u.Fatalf("get failed: %v\nout: %s", err, out)
78+
}
79+
}, func(u *Utility) {
80+
out, err := u.Run(cmd + " " + u.ID)
81+
lower := strings.ToLower(out)
82+
if err == nil || !(strings.Contains(lower, "not found") || strings.Contains(lower, "missing") || strings.Contains(lower, "gone")) {
83+
u.Fatalf("expected get after delete to fail with not found, got: %v\nout: %s", err, out)
84+
}
85+
}}
86+
}
87+
88+
func Delete(cmd string) Step {
89+
return func(u *Utility) {
90+
out, err := u.Run(cmd + " " + u.ID)
91+
if err != nil {
92+
u.Fatalf("delete failed: %v\nout: %s", err, out)
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)