forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_runner.go
164 lines (145 loc) · 3.67 KB
/
test_runner.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
package main
import (
"fmt"
"time"
)
const (
TEST_RESULT_PASS = "PASSED"
TEST_RESULT_FAIL = "FAILED"
TEST_RESULT_ERROR = "ERROR"
TEST_RESULT_SKIP = "SKIP"
TEST_STATUS_STARTED = "STARTED"
TEST_STATUS_FINISHED = "FINISHED"
TEST_STATUS_NOTRUN = "NO RUN"
)
// A test runner drives the execution of test cases
// It sets up the test environment and the test driver to run the tests
type TestRunner struct {
config TestConfig
stepChoice StepChoice
testDriver TestCaseDriver
target ExecutionTarget
verbose bool
result TestResult
}
type TestResult struct {
StartTime time.Time
Status string
Duration time.Duration
Result string
}
func (res *TestResult) Started() {
if res.Status != "" {
return
}
res.StartTime = time.Now()
res.Status = TEST_STATUS_STARTED
}
func (res *TestResult) Failed() {
if res.Result != "" {
panic("Test result already set")
}
res.Duration = time.Since(res.StartTime)
res.Result = TEST_RESULT_FAIL
res.Status = TEST_STATUS_FINISHED
}
func (res *TestResult) Passed() {
if res.Result != "" {
panic("Test result already set")
}
res.Duration = time.Since(res.StartTime)
res.Result = TEST_RESULT_PASS
res.Status = TEST_STATUS_FINISHED
}
func (res *TestResult) Error() {
if res.Result != "" {
panic("Test result already set")
}
res.Duration = time.Since(res.StartTime)
res.Result = TEST_RESULT_ERROR
res.Status = TEST_STATUS_FINISHED
}
// Run will set up the test environment and executes the tests
func (tr *TestRunner) Run() error {
tr.result = TestResult{}
tr.result.Started()
fmt.Printf("\n\n=============== running %s ===============\n", tr.stepChoice.name)
fmt.Println(tr.Info())
err := tr.checkConfig()
if err != nil {
return err
}
err = tr.setupEnvironment(tr.target)
if err != nil {
tr.result.Error()
return fmt.Errorf("error setting up test environment: %v", err)
}
tr.testDriver = GetTestCaseDriver(tr.config)
err = tr.testDriver.Run(tr.stepChoice.steps, tr.target, tr.verbose)
if err != nil {
tr.result.Failed()
// not tearing down environment for troubleshooting reasons on container
return fmt.Errorf("test run '%s' failed: %v", tr.config.name, err)
}
tr.result.Passed()
err = tr.teardownEnvironment()
fmt.Printf("==========================================\n")
return err
}
func (tr *TestRunner) checkConfig() error {
tr.config.validateStringLiterals()
return nil
}
func (tr *TestRunner) setupEnvironment(target ExecutionTarget) error {
tr.target = target
return target.Start()
}
func (tr *TestRunner) teardownEnvironment() error {
return tr.target.Stop()
}
func (tr *TestRunner) Setup(testCfg TestConfig) error {
tr.config = testCfg
return nil
}
func CreateTestRunner(config TestConfig, stepChoice StepChoice, target ExecutionTarget, verbose bool) TestRunner {
return TestRunner{
target: target,
stepChoice: stepChoice,
config: config,
verbose: verbose,
result: TestResult{Status: TEST_STATUS_NOTRUN},
}
}
// Info returns a header string containing useful information about the test runner
func (tr *TestRunner) Info() string {
return fmt.Sprintf(`
-------------------------------------------------
Test name : %s
Config: %s
Target: %s
-------------------------------------------------`,
tr.stepChoice.name,
tr.config.name,
tr.target.Info(),
)
}
func (tr *TestRunner) Report() string {
return fmt.Sprintf(`
-------------------------------------------------
Test name : %s
Config: %s
Target: %s
- Status: %s
- Result: %s
- Duration: %s
- StartTime: %s
-------------------------------------------------`,
tr.stepChoice.name,
tr.config.name,
tr.target.Info(),
tr.result.Status,
tr.result.Result,
tr.result.Duration,
tr.result.StartTime,
)
}