-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_test.go
122 lines (102 loc) · 4.41 KB
/
check_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"os"
"os/exec"
"strings"
"testing"
)
func TestMain(m *testing.M) {
os.Unsetenv(livenessCommandEnvName)
os.Unsetenv(readinessCommandEnvName)
os.Remove(livenessStateFileName)
os.Remove(readinessStateFileName)
code := m.Run()
os.Exit(code)
}
func runAndExpectExit(t *testing.T, command string, exitCodeShouldBeZero bool) string {
cmd := exec.Command("sh", "-c", command)
output, err := cmd.Output()
if e, ok := err.(*exec.ExitError); ok {
if exitCodeShouldBeZero != e.Success() {
t.Fatal("$", command, " execution success = ", e.Success(), " but expected = ", exitCodeShouldBeZero)
}
} else if !exitCodeShouldBeZero {
t.Fatal("$", command, " should not return exit code 0, but did anyway")
}
return string(output)
}
func runAndExpectOutputAndExit(t *testing.T, command string, exitCodeShouldBeZero bool, stringToContain string) {
actualOutput := runAndExpectExit(t, command, exitCodeShouldBeZero)
if !strings.Contains(actualOutput, stringToContain) {
t.Fatalf(command, " did not contain '", stringToContain, "', actual output was '", actualOutput, "'")
}
}
func TestPlainCall(t *testing.T) {
runAndExpectOutputAndExit(t, "./check", false, "Missing subcommand")
}
func TestLivenessNotDefined(t *testing.T) {
runAndExpectOutputAndExit(t, "./check run -type liveness", false, "must be set")
}
func TestLivenessFails(t *testing.T) {
os.Setenv(livenessCommandEnvName, "false")
runAndExpectOutputAndExit(t, "./check run -type liveness", false, "FAILURE")
}
func TestLivenessSucceeds(t *testing.T) {
os.Setenv(livenessCommandEnvName, "true")
runAndExpectOutputAndExit(t, "./check run -type liveness", true, "SUCCESS")
}
func TestLivenessLockedFailure(t *testing.T) {
os.Setenv(livenessCommandEnvName, "true")
runAndExpectOutputAndExit(t, "./check lock -type liveness -state failure", true, "")
runAndExpectOutputAndExit(t, "./check run -type liveness", false, "locked")
}
func TestLivenessLockedSuccess(t *testing.T) {
os.Setenv(livenessCommandEnvName, "false")
runAndExpectOutputAndExit(t, "./check lock -type liveness -state success", true, "")
runAndExpectOutputAndExit(t, "./check run -type liveness", true, "locked")
}
func TestLivenessUnlockedFailure(t *testing.T) {
os.Setenv(livenessCommandEnvName, "true")
runAndExpectOutputAndExit(t, "./check lock -type liveness -state failure", true, "")
runAndExpectOutputAndExit(t, "./check unlock -type liveness", true, "")
runAndExpectOutputAndExit(t, "./check run -type liveness", true, "SUCCESS")
}
func TestLivenessUnlockedSuccess(t *testing.T) {
os.Setenv(livenessCommandEnvName, "false")
runAndExpectOutputAndExit(t, "./check lock -type liveness -state success", true, "")
runAndExpectOutputAndExit(t, "./check unlock -type liveness", true, "")
runAndExpectOutputAndExit(t, "./check run -type liveness", false, "FAILURE")
}
func TestReadinessNotDefined(t *testing.T) {
runAndExpectOutputAndExit(t, "./check run -type Readiness", false, "must be set")
}
func TestReadinessFails(t *testing.T) {
os.Setenv(readinessCommandEnvName, "false")
runAndExpectOutputAndExit(t, "./check run -type Readiness", false, "FAILURE")
}
func TestReadinessSucceeds(t *testing.T) {
os.Setenv(readinessCommandEnvName, "true")
runAndExpectOutputAndExit(t, "./check run -type Readiness", true, "SUCCESS")
}
func TestReadinessLockedFailure(t *testing.T) {
os.Setenv(readinessCommandEnvName, "true")
runAndExpectOutputAndExit(t, "./check lock -type Readiness -state failure", true, "")
runAndExpectOutputAndExit(t, "./check run -type Readiness", false, "locked")
}
func TestReadinessLockedSuccess(t *testing.T) {
os.Setenv(readinessCommandEnvName, "false")
runAndExpectOutputAndExit(t, "./check lock -type Readiness -state success", true, "")
runAndExpectOutputAndExit(t, "./check run -type Readiness", true, "locked")
}
func TestReadinessUnlockedFailure(t *testing.T) {
os.Setenv(readinessCommandEnvName, "true")
runAndExpectOutputAndExit(t, "./check lock -type Readiness -state failure", true, "")
runAndExpectOutputAndExit(t, "./check unlock -type Readiness", true, "")
runAndExpectOutputAndExit(t, "./check run -type Readiness", true, "SUCCESS")
}
func TestReadinessUnlockedSuccess(t *testing.T) {
os.Setenv(readinessCommandEnvName, "false")
runAndExpectOutputAndExit(t, "./check lock -type Readiness -state success", true, "")
runAndExpectOutputAndExit(t, "./check unlock -type Readiness", true, "")
runAndExpectOutputAndExit(t, "./check run -type Readiness", false, "FAILURE")
}