Skip to content
6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ tasks:
cmds:
- go test -race -coverprofile=coverage.txt -covermode=atomic -v ./cmd/... {{ .CLI_ARGS }}

test-e2e:
desc: Run Integration tests
Comment thread
rocktavious marked this conversation as resolved.
Outdated
dir: "{{.SRC_DIR}}"
cmds:
- go test -race -v ./e2e/... {{ .CLI_ARGS }}

update-opslevel-go:
desc: Update opslevel-go version to latest release
dir: "{{.SRC_DIR}}"
Expand Down
59 changes: 59 additions & 0 deletions src/e2e/domain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package e2e

import (
"strings"
"testing"
)

func TestDomainHappyPath(t *testing.T) {
createInput := `
name: "Integration Test Domain"
description: "Created by integration test"
`
updateInput1 := `
name: "Integration Test Domain Updated"
description: "Updated by integration test"
`
updateInput2 := `
name: "Integration Test Domain Updated Again"
description: null
`
domainName := "Integration Test Domain"
updatedDomainName := "Integration Test Domain Updated"
updatedAgainDomainName := "Integration Test Domain Updated Again"

test := CLITest{
Create: Create("create domain -f -", createInput),
Get: Get("get domain"),
Delete: Delete("delete domain"),
Steps: []Step{
func(u *Utility) {
out, err := u.Run("list domain")
if err != nil || !strings.Contains(out, domainName) {
u.Fatalf("list failed: %v\nout: %s", err, out)
}
},
func(u *Utility) {
out, err := u.Run("update domain "+u.ID+" -f -", updateInput1)
Comment thread
rocktavious marked this conversation as resolved.
Outdated
if err != nil {
u.Fatalf("update1 failed: %v\nout: %s", err, out)
}
out, err = u.Run("get domain " + u.ID)
if err != nil || !strings.Contains(out, updatedDomainName) || !strings.Contains(out, "Updated by integration test") {
u.Fatalf("get after update1 failed: %v\nout: %s", err, out)
}
},
func(u *Utility) {
out, err := u.Run("update domain "+u.ID+" -f -", updateInput2)
Comment thread
rocktavious marked this conversation as resolved.
Outdated
if err != nil {
u.Fatalf("update2 (unset) failed: %v\nout: %s", err, out)
}
out, err = u.Run("get domain " + u.ID)
if err != nil || !strings.Contains(out, updatedAgainDomainName) || strings.Contains(out, "Updated by integration test") {
u.Fatalf("get after update2 failed (description should be unset): %v\nout: %s", err, out)
}
},
},
}
test.Run(t)
}
95 changes: 95 additions & 0 deletions src/e2e/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package e2e

import (
"bytes"
"os"
"os/exec"
"strings"
"testing"
)

type Utility struct {
*testing.T
ID string // The resource ID created by Create
}

// 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.
func (u *Utility) Run(args string, stdin ...string) (string, error) {
cmd := exec.Command("go", append([]string{"run", "main.go"}, strings.Split(args, " ")...)...)
cmd.Dir = ".."
cmd.Env = os.Environ()
var out bytes.Buffer
var errBuf bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errBuf
if len(stdin) > 0 {
cmd.Stdin = strings.NewReader(stdin[0])
}
err := cmd.Run()
return out.String() + errBuf.String(), err
}

type Step func(*Utility)

type CLITest struct {
Create Step
Get [2]Step
Delete Step
Steps []Step
}

func (ct *CLITest) Run(t *testing.T) {
util := &Utility{T: t}

defer func() {
if util.ID != "" {
ct.Delete(util)
}
}()

ct.Create(util)
ct.Get[0](util) // Should exist after create
for _, step := range ct.Steps {
step(util)
}
ct.Delete(util)
ct.Get[1](util) // Should not exist after delete
util.ID = "" // Mark as deleted so defer doesn't try again
}

func Create(cmd string, input string) Step {
return func(u *Utility) {
out, err := u.Run(cmd, input)
if err != nil {
u.Fatalf("create failed: %v\nout: %s", err, out)
}
u.ID = strings.TrimSpace(out)
if u.ID == "" {
u.Fatalf("expected ID, got: %q", out)
}
}
}

func Get(cmd string) [2]Step {
return [2]Step{func(u *Utility) {
out, err := u.Run(cmd + " " + u.ID)
if err != nil {
u.Fatalf("get failed: %v\nout: %s", err, out)
}
}, func(u *Utility) {
out, err := u.Run(cmd + " " + u.ID)
lower := strings.ToLower(out)
if err == nil || !(strings.Contains(lower, "not found") || strings.Contains(lower, "missing") || strings.Contains(lower, "gone")) {
u.Fatalf("expected get after delete to fail with not found, got: %v\nout: %s", err, out)
}
}}
}

func Delete(cmd string) Step {
return func(u *Utility) {
out, err := u.Run(cmd + " " + u.ID)
if err != nil {
u.Fatalf("delete failed: %v\nout: %s", err, out)
}
}
}
Loading