-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
348 lines (272 loc) · 8.54 KB
/
driver.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package gotd
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/TwiN/go-color"
)
type TestRun struct {
RunID string `json:"testrun_id"`
Config string `json:"testrun_config"`
StartTime time.Time `json:"testrun_startime"`
EndTime time.Time `json:"testrun_endtime"`
TestCases []TestCase `json:"testrun_testcases"`
ExecutedTestResults []TestCaseResult `json:"testrun_executed_test_results"`
}
func (tr *TestRun) GetExecutionDuration() time.Duration {
d := tr.EndTime.Sub(tr.StartTime)
return d
}
type TestRunAnalysis struct {
NumTCExecuted int
NumSubTCExecuted int
NumPassed int
NumFailed int
NumTimedOut int
TotalDuration time.Duration
}
func (tr *TestRun) GenerateTestRunAnalysis() TestRunAnalysis {
var tra TestRunAnalysis
num := len(tr.ExecutedTestResults)
for i := 0; i < num; i++ {
isSubTC := (tr.ExecutedTestResults[i].ParentId != "")
if isSubTC {
tra.NumSubTCExecuted++
continue
}
tra.NumTCExecuted++
if tr.ExecutedTestResults[i].TimedOut {
tra.NumTimedOut++
} else if tr.ExecutedTestResults[i].Passed {
tra.NumPassed++
} else {
tra.NumFailed++
}
tra.TotalDuration += tr.ExecutedTestResults[i].GetExecutionDuration()
}
return tra
}
func (tr *TestRun) DumpTestRun2JSON(filename string, dirpath string) string {
if dirpath == "" {
dirpath = "."
}
if filename == "" {
filename = tr.RunID
}
path := dirpath + "/" + filename
f, err := os.Create(path)
if err != nil {
panic(err)
} else {
defer f.Close()
}
r, err := json.Marshal(*tr)
if err == nil {
if _, err = f.Write(r); err != nil {
panic(err)
}
}
return string(r)
}
type TestDriver struct {
TestConfigPath string
TestSuiteRootPath string
config TestConfig
testRuns []*TestRun
mu sync.Mutex
}
func (td *TestDriver) LoadTestConfiguration(path string) error {
err := td.config.ReadConfigFile(path)
if err == nil {
abs, _ := filepath.Abs(path)
td.TestConfigPath = abs
}
configFileParentDir := filepath.Dir(td.TestConfigPath)
if filepath.IsAbs(td.config.Root) {
td.TestSuiteRootPath, _ = filepath.Abs(td.config.Root)
} else {
td.TestSuiteRootPath = filepath.Join(configFileParentDir, td.config.Root)
}
return err
}
func (td *TestDriver) createNewTestRun() *TestRun {
tr := TestRun{}
td.mu.Lock()
td.testRuns = append(td.testRuns, &tr)
td.mu.Unlock()
return &tr
}
func (td *TestDriver) ExecuteTestSuite(identifier string) TestRun {
tr := td.createNewTestRun()
tr.Config = td.TestConfigPath
tr.TestCases = td.config.RetrieveTestCasesByIdentifier(identifier)
tr.StartTime = time.Now()
log.Printf("There are %v test case(s) to be executed based on selection of test suite \"%s\" ...\n",
len(tr.TestCases), identifier)
for i := 0; i < len(tr.TestCases); i++ {
log.Printf("[%04d] Executing test case [%s/%s] ...\n",
i+1, tr.TestCases[i].Id, tr.TestCases[i].Name)
if td.config.Root != "" {
tr.TestCases[i].Program = td.config.Root + "/" + tr.TestCases[i].Program
}
tcr, err, subcaseResults := executeTestCase(tr.TestCases[i], td.TestSuiteRootPath)
var exec_str string
if err == nil {
exec_str = fmt.Sprintf("[%04d] %s %s (time of %v for %s)",
i+1, tcr.Id, getPassedFailedMsg(tcr.Passed), tcr.GetExecutionDuration(), tcr.CmdLine)
} else if tcr.TimedOut || err.Error() == "timed out" {
exec_str = fmt.Sprintf("[%04d] %s "+color.Colorize(color.Red,
"TIMED OUT")+" (terminated after %v for %s)",
i+1, tcr.Id, tcr.GetExecutionDuration(), tcr.CmdLine)
} else {
exec_str = fmt.Sprintf("[%04d] %s "+color.Colorize(color.Red,
"FAILED TO RUN")+" (time of %v for %s, error = '%v')",
i+1, tcr.Id, tcr.GetExecutionDuration(), tcr.CmdLine, err)
}
log.Println(exec_str)
tcr.Data = exec_str
tr.ExecutedTestResults = append(tr.ExecutedTestResults, *tcr)
if err == nil {
processSubcaseResults(tcr, tr, &subcaseResults)
for j := 0; j < len(subcaseResults); j++ {
scr := subcaseResults[j]
exec_str = fmt.Sprintf("[%04d-%04d] %s %s (time of %v for %s)",
i+1, j+1, scr.Id, getPassedFailedMsg(scr.Passed),
scr.GetExecutionDuration(), scr.CmdLine)
log.Println(exec_str)
}
}
}
tr.EndTime = time.Now()
tr.RunID = fmt.Sprintf("Run-%s-%s-%s",
identifier, tr.StartTime.Format(time.RFC3339), tr.EndTime.Format(time.RFC3339))
return *tr
}
func getPassedFailedMsg(passed bool) string {
if passed {
return color.Colorize(color.Green, "PASSED")
}
return color.Colorize(color.Red, "FAILED")
}
func processSubcaseResults(tcr *TestCaseResult, tr *TestRun, subcaseResults *[]TestCaseResult) {
for i := 0; i < len(*subcaseResults); i++ {
(*subcaseResults)[i].Id = fmt.Sprintf("%s:%s", tcr.Id, (*subcaseResults)[i].Id)
(*subcaseResults)[i].ParentId = tcr.Id
tr.ExecutedTestResults = append(tr.ExecutedTestResults, (*subcaseResults)[i])
}
}
type executionResult struct {
tcr *TestCaseResult
err error
subcaseResults []TestCaseResult
}
func executeTestCase(tc TestCase, tsRootPath string) (*TestCaseResult, error, []TestCaseResult) {
tcr := TestCaseResult{}
tcr.Id = tc.Id
if tc.StepsPrerun != "" {
prog, params := parseCmdLine4ProgNParams(tc.StepsPrerun)
if _, err := LaunchCommand(tsRootPath, prog, params); err != nil {
return &tcr, fmt.Errorf("test case %s pre-run steps [%s] failed: %s",
tcr.Id, tc.StepsPrerun, err), nil
} else {
log.Printf("Successfully executed pre-run command '%s'\n", tc.StepsPrerun)
}
}
tcr.CmdLine = tc.Program + " " + tc.CliParameters
result := make(chan executionResult, 1)
tcr.StartTest()
go func() {
result <- executeTestCaseInternal(tsRootPath, tc, &tcr)
}()
var ptcr *TestCaseResult
var err error
var subcaseResults []TestCaseResult
select {
case <-time.After(time.Duration(tc.TimeoutSecs) * time.Second):
tcr.EndTest()
tcr.TimedOut = true
ptcr = &tcr
err = fmt.Errorf("test case %s timed out", tcr.Id)
subcaseResults = nil
case result := <-result:
ptcr = result.tcr
err = result.err
subcaseResults = result.subcaseResults
}
if tc.StepsPostrun != "" {
prog, params := parseCmdLine4ProgNParams(tc.StepsPostrun)
if _, err := LaunchCommand(tsRootPath, prog, params); err != nil {
return &tcr, fmt.Errorf("test case %s post-run steps [%s] failed: %s",
tcr.Id, tc.StepsPostrun, err), nil
} else {
log.Printf("Successfully executed post-run command '%s'\n", tc.StepsPostrun)
}
}
return ptcr, err, subcaseResults
}
func executeTestCaseInternal(tsRootPath string, tc TestCase, tcr *TestCaseResult) executionResult {
tcr.Id = tc.Id
// TBD: Need to clean up how testcase program command line is passed down for invocation
// and constructed once there is a more clear understanding of how the test programs will
// be launched.
paramList := splitNClean(tc.CliParameters)
tcr.StartTest()
log.Printf("Executing command [%s %s] ...", tc.Program, strings.Join(paramList, " "))
output, err := LaunchCommand(tsRootPath, tc.Program, paramList)
tcr.EndTest()
if err != nil {
return executionResult{
tcr: tcr,
err: err,
subcaseResults: nil,
}
}
// TBD: Process stdout to determine result of test case
err, passed, subcaseResults := parseTestResult(output)
tcr.Passed = passed
return executionResult{
tcr: tcr,
err: err,
subcaseResults: subcaseResults,
}
}
type ScanResult struct {
Id string `json:"id"`
StartTime time.Time `json:"scan_starttime"`
EndTime time.Time `json:"scan_endtime"`
Test string `json:"test"`
TestPassed bool `json:"test_passed"`
Data string `json:"data`
}
type OverallTestResult struct {
StartTime time.Time `json:"starttime"`
EndTime time.Time `json:"endtime"`
Passed bool `json:"overall_passed"`
CombinedData []ScanResult `json:"combined_scan_results"`
}
func parseTestResult(result string) (error, bool, []TestCaseResult) {
//var m map[string]interface{}
var resultObj OverallTestResult
err := json.Unmarshal([]byte(result), &resultObj)
if err != nil {
return err, false, nil
}
var subCaseResults []TestCaseResult
for i := 0; i < len(resultObj.CombinedData); i++ {
scanResult := resultObj.CombinedData[i]
var tcr TestCaseResult
tcr.StartTime = scanResult.StartTime
tcr.EndTime = scanResult.EndTime
tcr.Passed = scanResult.TestPassed
tcr.CmdLine = scanResult.Test
tcr.Data = scanResult.Data
tcr.Id = scanResult.Id
subCaseResults = append(subCaseResults, tcr)
}
return nil, resultObj.Passed, subCaseResults
}