-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.go
212 lines (167 loc) · 5.56 KB
/
run_tests.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
term "github.com/buildkite/terminal"
au "github.com/logrusorgru/aurora"
)
// callRunTests is extracted from main() as it is called twice
// don't panic on error
func callRunTests(param RunTestsParam) {
// run tests first
err := runTests(param)
if err != nil {
fmt.Printf("%s: %s\n", au.Bold(au.Red("Error")), err.Error())
return
}
// then write out dashboard as static page
var pageBuffer string
history, err := getHistoryData(param.outputdir)
if err != nil {
sData := fmt.Sprintf("<p>Can't display dashboard: %s</p>", err.Error())
pageBuffer = fmt.Sprintf(pageMinimal(param.context, sData))
return
}
timeSummary := fmt.Sprintf("%s (%s)", history.lastRecord.Time, formatDuration(int64(history.lastRecord.Duration)))
terminal := history.logHead
terminal += strings.Join(history.logEntries, "\n")
terminal += "\n\n"
terminal += fmt.Sprintf("%s", au.Bold(au.Gray(timeSummary)))
terminal += "\n"
terminalBytes := []byte(terminal)
var chart01, chart02, chart03 string
chart01 = fmt.Sprintf(staticTextVis01, history.jsonResults, history.maxTests)
if param.duration {
chart02 = fmt.Sprintf(staticTextVis02, history.jsonDurations)
}
if param.histogram {
chart03 = fmt.Sprintf(staticTextVis03, history.jsonHistogram)
}
log := fmt.Sprintf(`<div class="term-container">%s</div>`, string(term.Render(terminalBytes)))
bgColorClass := "bg-success"
if history.lastRecord.Fail > 0 {
bgColorClass = "bg-danger"
}
pageBuffer = fmt.Sprintf(page(param.context, chart01, chart02, chart03, log, bgColorClass))
filename := fmt.Sprintf("%s/index.html", param.outputdir)
err = ioutil.WriteFile(filename, []byte(pageBuffer), 0644)
if err != nil {
fmt.Printf("can't write index file %s (%s)", filename, err.Error())
}
}
func runTests(param RunTestsParam) error {
// cleanup first
purgeOutput(param.outputdir, param.retain)
// update ignore list
ignoreSet := getIgnoreSet(param.datadir)
tests, err := filepath.Glob(fmt.Sprintf("%s/test*", param.datadir))
if err != nil {
return fmt.Errorf("can't glob test files (%s)", err.Error())
}
userNamespaces, _, err := execShellScript(fmt.Sprintf("%s/get_user_namespaces", param.datadir))
if err != nil {
return fmt.Errorf("can't determine user namespaces (%s)", err.Error())
}
nodes, _, err := execShellScript(fmt.Sprintf("%s/get_nodes", param.datadir))
if err != nil {
return fmt.Errorf("can't fetch cluster nodes (%s)", err.Error())
}
if param.cache {
podCache, _, err := execShellScript(fmt.Sprintf("%s/get_pod_cache", param.datadir))
if err != nil {
return fmt.Errorf("can't store pod cache")
}
os.Setenv("POD_CACHE", podCache)
nodeCache, _, err := execShellScript(fmt.Sprintf("%s/get_node_cache", param.datadir))
if err != nil {
return fmt.Errorf("can't store node cache")
}
os.Setenv("NODE_CACHE", nodeCache)
}
var successCount, failureCount, maxCount, testCount int
successCount = 0
failureCount = 0
maxCount = 0
var record Record
record.Histogram = map[string]int{} // initialise map
startTime := time.Now()
for _, match := range tests {
matchBasename := strings.TrimPrefix(match, fmt.Sprintf("%s/", param.datadir))
if _, ok := ignoreSet[matchBasename]; ok {
continue
}
t := time.Now()
basename := filepath.Base(match)
os.Setenv("USER_NAMESPACES", userNamespaces)
os.Setenv("NODES", nodes)
os.Setenv("HA_SERVICES", "")
os.Setenv("CLUSTER_TESTS_EXIT", "")
fmt.Printf("[%s] %s... ", t.Format("2006-01-02 15:04:05"), au.Bold(au.Cyan(basename)))
stdout, stderr, err := execTestShellScript(match)
testCount++
if err != nil {
message := strings.TrimRight(stdout, " \n")
if param.errors {
message = fmt.Sprintf("%s %s", stderr, message)
}
if len(message) == 0 {
fmt.Printf("%s\n", au.Bold(au.Red("failed")))
} else {
fmt.Printf("%s: %s\n", au.Bold(au.Red("failed")), message)
}
// observe return value
returnValue, err := extractReturnValue(err)
if err != nil {
returnValue = 1
}
failureCount += returnValue
// append to failure log
record.FailLog = append(record.FailLog, fmt.Sprintf("%s %s %s", basename, au.Bold(au.Red("failed")), au.Bold(au.Cyan(message))))
// update histogram
if value, ok := record.Histogram[basename]; ok {
record.Histogram[basename] = value + returnValue
} else {
record.Histogram[basename] = returnValue
}
} else {
fmt.Printf("%s\n", au.Bold(au.Green("ok")))
successCount++
record.PassLog = append(record.PassLog, fmt.Sprintf("%s %s", basename, au.Bold(au.Green("ok"))))
}
}
recordTime := time.Now()
record.Time = fmt.Sprintf("%s", recordTime.Format("2006-01-02 15:04:05"))
record.Fail = failureCount
record.Pass = successCount
record.Duration = int(time.Since(startTime).Nanoseconds() / 1000)
recordFilename := fmt.Sprintf("%s/%d.json", param.outputdir, recordTime.Unix())
total := failureCount + successCount
if total > maxCount {
maxCount = total
}
plural := "s"
if testCount == 1 {
plural = ""
}
fmt.Printf("Ran %d test%s\n", testCount, plural)
if failureCount == 0 {
record.Head = fmt.Sprintf("%s\n", au.Bold(au.Green("OK")))
} else {
record.Head = fmt.Sprintf("%s (failures=%d)\n", au.Bold(au.Red("FAILED")), failureCount)
}
fmt.Printf("%s", record.Head)
recordJSON, err := json.Marshal(record)
if err != nil {
return fmt.Errorf("can't marshal record (%s)", err.Error())
}
err = ioutil.WriteFile(recordFilename, recordJSON, 0644)
if err != nil {
return fmt.Errorf("can't write record to file %s (%s)", recordFilename, err.Error())
}
return nil
}