-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite.go
executable file
·233 lines (187 loc) · 5.19 KB
/
suite.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
package arp
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/yaml.v2"
)
const (
MissingDSKeyFmt = "Attempted to retrieve data from data store that does not exist: key: %v"
BadIndexDSFmt = "Attempted to index into a data store value with a non-positive or non-integer value: %v"
PrevTestFailMsg = "Test skipped due to a previous unrecoverable test execution error"
TestFailMsgTrailer = ": Remaining tests within suite will automatically fail"
IndexExceedsDSFmt = "Index for data store value exceeds its max length: %v"
StatusCodePath = "response.StatusCode"
HeadersPath = "response.Header"
)
type TestSuiteCfg struct {
Tests []TestCaseCfg `yaml:"tests"`
}
type TestSuite struct {
File string
Tests []*TestCase
GlobalDataStore DataStore
Verbose bool
}
type SuiteResult struct {
Results []*TestResult
Passed int
Failed int
Total int
Duration time.Duration
}
func NewTestSuite(testFile string, fixtures string) (*TestSuite, error) {
suite := &TestSuite{
GlobalDataStore: NewDataStore(),
File: testFile,
}
err := suite.InitializeDataStore(fixtures)
if err != nil {
return suite, err
}
status, err := suite.LoadTests(fixtures)
if !status && err == nil {
return nil, nil
} else if err != nil {
return suite, fmt.Errorf("failed to initialize test suite: %v", err)
}
return suite, nil
}
func (t *TestSuite) ReloadFile(fixtures string) (bool, error) {
t.Tests = make([]*TestCase, 0)
return t.LoadTests(fixtures)
}
func (t *TestSuite) InitializeDataStore(fixtures string) error {
f, err := t.LoadFixtures(fixtures)
if err != nil {
return err
}
for k := range f {
t.GlobalDataStore.Put(k, f[k])
}
for _, env := range os.Environ() {
pair := strings.SplitN(env, "=", 2)
t.GlobalDataStore.Put(pair[0], pair[1])
}
return nil
}
func (t *TestSuite) LoadFixtures(fixtures string) (map[string]interface{}, error) {
var config map[interface{}]interface{}
if fixtures == "" {
return nil, nil
}
fileInfo, err := os.Stat(fixtures)
if err != nil {
return nil, fmt.Errorf("failed to stat fixture file: %v - %v", fixtures, err)
}
if fileInfo.IsDir() {
return nil, fmt.Errorf("fixtures must be a file, not a directory: %v - %v", fixtures, err)
}
data, err := os.ReadFile(fixtures)
if err != nil {
return nil, fmt.Errorf("failed to read fixtures file: %v - %v", fixtures, err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal fixture file: %v - %v", fixtures, err)
}
return YamlToJson(config).(map[string]interface{}), nil
}
func (t *TestSuite) Close() {
for _, test := range t.Tests {
test.CloseWebsocket()
}
}
func (t *TestSuite) LoadTests(fixtures string) (bool, error) {
var readers []io.Reader
if fixtures != "" {
fix, err := os.Open(fixtures)
if err != nil {
return false, fmt.Errorf("failed to open fixture file: %v - %v", fixtures, err)
}
readers = append(readers, fix)
}
var tests *os.File
var err error
if t.File == "-" {
tests = os.Stdin
} else {
tests, err = os.Open(t.File)
}
if err != nil {
return false, fmt.Errorf("failed to open test file: %v - %v", t.File, err)
}
readers = append(readers, tests)
// combine fixtures and test file into a single source so tests can utilize yaml anchors defined in
// the fixtures file
multiReader := io.MultiReader(readers...)
data, err := io.ReadAll(multiReader)
if err != nil {
return false, fmt.Errorf("failed to load test file: %v - %v", t.File, err)
}
fp, _ := filepath.Abs(filepath.Dir(t.File))
t.GlobalDataStore.Put("TEST_DIR", fp)
var testSuiteCfg TestSuiteCfg
err = yaml.Unmarshal(data, &testSuiteCfg)
if err != nil {
return false, fmt.Errorf("failed to load test file: %v - %v", t.File, err)
}
for _, test := range testSuiteCfg.Tests {
tCase := TestCase{
GlobalDataStore: &t.GlobalDataStore,
}
err = tCase.LoadConfig(&test)
if err != nil {
return false, fmt.Errorf("failed to load test file: %v - %v", t.File, err)
}
t.Tests = append(t.Tests, &tCase)
}
return true, nil
}
func (t *TestSuite) ExecuteTests(testTags []string) (bool, SuiteResult, error) {
defer t.Close()
anyFailed := false
suiteResults := SuiteResult{
Results: []*TestResult{},
Total: len(t.Tests),
}
var criticalError error
for _, test := range t.Tests {
if test.Config.ExitOnRun {
break
}
if t.Verbose {
fmt.Printf(">> In Progress: %v\n", test.Config.Name)
}
var passed bool
var results *TestResult
if criticalError == nil {
passed, results, criticalError = test.Execute(testTags)
if criticalError != nil {
results = test.GetStubbedFailResult(criticalError.Error() + TestFailMsgTrailer)
}
} else {
passed = false
results = test.GetStubbedFailResult(PrevTestFailMsg)
}
if passed {
suiteResults.Passed += 1
} else {
anyFailed = true
suiteResults.Failed += 1
}
if t.Verbose {
statusStr := "Pass"
if !passed {
statusStr = "Fail"
}
fmt.Printf("<< Done: [%v] %v -> %v\n", statusStr, t.File, test.Config.Name)
}
suiteResults.Duration += results.EndTime.Sub(results.StartTime)
suiteResults.Results = append(suiteResults.Results, results)
}
return !anyFailed, suiteResults, criticalError
}