-
Notifications
You must be signed in to change notification settings - Fork 67
/
assertion_runner.go
212 lines (171 loc) 路 7.17 KB
/
assertion_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
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
package executor
import (
"context"
"errors"
"fmt"
"log"
"github.com/kubeshop/tracetest/server/expression"
"github.com/kubeshop/tracetest/server/http/middleware"
"github.com/kubeshop/tracetest/server/model/events"
"github.com/kubeshop/tracetest/server/pkg/maps"
"github.com/kubeshop/tracetest/server/pkg/pipeline"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/test"
"github.com/kubeshop/tracetest/server/variableset"
)
type defaultAssertionRunner struct {
updater RunUpdater
assertionExecutor AssertionExecutor
outputsProcessor OutputsProcessorFn
subscriptionManager subscription.Manager
eventEmitter EventEmitter
}
func NewAssertionRunner(
updater RunUpdater,
assertionExecutor AssertionExecutor,
op OutputsProcessorFn,
subscriptionManager subscription.Manager,
eventEmitter EventEmitter,
) *defaultAssertionRunner {
return &defaultAssertionRunner{
outputsProcessor: op,
updater: updater,
assertionExecutor: assertionExecutor,
subscriptionManager: subscriptionManager,
eventEmitter: eventEmitter,
}
}
func (e *defaultAssertionRunner) SetOutputQueue(pipeline.Enqueuer[Job]) {
// this is a no-op, as assertion runner does not need to enqueue anything
}
func (e *defaultAssertionRunner) ProcessItem(ctx context.Context, job Job) {
select {
default:
case <-ctx.Done():
return
}
run, err := e.runAssertionsAndUpdateResult(ctx, job)
log.Printf("[AssertionRunner] Test %s Run %d: update channel start\n", job.Test.ID, job.Run.ID)
e.subscriptionManager.PublishUpdate(subscription.Message{
ResourceID: run.TransactionStepResourceID(),
Type: "run_update",
Content: RunResult{Run: run, Err: err},
})
log.Printf("[AssertionRunner] Test %s Run %d: update channel complete\n", job.Test.ID, job.Run.ID)
if err != nil {
log.Printf("[AssertionRunner] Test %s Run %d: error with runAssertionsAndUpdateResult: %s\n", job.Test.ID, job.Run.ID, err.Error())
}
}
func (e *defaultAssertionRunner) runAssertionsAndUpdateResult(ctx context.Context, job Job) (test.Run, error) {
log.Printf("[AssertionRunner] Test %s Run %d: Starting\n", job.Test.ID, job.Run.ID)
err := e.eventEmitter.Emit(ctx, events.TestSpecsRunStart(job.Test.ID, job.Run.ID))
if err != nil {
log.Printf("[AssertionRunner] Test %s Run %d: fail to emit TestSpecsRunStart event: %s\n", job.Test.ID, job.Run.ID, err.Error())
}
run, err := e.executeAssertions(ctx, job)
if err != nil {
log.Printf("[AssertionRunner] Test %s Run %d: error executing assertions: %s\n", job.Test.ID, job.Run.ID, err.Error())
anotherErr := e.eventEmitter.Emit(ctx, events.TestSpecsRunError(job.Test.ID, job.Run.ID, err))
if anotherErr != nil {
log.Printf("[AssertionRunner] Test %s Run %d: fail to emit TestSpecsRunError event: %s\n", job.Test.ID, job.Run.ID, anotherErr.Error())
}
run = run.AssertionFailed(err)
middleware.SendEventWithProperties("test_run_finished", "error", "", map[string]string{"finalState": string(run.State)}, ctx)
return test.Run{}, e.updater.Update(ctx, run)
}
log.Printf("[AssertionRunner] Test %s Run %d: Success. pass: %d, fail: %d\n", job.Test.ID, job.Run.ID, run.Pass, run.Fail)
err = e.updater.Update(ctx, run)
if err != nil {
log.Printf("[AssertionRunner] Test %s Run %d: error updating run: %s\n", job.Test.ID, job.Run.ID, err.Error())
anotherErr := e.eventEmitter.Emit(ctx, events.TestSpecsRunPersistenceError(job.Test.ID, job.Run.ID, err))
if anotherErr != nil {
log.Printf("[AssertionRunner] Test %s Run %d: fail to emit TestSpecsRunPersistenceError event: %s\n", job.Test.ID, job.Run.ID, anotherErr.Error())
}
return test.Run{}, fmt.Errorf("could not save result on database: %w", err)
}
err = e.eventEmitter.Emit(ctx, events.TestSpecsRunSuccess(job.Test.ID, job.Run.ID))
if err != nil {
log.Printf("[AssertionRunner] Test %s Run %d: fail to emit TestSpecsRunSuccess event: %s\n", job.Test.ID, job.Run.ID, err.Error())
}
return run, nil
}
func (e *defaultAssertionRunner) executeAssertions(ctx context.Context, req Job) (test.Run, error) {
run := req.Run
if run.Trace == nil {
return test.Run{}, fmt.Errorf("trace not available")
}
ds := []expression.DataStore{expression.VariableDataStore{
Values: req.Run.VariableSet.Values,
}}
outputs, err := e.outputsProcessor(ctx, req.Test.Outputs, *run.Trace, ds)
if err != nil {
return test.Run{}, fmt.Errorf("cannot process outputs: %w", err)
}
e.validateOutputResolution(ctx, req, outputs)
newVariableSet := createVariableSet(req.Run.VariableSet, outputs)
ds = []expression.DataStore{expression.VariableDataStore{Values: newVariableSet.Values}}
assertionResult, allPassed := e.assertionExecutor.Assert(ctx, req.Test.Specs, *run.Trace, ds)
e.emitFailedAssertions(ctx, req, assertionResult)
run = run.SuccessfullyAsserted(
outputs,
newVariableSet,
assertionResult,
allPassed,
)
middleware.SendEventWithProperties("test_run_finished", "successful", "", map[string]string{"finalState": string(run.State)}, ctx)
return run, nil
}
func (e *defaultAssertionRunner) emitFailedAssertions(ctx context.Context, req Job, result maps.Ordered[test.SpanQuery, []test.AssertionResult]) {
for _, assertionResults := range result.Unordered() {
for _, assertionResult := range assertionResults {
for _, spanAssertionResult := range assertionResult.Results {
if errors.Is(spanAssertionResult.CompareErr, expression.ErrExpressionResolution) {
unwrappedError := errors.Unwrap(spanAssertionResult.CompareErr)
e.eventEmitter.Emit(ctx, events.TestSpecsAssertionWarning(
req.Run.TestID,
req.Run.ID,
unwrappedError,
spanAssertionResult.SpanIDString(),
string(assertionResult.Assertion),
))
}
if errors.Is(spanAssertionResult.CompareErr, expression.ErrInvalidSyntax) {
e.eventEmitter.Emit(ctx, events.TestSpecsAssertionWarning(
req.Run.TestID,
req.Run.ID,
spanAssertionResult.CompareErr,
spanAssertionResult.SpanIDString(),
string(assertionResult.Assertion),
))
}
}
}
}
}
func createVariableSet(env variableset.VariableSet, outputs maps.Ordered[string, test.RunOutput]) variableset.VariableSet {
outputVariables := make([]variableset.VariableSetValue, 0)
outputs.ForEach(func(key string, val test.RunOutput) error {
outputVariables = append(outputVariables, variableset.VariableSetValue{
Key: val.Name,
Value: val.Value,
})
return nil
})
outputEnv := variableset.VariableSet{Values: outputVariables}
return env.Merge(outputEnv)
}
func (e *defaultAssertionRunner) validateOutputResolution(ctx context.Context, job Job, outputs maps.Ordered[string, test.RunOutput]) {
err := outputs.ForEach(func(outputName string, outputModel test.RunOutput) error {
if outputModel.Resolved {
return nil
}
anotherErr := e.eventEmitter.Emit(ctx, events.TestOutputGenerationWarning(job.Test.ID, job.Run.ID, outputModel.Error, outputName))
if anotherErr != nil {
log.Printf("[AssertionRunner] Test %s Run %d: fail to emit TestOutputGenerationWarning event: %s\n", job.Test.ID, job.Run.ID, anotherErr.Error())
}
return nil
})
if err != nil {
log.Printf("[AssertionRunner] Test %s Run %d: fail to validate outputs: %s\n", job.Test.ID, job.Run.ID, err.Error())
}
}