-
Notifications
You must be signed in to change notification settings - Fork 67
/
transaction_runner_test.go
186 lines (150 loc) 路 4.93 KB
/
transaction_runner_test.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
package executor_test
import (
"context"
"fmt"
"strconv"
"testing"
"time"
"github.com/kubeshop/tracetest/server/executor"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/maps"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/testmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fakeTestRunner struct {
db model.Repository
subscriptionManager *subscription.Manager
returnErr bool
uid int
}
func (r *fakeTestRunner) Run(ctx context.Context, test model.Test, metadata model.RunMetadata, env model.Environment) model.Run {
run := model.NewRun()
run.Environment = env
run.State = model.RunStateCreated
newRun, err := r.db.CreateRun(ctx, test, run)
if err != nil {
panic(err)
}
go func() {
run := newRun // make a local copy to avoid race conditions
time.Sleep(100 * time.Millisecond) // simulate some real work
if r.returnErr {
run.State = model.RunStateTriggerFailed
run.LastError = fmt.Errorf("failed to do something")
} else {
run.State = model.RunStateFinished
}
r.uid++
run.Outputs = (maps.Ordered[string, model.RunOutput]{}).MustAdd("USER_ID", model.RunOutput{
Value: strconv.Itoa(r.uid),
})
err = r.db.UpdateRun(ctx, run)
r.subscriptionManager.PublishUpdate(subscription.Message{
ResourceID: newRun.ResourceID(),
Type: "result_update",
Content: run,
})
}()
return newRun
}
func TestTransactionRunner(t *testing.T) {
t.Run("NoErrors", func(t *testing.T) {
runTransactionRunnerTest(t, false, func(t *testing.T, actual model.TransactionRun) {
assert.Equal(t, model.TransactionRunStateFinished, actual.State)
assert.Len(t, actual.Steps, 2)
assert.Equal(t, actual.Steps[0].State, model.RunStateFinished)
assert.Equal(t, actual.Steps[1].State, model.RunStateFinished)
assert.Equal(t, "http://my-service.com", actual.Environment.Get("url"))
assert.Equal(t, model.RunOutput{Name: "", Value: "1", SpanID: ""}, actual.Steps[0].Outputs.Get("USER_ID"))
// this assertion is supposed to test that the output from the previous step
// is injected in the env for the next. In practice, this depends
// on the `fakeTestRunner` used here to actually save the environment
// to the test run, like the real test runner would.
// see line 27
assert.Equal(t, "1", actual.Steps[1].Environment.Get("USER_ID"))
assert.Equal(t, model.RunOutput{Name: "", Value: "2", SpanID: ""}, actual.Steps[1].Outputs.Get("USER_ID"))
assert.Equal(t, "2", actual.Environment.Get("USER_ID"))
})
})
t.Run("WithErrors", func(t *testing.T) {
runTransactionRunnerTest(t, true, func(t *testing.T, actual model.TransactionRun) {
assert.Equal(t, model.TransactionRunStateFailed, actual.State)
require.Len(t, actual.Steps, 1)
assert.Equal(t, model.RunStateTriggerFailed, actual.Steps[0].State)
})
})
}
func getDB() (model.Repository, func()) {
db, err := testmock.GetTestingDatabase()
if err != nil {
panic(err)
}
clean := func() {
err = db.Drop()
if err != nil {
panic(err)
}
}
return db, clean
}
func runTransactionRunnerTest(t *testing.T, withErrors bool, assert func(t *testing.T, actual model.TransactionRun)) {
ctx := context.Background()
db, clear := getDB()
defer clear()
subscriptionManager := subscription.NewManager()
testRunner := &fakeTestRunner{
db,
subscriptionManager,
withErrors,
0,
}
test1, err := db.CreateTest(ctx, model.Test{Name: "Test 1"})
require.NoError(t, err)
test2, err := db.CreateTest(ctx, model.Test{Name: "Test 2"})
require.NoError(t, err)
transaction, err := db.CreateTransaction(ctx, model.Transaction{
Name: "transaction",
Steps: []model.Test{test1, test2},
})
require.NoError(t, err)
metadata := model.RunMetadata{
"environment": "production",
"service": "tracetest",
}
env, err := db.CreateEnvironment(ctx, model.Environment{
Name: "production",
Values: []model.EnvironmentValue{
{
Key: "url",
Value: "http://my-service.com",
},
},
})
require.NoError(t, err)
runner := executor.NewTransactionRunner(testRunner, db, subscriptionManager)
runner.Start(1)
ctxWithTimeout, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
transactionRun := runner.Run(ctxWithTimeout, transaction, metadata, env)
done := make(chan model.TransactionRun, 1)
sf := subscription.NewSubscriberFunction(func(m subscription.Message) error {
tr := m.Content.(model.TransactionRun)
if tr.State.IsFinal() {
done <- tr
}
return nil
})
subscriptionManager.Subscribe(transactionRun.ResourceID(), sf)
var finalRun model.TransactionRun
select {
case finalRun = <-done:
subscriptionManager.Unsubscribe(transactionRun.ResourceID(), sf.ID()) //cleanup to avoid race conditions
fmt.Println("run completed")
case <-time.After(10 * time.Second):
t.Log("timeout after 10 second")
t.FailNow()
}
assert(t, finalRun)
}