|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRelease(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + cli string |
| 18 | + wantFormat format |
| 19 | + wantLines int |
| 20 | + wantAPIRequests []string |
| 21 | + ignoreCLIOutput bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "release-ls", |
| 25 | + cli: "release ls", |
| 26 | + wantFormat: FormatTable, |
| 27 | + wantLines: 1, |
| 28 | + wantAPIRequests: []string{ |
| 29 | + "GET:/v3/app/id/releases", |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + for _, tt := range tests { |
| 35 | + t.Run(tt.name, func(t *testing.T) { |
| 36 | + args := strings.Split(tt.cli, " ") |
| 37 | + args = append(args, "--integration-test", tt.name) |
| 38 | + |
| 39 | + apiCallLog, err := os.CreateTemp("", "") |
| 40 | + if err != nil { |
| 41 | + log.Fatal(err) |
| 42 | + } |
| 43 | + |
| 44 | + defer os.RemoveAll(apiCallLog.Name()) |
| 45 | + |
| 46 | + args = append(args, "--log-api-calls", apiCallLog.Name()) |
| 47 | + |
| 48 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 49 | + defer cancel() |
| 50 | + |
| 51 | + cmd := exec.CommandContext(ctx, CLIPath(), args...) |
| 52 | + |
| 53 | + if ctx.Err() == context.DeadlineExceeded { |
| 54 | + t.Fatalf("Command execution timed out") |
| 55 | + } |
| 56 | + |
| 57 | + out, err := cmd.CombinedOutput() |
| 58 | + if err != nil { |
| 59 | + t.Errorf("error running cli: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + fmt.Printf("out: %s\n", string(out)) |
| 63 | + if !tt.ignoreCLIOutput { |
| 64 | + AssertCLIOutput(t, string(out), tt.wantFormat, tt.wantLines) |
| 65 | + } |
| 66 | + |
| 67 | + AssertAPIRequests(t, tt.wantAPIRequests, apiCallLog.Name()) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
0 commit comments