/
api_testsuite.go
437 lines (386 loc) · 13.1 KB
/
api_testsuite.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/programmfabrik/apitest/internal/httpproxy"
"github.com/programmfabrik/apitest/pkg/lib/datastore"
"github.com/programmfabrik/apitest/pkg/lib/filesystem"
"github.com/programmfabrik/apitest/pkg/lib/report"
"github.com/programmfabrik/apitest/pkg/lib/template"
"github.com/programmfabrik/apitest/pkg/lib/util"
)
// Suite defines the structure of our apitest. We do read this in with the config loader
type Suite struct {
Name string `json:"name"`
Description string `json:"description"`
HttpServer *struct {
Addr string `json:"addr"`
Dir string `json:"dir"`
Testmode bool `json:"testmode"`
Proxy httpproxy.ProxyConfig `json:"proxy"`
} `json:"http_server,omitempty"`
Tests []any `json:"tests"`
Store map[string]any `json:"store"`
StandardHeader map[string]*string `yaml:"header" json:"header"`
StandardHeaderFromStore map[string]string `yaml:"header_from_store" json:"header_from_store"`
Config TestToolConfig
datastore *datastore.Datastore
manifestRelDir string
manifestDir string
manifestPath string
reporterRoot *report.ReportElement
index int
serverURL string
httpServer http.Server
httpServerProxy *httpproxy.Proxy
httpServerDir string
idleConnsClosed chan struct{}
HTTPServerHost string
loader template.Loader
}
// NewTestSuite creates a new suite on which we execute our tests on. Normally this only gets call from within the apitest main command
func NewTestSuite(config TestToolConfig, manifestPath string, manifestDir string, r *report.ReportElement, datastore *datastore.Datastore, index int) (*Suite, error) {
suite := Suite{
Config: config,
manifestDir: filepath.Dir(manifestPath),
manifestPath: manifestPath,
manifestRelDir: manifestDir,
reporterRoot: r,
datastore: datastore,
index: index,
}
// Here we create this additional struct in order to preload the suite manifest
// It is needed, for example, for getting the suite HTTP server address
// Then preloaded values are used to load again the manifest with relevant replacements
suitePreload := Suite{
Config: config,
manifestDir: filepath.Dir(manifestPath),
manifestPath: manifestPath,
manifestRelDir: manifestDir,
reporterRoot: r,
datastore: datastore,
index: index,
}
manifest, err := suitePreload.loadManifest()
if err != nil {
err = fmt.Errorf("error loading manifest: %s", err)
suitePreload.reporterRoot.Failure = fmt.Sprintf("%s", err)
return &suitePreload, err
}
err = util.Unmarshal(manifest, &suitePreload)
if err != nil {
err = fmt.Errorf("error unmarshaling manifest '%s': %s", manifestPath, err)
suitePreload.reporterRoot.Failure = fmt.Sprintf("%s", err)
return &suitePreload, err
}
// Add external http server url here, as only after this point the http_server.addr may be available
hsru := new(url.URL)
shsu := new(url.URL)
if httpServerReplaceHost != "" {
hsru, err = url.Parse("//" + httpServerReplaceHost)
if err != nil {
return nil, errors.Wrap(err, "set http_server_host failed (command argument)")
}
}
if suitePreload.HttpServer != nil {
preloadHTTPAddrStr := suitePreload.HttpServer.Addr
if preloadHTTPAddrStr == "" {
preloadHTTPAddrStr = ":80"
}
// We need to append it as the golang URL parser is not smart enough to differenciate between hostname and protocol
shsu, err = url.Parse("//" + preloadHTTPAddrStr)
if err != nil {
return nil, errors.Wrap(err, "set http_server_host failed (manifesr addr)")
}
}
suitePreload.HTTPServerHost = ""
if hsru.Hostname() != "" {
suitePreload.HTTPServerHost = hsru.Hostname()
} else if shsu.Hostname() != "" {
suitePreload.HTTPServerHost = shsu.Hostname()
} else {
suitePreload.HTTPServerHost = "localhost"
}
if suite.HTTPServerHost == "0.0.0.0" {
suitePreload.HTTPServerHost = "localhost"
}
if hsru.Port() != "" {
suitePreload.HTTPServerHost += ":" + hsru.Port()
} else if shsu.Port() != "" {
suitePreload.HTTPServerHost += ":" + shsu.Port()
}
// Here we load the usable manifest, now that we can do all potential replacements
manifest, err = suitePreload.loadManifest()
if err != nil {
err = fmt.Errorf("error loading manifest: %s", err)
suite.reporterRoot.Failure = fmt.Sprintf("%s", err)
return &suite, err
}
// fmt.Printf("%s", string(manifest))
// We unmarshall the final manifest into the final working suite
err = util.Unmarshal(manifest, &suite)
if err != nil {
err = fmt.Errorf("error unmarshaling manifest '%s': %s", manifestPath, err)
suite.reporterRoot.Failure = fmt.Sprintf("%s", err)
return &suite, err
}
suite.HTTPServerHost = suitePreload.HTTPServerHost
suite.loader = suitePreload.loader
//Append suite manifest path to name, so we know in an automatic setup where the test is loaded from
suite.Name = fmt.Sprintf("%s (%s)", suite.Name, manifestPath)
// init store
err = suite.datastore.SetMap(suite.Store)
if err != nil {
err = fmt.Errorf("error setting datastore map:%s", err)
suite.reporterRoot.Failure = fmt.Sprintf("%s", err)
return &suite, err
}
return &suite, nil
}
// Run run the given testsuite
func (ats *Suite) Run() bool {
r := ats.reporterRoot
if !ats.Config.LogShort {
logrus.Infof("[%2d] '%s'", ats.index, ats.Name)
}
ats.StartHttpServer()
err := os.Chdir(ats.manifestDir)
if err != nil {
logrus.Errorf("Unable to switch working directory to %q", ats.manifestDir)
}
start := time.Now()
success := true
for k, v := range ats.Tests {
child := r.NewChild(strconv.Itoa(k))
sTestSuccess := ats.parseAndRunTest(v, ats.manifestDir, ats.manifestPath, k, 1, false, child, ats.loader)
child.Leave(sTestSuccess)
if !sTestSuccess {
success = false
break
}
}
elapsed := time.Since(start)
r.Leave(success)
if success {
if ats.Config.LogShort {
fmt.Printf("OK '%s' (%.3fs)\n", ats.manifestRelDir, elapsed.Seconds())
} else {
logrus.WithFields(logrus.Fields{"elapsed": elapsed.Seconds()}).Infof("[%2d] success", ats.index)
}
} else {
if ats.Config.LogShort {
fmt.Printf("FAIL '%s' (%.3fs)\n", ats.manifestRelDir, elapsed.Seconds())
} else {
logrus.WithFields(logrus.Fields{"elapsed": elapsed.Seconds()}).Warnf("[%2d] failure", ats.index)
}
}
ats.StopHttpServer()
return success
}
type TestContainer struct {
CaseByte json.RawMessage
Path string
}
func (ats *Suite) parseAndRunTest(v any, manifestDir, testFilePath string, k, repeatNTimes int, runParallel bool, r *report.ReportElement, rootLoader template.Loader) bool {
//Init variables
// logrus.Warnf("Test %s, Prev delimiters: %#v", testFilePath, rootLoader.Delimiters)
loader := template.NewLoader(ats.datastore)
loader.Delimiters = rootLoader.Delimiters
loader.HTTPServerHost = ats.HTTPServerHost
serverURL, err := url.Parse(ats.Config.ServerURL)
if err != nil {
logrus.Error(fmt.Errorf("can not load server url into test (%s): %s", testFilePath, err))
return false
}
loader.ServerURL = serverURL
loader.OAuthClient = ats.Config.OAuthClient
isParallelPathSpec := false
switch t := v.(type) {
case string:
isParallelPathSpec = util.IsParallelPathSpec(t)
}
//Get the Manifest with @ logic
fileh, testObj, err := template.LoadManifestDataAsRawJson(v, manifestDir)
dir := filepath.Dir(fileh)
if fileh != "" {
testFilePath = filepath.Join(filepath.Dir(testFilePath), fileh)
}
if err != nil {
r.SaveToReportLog(err.Error())
logrus.Error(fmt.Errorf("can not LoadManifestDataAsRawJson (%s): %s", testFilePath, err))
return false
}
//Try to directly unmarshal the manifest into testcase array
var testCases []json.RawMessage
err = util.Unmarshal(testObj, &testCases)
if err == nil {
d := 1
if isParallelPathSpec || runParallel {
if repeatNTimes > 1 {
logrus.Debugf("run %s parallel: repeat %d times", filepath.Base(testFilePath), repeatNTimes)
}
d = len(testCases)
}
waitCh := make(chan bool, repeatNTimes*d)
succCh := make(chan bool, repeatNTimes*len(testCases))
go func() {
for kn := 0; kn < repeatNTimes; kn++ {
for ki, v := range testCases {
waitCh <- true
go testGoRoutine(k, kn+ki*repeatNTimes, v, ats, testFilePath, manifestDir, dir, r, loader, waitCh, succCh, isParallelPathSpec || runParallel)
}
}
}()
for i := 0; i < repeatNTimes*len(testCases); i++ {
select {
case succ := <-succCh:
if succ == false {
return false
}
}
}
} else {
// We were not able unmarshal into array, so we try to unmarshal into raw message
// Get the (optional) number of repititions from the test path spec
parallelRepititions := 1
if isParallelPathSpec {
switch t := v.(type) {
case string:
parallelRepititions, _ = util.GetParallelPathSpec(t)
}
}
// Parse as template always
requestBytes, lErr := loader.Render(testObj, filepath.Join(manifestDir, dir), nil)
if lErr != nil {
r.SaveToReportLog(lErr.Error())
logrus.Error(fmt.Errorf("can not render template (%s): %s", testFilePath, lErr))
return false
}
// If objects are different, we did have a Go template, recurse one level deep
if string(requestBytes) != string(testObj) {
return ats.parseAndRunTest([]byte(requestBytes), filepath.Join(manifestDir, dir),
testFilePath, k, parallelRepititions, isParallelPathSpec, r, loader)
}
// Its a JSON at this point, assign and proceed to parse
testObj = requestBytes
var singleTest json.RawMessage
err = util.Unmarshal(testObj, &singleTest)
if err == nil {
//Check if is @ and if so load the test
if util.IsPathSpec(string(testObj)) {
var sS string
err := util.Unmarshal(testObj, &sS)
if err != nil {
r.SaveToReportLog(err.Error())
logrus.Error(fmt.Errorf("can not unmarshal (%s): %s", testFilePath, err))
return false
}
return ats.parseAndRunTest(sS, filepath.Join(manifestDir, dir), testFilePath, k, parallelRepititions, isParallelPathSpec, r, template.Loader{})
} else {
return ats.runSingleTest(TestContainer{CaseByte: testObj, Path: filepath.Join(manifestDir, dir)}, r, testFilePath, loader, k, runParallel)
}
} else {
// Malformed json
r.SaveToReportLog(err.Error())
logrus.Error(fmt.Errorf("can not unmarshal (%s): %s", testFilePath, err))
return false
}
}
return true
}
func (ats *Suite) runSingleTest(tc TestContainer, r *report.ReportElement, testFilePath string, loader template.Loader, k int, isParallel bool) bool {
r.SetName(testFilePath)
var test Case
jErr := util.Unmarshal(tc.CaseByte, &test)
if jErr != nil {
r.SaveToReportLog(jErr.Error())
logrus.Error(fmt.Errorf("can not unmarshal single test (%s): %s", testFilePath, jErr))
return false
}
test.Filename = testFilePath
test.loader = loader
test.manifestDir = tc.Path
test.suiteIndex = ats.index
test.index = k
test.dataStore = ats.datastore
test.standardHeader = ats.StandardHeader
test.standardHeaderFromStore = ats.StandardHeaderFromStore
if isParallel {
test.ContinueOnFailure = true
}
if test.LogNetwork == nil {
test.LogNetwork = &ats.Config.LogNetwork
}
if test.LogVerbose == nil {
test.LogVerbose = &ats.Config.LogVerbose
}
if test.LogShort == nil {
test.LogShort = &ats.Config.LogShort
}
if test.ServerURL == "" {
test.ServerURL = ats.Config.ServerURL
}
success := test.runAPITestCase(r)
if !success && !test.ContinueOnFailure {
return false
}
return true
}
func (ats *Suite) loadManifest() ([]byte, error) {
var res []byte
if !ats.Config.LogShort {
logrus.Tracef("Loading manifest: %s", ats.manifestPath)
}
loader := template.NewLoader(ats.datastore)
loader.HTTPServerHost = ats.HTTPServerHost
serverURL, err := url.Parse(ats.Config.ServerURL)
if err != nil {
return nil, fmt.Errorf("can not load server url into manifest (%s): %s", ats.manifestPath, err)
}
loader.ServerURL = serverURL
loader.OAuthClient = ats.Config.OAuthClient
manifestFile, err := filesystem.Fs.Open(ats.manifestPath)
if err != nil {
return res, fmt.Errorf("error opening manifestPath (%s): %s", ats.manifestPath, err)
}
defer manifestFile.Close()
manifestTmpl, err := ioutil.ReadAll(manifestFile)
if err != nil {
return res, fmt.Errorf("error loading manifest (%s): %s", ats.manifestPath, err)
}
b, err := loader.Render(manifestTmpl, ats.manifestDir, nil)
ats.loader = loader
return b, err
}
func testGoRoutine(k, ki int, v json.RawMessage, ats *Suite, testFilePath, manifestDir, dir string, r *report.ReportElement, loader template.Loader, waitCh, succCh chan bool, runParallel bool) {
success := false
//Check if is @ and if so load the test
switch util.IsPathSpec(string(v)) {
case true:
var sS string
err := util.Unmarshal(v, &sS)
if err != nil {
r.SaveToReportLog(err.Error())
logrus.Error(fmt.Errorf("can not unmarshal (%s): %s", testFilePath, err))
success = false
break
}
success = ats.parseAndRunTest(sS, filepath.Join(manifestDir, dir), testFilePath, k+ki, 1, runParallel, r, loader)
default:
success = ats.runSingleTest(TestContainer{CaseByte: v, Path: filepath.Join(manifestDir, dir)},
r, testFilePath, loader, ki, runParallel)
}
succCh <- success
if success {
<-waitCh
}
}