-
Notifications
You must be signed in to change notification settings - Fork 0
/
yester.go
295 lines (253 loc) · 6.34 KB
/
yester.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gookit/color"
"github.com/pkg/errors"
"github.com/robertkrimen/otto"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
type TestGroup struct {
Package string
Base string
Tests map[string]*Test
FailCount int
wg sync.WaitGroup
}
type Test struct {
Name string
After string
Request struct {
Method string
Path string
Headers map[string]string
QueryParams interface{}
Body interface{}
}
Validation struct {
StatusCode string
Headers map[string]string
Body []string
}
Result struct {
Passed bool
Errors []error
}
}
var TestQueue = make(chan *TestNode)
// Main entrypoint
func run() {
// Run tests as they're queued
go func() {
for {
node := <-TestQueue
go runTest(TestQueue, node)
}
}()
var configsWg sync.WaitGroup
configs := findConfigs()
for i := range configs {
configsWg.Add(1)
go processConfig(&configsWg, &configs[i])
}
configsWg.Wait()
totalFails := 0
for _, config := range configs {
totalFails += config.FailCount
}
os.Exit(totalFails)
}
func processConfig(configWg *sync.WaitGroup, config *TestGroup) {
defer configWg.Done()
// Create a node for each test
nodes := make(map[string]*TestNode)
for key, test := range config.Tests {
test.Name = key
nodes[key] = &TestNode{Key: key, Test: test, Config: config}
}
// Construct "graph" of nodes based on test dependencies
var rootNodes []*TestNode
for _, node := range nodes {
config.wg.Add(1)
if node.Test.After == "" {
rootNodes = append(rootNodes, node)
} else {
nodes[node.Test.After].Children = append(nodes[node.Test.After].Children, node)
}
}
// Queue tests with no "dependencies"
for _, node := range rootNodes {
TestQueue <- node
}
config.wg.Wait()
printSummary(config)
printDetails(config)
fmt.Println()
}
// Recursively walk down from the current directory to find test configs
func findConfigs() []TestGroup {
var configs []TestGroup
err := filepath.Walk(".",
func(path string, _ os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Base(path) != "yest.yml" {
return nil
}
var config TestGroup
parent := strings.Replace(path, "yest.yml", "", 1) // TODO: A slice would be more efficient
parent = strings.TrimSuffix(parent, "/")
if parent == "" { // handle root case
wd, _ := os.Getwd()
parent = filepath.Base(wd)
}
config.Package = parent
data, err := ioutil.ReadFile(path)
if err != nil {
log.Println(err)
return nil
}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Println(err)
return nil
}
configs = append(configs, config)
return nil
})
if err != nil {
log.Println(err)
}
return configs
}
// Print the testing summary
func printSummary(config *TestGroup) {
total := len(config.Tests)
passed := total - config.FailCount
fmt.Printf("== [%s] Result Summary ==\n", config.Package)
if config.FailCount == 0 {
color.Green.Printf("%d/%d Tests Passed\n", passed, total)
} else if config.FailCount == total {
color.Red.Printf("%d/%d Tests Passed\n", passed, total)
} else {
color.Yellow.Printf("%d/%d Tests Passed\n", passed, total)
}
}
// Print any errors that arose during testing
func printDetails(config *TestGroup) {
for _, test := range config.Tests {
if test.Result.Passed {
if verbose {
color.Green.Print("PASSED")
fmt.Printf(" [%s]\n", test.Name)
}
continue
}
color.Red.Print("FAILED")
padding := " "
for _, err := range test.Result.Errors {
fmt.Printf("%s[%s]: %s\n", padding, test.Name, err)
padding = " "
}
fmt.Println()
}
}
// Run a single test, using goroutines to parallelize
func runTest(q chan *TestNode, node *TestNode) {
test := node.Test
config := node.Config
defer config.wg.Done()
// -- Prepare query
if test.Request.Method == "" { // default to GET
test.Request.Method = "GET"
}
var body []byte
if test.Request.Body != nil {
var err error
body, err = json.Marshal(test.Request.Body)
if err != nil {
fmt.Println("INTERNAL ERROR")
test.Result.Errors = append(test.Result.Errors, err)
return
}
}
req, err := http.NewRequest(test.Request.Method, config.Base+test.Request.Path, bytes.NewBuffer(body))
if err != nil {
test.Result.Errors = append(test.Result.Errors, err)
return
}
for k, v := range test.Request.Headers {
req.Header.Set(k, v)
}
// -- Execute query
resp, err := http.DefaultClient.Do(req)
if err != nil {
test.Result.Errors = append(test.Result.Errors, err)
return
}
// -- Validation
v := test.Validation
// Status Code
if v.StatusCode != "" && strconv.Itoa(resp.StatusCode) != v.StatusCode {
e := errors.New(fmt.Sprintf("expected status code: %s, actual: %d", v.StatusCode, resp.StatusCode))
test.Result.Errors = append(test.Result.Errors, e)
}
// Headers
for header, expected := range v.Headers {
actual := resp.Header.Get(header)
if actual != expected {
e := errors.New(fmt.Sprintf("expected header %s: %s, actual: %s", header, expected, actual))
test.Result.Errors = append(test.Result.Errors, e)
}
}
// Body
if test.Validation.Body != nil {
// TODO: probably don't ignore this error lol
bodyString, _ := ioutil.ReadAll(resp.Body)
var body interface{}
_ = json.Unmarshal(bodyString, &body)
// start a Javascript interpreter and checks each expression in the YAML config
vm := otto.New()
err = vm.Set("body", body)
if err != nil {
log.Fatal(err)
}
for _, expr := range test.Validation.Body {
_, err = vm.Run(fmt.Sprintf("result = %s", expr))
if err != nil {
e := errors.New(fmt.Sprintf("(%s) evaluated with error: %s", expr, err))
test.Result.Errors = append(test.Result.Errors, e)
continue
}
value, err := vm.Get("result")
if err != nil {
log.Fatal(err) // TODO: this maybe shouldn't be this.
}
result, _ := value.ToBoolean()
if !result {
e := errors.New(fmt.Sprintf("(%s) evaluated to false", expr))
test.Result.Errors = append(test.Result.Errors, e)
}
}
}
test.Result.Passed = len(test.Result.Errors) == 0
if !test.Result.Passed {
config.FailCount++
}
// Enqueue dependent tests
time.Sleep(100 * time.Millisecond) // Sleep between successive tests. TODO: Make configurable
for _, child := range node.Children {
q <- child
}
}