-
Notifications
You must be signed in to change notification settings - Fork 67
/
run_test_action.go
580 lines (476 loc) 路 15 KB
/
run_test_action.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package actions
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"sync"
"time"
cienvironment "github.com/cucumber/ci-environment/go"
"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/file"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/ui"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/kubeshop/tracetest/server/model/yaml"
"go.uber.org/zap"
)
type RunResourceArgs struct {
DefinitionFile string
EnvID string
WaitForResult bool
JUnit string
}
type runTestAction struct {
config config.Config
logger *zap.Logger
client *openapi.APIClient
environmentActions environmentsActions
cliExit func(int)
}
var _ Action[RunResourceArgs] = &runTestAction{}
type runDefParams struct {
DefinitionFile string
EnvID string
WaitForResult bool
JunitFile string
Metadata map[string]string
EnvironmentVariables map[string]string
}
func NewRunTestAction(config config.Config, logger *zap.Logger, client *openapi.APIClient, environmentActions environmentsActions, cliExit func(int)) runTestAction {
return runTestAction{config, logger, client, environmentActions, cliExit}
}
func (a runTestAction) Run(ctx context.Context, args RunResourceArgs) error {
if args.DefinitionFile == "" {
return fmt.Errorf("you must specify a definition file to run a test")
}
if args.JUnit != "" && !args.WaitForResult {
return fmt.Errorf("--junit option requires --wait-for-result")
}
a.logger.Debug(
"Running test from definition",
zap.String("definitionFile", args.DefinitionFile),
zap.String("environment", args.EnvID),
zap.Bool("waitForResults", args.WaitForResult),
zap.String("junit", args.JUnit),
)
envID := args.EnvID
if utils.StringReferencesFile(args.EnvID) {
a.logger.Debug("resolve envID from file reference", zap.String("path", envID))
envResource, err := a.environmentActions.FromFile(ctx, args.EnvID)
if err != nil {
return fmt.Errorf("could not read environment file: %w", err)
}
_, err = a.environmentActions.ApplyResource(ctx, envResource)
if err != nil {
return fmt.Errorf("could not apply environmen file: %w", err)
}
envID = envResource.Spec.GetId()
}
a.logger.Debug("resolved env", zap.String("envID", envID))
params := runDefParams{
DefinitionFile: args.DefinitionFile,
EnvID: envID,
WaitForResult: args.WaitForResult,
JunitFile: args.JUnit,
Metadata: a.getMetadata(),
}
err := a.runDefinition(ctx, params)
if err != nil {
return fmt.Errorf("could not run definition: %w", err)
}
return nil
}
func (a runTestAction) testFileToID(ctx context.Context, originalPath, filePath string) (string, error) {
path := filepath.Join(originalPath, filePath)
f, err := file.Read(path)
if err != nil {
return "", err
}
body, _, err := a.client.ApiApi.
UpsertDefinition(ctx).
TextDefinition(openapi.TextDefinition{
Content: openapi.PtrString(f.Contents()),
}).
Execute()
if err != nil {
return "", fmt.Errorf("could not upsert definition: %w", err)
}
return body.GetId(), nil
}
func (a runTestAction) runDefinition(ctx context.Context, params runDefParams) error {
f, err := file.Read(params.DefinitionFile)
if err != nil {
return err
}
defFile := f.Definition()
if err = defFile.Validate(); err != nil {
return fmt.Errorf("invalid definition file: %w", err)
}
return a.runDefinitionFile(ctx, f, params)
}
func (a runTestAction) runDefinitionFile(ctx context.Context, f file.File, params runDefParams) error {
f, err := f.ResolveVariables()
if err != nil {
return err
}
if t, err := f.Definition().Transaction(); err == nil {
for i, step := range t.Steps {
// since step could be a relative path in relation of the definition file,
// to check it properly we need to convert it to an absolute path
stepPath := filepath.Join(f.AbsDir(), step)
if !utils.StringReferencesFile(stepPath) {
// not referencing a file, keep the value
continue
}
// references a file, resolve to its ID
id, err := a.testFileToID(ctx, f.AbsDir(), step)
if err != nil {
return fmt.Errorf(`cannot translate path "%s" to an ID: %w`, step, err)
}
t.Steps[i] = id
}
def := yaml.File{
Type: yaml.FileTypeTransaction,
Spec: t,
}
updated, err := def.Encode()
if err != nil {
return fmt.Errorf(`cannot encode updated transaction: %w`, err)
}
f, err = file.New(f.Path(), updated)
if err != nil {
return fmt.Errorf(`cannot recreate updated file: %w`, err)
}
}
variables := make([]openapi.EnvironmentValue, 0)
for name, value := range params.EnvironmentVariables {
variables = append(variables, openapi.EnvironmentValue{Key: openapi.PtrString(name), Value: openapi.PtrString(value)})
}
body, resp, err := a.client.ApiApi.
ExecuteDefinition(ctx).
TextDefinition(openapi.TextDefinition{
Content: openapi.PtrString(f.Contents()),
RunInformation: &openapi.RunInformation{
Metadata: params.Metadata,
EnvironmentId: ¶ms.EnvID,
Variables: variables,
},
}).
Execute()
if resp != nil && resp.StatusCode == http.StatusUnprocessableEntity {
filledVariables, err := a.askForMissingVariables(resp)
if err != nil {
return err
}
params.EnvironmentVariables = filledVariables
return a.runDefinitionFile(ctx, f, params)
}
if err != nil {
return fmt.Errorf("could not execute definition: %w", err)
}
if resp.StatusCode == http.StatusCreated && !f.HasID() {
f, err = f.SetID(body.GetId())
if err != nil {
return fmt.Errorf("could not update definition file: %w", err)
}
_, err = f.Write()
if err != nil {
return fmt.Errorf("could not update definition file: %w", err)
}
}
runID := getTestRunIDFromString(body.GetRunId())
a.logger.Debug(
"executed",
zap.Int32("runID", runID),
zap.String("runType", body.GetType()),
)
switch body.GetType() {
case "Test":
test, err := a.getTest(ctx, body.GetId())
if err != nil {
return fmt.Errorf("could not get test info: %w", err)
}
return a.testRun(ctx, test, runID, params)
case "Transaction":
test, err := a.getTransaction(ctx, body.GetId())
if err != nil {
return fmt.Errorf("could not get test info: %w", err)
}
return a.transactionRun(ctx, test, runID, params)
}
return fmt.Errorf(`unsuported run type "%s"`, body.GetType())
}
func (a runTestAction) askForMissingVariables(resp *http.Response) (map[string]string, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return map[string]string{}, fmt.Errorf("could not read response body: %w", err)
}
var missingVariablesError openapi.MissingVariablesError
err = json.Unmarshal(body, &missingVariablesError)
if err != nil {
return map[string]string{}, fmt.Errorf("could not unmarshal response: %w", err)
}
uniqueMissingVariables := map[string]string{}
for _, missingVariables := range missingVariablesError.MissingVariables {
for _, variable := range missingVariables.Variables {
defaultValue := ""
if variable.DefaultValue != nil {
defaultValue = *variable.DefaultValue
}
uniqueMissingVariables[*variable.Key] = defaultValue
}
}
if len(uniqueMissingVariables) > 0 {
ui.DefaultUI.Warning("Some variables are required by one or more tests")
ui.DefaultUI.Info("Fill the values for each variable:")
}
filledVariables := map[string]string{}
for variableName, variableDefaultValue := range uniqueMissingVariables {
value := ui.DefaultUI.TextInput(variableName, variableDefaultValue)
filledVariables[variableName] = value
}
return filledVariables, nil
}
func (a runTestAction) getTransaction(ctx context.Context, id string) (openapi.Transaction, error) {
a.client.GetConfig().AddDefaultHeader("X-Tracetest-Augmented", "true")
transaction, _, err := a.client.ResourceApiApi.
GetTransaction(ctx, id).
Execute()
// reset augmented header
delete(a.client.GetConfig().DefaultHeader, "X-Tracetest-Augmented")
if err != nil {
return openapi.Transaction{}, fmt.Errorf("could not execute request: %w", err)
}
return transaction.GetSpec(), nil
}
func (a runTestAction) getTest(ctx context.Context, id string) (openapi.Test, error) {
test, _, err := a.client.ApiApi.
GetTest(ctx, id).
Execute()
if err != nil {
return openapi.Test{}, fmt.Errorf("could not execute request: %w", err)
}
return *test, nil
}
func (a runTestAction) testRun(ctx context.Context, test openapi.Test, runID int32, params runDefParams) error {
a.logger.Debug("run test", zap.Bool("wait-for-results", params.WaitForResult))
testID := test.GetId()
testRun, err := a.getTestRun(ctx, testID, runID)
if err != nil {
return fmt.Errorf("could not run test: %w", err)
}
if params.WaitForResult {
updatedTestRun, err := a.waitForTestResult(ctx, testID, getTestRunID(testRun))
if err != nil {
return fmt.Errorf("could not wait for result: %w", err)
}
testRun = updatedTestRun
if err := a.saveJUnitFile(ctx, testID, getTestRunID(testRun), params.JunitFile); err != nil {
return fmt.Errorf("could not save junit file: %w", err)
}
}
tro := formatters.TestRunOutput{
HasResults: params.WaitForResult,
Test: test,
Run: testRun,
}
formatter := formatters.TestRun(a.config, true)
formattedOutput := formatter.Format(tro)
fmt.Print(formattedOutput)
allPassed := tro.Run.Result.GetAllPassed()
if params.WaitForResult && !allPassed {
// It failed, so we have to return an error status
a.cliExit(1)
}
return nil
}
func (a runTestAction) transactionRun(ctx context.Context, transaction openapi.Transaction, rid int32, params runDefParams) error {
a.logger.Debug("run transaction", zap.Bool("wait-for-results", params.WaitForResult))
transactionID := transaction.GetId()
transactionRun, err := a.getTransactionRun(ctx, transactionID, int32(rid))
if err != nil {
return fmt.Errorf("could not run transaction: %w", err)
}
if params.WaitForResult {
updatedTestRun, err := a.waitForTransactionResult(ctx, transactionID, transactionRun.GetId())
if err != nil {
return fmt.Errorf("could not wait for result: %w", err)
}
transactionRun = updatedTestRun
}
tro := formatters.TransactionRunOutput{
HasResults: params.WaitForResult,
Transaction: transaction,
Run: transactionRun,
}
formatter := formatters.TransactionRun(a.config, true)
formattedOutput := formatter.Format(tro)
fmt.Print(formattedOutput)
if params.WaitForResult {
if utils.RunStateIsFailed(tro.Run.GetState()) {
// It failed, so we have to return an error status
a.cliExit(1)
}
for _, step := range tro.Run.Steps {
if !step.Result.GetAllPassed() {
// if any test doesn't pass, fail the transaction execution
a.cliExit(1)
}
}
}
return nil
}
func (a runTestAction) saveJUnitFile(ctx context.Context, testId string, testRunId int32, outputFile string) error {
if outputFile == "" {
return nil
}
req := a.client.ApiApi.GetRunResultJUnit(ctx, testId, testRunId)
junit, _, err := a.client.ApiApi.GetRunResultJUnitExecute(req)
if err != nil {
return fmt.Errorf("could not execute request: %w", err)
}
f, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("could not create junit output file: %w", err)
}
_, err = f.WriteString(junit)
return err
}
func (a runTestAction) getTestRun(ctx context.Context, testID string, runID int32) (openapi.TestRun, error) {
run, _, err := a.client.ApiApi.
GetTestRun(ctx, testID, runID).
Execute()
if err != nil {
return openapi.TestRun{}, fmt.Errorf("could not execute request: %w", err)
}
return *run, nil
}
func (a runTestAction) getTransactionRun(ctx context.Context, transactionID string, runID int32) (openapi.TransactionRun, error) {
run, _, err := a.client.ApiApi.
GetTransactionRun(ctx, transactionID, runID).
Execute()
if err != nil {
return openapi.TransactionRun{}, fmt.Errorf("could not execute request: %w", err)
}
return *run, nil
}
func (a runTestAction) waitForTestResult(ctx context.Context, testID string, testRunID int32) (openapi.TestRun, error) {
var (
testRun openapi.TestRun
lastError error
wg sync.WaitGroup
)
wg.Add(1)
ticker := time.NewTicker(1 * time.Second) // TODO: change to websockets
go func() {
for {
select {
case <-ticker.C:
readyTestRun, err := a.isTestReady(ctx, testID, testRunID)
if err != nil {
lastError = err
wg.Done()
return
}
if readyTestRun != nil {
testRun = *readyTestRun
wg.Done()
return
}
}
}
}()
wg.Wait()
if lastError != nil {
return openapi.TestRun{}, lastError
}
return testRun, nil
}
func (a runTestAction) waitForTransactionResult(ctx context.Context, transactionID, transactionRunID string) (openapi.TransactionRun, error) {
var (
transactionRun openapi.TransactionRun
lastError error
wg sync.WaitGroup
)
wg.Add(1)
ticker := time.NewTicker(1 * time.Second) // TODO: change to websockets
go func() {
for {
select {
case <-ticker.C:
readyTransactionRun, err := a.isTransactionReady(ctx, transactionID, transactionRunID)
if err != nil {
lastError = err
wg.Done()
return
}
if readyTransactionRun != nil {
transactionRun = *readyTransactionRun
wg.Done()
return
}
}
}
}()
wg.Wait()
if lastError != nil {
return openapi.TransactionRun{}, lastError
}
return transactionRun, nil
}
func (a runTestAction) isTestReady(ctx context.Context, testID string, testRunId int32) (*openapi.TestRun, error) {
req := a.client.ApiApi.GetTestRun(ctx, testID, testRunId)
run, _, err := a.client.ApiApi.GetTestRunExecute(req)
if err != nil {
return &openapi.TestRun{}, fmt.Errorf("could not execute GetTestRun request: %w", err)
}
if utils.RunStateIsFinished(run.GetState()) {
return run, nil
}
return nil, nil
}
func (a runTestAction) isTransactionReady(ctx context.Context, transactionID, transactionRunId string) (*openapi.TransactionRun, error) {
runId, err := strconv.Atoi(transactionRunId)
if err != nil {
return nil, fmt.Errorf("invalid transaction run id format: %w", err)
}
req := a.client.ApiApi.GetTransactionRun(ctx, transactionID, int32(runId))
run, _, err := a.client.ApiApi.GetTransactionRunExecute(req)
if err != nil {
return nil, fmt.Errorf("could not execute GetTestRun request: %w", err)
}
if utils.RunStateIsFinished(run.GetState()) {
return run, nil
}
return nil, nil
}
func (a runTestAction) getMetadata() map[string]string {
ci := cienvironment.DetectCIEnvironment()
if ci == nil {
return map[string]string{}
}
metadata := map[string]string{
"name": ci.Name,
"url": ci.URL,
"buildNumber": ci.BuildNumber,
}
if ci.Git != nil {
metadata["branch"] = ci.Git.Branch
metadata["tag"] = ci.Git.Tag
metadata["revision"] = ci.Git.Revision
}
return metadata
}
func getTestRunIDFromString(testRunIDAsString string) int32 {
testRunID, _ := strconv.Atoi(testRunIDAsString)
return int32(testRunID)
}
func getTestRunID(testRun openapi.TestRun) int32 {
return getTestRunIDFromString(testRun.GetId())
}