|
| 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