-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathfake-gce.go
671 lines (588 loc) · 22.2 KB
/
fake-gce.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
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 gcecloudprovider
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
csi "github.com/container-storage-interface/spec/lib/go/csi"
computev1 "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
DiskSizeGb = 10
Timestamp = "2018-09-05T15:17:08.270-07:00"
BasePath = "https://www.googleapis.com/compute/v1/"
snapshotURITemplateGlobal = "projects/%s/global/snapshots/%s" //{gce.projectID}/global/snapshots/{snapshot.Name}"
imageURITemplateGlobal = "projects/%s/global/images/%s" //{gce.projectID}/global/images/{image.Name}"
)
var (
// Snaphsot and Image Regex must comply with RFC1035
rfc1035Regex = regexp.MustCompile("^[a-z]([-a-z0-9]*[a-z0-9])?$")
)
func isRFC1035(value string) bool {
return rfc1035Regex.MatchString(strings.ToLower(value))
}
type FakeCloudProvider struct {
project string
zone string
disks map[string]*CloudDisk
pageTokens map[string]sets.String
instances map[string]*computev1.Instance
snapshots map[string]*computev1.Snapshot
images map[string]*computev1.Image
// marker to set disk status during InsertDisk operation.
mockDiskStatus string
}
var _ GCECompute = &FakeCloudProvider{}
func CreateFakeCloudProvider(project, zone string, cloudDisks []*CloudDisk) (*FakeCloudProvider, error) {
fcp := &FakeCloudProvider{
project: project,
zone: zone,
disks: map[string]*CloudDisk{},
instances: map[string]*computev1.Instance{},
snapshots: map[string]*computev1.Snapshot{},
images: map[string]*computev1.Image{},
pageTokens: map[string]sets.String{},
// A newly created disk is marked READY by default.
mockDiskStatus: "READY",
}
for _, d := range cloudDisks {
diskZone := d.GetZone()
if diskZone == "" {
diskZone = zone
}
fcp.disks[meta.ZonalKey(d.GetName(), diskZone).String()] = d
}
return fcp, nil
}
func (cloud *FakeCloudProvider) GetDefaultProject() string {
return cloud.project
}
func (cloud *FakeCloudProvider) GetDefaultZone() string {
return cloud.zone
}
func (cloud *FakeCloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, project string, volumeKey *meta.Key) (string, *meta.Key, error) {
if project == common.UnspecifiedValue {
project = cloud.project
}
switch volumeKey.Type() {
case meta.Zonal:
if volumeKey.Zone != common.UnspecifiedValue {
return project, volumeKey, nil
}
for diskVolKey, d := range cloud.disks {
if diskVolKey == volumeKey.String() {
volumeKey.Zone = d.GetZone()
return project, volumeKey, nil
}
}
return "", nil, notFoundError()
case meta.Regional:
if volumeKey.Region != common.UnspecifiedValue {
return project, volumeKey, nil
}
r, err := common.GetRegionFromZones([]string{cloud.zone})
if err != nil {
return "", nil, fmt.Errorf("failed to get region from zones: %w", err)
}
volumeKey.Region = r
return project, volumeKey, nil
default:
return "", nil, fmt.Errorf("Volume key %v not zonal nor regional", volumeKey.Name)
}
}
func (cloud *FakeCloudProvider) ListZones(ctx context.Context, region string) ([]string, error) {
return []string{cloud.zone, "country-region-fakesecondzone"}, nil
}
func (cloud *FakeCloudProvider) ListCompatibleDiskTypeZones(ctx context.Context, project string, zones []string, diskType string) ([]string, error) {
// Assume all zones are compatible
return zones, nil
}
func (cloud *FakeCloudProvider) ListDisksWithFilter(ctx context.Context, fields []googleapi.Field, filter string) ([]*computev1.Disk, string, error) {
return cloud.ListDisks(ctx, fields)
}
func (cloud *FakeCloudProvider) ListDisks(ctx context.Context, fields []googleapi.Field) ([]*computev1.Disk, string, error) {
d := []*computev1.Disk{}
for _, cd := range cloud.disks {
if cd.disk != nil {
d = append(d, cd.disk)
} else if cd.betaDisk != nil {
betaDisk := convertBetaDiskToV1Disk(cd.betaDisk)
d = append(d, betaDisk)
}
}
return d, "", nil
}
func (cloud *FakeCloudProvider) ListInstances(ctx context.Context, fields []googleapi.Field) ([]*computev1.Instance, string, error) {
instances := []*computev1.Instance{}
for _, instance := range cloud.instances {
instances = append(instances, instance)
}
return instances, "", nil
}
func (cloud *FakeCloudProvider) ListSnapshots(ctx context.Context, filter string) ([]*computev1.Snapshot, string, error) {
var sourceDisk string
snapshots := []*computev1.Snapshot{}
if len(filter) > 0 {
filterSplits := strings.Fields(filter)
if len(filterSplits) != 3 || filterSplits[0] != "sourceDisk" {
return nil, "", invalidError()
}
sourceDisk = filterSplits[2]
}
for _, snapshot := range cloud.snapshots {
if len(sourceDisk) > 0 {
if snapshot.SourceDisk == sourceDisk {
continue
}
}
snapshots = append(snapshots, snapshot)
}
return snapshots, "", nil
}
// Disk Methods
func (cloud *FakeCloudProvider) GetDisk(ctx context.Context, project string, volKey *meta.Key, api GCEAPIVersion) (*CloudDisk, error) {
disk, ok := cloud.disks[volKey.String()]
if !ok {
return nil, notFoundError()
}
return disk, nil
}
func (cloud *FakeCloudProvider) ValidateExistingDisk(ctx context.Context, resp *CloudDisk, params common.DiskParameters, reqBytes, limBytes int64, multiWriter bool) error {
if resp == nil {
return fmt.Errorf("disk does not exist")
}
requestValid := common.GbToBytes(resp.GetSizeGb()) >= reqBytes || reqBytes == 0
responseValid := common.GbToBytes(resp.GetSizeGb()) <= limBytes || limBytes == 0
if !requestValid || !responseValid {
return fmt.Errorf(
"disk already exists with incompatible capacity. Need %v (Required) < %v (Existing) < %v (Limit)",
reqBytes, common.GbToBytes(resp.GetSizeGb()), limBytes)
}
respType := strings.Split(resp.GetPDType(), "/")
typeMatch := strings.TrimSpace(respType[len(respType)-1]) == strings.TrimSpace(params.DiskType)
typeDefault := params.DiskType == "" && strings.TrimSpace(respType[len(respType)-1]) == "pd-standard"
if !typeMatch && !typeDefault {
return fmt.Errorf("disk already exists with incompatible type. Need %v. Got %v",
params.DiskType, respType[len(respType)-1])
}
// We are assuming here that a multiWriter disk could be used as non-multiWriter
if multiWriter && !resp.GetMultiWriter() {
return fmt.Errorf("disk already exists with incompatible capability. Need MultiWriter. Got non-MultiWriter")
}
klog.V(4).Infof("Compatible disk already exists")
return ValidateDiskParameters(resp, params)
}
func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode string) error {
if disk, ok := cloud.disks[volKey.String()]; ok {
err := cloud.ValidateExistingDisk(ctx, disk, params,
int64(capacityRange.GetRequiredBytes()),
int64(capacityRange.GetLimitBytes()),
multiWriter)
if err != nil {
return err
}
}
computeDisk := &computev1.Disk{
Name: volKey.Name,
SizeGb: common.BytesToGbRoundUp(capBytes),
Description: "Disk created by GCE-PD CSI Driver",
Type: cloud.GetDiskTypeURI(project, volKey, params.DiskType),
SourceDiskId: volumeContentSourceVolumeID,
Status: cloud.mockDiskStatus,
Labels: params.Labels,
ProvisionedIops: params.ProvisionedIOPSOnCreate,
ProvisionedThroughput: params.ProvisionedThroughputOnCreate,
}
if snapshotID != "" {
_, snapshotType, _, err := common.SnapshotIDToProjectKey(snapshotID)
if err != nil {
return err
}
switch snapshotType {
case common.DiskSnapshotType:
computeDisk.SourceSnapshotId = snapshotID
case common.DiskImageType:
computeDisk.SourceImageId = snapshotID
default:
return fmt.Errorf("invalid snapshot type in snapshot ID: %s", snapshotType)
}
}
if params.DiskEncryptionKMSKey != "" {
computeDisk.DiskEncryptionKey = &computev1.CustomerEncryptionKey{
KmsKeyName: params.DiskEncryptionKMSKey,
}
}
switch volKey.Type() {
case meta.Zonal:
computeDisk.Zone = volKey.Zone
computeDisk.SelfLink = fmt.Sprintf("%sprojects/%s/zones/%s/disks/%s", BasePath, project, volKey.Zone, volKey.Name)
case meta.Regional:
computeDisk.Region = volKey.Region
computeDisk.SelfLink = fmt.Sprintf("%sprojects/%s/regions/%s/disks/%s", BasePath, project, volKey.Region, volKey.Name)
default:
return fmt.Errorf("could not create disk, key was neither zonal nor regional, instead got: %v", volKey.String())
}
if containsBetaDiskType(hyperdiskTypes, params.DiskType) {
betaDisk := convertV1DiskToBetaDisk(computeDisk)
betaDisk.EnableConfidentialCompute = params.EnableConfidentialCompute
cloud.disks[volKey.String()] = CloudDiskFromBeta(betaDisk)
} else {
cloud.disks[volKey.String()] = CloudDiskFromV1(computeDisk)
}
return nil
}
func (cloud *FakeCloudProvider) UpdateDisk(ctx context.Context, project string, volKey *meta.Key, existingDisk *CloudDisk, params common.ModifyVolumeParameters) error {
_, ok := cloud.disks[volKey.String()]
if !ok {
return notFoundError()
}
specifiedIops := params.IOPS != nil && *params.IOPS != 0
specifiedThroughput := params.Throughput != nil && *params.Throughput != 0
if !specifiedIops && !specifiedThroughput {
return fmt.Errorf("no IOPS or Throughput specified for disk %v", existingDisk.GetSelfLink())
}
if params.IOPS != nil {
if existingDisk.betaDisk != nil {
existingDisk.betaDisk.ProvisionedIops = *params.IOPS
} else if existingDisk.disk != nil {
existingDisk.disk.ProvisionedIops = *params.IOPS
}
}
if params.Throughput != nil {
if existingDisk.betaDisk != nil {
existingDisk.betaDisk.ProvisionedThroughput = *params.Throughput
} else if existingDisk.disk != nil {
existingDisk.disk.ProvisionedThroughput = *params.Throughput
}
}
cloud.disks[volKey.String()] = existingDisk
return nil
}
func (cloud *FakeCloudProvider) DeleteDisk(ctx context.Context, project string, volKey *meta.Key) error {
delete(cloud.disks, volKey.String())
return nil
}
func (cloud *FakeCloudProvider) AttachDisk(ctx context.Context, project string, volKey *meta.Key, readWrite, diskType, instanceZone, instanceName string, forceAttach bool) error {
source := cloud.GetDiskSourceURI(project, volKey)
attachedDiskV1 := &computev1.AttachedDisk{
DeviceName: volKey.Name,
Kind: diskKind,
Mode: readWrite,
Source: source,
Type: diskType,
ForceAttach: forceAttach,
}
instance, ok := cloud.instances[instanceName]
if !ok {
return fmt.Errorf("Failed to get instance %v", instanceName)
}
instance.Disks = append(instance.Disks, attachedDiskV1)
return nil
}
func (cloud *FakeCloudProvider) DetachDisk(ctx context.Context, project, deviceName, instanceZone, instanceName string) error {
instance, ok := cloud.instances[instanceName]
if !ok {
return fmt.Errorf("Failed to get instance %v", instanceName)
}
found := -1
for i, disk := range instance.Disks {
if disk.DeviceName == deviceName {
found = i
break
}
}
instance.Disks[found] = instance.Disks[len(instance.Disks)-1]
instance.Disks = instance.Disks[:len(instance.Disks)-1]
return nil
}
func (cloud *FakeCloudProvider) SetDiskAccessMode(ctx context.Context, project string, volKey *meta.Key, accessMode string) error {
disk, ok := cloud.disks[volKey.String()]
if !ok {
return fmt.Errorf("disk %v not found", volKey)
}
if disk.disk != nil {
disk.disk.AccessMode = accessMode
}
if disk.betaDisk != nil {
disk.betaDisk.AccessMode = accessMode
}
return nil
}
func (cloud *FakeCloudProvider) GetDiskTypeURI(project string, volKey *meta.Key, diskType string) string {
switch volKey.Type() {
case meta.Zonal:
return cloud.getZonalDiskTypeURI(project, volKey.Zone, diskType)
case meta.Regional:
return cloud.getRegionalDiskTypeURI(project, volKey.Region, diskType)
default:
return fmt.Sprintf("could not get disk type uri, key was neither zonal nor regional, instead got: %v", volKey.String())
}
}
func (cloud *FakeCloudProvider) getZonalDiskTypeURI(project, zone, diskType string) string {
return fmt.Sprintf(diskTypeURITemplateSingleZone, project, zone, diskType)
}
func (cloud *FakeCloudProvider) getRegionalDiskTypeURI(project, region, diskType string) string {
return fmt.Sprintf(diskTypeURITemplateRegional, project, region, diskType)
}
func (cloud *FakeCloudProvider) WaitForAttach(ctx context.Context, project string, volKey *meta.Key, diskType, instanceZone, instanceName string) error {
return nil
}
// Regional Disk Methods
func (cloud *FakeCloudProvider) GetReplicaZoneURI(project, zone string) string {
return ""
}
// Instance Methods
func (cloud *FakeCloudProvider) InsertInstance(instance *computev1.Instance, instanceZone, instanceName string) {
cloud.instances[instanceName] = instance
return
}
func (cloud *FakeCloudProvider) GetInstanceOrError(ctx context.Context, instanceZone, instanceName string) (*computev1.Instance, error) {
instance, ok := cloud.instances[instanceName]
if !ok {
return nil, notFoundError()
}
return instance, nil
}
// Snapshot Methods
func (cloud *FakeCloudProvider) GetSnapshot(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error) {
if !isRFC1035(snapshotName) {
return nil, fmt.Errorf("invalid snapshot name %v: %w", snapshotName, invalidError())
}
snapshot, ok := cloud.snapshots[snapshotName]
if !ok {
return nil, notFoundError()
}
snapshot.Status = "READY"
return snapshot, nil
}
func (cloud *FakeCloudProvider) CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, snapshotParams common.SnapshotParameters) (*computev1.Snapshot, error) {
if snapshot, ok := cloud.snapshots[snapshotName]; ok {
return snapshot, nil
}
snapshotToCreate := &computev1.Snapshot{
Name: snapshotName,
DiskSizeGb: int64(DiskSizeGb),
CreationTimestamp: Timestamp,
Status: "UPLOADING",
SelfLink: cloud.getGlobalSnapshotURI(project, snapshotName),
StorageLocations: snapshotParams.StorageLocations,
Labels: snapshotParams.Labels,
}
switch volKey.Type() {
case meta.Zonal:
snapshotToCreate.SourceDisk = cloud.getZonalDiskSourceURI(project, volKey.Name, volKey.Zone)
case meta.Regional:
snapshotToCreate.SourceDisk = cloud.getRegionalDiskSourceURI(project, volKey.Name, volKey.Region)
default:
return nil, fmt.Errorf("could not create snapshot, disk key was neither zonal nor regional, instead got: %v", volKey.String())
}
cloud.snapshots[snapshotName] = snapshotToCreate
return snapshotToCreate, nil
}
func (cloud *FakeCloudProvider) ResizeDisk(ctx context.Context, project string, volKey *meta.Key, requestBytes int64) (int64, error) {
disk, ok := cloud.disks[volKey.String()]
if !ok {
return -1, notFoundError()
}
requestSizGb := common.BytesToGbRoundUp(requestBytes)
disk.setSizeGb(requestSizGb)
return requestSizGb, nil
}
// Snapshot Methods
func (cloud *FakeCloudProvider) DeleteSnapshot(ctx context.Context, project, snapshotName string) error {
delete(cloud.snapshots, snapshotName)
return nil
}
func (cloud *FakeCloudProvider) ListImages(ctx context.Context, filter string) ([]*computev1.Image, string, error) {
var sourceDisk string
images := []*computev1.Image{}
if len(filter) > 0 {
filterSplits := strings.Fields(filter)
if len(filterSplits) != 3 || filterSplits[0] != "sourceDisk" {
return nil, "", invalidError()
}
sourceDisk = filterSplits[2]
}
for _, image := range cloud.images {
if len(sourceDisk) > 0 {
if image.SourceDisk == sourceDisk {
continue
}
}
images = append(images, image)
}
return images, "", nil
}
func (cloud *FakeCloudProvider) GetImage(ctx context.Context, project, imageName string) (*computev1.Image, error) {
if !isRFC1035(imageName) {
return nil, fmt.Errorf("invalid image name %v: %w", imageName, invalidError())
}
image, ok := cloud.images[imageName]
if !ok {
return nil, notFoundError()
}
image.Status = "READY"
return image, nil
}
func (cloud *FakeCloudProvider) CreateImage(ctx context.Context, project string, volKey *meta.Key, imageName string, snapshotParams common.SnapshotParameters) (*computev1.Image, error) {
if image, ok := cloud.images[imageName]; ok {
return image, nil
}
imageToCreate := &computev1.Image{
CreationTimestamp: Timestamp,
DiskSizeGb: int64(DiskSizeGb),
Family: snapshotParams.ImageFamily,
Name: imageName,
SelfLink: cloud.getGlobalImageURI(project, imageName),
SourceType: "RAW",
Status: "PENDING",
StorageLocations: snapshotParams.StorageLocations,
Labels: snapshotParams.Labels,
}
switch volKey.Type() {
case meta.Zonal:
imageToCreate.SourceDisk = cloud.getZonalDiskSourceURI(project, volKey.Name, volKey.Zone)
case meta.Regional:
imageToCreate.SourceDisk = cloud.getRegionalDiskSourceURI(project, volKey.Name, volKey.Region)
default:
return nil, fmt.Errorf("could not create image, disk key was neither zonal nor regional, instead got: %v", volKey.String())
}
cloud.images[imageName] = imageToCreate
return imageToCreate, nil
}
func (cloud *FakeCloudProvider) DeleteImage(ctx context.Context, project, imageName string) error {
delete(cloud.images, imageName)
return nil
}
func (cloud *FakeCloudProvider) ValidateExistingSnapshot(resp *computev1.Snapshot, volKey *meta.Key) error {
if resp == nil {
return fmt.Errorf("disk does not exist")
}
diskSource := cloud.GetDiskSourceURI(cloud.project, volKey)
if resp.SourceDisk != diskSource {
return status.Error(codes.AlreadyExists, fmt.Sprintf("snapshot already exists with same name but with a different disk source %s, expected disk source %s", diskSource, resp.SourceDisk))
}
// Snapshot exists with matching source disk.
klog.V(4).Infof("Compatible snapshot already exists. Reusing existing.")
return nil
}
func (cloud *FakeCloudProvider) GetDiskSourceURI(project string, volKey *meta.Key) string {
switch volKey.Type() {
case meta.Zonal:
return cloud.getZonalDiskSourceURI(project, volKey.Name, volKey.Zone)
case meta.Regional:
return cloud.getRegionalDiskSourceURI(project, volKey.Name, volKey.Region)
default:
return ""
}
}
func (cloud *FakeCloudProvider) getZonalDiskSourceURI(project, diskName, zone string) string {
return BasePath + fmt.Sprintf(
diskSourceURITemplateSingleZone,
project,
zone,
diskName)
}
func (cloud *FakeCloudProvider) getRegionalDiskSourceURI(project, diskName, region string) string {
return BasePath + fmt.Sprintf(
diskSourceURITemplateRegional,
project,
region,
diskName)
}
func (cloud *FakeCloudProvider) getGlobalSnapshotURI(project, snapshotName string) string {
return BasePath + fmt.Sprintf(
snapshotURITemplateGlobal,
project,
snapshotName)
}
func (cloud *FakeCloudProvider) getGlobalImageURI(project, imageName string) string {
return BasePath + fmt.Sprintf(
imageURITemplateGlobal,
project,
imageName)
}
func (cloud *FakeCloudProvider) UpdateDiskStatus(s string) {
cloud.mockDiskStatus = s
}
type FakeBlockingCloudProvider struct {
*FakeCloudProvider
ReadyToExecute chan chan Signal
}
// FakeBlockingCloudProvider's method adds functionality to finely control the order of execution of CreateSnapshot calls.
// Upon starting a CreateSnapshot, it passes a chan 'executeCreateSnapshot' into readyToExecute, then blocks on executeCreateSnapshot.
// The test calling this function can block on readyToExecute to ensure that the operation has started and
// allowed the CreateSnapshot to continue by passing a struct into executeCreateSnapshot.
func (cloud *FakeBlockingCloudProvider) CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, snapshotParams common.SnapshotParameters) (*computev1.Snapshot, error) {
executeCreateSnapshot := make(chan Signal)
cloud.ReadyToExecute <- executeCreateSnapshot
<-executeCreateSnapshot
return cloud.FakeCloudProvider.CreateSnapshot(ctx, project, volKey, snapshotName, snapshotParams)
}
func (cloud *FakeBlockingCloudProvider) CreateImage(ctx context.Context, project string, volKey *meta.Key, imageName string, snapshotParams common.SnapshotParameters) (*computev1.Image, error) {
executeCreateSnapshot := make(chan Signal)
cloud.ReadyToExecute <- executeCreateSnapshot
<-executeCreateSnapshot
return cloud.FakeCloudProvider.CreateImage(ctx, project, volKey, imageName, snapshotParams)
}
func (cloud *FakeBlockingCloudProvider) DetachDisk(ctx context.Context, project, deviceName, instanceZone, instanceName string) error {
execute := make(chan Signal)
cloud.ReadyToExecute <- execute
val := <-execute
if val.ReportError {
return fmt.Errorf("force mock error for DetachDisk device %s", deviceName)
}
return cloud.FakeCloudProvider.DetachDisk(ctx, project, deviceName, instanceZone, instanceName)
}
func (cloud *FakeBlockingCloudProvider) AttachDisk(ctx context.Context, project string, volKey *meta.Key, readWrite, diskType, instanceZone, instanceName string, forceAttach bool) error {
execute := make(chan Signal)
cloud.ReadyToExecute <- execute
val := <-execute
if val.ReportError {
return fmt.Errorf("force mock error for AttachDisk: volkey %s", volKey)
}
return cloud.FakeCloudProvider.AttachDisk(ctx, project, volKey, readWrite, diskType, instanceZone, instanceName, forceAttach)
}
func notFoundError() *googleapi.Error {
return &googleapi.Error{
Errors: []googleapi.ErrorItem{
{
Reason: "notFound",
},
},
}
}
func invalidError() *googleapi.Error {
return &googleapi.Error{
Code: http.StatusBadRequest,
Errors: []googleapi.ErrorItem{
{
Reason: "invalid",
},
},
}
}
type Signal struct {
ReportError bool
ReportRunning bool
}