-
Notifications
You must be signed in to change notification settings - Fork 73
/
fake.go
512 lines (450 loc) · 15.7 KB
/
fake.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
Copyright 2018 The Kubernetes Authors.
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 file
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
filev1beta1 "google.golang.org/api/file/v1beta1"
filev1beta1multishare "google.golang.org/api/file/v1beta1"
"google.golang.org/api/googleapi"
"google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/grpc/codes"
)
const (
defaultProject = "test-project"
defaultZone = "us-central1-c"
defaultRegion = "us-central1"
defaultTier = "BASIC_HDD"
defaultCapacityGb = 1024
defaultNetwork = "default"
)
type fakeServiceManager struct {
createdInstances map[string]*ServiceInstance
backups map[string]*Backup
createdMultishareInstance map[string]*MultishareInstance
createdMultishares map[string]*Share
multishareops []*filev1beta1multishare.Operation
}
var _ Service = &fakeServiceManager{}
func NewFakeService() (Service, error) {
return &fakeServiceManager{
createdInstances: map[string]*ServiceInstance{},
backups: map[string]*Backup{},
createdMultishareInstance: make(map[string]*MultishareInstance),
createdMultishares: make(map[string]*Share),
}, nil
}
func NewFakeServiceForMultishare(instances []*MultishareInstance, shares []*Share, ops []*filev1beta1multishare.Operation) (Service, error) {
s := &fakeServiceManager{
createdInstances: map[string]*ServiceInstance{},
backups: map[string]*Backup{},
createdMultishareInstance: make(map[string]*MultishareInstance),
createdMultishares: make(map[string]*Share),
multishareops: make([]*filev1beta1multishare.Operation, 0),
}
for _, instance := range instances {
s.createdMultishareInstance[instance.Name] = instance
}
for _, share := range shares {
s.createdMultishares[share.Name] = share
}
s.multishareops = append(s.multishareops, ops...)
return s, nil
}
func (manager *fakeServiceManager) CreateInstance(ctx context.Context, obj *ServiceInstance) (*ServiceInstance, error) {
instance := &ServiceInstance{
Project: defaultProject,
Location: defaultZone,
Name: obj.Name,
Tier: obj.Tier,
Volume: Volume{
Name: obj.Volume.Name,
SizeBytes: obj.Volume.SizeBytes,
},
Network: Network{
Name: obj.Network.Name,
Ip: "1.1.1.1",
ReservedIpRange: obj.Network.ReservedIpRange,
},
Labels: obj.Labels,
State: "READY",
BackupSource: obj.BackupSource,
NfsExportOptions: obj.NfsExportOptions,
}
manager.createdInstances[obj.Name] = instance
return instance, nil
}
func (manager *fakeServiceManager) DeleteInstance(ctx context.Context, obj *ServiceInstance) error {
return nil
}
func (manager *fakeServiceManager) GetInstance(ctx context.Context, obj *ServiceInstance) (*ServiceInstance, error) {
instance, exists := manager.createdInstances[obj.Name]
if exists {
return instance, nil
}
return nil, &googleapi.Error{
Errors: []googleapi.ErrorItem{
{
Reason: "notFound",
},
},
}
}
func (manager *fakeServiceManager) ListInstances(ctx context.Context, obj *ServiceInstance) ([]*ServiceInstance, error) {
instances := []*ServiceInstance{
{
Project: defaultProject,
Location: defaultZone,
Name: "test",
Tier: defaultTier,
Network: Network{
ReservedIpRange: "192.168.92.32/29",
Name: defaultNetwork,
},
State: "READY",
},
{
Project: defaultProject,
Location: defaultZone,
Name: "test",
Tier: defaultTier,
Network: Network{
ReservedIpRange: "192.168.92.40/29",
Name: defaultNetwork,
},
State: "READY",
},
}
return instances, nil
}
func (manager *fakeServiceManager) ResizeInstance(ctx context.Context, obj *ServiceInstance) (*ServiceInstance, error) {
instance, ok := manager.createdInstances[obj.Name]
if !ok {
return nil, fmt.Errorf("Instance %v not found", obj.Name)
}
instance.Volume.SizeBytes = obj.Volume.SizeBytes
manager.createdInstances[obj.Name] = instance
return instance, nil
}
func (manager *fakeServiceManager) CreateBackup(ctx context.Context, backupInfo *BackupInfo) (*filev1beta1.Backup, error) {
if backupInfo.SourceInstanceName == "" || backupInfo.SourceShare == "" || backupInfo.SourceVolumeId == "" || backupInfo.BackupURI == "" {
return nil, fmt.Errorf("BackupInfo fields are not set %+v", backupInfo)
}
backupUri := backupInfo.BackupURI
backupSource := backupInfo.BackupSource()
if backup, ok := manager.backups[backupUri]; ok {
if backup.SourceInstance != backupSource && backup.SourceShare != backupInfo.SourceShare {
// TODO: format the right info
return nil, fmt.Errorf("Mismatch in source for existing snapshot %v", backup)
}
return backup.Backup, nil
}
backupToCreate := &filev1beta1.Backup{
Name: backupUri,
SourceFileShare: backupInfo.SourceShare,
SourceInstance: backupSource,
SourceInstanceTier: backupInfo.Tier,
CreateTime: "2020-10-02T15:01:23Z",
State: "READY",
CapacityGb: defaultCapacityGb,
Labels: backupInfo.Labels,
}
manager.backups[backupUri] = &Backup{
Backup: backupToCreate,
SourceInstance: backupSource,
SourceShare: backupInfo.SourceShare,
}
return backupToCreate, nil
}
func (manager *fakeServiceManager) DeleteBackup(ctx context.Context, backupName string) error {
delete(manager.backups, backupName)
return nil
}
func (manager *fakeServiceManager) GetBackup(ctx context.Context, backupUri string) (*Backup, error) {
backupInfo, ok := manager.backups[backupUri]
if !ok || backupInfo.Backup == nil {
return nil, notFoundError()
}
return backupInfo, nil
}
func (m *fakeServiceManager) HasOperations(ctx context.Context, obj *ServiceInstance, operationType string, done bool) (bool, error) {
return false, nil
}
func notFoundError() *googleapi.Error {
return &googleapi.Error{
Errors: []googleapi.ErrorItem{
{
Reason: "notFound",
},
},
}
}
type fakeBlockingServiceManager struct {
*fakeServiceManager
// 'OperationUnblocker' channel is used to block the execution of the respective function using it. This is done by sending a channel of empty struct over 'OperationUnblocker' channel, and wait until the tester gives a go-ahead to proceed further in the execution of the function.
OperationUnblocker chan chan struct{}
MultishareUnblocker chan chan Signal
}
func NewFakeBlockingService(operationUnblocker chan chan struct{}) (Service, error) {
return &fakeBlockingServiceManager{
fakeServiceManager: &fakeServiceManager{
createdInstances: map[string]*ServiceInstance{},
backups: map[string]*Backup{},
},
OperationUnblocker: operationUnblocker,
}, nil
}
func (m *fakeBlockingServiceManager) CreateInstance(ctx context.Context, obj *ServiceInstance) (*ServiceInstance, error) {
execute := make(chan struct{})
m.OperationUnblocker <- execute
<-execute
return m.fakeServiceManager.CreateInstance(ctx, obj)
}
func (m *fakeBlockingServiceManager) DeleteInstance(ctx context.Context, obj *ServiceInstance) error {
execute := make(chan struct{})
m.OperationUnblocker <- execute
<-execute
return m.fakeServiceManager.DeleteInstance(ctx, obj)
}
func (m *fakeBlockingServiceManager) HasOperations(ctx context.Context, obj *ServiceInstance, operationType string, done bool) (bool, error) {
return false, nil
}
// Multishare fake functions defined here
func (manager *fakeServiceManager) GetMultishareInstance(ctx context.Context, obj *MultishareInstance) (*MultishareInstance, error) {
instance, ok := manager.createdMultishareInstance[obj.Name]
if !ok {
return nil, &googleapi.Error{
Code: int(code.Code_NOT_FOUND),
Errors: []googleapi.ErrorItem{
{
Reason: "notFound",
},
},
}
}
return instance, nil
}
func (manager *fakeServiceManager) ListMultishareInstances(ctx context.Context, filter *ListFilter) ([]*MultishareInstance, error) {
var ilist []*MultishareInstance
for _, v := range manager.createdMultishareInstance {
ilist = append(ilist, v)
}
return ilist, nil
}
func (manager *fakeServiceManager) StartCreateMultishareInstanceOp(ctx context.Context, obj *MultishareInstance) (*filev1beta1multishare.Operation, error) {
instance := &MultishareInstance{
Project: defaultProject,
Location: obj.Location,
Name: obj.Name,
Tier: obj.Tier,
CapacityBytes: obj.CapacityBytes,
Network: obj.Network,
KmsKeyName: obj.KmsKeyName,
Labels: obj.Labels,
State: "READY",
}
manager.createdMultishareInstance[obj.Name] = instance
meta := &filev1beta1multishare.OperationMetadata{
Target: fmt.Sprintf(instanceURIFmt, instance.Project, instance.Location, instance.Name),
Verb: "create",
}
metaBytes, _ := json.Marshal(meta)
op := &filev1beta1multishare.Operation{
Name: "operation-" + uuid.New().String(),
Metadata: metaBytes,
}
return op, nil
}
type Signal struct {
ReportError bool
ReportNotFoundError bool
ReportRunning bool
ReportOpWithErrorStatus bool
}
func (manager *fakeServiceManager) StartDeleteMultishareInstanceOp(ctx context.Context, obj *MultishareInstance) (*filev1beta1multishare.Operation, error) {
delete(manager.createdMultishareInstance, obj.Name)
meta := &filev1beta1multishare.OperationMetadata{
Target: fmt.Sprintf(instanceURIFmt, obj.Project, obj.Location, obj.Name),
Verb: "create",
}
metaBytes, _ := json.Marshal(meta)
op := &filev1beta1multishare.Operation{
Name: "operation-" + uuid.New().String(),
Metadata: metaBytes,
}
return op, nil
}
func (manager *fakeServiceManager) StartResizeMultishareInstanceOp(ctx context.Context, obj *MultishareInstance) (*filev1beta1multishare.Operation, error) {
manager.createdMultishareInstance[obj.Name].CapacityBytes = obj.CapacityBytes
meta := &filev1beta1multishare.OperationMetadata{
Target: fmt.Sprintf(instanceURIFmt, obj.Project, obj.Location, obj.Name),
Verb: "update",
}
metaBytes, _ := json.Marshal(meta)
op := &filev1beta1multishare.Operation{
Name: "operation-" + uuid.New().String(),
Metadata: metaBytes,
}
return op, nil
}
func (manager *fakeServiceManager) StartCreateShareOp(ctx context.Context, obj *Share) (*filev1beta1multishare.Operation, error) {
if _, ok := manager.createdMultishareInstance[obj.Parent.Name]; !ok {
return nil, fmt.Errorf("host instance %s not found", obj.Parent.Name)
}
parent := &MultishareInstance{
Project: obj.Parent.Project,
Location: obj.Parent.Location,
Name: obj.Parent.Name,
Tier: obj.Parent.Tier,
CapacityBytes: obj.CapacityBytes,
Network: Network{
Name: obj.Parent.Network.Name,
Ip: obj.Parent.Network.Ip,
ReservedIpRange: obj.Parent.Network.ReservedIpRange,
},
Labels: obj.Parent.Labels,
State: "READY",
}
share := &Share{
Name: obj.Name,
Parent: parent,
CapacityBytes: obj.CapacityBytes,
Labels: obj.Labels,
MountPointName: obj.Name,
BackupId: obj.BackupId,
State: "READY",
NfsExportOptions: obj.NfsExportOptions,
}
manager.createdMultishares[share.Name] = share
meta := &filev1beta1.OperationMetadata{
Target: fmt.Sprintf(shareURIFmt, share.Parent.Project, share.Parent.Location, share.Parent.Name, share.Name),
Verb: "create",
}
metaBytes, _ := json.Marshal(meta)
op := &filev1beta1multishare.Operation{
Name: "operation-" + uuid.New().String(),
Metadata: metaBytes,
}
return op, nil
}
func (manager *fakeServiceManager) StartDeleteShareOp(ctx context.Context, obj *Share) (*filev1beta1multishare.Operation, error) {
delete(manager.createdMultishares, obj.Name)
meta := &filev1beta1multishare.OperationMetadata{
Target: fmt.Sprintf(shareURIFmt, obj.Parent.Project, obj.Parent.Location, obj.Parent.Name, obj.Name),
Verb: "DELETE",
}
metaBytes, _ := json.Marshal(meta)
op := &filev1beta1multishare.Operation{
Name: "operation-" + uuid.New().String(),
Metadata: metaBytes,
}
return op, nil
}
func (manager *fakeServiceManager) StartResizeShareOp(ctx context.Context, obj *Share) (*filev1beta1multishare.Operation, error) {
manager.createdMultishares[obj.Name].CapacityBytes = obj.CapacityBytes
meta := &filev1beta1multishare.OperationMetadata{
Target: fmt.Sprintf(shareURIFmt, obj.Parent.Project, obj.Parent.Location, obj.Parent.Name, obj.Name),
Verb: "update",
}
metaBytes, _ := json.Marshal(meta)
op := &filev1beta1multishare.Operation{
Name: "operation-" + uuid.New().String(),
Metadata: metaBytes,
}
return op, nil
}
func (manager *fakeServiceManager) WaitForOpWithOpts(ctx context.Context, op string, opts PollOpts) error {
return nil
}
func (manager *fakeServiceManager) GetOp(ctx context.Context, opName string) (*filev1beta1multishare.Operation, error) {
op := &filev1beta1multishare.Operation{
Name: opName,
Done: true,
}
return op, nil
}
func (manager *fakeServiceManager) IsOpDone(*filev1beta1multishare.Operation) (bool, error) {
return true, nil
}
func (manager *fakeServiceManager) GetShare(ctx context.Context, obj *Share) (*Share, error) {
share, ok := manager.createdMultishares[obj.Name]
if !ok {
return nil, notFoundError()
}
return share, nil
}
func (manager *fakeServiceManager) ListShares(ctx context.Context, filter *ListFilter) ([]*Share, error) {
var slist []*Share
for _, v := range manager.createdMultishares {
slist = append(slist, v)
}
return slist, nil
}
func (manager *fakeServiceManager) AddMultishareOps(ops []*filev1beta1multishare.Operation) {
manager.multishareops = append(manager.multishareops, ops...)
}
func (manager *fakeServiceManager) ListOps(ctx context.Context, resource *ListFilter) ([]*filev1beta1multishare.Operation, error) {
return manager.multishareops, nil
}
func NewFakeBlockingServiceForMultishare(unblocker chan chan Signal) (Service, error) {
return &fakeBlockingServiceManager{
fakeServiceManager: &fakeServiceManager{
createdMultishareInstance: make(map[string]*MultishareInstance),
createdMultishares: make(map[string]*Share),
},
MultishareUnblocker: unblocker,
}, nil
}
func (manager *fakeBlockingServiceManager) GetMultishareInstance(ctx context.Context, instance *MultishareInstance) (*MultishareInstance, error) {
execute := make(chan Signal)
manager.MultishareUnblocker <- execute
val := <-execute
if val.ReportError {
return nil, fmt.Errorf("mock error")
}
if val.ReportNotFoundError {
return nil, notFoundError()
}
return manager.fakeServiceManager.GetMultishareInstance(ctx, instance)
}
func (manager *fakeBlockingServiceManager) GetOp(ctx context.Context, opName string) (*filev1beta1multishare.Operation, error) {
execute := make(chan Signal)
manager.MultishareUnblocker <- execute
val := <-execute
if val.ReportError {
return nil, fmt.Errorf("mock error")
}
op := &filev1beta1multishare.Operation{
Name: opName,
Done: true,
}
if val.ReportOpWithErrorStatus {
op.Error = &filev1beta1multishare.Status{Code: int64(codes.Internal)}
return op, nil
}
if val.ReportRunning {
op.Done = false
}
return op, nil
}
func (manager *fakeBlockingServiceManager) IsOpDone(*filev1beta1multishare.Operation) (bool, error) {
execute := make(chan Signal)
manager.MultishareUnblocker <- execute
val := <-execute
if val.ReportError {
return !val.ReportRunning, fmt.Errorf("mock error")
}
return !val.ReportRunning, nil
}