forked from GoogleCloudPlatform/compute-image-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_registry_test.go
485 lines (431 loc) · 16.3 KB
/
resource_registry_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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package daisy
import (
"fmt"
"reflect"
"sync"
"testing"
"time"
)
func TestPlaceholderResourceRegistryCleanup(t *testing.T) {
w := testWorkflow()
w.forceCleanup = true
d1 := &Resource{RealName: "d1", link: "link", NoCleanup: false}
d2 := &Resource{RealName: "d2", link: "link", NoCleanup: true}
im1 := &Resource{RealName: "im1", link: "link", NoCleanup: false}
im2 := &Resource{RealName: "im2", link: "link", NoCleanup: true}
in1 := &Resource{RealName: "in1", link: "link", NoCleanup: false}
in2 := &Resource{RealName: "in2", link: "link", NoCleanup: true}
mi1 := &Resource{RealName: "mi1", link: "link", NoCleanup: false}
mi2 := &Resource{RealName: "mi2", link: "link", NoCleanup: true}
w.disks.m = map[string]*Resource{"d1": d1, "d2": d2}
w.images.m = map[string]*Resource{"im1": im1, "im2": im2}
w.machineImages.m = map[string]*Resource{"mi1": mi1, "mi2": mi2}
w.instances.m = map[string]*Resource{"in1": in1, "in2": in2}
w.cleanup()
for _, r := range []*Resource{d1, d2, im1, im2, in1, in2, mi1, mi2} {
if r.deleted {
t.Errorf("cleanup deleted %q which was a placeholder resource", r.RealName)
}
}
}
func TestResourceRegistryNotCreatedByWorkflowCleanup(t *testing.T) {
w := testWorkflow()
w.forceCleanup = true
s := &Step{}
d1 := &Resource{RealName: "d1", link: "link", NoCleanup: false, creator: s}
d2 := &Resource{RealName: "d2", link: "link", NoCleanup: true, creator: s}
im1 := &Resource{RealName: "im1", link: "link", NoCleanup: false, creator: s}
im2 := &Resource{RealName: "im2", link: "link", NoCleanup: true, creator: s}
in1 := &Resource{RealName: "in1", link: "link", NoCleanup: false, creator: s}
in2 := &Resource{RealName: "in2", link: "link", NoCleanup: true, creator: s}
mi1 := &Resource{RealName: "mi1", link: "link", NoCleanup: false, creator: s}
mi2 := &Resource{RealName: "mi2", link: "link", NoCleanup: true, creator: s}
w.disks.m = map[string]*Resource{"d1": d1, "d2": d2}
w.images.m = map[string]*Resource{"im1": im1, "im2": im2}
w.machineImages.m = map[string]*Resource{"mi1": mi1, "mi2": mi2}
w.instances.m = map[string]*Resource{"in1": in1, "in2": in2}
w.cleanup()
for _, r := range []*Resource{d1, d2, im1, im2, in1, in2, mi1, mi2} {
if r.deleted {
t.Errorf("cleanup deleted %q which was not created in current workflow", r.RealName)
}
}
}
func TestResourceRegistryCleanup(t *testing.T) {
w := testWorkflow()
s := &Step{}
d1 := &Resource{RealName: "d1", link: "link", NoCleanup: false, creator: s, createdInWorkflow: true}
d2 := &Resource{RealName: "d2", link: "link", NoCleanup: true, creator: s, createdInWorkflow: true}
im1 := &Resource{RealName: "im1", link: "link", NoCleanup: false, creator: s, createdInWorkflow: true}
im2 := &Resource{RealName: "im2", link: "link", NoCleanup: true, creator: s, createdInWorkflow: true}
in1 := &Resource{RealName: "in1", link: "link", NoCleanup: false, creator: s, createdInWorkflow: true}
in2 := &Resource{RealName: "in2", link: "link", NoCleanup: true, creator: s, createdInWorkflow: true}
mi1 := &Resource{RealName: "mi1", link: "link", NoCleanup: false, creator: s, createdInWorkflow: true}
mi2 := &Resource{RealName: "mi2", link: "link", NoCleanup: true, creator: s, createdInWorkflow: true}
w.disks.m = map[string]*Resource{"d1": d1, "d2": d2}
w.images.m = map[string]*Resource{"im1": im1, "im2": im2}
w.machineImages.m = map[string]*Resource{"mi1": mi1, "mi2": mi2}
w.instances.m = map[string]*Resource{"in1": in1, "in2": in2}
w.cleanup()
for _, r := range []*Resource{d1, d2, im1, im2, in1, in2, mi1, mi2} {
if r.NoCleanup && r.deleted {
t.Errorf("cleanup deleted %q which was marked for NoCleanup", r.RealName)
} else if !r.NoCleanup && !r.deleted {
t.Errorf("cleanup didn't delete %q", r.RealName)
}
}
}
func TestResourceRegistryForcedCleanup(t *testing.T) {
w := testWorkflow()
w.forceCleanup = true
s := &Step{}
d1 := &Resource{RealName: "d1", link: "link", NoCleanup: false, creator: s, createdInWorkflow: true}
d2 := &Resource{RealName: "d2", link: "link", NoCleanup: true, creator: s, createdInWorkflow: true}
im1 := &Resource{RealName: "im1", link: "link", NoCleanup: false, creator: s, createdInWorkflow: true}
im2 := &Resource{RealName: "im2", link: "link", NoCleanup: true, creator: s, createdInWorkflow: true}
in1 := &Resource{RealName: "in1", link: "link", NoCleanup: false, creator: s, createdInWorkflow: true}
in2 := &Resource{RealName: "in2", link: "link", NoCleanup: true, creator: s, createdInWorkflow: true}
mi1 := &Resource{RealName: "mi1", link: "link", NoCleanup: false, creator: s, createdInWorkflow: true}
mi2 := &Resource{RealName: "mi2", link: "link", NoCleanup: true, creator: s, createdInWorkflow: true}
w.disks.m = map[string]*Resource{"d1": d1, "d2": d2}
w.images.m = map[string]*Resource{"im1": im1, "im2": im2}
w.machineImages.m = map[string]*Resource{"mi1": mi1, "mi2": mi2}
w.instances.m = map[string]*Resource{"in1": in1, "in2": in2}
w.cleanup()
for _, r := range []*Resource{d1, d2, im1, im2, in1, in2, mi1, mi2} {
if !r.deleted {
t.Errorf("cleanup didn't delete %q", r.RealName)
}
}
}
func TestResourceRegistryConcurrency(t *testing.T) {
rr := baseResourceRegistry{w: testWorkflow()}
rr.init()
tests := []struct {
desc string
f func()
}{
{"regCreate", func() { rr.regCreate("foo", &Resource{}, nil, false) }},
{"regDelete", func() { rr.regDelete("foo", nil) }},
{"regUse", func() { rr.regUse("foo", nil) }},
{"get", func() { rr.get("foo") }},
{"delete", func() { rr.get("foo") }},
}
for _, tt := range tests {
order := []string{}
releaseStr := "lock released"
returnedStr := "func returned"
want := []string{releaseStr, returnedStr}
gunshot := sync.Mutex{}
gunshot.Lock() // Wait for the goroutine to say we can go ahead.
go func() {
rr.mx.Lock()
defer rr.mx.Unlock()
gunshot.Unlock()
time.Sleep(1 * time.Millisecond)
order = append(order, releaseStr)
}()
gunshot.Lock() // Wait for the go ahead.
tt.f()
order = append(order, returnedStr)
if !reflect.DeepEqual(order, want) {
t.Errorf("%q case failed, unexpected concurrency order, want: %v; got: %v", tt.desc, want, order)
}
}
}
func TestResourceRegistryDelete(t *testing.T) {
var deleteFnErr DError
r := &baseResourceRegistry{m: map[string]*Resource{}}
r.deleteFn = func(r *Resource) DError {
return deleteFnErr
}
r.m["foo"] = &Resource{}
r.m["baz"] = &Resource{}
tests := []struct {
desc, input string
deleteFnErr DError
shouldErr bool
}{
{"good case", "foo", nil, false},
{"bad redelete case", "foo", nil, true},
{"bad resource dne case", "bar", nil, true},
{"bad deleteFn error case", "baz", Errf("error"), true},
}
for _, tt := range tests {
deleteFnErr = tt.deleteFnErr
err := r.delete(tt.input)
if tt.shouldErr && err == nil {
t.Errorf("%s: should have erred but didn't", tt.desc)
} else if !tt.shouldErr && err != nil {
t.Errorf("%s: unexpected error: %v", tt.desc, err)
}
}
wantM := map[string]*Resource{
"foo": {deleted: true, deleteMx: r.m["foo"].deleteMx},
"baz": {deleted: false, deleteMx: r.m["baz"].deleteMx},
}
if diffRes := diff(r.m, wantM, 0); diffRes != "" {
t.Errorf("resourceMap not modified as expected: (-got,+want)\n%s", diffRes)
}
}
func TestResourceRegistryStart(t *testing.T) {
var startFnErr DError
var stopFnErr DError
r := &baseResourceRegistry{m: map[string]*Resource{}}
r.startFn = func(r *Resource) DError {
return startFnErr
}
r.stopFn = func(r *Resource) DError {
return stopFnErr
}
r.m["foo"] = &Resource{}
r.m["baz"] = &Resource{}
r.m["stopped"] = &Resource{}
r.stop("stopped")
r.m["keep_stopped"] = &Resource{}
r.stop("keep_stopped")
tests := []struct {
desc, input string
startFnErr DError
shouldErr bool
}{
{"stopped case", "stopped", nil, false},
{"neither stopped nor started case", "foo", nil, false},
{"bad resource dne case", "bar", nil, true},
{"checks startedByWf state on creation", "baz", Errf("error"), true},
{"bad startFn error case", "keep_stopped", Errf("error"), true},
}
for _, tt := range tests {
startFnErr = tt.startFnErr
err := r.start(tt.input)
if tt.shouldErr && err == nil {
t.Errorf("%s: should have erred but didn't", tt.desc)
} else if !tt.shouldErr && err != nil {
t.Errorf("%s: unexpected error: %v", tt.desc, err)
}
}
wantM := map[string]*Resource{
"foo": {startedByWf: true, stoppedByWf: false},
"baz": {startedByWf: false, stoppedByWf: false},
"stopped": {startedByWf: true, stoppedByWf: false},
"keep_stopped": {startedByWf: false, stoppedByWf: true},
}
if diffRes := diff(r.m, wantM, 0); diffRes != "" {
t.Errorf("resourceMap not modified as expected: (-got,+want)\n%s", diffRes)
}
}
func TestResourceRegistryStop(t *testing.T) {
var stopFnErr DError
r := &baseResourceRegistry{m: map[string]*Resource{}}
r.stopFn = func(r *Resource) DError {
return stopFnErr
}
r.m["foo"] = &Resource{}
r.m["baz"] = &Resource{}
tests := []struct {
desc, input string
stopFnErr DError
shouldErr bool
}{
{"good case", "foo", nil, false},
{"bad restop case", "foo", nil, true},
{"bad resource dne case", "bar", nil, true},
{"bad stopFn error case", "baz", Errf("error"), true},
}
for _, tt := range tests {
stopFnErr = tt.stopFnErr
err := r.stop(tt.input)
if tt.shouldErr && err == nil {
t.Errorf("%s: should have erred but didn't", tt.desc)
} else if !tt.shouldErr && err != nil {
t.Errorf("%s: unexpected error: %v", tt.desc, err)
}
}
wantM := map[string]*Resource{
"foo": {stoppedByWf: true},
"baz": {stoppedByWf: false},
}
if diffRes := diff(r.m, wantM, 0); diffRes != "" {
t.Errorf("resourceMap not modified as expected: (-got,+want)\n%s", diffRes)
}
}
func TestResourceRegistryGet(t *testing.T) {
xRes := &Resource{}
yRes := &Resource{}
rr := baseResourceRegistry{m: map[string]*Resource{"x": xRes, "y": yRes}}
tests := []struct {
desc, input string
wantR *Resource
wantOk bool
}{
{"normal get", "y", yRes, true},
{"get dne", "dne", nil, false},
}
for _, tt := range tests {
if gotR, gotOk := rr.get(tt.input); !(gotOk == tt.wantOk && gotR == tt.wantR) {
t.Errorf("%q case failed, want: (%v, %t); got: (%v, %t)", tt.desc, tt.wantR, tt.wantOk, gotR, gotOk)
}
}
}
func TestResourceRegistryRegCreate(t *testing.T) {
rr := &baseResourceRegistry{w: testWorkflow()}
rr.init()
r := &Resource{link: "projects/foo/global/images/bar"}
s := &Step{}
// Normal create.
if err := rr.regCreate("foo", r, s, false); err != nil {
t.Fatalf("unexpected error registering creation of foo: %v", err)
}
if r.creator != s {
t.Error("foo does not have the correct creator")
}
if diffRes := diff(rr.m, map[string]*Resource{"foo": r}, 0); diffRes != "" {
t.Errorf("resource registry does not match expectation: (-got +want)\n%s", diffRes)
}
// Test duplication create.
if err := rr.regCreate("foo", r, nil, false); err == nil {
t.Error("should have returned an error, but didn't")
}
// Test overwrite create should not error on dupe.
if err := rr.regCreate("foo", r, nil, true); err == nil {
t.Fatalf("unexpected error registering creation of foo: %v", err)
}
}
func TestResourceRegistryRegDelete(t *testing.T) {
w := testWorkflow()
creator := &Step{name: "creator", w: w}
user := &Step{name: "user", w: w}
deleter := &Step{name: "deleter", w: w}
badDeleter := &Step{name: "badDeleter", w: w}
badDeleter2 := &Step{name: "badDeleter2", w: w}
dupeDeleter := &Step{name: "dupeDeleter", w: w}
w.Steps = map[string]*Step{"creator": creator, "user": user, "deleter": deleter, "badDeleter": badDeleter, "badDeleter2": badDeleter2, "dupeDeleter": dupeDeleter}
w.Dependencies = map[string][]string{
"user": {"creator"},
"deleter": {"user"},
"dupeDeleter": {"user"},
"badDeleter2": {"creator"},
}
r := &Resource{creator: creator, users: []*Step{user}}
rr := &baseResourceRegistry{m: map[string]*Resource{"r": r}}
tests := []struct {
desc string
name string
step *Step
wantErr bool
}{
{"missing dependency on creator case", "r", badDeleter, true},
{"missing reference case", "bar", badDeleter, true},
{"missing dependency on user case", "r", badDeleter2, true},
{"normal case", "r", deleter, false},
{"dupe delete case", "r", dupeDeleter, true},
}
for _, tt := range tests {
err := rr.regDelete(tt.name, tt.step)
if tt.wantErr {
if err == nil {
t.Errorf("%s: did not return an error as expected", tt.desc)
}
} else if err != nil {
t.Errorf("%s: unexepected error: %v", tt.desc, err)
}
}
}
func TestResourceRegistryRegURL(t *testing.T) {
rr := &baseResourceRegistry{w: testWorkflow()}
rr.init()
defURL := fmt.Sprintf("projects/%s/zones/%s/disks/%s", testProject, testZone, testDisk)
tests := []struct {
desc, url string
wantR *Resource
shouldErr bool
}{
{"normal case", defURL, &Resource{RealName: testDisk, link: defURL, NoCleanup: true}, false},
{"dupe case", defURL, &Resource{RealName: testDisk, link: defURL, NoCleanup: true}, false},
{"incomplete partial URL case", "zones/z/disks/bad", nil, true},
{"already exists", fmt.Sprintf("projects/%s/global/images/my-image", testProject), nil, true},
}
for _, tt := range tests {
r, err := rr.regURL(tt.url, true)
if !tt.shouldErr {
if err != nil {
t.Errorf("%s: unexpected error: %v", tt.desc, err)
} else if diffRes := diff(r, tt.wantR, 0); diffRes != "" {
t.Errorf("%s: generated resource doesn't match expectation (-got +want)\n%s", tt.desc, diffRes)
} else if rr.m[tt.url] != r {
t.Errorf("%s: resource was not added to the resource map", tt.desc)
}
} else if err == nil {
t.Errorf("%s: should have returned an error, but didn't", tt.desc)
}
}
if diffRes := diff(rr.m, map[string]*Resource{defURL: {RealName: testDisk, link: defURL, NoCleanup: true}}, 0); diffRes != "" {
t.Errorf("resource registry doesn't match expectation (-got +want)\n%s", diffRes)
}
}
func TestResourceRegistryRegUse(t *testing.T) {
w := testWorkflow()
creator := &Step{name: "creator", w: w}
deleter := &Step{name: "deleter", w: w}
user := &Step{name: "user", w: w}
badUser := &Step{name: "badUser", w: w} // Doesn't depend on creator.
badUser2 := &Step{name: "badUser2", w: w} // Tries to use a resource marked for deletion.
badUser3 := &Step{name: "badUser3", w: w} // Fails on the resource type's use hook.
w.Steps = map[string]*Step{"creator": creator, "user": user, "badUser": badUser, "badUser2": badUser2, "badUser3": badUser3, "deleter": deleter}
w.Dependencies = map[string][]string{
"user": {"creator"},
"badUser2": {"creator"},
"badUser3": {"creator"},
"deleter": {"user", "badUser3"},
}
r1 := &Resource{creator: creator}
r2 := &Resource{creator: creator, deleter: deleter}
rr := &baseResourceRegistry{m: map[string]*Resource{"r1": r1, "r2": r2}}
tests := []struct {
desc string
name string
step *Step
wantErr bool
wantRes *Resource
}{
{"normal case", "r1", user, false, r1},
{"missing dependency on creator case", "r1", badUser, true, nil},
{"use deleted case", "r2", badUser2, true, nil},
}
for _, tt := range tests {
r, err := rr.regUse(tt.name, tt.step)
if tt.wantErr {
if err == nil {
t.Errorf("%s: did not return an error as expected", tt.desc)
}
} else if err != nil {
t.Errorf("%s: unexpected error: %v", tt.desc, err)
} else if r != tt.wantRes {
t.Errorf("%s: unexpected resource returned: want: %v, got: %v", tt.desc, tt.wantRes, r)
}
}
// Clear s.Workflow -- unimportant and causes pretty.Compare stack overflow otherwise.
ss := []*Step{creator, deleter, user, badUser, badUser2, badUser3}
for _, s := range ss {
s.w = nil
}
if diffRes := diff(r1.users, []*Step{user}, 0); diffRes != "" {
t.Errorf("r1 users list does not match expectation: (-got +want)\n%s", diffRes)
}
if diffRes := diff(r2.users, []*Step(nil), 0); diffRes != "" {
t.Errorf("r2 users list does not match expectation: (-got +want)\n%s", diffRes)
}
}