-
Notifications
You must be signed in to change notification settings - Fork 71
/
testsuite_runner.go
141 lines (115 loc) · 3.68 KB
/
testsuite_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
package runner
import (
"context"
"fmt"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/pkg/fileutil"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
)
type testSuiteFormatter interface {
Format(output formatters.TestSuiteRunOutput, format formatters.Output) string
}
type testSuiteRunner struct {
client resourcemanager.Client
openapiClient *openapi.APIClient
formatter testSuiteFormatter
}
func TestSuiteRunner(
client resourcemanager.Client,
openapiClient *openapi.APIClient,
formatter testSuiteFormatter,
) Runner {
return testSuiteRunner{
client: client,
openapiClient: openapiClient,
formatter: formatter,
}
}
func (r testSuiteRunner) Name() string {
return "testsuite"
}
func (r testSuiteRunner) GetByID(_ context.Context, id string) (resource any, _ error) {
jsonTestSuite, err := r.client.Get(context.Background(), id, jsonFormat)
if err != nil {
return nil, fmt.Errorf("cannot get test suite '%s': %w", id, err)
}
var testSuite openapi.TestSuiteResource
err = jsonFormat.Unmarshal([]byte(jsonTestSuite), &testSuite)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal test suite definition file: %w", err)
}
return testSuite, nil
}
func (r testSuiteRunner) Apply(ctx context.Context, df fileutil.File) (resource any, _ error) {
updated, err := r.client.Apply(ctx, df, yamlFormat)
if err != nil {
return nil, fmt.Errorf("could not read test suite file: %w", err)
}
var parsed openapi.TestSuiteResource
err = yamlFormat.Unmarshal([]byte(updated), &parsed)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal test suite definition file: %w", err)
}
return parsed, nil
}
func (r testSuiteRunner) StartRun(ctx context.Context, resource any, runInfo openapi.RunInformation) (RunResult, error) {
tran := resource.(openapi.TestSuiteResource)
run, resp, err := r.openapiClient.ApiApi.
RunTestSuite(ctx, tran.Spec.GetId()).
RunInformation(runInfo).
Execute()
err = HandleRunError(resp, err)
if err != nil {
return RunResult{}, err
}
full, err := r.client.Get(ctx, tran.Spec.GetId(), jsonFormat)
if err != nil {
return RunResult{}, fmt.Errorf("cannot get full test suite '%s': %w", tran.Spec.GetId(), err)
}
err = jsonFormat.Unmarshal([]byte(full), &tran)
if err != nil {
return RunResult{}, fmt.Errorf("cannot get full test suite '%s': %w", tran.Spec.GetId(), err)
}
return RunResult{
Resource: tran,
Run: *run,
}, nil
}
func (r testSuiteRunner) UpdateResult(ctx context.Context, result RunResult) (RunResult, error) {
testSuite := result.Resource.(openapi.TestSuiteResource)
run := result.Run.(openapi.TestSuiteRun)
updated, _, err := r.openapiClient.ApiApi.
GetTestSuiteRun(ctx, testSuite.Spec.GetId(), *run.Id).
Execute()
if err != nil {
return RunResult{}, err
}
allPassed := true
for _, s := range updated.GetSteps() {
if !s.Result.GetAllPassed() {
allPassed = false
break
}
}
passed := !isStateFailed(updated.GetState()) && allPassed && updated.GetAllStepsRequiredGatesPassed()
return RunResult{
Resource: testSuite,
Run: *updated,
Finished: isStateFinished(updated.GetState()),
Passed: passed,
}, nil
}
func (r testSuiteRunner) JUnitResult(ctx context.Context, result RunResult) (string, error) {
return "", ErrJUnitNotSupported
}
func (r testSuiteRunner) FormatResult(result RunResult, format string) string {
testSuite := result.Resource.(openapi.TestSuiteResource)
run := result.Run.(openapi.TestSuiteRun)
tro := formatters.TestSuiteRunOutput{
HasResults: result.Finished,
TestSuite: testSuite.GetSpec(),
Run: run,
}
return r.formatter.Format(tro, formatters.Output(format))
}