-
Notifications
You must be signed in to change notification settings - Fork 127
/
mongo_test.go
277 lines (229 loc) · 11.4 KB
/
mongo_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
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
//go:build integration
package result
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/kubeshop/testkube/internal/pkg/api/datefilter"
"github.com/kubeshop/testkube/internal/pkg/api/repository/storage"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/rand"
)
const (
mongoDns = "mongodb://localhost:27017"
mongoDbName = "testkube"
)
func TestStorage(t *testing.T) {
assert := require.New(t)
repository, err := getRepository()
assert.NoError(err)
err = repository.Coll.Drop(context.TODO())
assert.NoError(err)
oneDayAgo := time.Now().Add(-24 * time.Hour)
twoDaysAgo := time.Now().Add(-48 * time.Hour)
defaultName := "name"
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, time.Now(), []string{"test1", "test2"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, time.Now(), []string{"test1", "test2"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, time.Now(), []string{"test3", "test4"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, time.Now(), []string{"test3", "test4"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.SUCCESS_ExecutionStatus, time.Now(), []string{"test1", "test4"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.QUEUED_ExecutionStatus, time.Now(), []string{"test1", "test3"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.PENDING_ExecutionStatus, time.Now(), []string{"test5", "test6"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, oneDayAgo, []string{"test1", "test5"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, oneDayAgo, []string{"test1", "test6"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, oneDayAgo, []string{"test2", "test4"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, oneDayAgo, []string{"test2", "test5"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.SUCCESS_ExecutionStatus, oneDayAgo, []string{"test7", "test8"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.QUEUED_ExecutionStatus, oneDayAgo, []string{"test7", "test8"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.PENDING_ExecutionStatus, oneDayAgo, []string{"test7", "test8"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, twoDaysAgo, []string{"test7", "test8"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, twoDaysAgo, []string{"test1", "test2"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, twoDaysAgo, []string{"test1", "test2"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.ERROR__ExecutionStatus, twoDaysAgo, []string{"test1", "test2"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.SUCCESS_ExecutionStatus, twoDaysAgo, []string{"test3", "test6"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.QUEUED_ExecutionStatus, twoDaysAgo, []string{"test3", "test5"})
assert.NoError(err)
err = repository.insertExecutionResult(defaultName, testkube.PENDING_ExecutionStatus, twoDaysAgo, []string{"test4", "test6"})
assert.NoError(err)
numberOfTags := 8
t.Run("filter with status should return only executions with that status", func(t *testing.T) {
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithStatus(testkube.ERROR__ExecutionStatus))
assert.NoError(err)
assert.Len(executions, 12)
assert.Equal(*executions[0].ExecutionResult.Status, testkube.ERROR__ExecutionStatus)
})
t.Run("filter with status should return only totals with that status", func(t *testing.T) {
filteredTotals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithStatus(testkube.ERROR__ExecutionStatus))
assert.NoError(err)
assert.Equal(int32(12), filteredTotals.Results)
assert.Equal(int32(12), filteredTotals.Failed)
assert.Equal(int32(0), filteredTotals.Passed)
assert.Equal(int32(0), filteredTotals.Queued)
assert.Equal(int32(0), filteredTotals.Pending)
})
t.Run("getting totals without filters should return all the executions", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background())
assert.NoError(err)
assert.Equal(int32(21), totals.Results)
assert.Equal(int32(12), totals.Failed)
assert.Equal(int32(3), totals.Passed)
assert.Equal(int32(3), totals.Queued)
assert.Equal(int32(3), totals.Pending)
})
dateFilter := datefilter.NewDateFilter(oneDayAgo.Format(datefilter.DateFormatISO8601), "")
assert.True(dateFilter.IsStartValid)
t.Run("filter with startDate should return only executions after that day", func(t *testing.T) {
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithStartDate(dateFilter.Start))
assert.NoError(err)
assert.Len(executions, 14)
assert.True(executions[0].StartTime.After(dateFilter.Start) || executions[0].StartTime.Equal(dateFilter.Start))
})
t.Run("filter with tags should return only filters with given tags", func(t *testing.T) {
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithTags([]string{"test1", "test2"}))
assert.NoError(err)
assert.Len(executions, 5)
})
t.Run("getting totals with filter by date start date should return only the results after this date", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithStartDate(dateFilter.Start))
assert.NoError(err)
assert.Equal(int32(14), totals.Results)
assert.Equal(int32(8), totals.Failed)
assert.Equal(int32(2), totals.Passed)
assert.Equal(int32(2), totals.Queued)
assert.Equal(int32(2), totals.Pending)
})
dateFilter = datefilter.NewDateFilter("", oneDayAgo.Format(datefilter.DateFormatISO8601))
assert.True(dateFilter.IsEndValid)
t.Run("filter with endDate should return only executions before that day", func(t *testing.T) {
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithEndDate(dateFilter.End))
assert.NoError(err)
assert.Len(executions, 7)
assert.True(executions[0].StartTime.Before(dateFilter.End) || executions[0].StartTime.Equal(dateFilter.End))
})
t.Run("getting totals with filter by date start date should return only the results before this date", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithEndDate(dateFilter.End))
assert.NoError(err)
assert.Equal(int32(7), totals.Results)
assert.Equal(int32(4), totals.Failed)
assert.Equal(int32(1), totals.Passed)
assert.Equal(int32(1), totals.Queued)
assert.Equal(int32(1), totals.Pending)
})
t.Run("filter with script name that doesn't exist should return 0 results", func(t *testing.T) {
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithScriptName("noneExisting"))
assert.NoError(err)
assert.Empty(executions)
})
t.Run("getting totals with script name that doesn't exist should return 0 results", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithScriptName("noneExisting"))
assert.NoError(err)
assert.Equal(int32(0), totals.Results)
assert.Equal(int32(0), totals.Failed)
assert.Equal(int32(0), totals.Passed)
assert.Equal(int32(0), totals.Queued)
assert.Equal(int32(0), totals.Pending)
})
t.Run("filter with ccombined filter should return corresponding results", func(t *testing.T) {
filter := NewExecutionsFilter().
WithStatus(testkube.SUCCESS_ExecutionStatus).
WithStartDate(twoDaysAgo).
WithEndDate(oneDayAgo).
WithScriptName(defaultName)
executions, err := repository.GetExecutions(context.Background(), filter)
assert.NoError(err)
assert.Len(executions, 2)
})
t.Run("getting totals with ccombined filter should return corresponding results", func(t *testing.T) {
filter := NewExecutionsFilter().
WithStatus(testkube.SUCCESS_ExecutionStatus).
WithStartDate(twoDaysAgo).
WithEndDate(oneDayAgo).
WithScriptName(defaultName)
totals, err := repository.GetExecutionTotals(context.Background(), filter)
assert.NoError(err)
assert.Equal(int32(2), totals.Results)
assert.Equal(int32(0), totals.Failed)
assert.Equal(int32(2), totals.Passed)
assert.Equal(int32(0), totals.Queued)
assert.Equal(int32(0), totals.Pending)
})
name := "someDifferentName"
err = repository.insertExecutionResult(name, testkube.PENDING_ExecutionStatus, twoDaysAgo, nil)
assert.NoError(err)
t.Run("filter with script name should return result only for that script name", func(t *testing.T) {
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithScriptName(name))
assert.NoError(err)
assert.Len(executions, 1)
assert.Equal(executions[0].ScriptName, name)
})
t.Run("getting totals with script name should return result only for that script name", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithScriptName(name))
assert.NoError(err)
assert.Equal(int32(1), totals.Results)
assert.Equal(int32(0), totals.Failed)
assert.Equal(int32(0), totals.Passed)
assert.Equal(int32(0), totals.Queued)
assert.Equal(int32(1), totals.Pending)
})
t.Run("scripts should be sorted with most recent first", func(t *testing.T) {
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter())
assert.NoError(err)
assert.NotEmpty(executions)
assert.True(executions[0].StartTime.After(executions[len(executions)-1].StartTime), "executions are not sorted with the most recent first")
})
t.Run("getting tags should return all available tags", func(t *testing.T) {
tags, err := repository.GetTags(context.Background())
assert.NoError(err)
assert.Len(tags, numberOfTags)
})
}
func TestTags(t *testing.T) {
assert := require.New(t)
repository, err := getRepository()
assert.NoError(err)
err = repository.Coll.Drop(context.TODO())
assert.NoError(err)
t.Run("getting tags when there are no tags should return empty slice", func(t *testing.T) {
tags, err := repository.GetTags(context.Background())
assert.NoError(err)
assert.Len(tags, 0)
})
}
func getRepository() (*MongoRepository, error) {
db, err := storage.GetMongoDataBase(mongoDns, mongoDbName)
repository := NewMongoRespository(db)
return repository, err
}
func (repository *MongoRepository) insertExecutionResult(scriptName string, execStatus testkube.ExecutionStatus, startTime time.Time, tags []string) error {
return repository.Insert(context.Background(),
testkube.Execution{
Id: rand.Name(),
ScriptName: scriptName,
Name: "dummyName",
ScriptType: "test/curl",
StartTime: startTime,
EndTime: time.Now(),
ExecutionResult: &testkube.ExecutionResult{Status: &execStatus},
Tags: tags,
})
}