-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
407 lines (312 loc) · 10.3 KB
/
client_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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package integresql
import (
"context"
"database/sql"
"errors"
"sync"
"testing"
_ "github.com/lib/pq"
)
func TestClientInitializeTemplate(t *testing.T) {
ctx := context.Background()
c, err := DefaultClientFromEnv()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
if err := c.ResetAllTracking(ctx); err != nil {
t.Fatalf("failed to reset all test pool tracking: %v", err)
}
hash := "hashinghash1"
template, err := c.InitializeTemplate(ctx, hash)
if err != nil {
t.Fatalf("failed to initialize template: %v", err)
}
if len(template.Config.Database) == 0 {
t.Error("received invalid template database config")
}
}
func TestClientDiscardTemplate(t *testing.T) {
ctx := context.Background()
c, err := DefaultClientFromEnv()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
if err := c.ResetAllTracking(ctx); err != nil {
t.Fatalf("failed to reset all test pool tracking: %v", err)
}
hash := "hashinghash2"
if _, err := c.InitializeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to initialize template: %v", err)
}
if err := c.DiscardTemplate(ctx, hash); err != nil {
t.Fatalf("failed to discard template: %v", err)
}
if _, err := c.InitializeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to reinitialize template: %v", err)
}
if err := c.FinalizeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to refinalize template: %v", err)
}
}
func TestClientFinalizeTemplate(t *testing.T) {
ctx := context.Background()
c, err := DefaultClientFromEnv()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
if err := c.ResetAllTracking(ctx); err != nil {
t.Fatalf("failed to reset all test pool tracking: %v", err)
}
hash := "hashinghash2"
if _, err := c.InitializeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to initialize template: %v", err)
}
if err := c.FinalizeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to finalize template: %v", err)
}
}
func TestClientGetTestDatabase(t *testing.T) {
ctx := context.Background()
c, err := DefaultClientFromEnv()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
if err := c.ResetAllTracking(ctx); err != nil {
t.Fatalf("failed to reset all test pool tracking: %v", err)
}
hash := "hashinghash3"
if _, err := c.InitializeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to initialize template: %v", err)
}
if err := c.FinalizeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to finalize template: %v", err)
}
test, err := c.GetTestDatabase(ctx, hash)
if err != nil {
t.Fatalf("failed to get test database: %v", err)
}
if test.TemplateHash != hash {
t.Errorf("test database has invalid template hash, got %q, want %q", test.TemplateHash, hash)
}
db, err := sql.Open("postgres", test.Config.ConnectionString())
if err != nil {
t.Fatalf("failed to open test database connection: %v", err)
}
defer db.Close()
if err := db.Ping(); err != nil {
t.Fatalf("failed to ping test database connection: %v", err)
}
test2, err := c.GetTestDatabase(ctx, hash)
if err != nil {
t.Fatalf("failed to get second test database: %v", err)
}
if test2.TemplateHash != hash {
t.Errorf("test database has invalid second template hash, got %q, want %q", test2.TemplateHash, hash)
}
if test2.ID == test.ID {
t.Error("received same test database a second time without returning")
}
db2, err := sql.Open("postgres", test2.Config.ConnectionString())
if err != nil {
t.Fatalf("failed to open second test database connection: %v", err)
}
defer db2.Close()
if err := db2.Ping(); err != nil {
t.Fatalf("failed to ping second test database connection: %v", err)
}
}
func TestClientReturnTestDatabase(t *testing.T) {
ctx := context.Background()
c, err := DefaultClientFromEnv()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
if err := c.ResetAllTracking(ctx); err != nil {
t.Fatalf("failed to reset all test pool tracking: %v", err)
}
hash := "hashinghash4"
if _, err := c.InitializeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to initialize template: %v", err)
}
if err := c.FinalizeTemplate(ctx, hash); err != nil {
t.Fatalf("failed to finalize template: %v", err)
}
test, err := c.GetTestDatabase(ctx, hash)
if err != nil {
t.Fatalf("failed to get test database: %v", err)
}
if test.TemplateHash != hash {
t.Errorf("test database has invalid template hash, got %q, want %q", test.TemplateHash, hash)
}
if err := c.ReturnTestDatabase(ctx, hash, test.ID); err != nil {
t.Fatalf("failed to return test database: %v", err)
}
test2, err := c.GetTestDatabase(ctx, hash)
if err != nil {
t.Fatalf("failed to get second test database: %v", err)
}
if test2.TemplateHash != hash {
t.Errorf("test database has invalid second template hash, got %q, want %q", test2.TemplateHash, hash)
}
if test2.ID != test.ID {
t.Errorf("received invalid test database, want %d, got %d", test.ID, test2.ID)
}
}
func populateTemplateDB(ctx context.Context, db *sql.DB) error {
if _, err := db.ExecContext(ctx, `
CREATE EXTENSION "uuid-ossp";
CREATE TABLE pilots (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
"name" text NOT NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NULL,
CONSTRAINT pilot_pkey PRIMARY KEY (id)
);
CREATE TABLE jets (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
pilot_id uuid NOT NULL,
age int4 NOT NULL,
"name" text NOT NULL,
color text NOT NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NULL,
CONSTRAINT jet_pkey PRIMARY KEY (id)
);
ALTER TABLE jets ADD CONSTRAINT jet_pilots_fkey FOREIGN KEY (pilot_id) REFERENCES pilots(id);
`); err != nil {
return err
}
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, `
INSERT INTO pilots (id, "name", created_at, updated_at) VALUES ('744a1a87-5ef7-4309-8814-0f1054751156', 'Mario', '2020-03-23 09:44:00.548', '2020-03-23 09:44:00.548');
INSERT INTO pilots (id, "name", created_at, updated_at) VALUES ('20d9d155-2e95-49a2-8889-2ae975a8617e', 'Nick', '2020-03-23 09:44:00.548', '2020-03-23 09:44:00.548');
INSERT INTO jets (id, pilot_id, age, "name", color, created_at, updated_at) VALUES ('67d9d0c7-34e5-48b0-9c7d-c6344995353c', '744a1a87-5ef7-4309-8814-0f1054751156', 26, 'F-14B', 'grey', '2020-03-23 09:44:00.000', '2020-03-23 09:44:00.000');
INSERT INTO jets (id, pilot_id, age, "name", color, created_at, updated_at) VALUES ('facaf791-21b4-401a-bbac-67079ae4921f', '20d9d155-2e95-49a2-8889-2ae975a8617e', 27, 'F-14B', 'grey/red', '2020-03-23 09:44:00.000', '2020-03-23 09:44:00.000');
`); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return err
}
return nil
}
func TestSetupTemplateWithDBClient(t *testing.T) {
ctx := context.Background()
c, err := DefaultClientFromEnv()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
if err := c.ResetAllTracking(ctx); err != nil {
t.Fatalf("failed to reset all test pool tracking: %v", err)
}
hash := "hashinghash5"
if err := c.SetupTemplateWithDBClient(ctx, hash, func(db *sql.DB) error {
// setup code
if err := populateTemplateDB(ctx, db); err != nil {
t.Fatalf("failed to populate template db: %v", err)
}
return err
}); err != nil {
t.Fatalf("Failed to setup template database for hash %q: %v", hash, err)
}
}
func getTestDB(wg *sync.WaitGroup, errs chan<- error, c *Client, hash string) {
defer wg.Done()
_, err := c.GetTestDatabase(context.Background(), hash)
if err != nil {
errs <- err
return
}
errs <- nil
}
func TestSetupTemplateWithDBClientFailingSetupCodeAndReinitialize(t *testing.T) {
ctx := context.Background()
c, err := DefaultClientFromEnv()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
if err := c.ResetAllTracking(ctx); err != nil {
t.Fatalf("failed to reset all test pool tracking: %v", err)
}
hash := "hashinghash5"
testDBWhileInitializeCount := 5
testDBPreDiscardCount := 5
testDBAfterDiscardCount := 5
allTestDbCount := testDBWhileInitializeCount + testDBPreDiscardCount + testDBAfterDiscardCount
var errs = make(chan error, allTestDbCount)
var wg sync.WaitGroup
if err := c.SetupTemplateWithDBClient(ctx, hash, func(db *sql.DB) error {
// setup code
if err := populateTemplateDB(ctx, db); err != nil {
t.Fatalf("failed to populate template db: %v", err)
}
// some other participents are already connecting...
wg.Add(testDBWhileInitializeCount)
for i := 0; i < testDBWhileInitializeCount; i++ {
go getTestDB(&wg, errs, c, hash)
}
// but then we throw an error during our test setup!
err = errors.New("FAILED ERR DURING INITIALIZE")
return err
}); err == nil {
t.Fatalf("we expected this to error!!")
}
// some other participents are still want to be part of this...
wg.Add(testDBPreDiscardCount)
for i := 0; i < testDBPreDiscardCount; i++ {
go getTestDB(&wg, errs, c, hash)
}
// SIGNAL DISCARD!
err = c.DiscardTemplate(ctx, hash)
if err != nil {
t.Fatalf("failed to discard template database after error during initialize: %v", err)
}
// finalize template should now no longer work
err = c.FinalizeTemplate(ctx, hash)
if err == nil {
t.Fatalf("finalize template should not work after a successful discard!: %v", err)
}
// haven't learned other participents are still want to be part of this...
wg.Add(testDBAfterDiscardCount)
for i := 0; i < testDBAfterDiscardCount; i++ {
go getTestDB(&wg, errs, c, hash)
}
// wait for all the errors with getTestDB to arrive...
wg.Wait()
var results = make([]error, 0, allTestDbCount)
for i := 0; i < allTestDbCount; i++ {
results = append(results, <-errs)
}
close(errs)
// check all getTestDatabase clients also errored out...
success := 0
errored := 0
for _, err := range results {
if err == nil {
success++
} else {
// fmt.Println(err)
errored++
}
}
if errored != allTestDbCount {
t.Errorf("invalid number of errored retrievals, got %d, want %d", errored, allTestDbCount)
}
if success != 0 {
t.Errorf("invalid number of successful retrievals, got %d, want %d", success, 0)
}
// then test a successful reinitialize...
if err := c.SetupTemplateWithDBClient(ctx, hash, func(db *sql.DB) error {
errr := populateTemplateDB(ctx, db)
// setup code
if errr != nil {
t.Fatalf("failed to repopulate template db: %v", errr)
}
return errr
}); err != nil {
t.Fatalf("Failed to resetup template database for hash %q: %v", hash, err)
}
}