-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
108 lines (97 loc) · 2.45 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
)
type artifactTest struct {
expectedOutput string
expectedStdout string
expectedStderr string
expectedStdoutStderr string
}
func checkArtifacts(t *testing.T, test artifactTest) {
content, _ := ioutil.ReadFile("artifacts/analysis/output")
if string(content) != test.expectedOutput {
t.Errorf(fmt.Sprintf("expected artifacts/analysis/output to contain %s, but got %s", test.expectedOutput, content))
}
stdout, _ := ioutil.ReadFile("artifacts/analysis/stdout")
if string(stdout) != test.expectedStdout {
t.Errorf(fmt.Sprintf("expected to log %s to artifacts/analysis/stdout, but got: %s", test.expectedStdout, stdout))
}
stderr, _ := ioutil.ReadFile("artifacts/analysis/stderr")
if string(stderr) != test.expectedStderr {
t.Errorf(fmt.Sprintf("expected to log %s to artifacts/analysis/stderr, but got: %s", test.expectedStderr, stderr))
}
stdoutStderr, _ := ioutil.ReadFile("artifacts/analysis/stdoutStderr")
if string(stdoutStderr) != test.expectedStdoutStderr {
t.Errorf(fmt.Sprintf("expected to log %s to artifacts/analysis/stdoutStderr, but got: %s", test.expectedStdoutStderr, stdoutStderr))
}
}
func makeBinary() {
cmd := exec.Command(
"go",
"build",
"-o",
"az",
)
cmd.Run()
}
func cleanup() {
os.Remove("./az")
os.RemoveAll("artifacts")
}
func TestMain(m *testing.M) {
fmt.Println("... creating analysis binary ...")
makeBinary()
code := m.Run()
fmt.Println("... removing analysis binary ...")
cleanup()
os.Exit(code)
}
func TestExitcodeSuccess(t *testing.T) {
config := "infra foo"
cmd := exec.Command(
"./az",
"--type",
"exitcode",
"--config",
config,
"fixtures/exit_zero.sh",
)
cmd.Run()
code := cmd.ProcessState.ExitCode()
if code != 0 {
t.Errorf("expected process to exit with code 0")
}
checkArtifacts(t, artifactTest{
expectedOutput: "success",
expectedStdout: "foo\n",
expectedStderr: "bar\n",
expectedStdoutStderr: "foo\nbar\n",
})
}
func TestExitcodeFailure(t *testing.T) {
config := "infra foo"
cmd := exec.Command(
"./az",
"--type",
"exitcode",
"--config",
config,
"fixtures/exit_nonzero.sh",
)
cmd.Run()
code := cmd.ProcessState.ExitCode()
if code != 1 {
t.Errorf("expected process to exit with code 1")
}
checkArtifacts(t, artifactTest{
expectedOutput: config,
expectedStdout: "foo\n",
expectedStderr: "bar\n",
expectedStdoutStderr: "foo\nbar\n",
})
}