-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain_test.go
More file actions
383 lines (316 loc) · 9.21 KB
/
main_test.go
File metadata and controls
383 lines (316 loc) · 9.21 KB
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
/**
Tests for the API and Client. Runs a local Houston API server in a goroutine and uses Houston's Go client to run tests.
Each test creates a new mission, with the ID set to the name of the test.
*/
package main
import (
"encoding/json"
"fmt"
"github.com/datasparq-ai/houston/api"
"github.com/datasparq-ai/houston/client"
"github.com/datasparq-ai/houston/model"
"os"
"strings"
"testing"
"time"
)
var testKeyId = "test"
// creates a local server that is used by most of the tests in this file
func TestMain(m *testing.M) {
a := api.New("")
a.DeleteKey(testKeyId)
a.CreateKey(testKeyId, "unittest-1")
go a.Run()
code := m.Run() // run tests
a.DeleteKey(testKeyId) // clean up
os.Exit(code)
}
// create a mission using the client
func Test_GetMission(t *testing.T) {
c := client.New(testKeyId, "")
data, _ := os.ReadFile("tests/test_plan.json")
var originalPlan model.Plan
_ = json.Unmarshal(data, &originalPlan)
res, err := c.CreateMission(string(data), "TestAPI_GetMission", nil)
if err != nil {
t.Fatalf(`Could not create mission`)
}
missionId := res.Id
m, err := c.GetMission(missionId)
if err != nil {
t.Fatalf(`Could not get mission`)
}
if m.Name != originalPlan.Name {
t.Fatalf("Got the wrong mission/plan back!")
}
missions, err := c.ListActiveMissions()
missionExists := false
for i := range missions {
if missions[i] == missionId {
missionExists = true
}
}
if !missionExists {
t.Fatalf("Mission not found when listing missions")
}
}
func Test_PostMissionStage(t *testing.T) {
c := client.New(testKeyId, "")
data, _ := os.ReadFile("tests/test_plan.json")
res, err := c.CreateMission(string(data), "TestAPI_PostMissionStage", nil)
if err != nil {
t.Fatalf(`Could not create mission`)
}
missionId := res.Id
activeMissions, err := c.ListActiveMissions()
if err != nil {
t.Fatalf("Got an error trying to list active missions")
}
if len(activeMissions) < 1 {
t.Fatalf("There should be at least one active mission")
}
isThisMissionListed := false
for _, m := range activeMissions {
if m == "n" {
t.Fatalf("Reserved key was listed as a mission")
}
if m != missionId {
isThisMissionListed = true
break
}
}
if !isThisMissionListed {
t.Fatalf("The active mission list should contain the same ID as the one we just created")
}
r, err := c.StartStage(missionId, "stage-1", false)
if err != nil || !r.Success {
t.Fatalf(`Could not start stage`)
}
_, err = c.StartStage(missionId, "stage-1", false)
if err == nil {
t.Fatalf(`Didn't get an error when starting stage twice`)
}
start := time.Now()
_, err = c.FinishStage(missionId, "stage-1", false)
if err != nil {
t.Fatalf(`Could not finish stage`)
}
if td := time.Since(start); td > time.Millisecond*5 {
t.Fatalf("Finishing a stage took too long at %v", td)
}
}
func Test_PostMissionStage_StartStage_IgnoreDependencies(t *testing.T) {
c := client.New(testKeyId, "")
data, _ := os.ReadFile("tests/test_plan.json")
res, err := c.CreateMission(string(data), "TestAPI_PostMissionStage_StartStage_IgnoreDependencies", nil)
if err != nil {
t.Fatalf(`Could not create mission`)
}
missionId := res.Id
r, err := c.StartStage(missionId, "stage-2", true)
if err != nil || !r.Success {
t.Fatalf(`Could not start stage despite ignoring dependencies`)
}
_, err = c.StartStage(missionId, "stage-1", false)
if err == nil {
t.Fatalf(`Didn't get an error when starting excluded stage`)
}
r, err = c.FinishStage(missionId, "stage-2", false)
if err != nil {
t.Fatalf(`Could not finish stage`)
}
if !r.IsComplete {
t.Fatalf(`Mission should be complete`)
}
}
func Test_SavePlan(t *testing.T) {
c := client.New(testKeyId, "")
err := c.SavePlan("tests/test_plan.json")
if err != nil {
t.Fatalf(`Plan should be saved without error`)
}
err = c.SavePlan("tests/test_plan.json") // try to save the save plan again
if err != nil {
t.Fatalf(`Couldn't update saved plan`)
}
res, err := c.CreateMission("test-plan", "", nil)
if err != nil {
t.Fatalf(`Couldn't create mission with newly saved plan`)
}
m, err := c.GetMission(res.Id)
if err != nil {
t.Fatalf(`Couldn't get mission`)
}
stageCount := 0
for _, stage := range m.Stages {
if stage.Name == "stage-1" || stage.Name == "stage-2" {
stageCount += 1
}
}
if stageCount != 2 {
t.Fatalf(`Number of stages in the mission did not match the saved plan`)
}
_, err = c.StartStage(res.Id, "stage-1", false)
if err != nil {
t.Fatalf(`Couldn't start stage of mission from saved plan`)
}
plans, err := c.ListPlans()
planExists := false
for i := range plans {
if plans[i] == "test-plan" {
planExists = true
}
}
if !planExists {
t.Fatalf("Plan not found when listing plans")
}
}
func Test_DeletePlan(t *testing.T) {
c := client.New(testKeyId, "")
c.SavePlan("tests/test_plan_deleted.json")
p, err := c.GetPlan("test-plan-deleted")
if err != nil {
t.Fatalf(`Couldn't get saved plan`)
}
if p.Name != "test-plan-deleted" && len(p.Stages) != 2 {
t.Fatalf(`Client.GetPlan didn't return the right plan`)
}
err = c.DeletePlan("test-plan-deleted")
if err != nil {
t.Fatalf(`Couldn't delete saved plan`)
}
_, err = c.CreateMission("test-plan-deleted", "", nil)
if err == nil {
t.Fatalf(`Created mission with deleted plan`)
}
plans, err := c.ListPlans()
if err != nil {
t.Fatalf("Got an error when trying to list plans")
}
planExists := false
for i := range plans {
if plans[i] == "test-plan-deleted" {
planExists = true
}
}
if planExists {
t.Fatalf("Deleted plan still exists when listing plans")
}
}
// create keys in a password protected API
func Test_UsePassword(t *testing.T) {
// this uses a different port and redis database (if redis is used)
a := api.New("tests/test_config.yaml")
go a.Run()
// use password to create a key
c := client.New("", "http://localhost:8001/api/v1")
_, err := c.CreateKey("", "TestAPI_UsePassword", "foobar1234")
if err != nil {
t.Fatalf("Could not create key in password protected API")
}
_, err = c.CreateKey("", "TestAPI_UsePassword", "wrongpassword")
if err == nil {
t.Fatalf("Using the wrong password should give an error")
}
_, err = c.CreateKey("", "TestAPI_UsePassword", "")
if err == nil {
t.Fatalf("Using no password should give an error")
}
}
// createAConflict attempts to update the mission and returns any errors. There should be 429 errors if this function
// is run multiple times at the same time, but the client should retry for them until the stages complete successfully.
func createAConflict(client *client.Client, missionId string, stage rune, errorChannel chan error) {
_, err := client.StartStage(missionId, "s1"+string(stage), false)
errorChannel <- err
_, err = client.FinishStage(missionId, "s1"+string(stage), false)
errorChannel <- err
_, err = client.StartStage(missionId, "s2"+string(stage), false)
errorChannel <- err
_, err = client.FinishStage(missionId, "s2"+string(stage), false)
errorChannel <- err
fmt.Println("finished all")
}
// note: this tests takes longer because the client has to retry!
func Test_ConcurrentMissionUpdates(t *testing.T) {
c := client.New(testKeyId, "")
res, err := c.CreateMission("tests/test_plan_big.json", "ConcurrentMissionUpdates", nil)
if err != nil {
t.Fatalf("Got an error when creating mission: %s", err.Error())
}
errorChannel := make(chan error)
c.StartStage(res.Id, "s0", false)
c.FinishStage(res.Id, "s0", false)
for _, letter := range "abcdefghij" {
go createAConflict(&c, res.Id, letter, errorChannel)
}
gotConflict := false
counter := 0
for err := range errorChannel {
counter += 1
if err != nil {
gotConflict = true
fmt.Println(err.Error())
}
if counter >= 10*4 {
close(errorChannel)
}
}
if gotConflict {
t.Fatalf("A stage failed!")
}
_, err = c.StartStage(res.Id, "s3", false)
if err != nil {
t.Fatalf("Couldn't start final stage (this means concurrent stages failed)")
}
_, err = c.FinishStage(res.Id, "s3", false)
if err != nil {
t.Fatalf("Couldn't start final stage (this means concurrent stages failed)")
}
}
func Test_ListKeys(t *testing.T) {
c := client.New(testKeyId, "")
// Generate 2 more keys
c.CreateKey("test2", "", "")
c.CreateKey("test3", "", "")
// Extract list of keys
keys, err := c.ListKeys("")
if err != nil {
panic(err)
t.Fatalf("Got an error when trying to list keys")
}
// note: this gives a different result when running with local db vs Redis db, because there is only one instance
// of redis, whereas different tests use completely new instances of the local db
// Should have the 3 keys in db including original test key
//noKeys := len(keys)
foundKeys := []int{0, 0, 0}
for _, key := range keys {
if key == testKeyId {
foundKeys[0]++
} else if key == "test2" {
foundKeys[1]++
} else if key == "test3" {
foundKeys[2]++
}
}
for _, keyCount := range foundKeys {
if keyCount != 1 {
t.Fatalf("A key was not listed exactly one time, got " + strings.Join(keys, ", "))
}
}
}
func Test_DeleteKey(t *testing.T) {
c := client.New(testKeyId, "")
c.DeleteKey("")
// Extract list of keys
keys, _ := c.ListKeys("")
// Ensure test key is not in list of keys
keyExists := false
for i := range keys {
if keys[i] == testKeyId {
keyExists = true
}
}
if keyExists {
t.Fatalf("Deleted key still exists when listing keys")
}
}