-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
146 lines (131 loc) · 3.84 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"os"
"testing"
)
func TestGetInterval(t *testing.T) {
// Env BAR is not set
omitted := getConfigInterval("BAR")
if omitted != "60m" {
t.Errorf("Expect 60m as default if BAR is not set")
}
// Env FOO is set to "10"
os.Setenv("FOO", "10")
interval := getConfigInterval("FOO")
if interval != "10m" {
t.Errorf("Expect %vm, get %v", interval, interval)
}
// Env FOO is set to "10m"
os.Setenv("FOO", "10m")
interval = getConfigInterval("FOO")
if interval != "10m" {
t.Errorf("Expect %v, get %v", os.Getenv("FOO"), interval)
}
}
func TestGetJSONLog(t *testing.T) {
// Env JSONLOG is not set
result := getConfigLogFormat("JSONLOG")
if result != false {
t.Errorf("Expect FALSE as default if JSONLOG is not set")
}
// Env JSONLOG is set to "1"
os.Setenv("JSONLOG", "1")
result = getConfigLogFormat("JSONLOG")
if result != true {
t.Errorf("Expect TRUE as JSONLOG is set to \"1\"")
}
// Env JSONLOG is set to "true"
os.Setenv("JSONLOG", "true")
result = getConfigLogFormat("JSONLOG")
if result != true {
t.Errorf("Expect TRUE as JSONLOG is set to \"true\"")
}
// Env JSONLOG set to anything except "1" or "true"
os.Setenv("JSONLOG", "whatever")
result = getConfigLogFormat("JSONLOG")
if result != false {
t.Errorf("Expect FALSE as JSONLOG is not set to \"1\" or \"true\"")
}
}
func TestGetImages(t *testing.T) {
// INPUT: Env IMAGES is not set"
// EXPECTED OUTPUT, ok is false
var expected []string
result, ok := getImages("IMAGES")
if ok {
t.Errorf("The \"ok\" flag should be \"false\"")
}
if !testEqual(result, expected) {
t.Errorf("Empty input test, Result is not as expected")
}
// INPUT: Env IMAGES is set to 1 image without tag "busybox"
os.Setenv("IMAGES", "busybox")
// EXPECTED OUTPUT
expected = []string{"busybox"}
if result, ok := getImages("IMAGES"); ok {
if !testEqual(result, expected) {
t.Errorf("Single image test, Result is not as expected")
}
} else {
t.Errorf("IMAGES un-parsable")
}
// INPUT: Env IMAGES is set to multiple images with tag "busybox:latest,alpine:latest"
os.Setenv("IMAGES", "busybox:latest,alpine:latest")
// EXPECTED OUTPUT
expected = []string{"busybox:latest", "alpine:latest"}
if result, ok := getImages("IMAGES"); ok {
if !testEqual(result, expected) {
t.Errorf("Multiple image with tag, Result is not as expected")
}
} else {
t.Errorf("IMAGES un-parsable")
}
// INPUT: Env IMAGES is set to multiple images with tag "[busybox:latest,alpine:latest,:;]"
os.Setenv("IMAGES", "[busybox:latest,alpine:latest,:;]")
// EXPECTED OUTPUT
expected = []string{"busybox:latest", "alpine:latest"}
if result, ok := getImages("IMAGES"); ok {
if !testEqual(result, expected) {
t.Errorf("Multiple image with tag, Result is not as expected")
}
} else {
t.Errorf("IMAGES un-parsable")
}
// INPUT: Env IMAGES is set to multiple images with tag "[busybox:latest,alpine:latest]"
os.Setenv("IMAGES", "[busybox:latest,alpine:latest]")
// EXPECTED OUTPUT
expected = []string{"busybox:latest", "alpine:latest"}
if result, ok := getImages("IMAGES"); ok {
if !testEqual(result, expected) {
t.Errorf("Multiple image with tag, Result is not as expected")
}
} else {
t.Errorf("IMAGES un-parsable")
}
// INPUT: Env IMAGES is set to multiple images with tag "{busybox:latest,alpine:latest}"
os.Setenv("IMAGES", "{busybox:latest,alpine:latest}")
// EXPECTED OUTPUT
expected = []string{"busybox:latest", "alpine:latest"}
if result, ok := getImages("IMAGES"); ok {
if !testEqual(result, expected) {
t.Errorf("Multiple image with tag, Result is not as expected")
}
} else {
t.Errorf("IMAGES un-parsable")
}
}
func testEqual(a, b []string) bool {
// If one is nil, the other must also be nil.
if (a == nil) != (b == nil) {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}