How to use Create method of gce Package

Best Syzkaller code snippet using gce.Create

gce_disks_test.go

Source:gce_disks_test.go Github

copy

Full Screen

...26 cloudprovider "k8s.io/cloud-provider"27)28// TODO TODO write a test for GetDiskByNameUnknownZone and make sure casting logic works29// TODO TODO verify that RegionDisks.Get does not return non-replica disks30func TestCreateDisk_Basic(t *testing.T) {31 /* Arrange */32 gceProjectID := "test-project"33 gceRegion := "fake-region"34 zonesWithNodes := []string{"zone1"}35 fakeManager := newFakeManager(gceProjectID, gceRegion)36 alphaFeatureGate := NewAlphaFeatureGate([]string{})37 gce := Cloud{38 manager: fakeManager,39 managedZones: []string{"zone1"},40 projectID: gceProjectID,41 AlphaFeatureGate: alphaFeatureGate,42 nodeZones: createNodeZones(zonesWithNodes),43 nodeInformerSynced: func() bool { return true },44 }45 diskName := "disk"46 diskType := DiskTypeSSD47 zone := "zone1"48 const sizeGb int64 = 12849 tags := make(map[string]string)50 tags["test-tag"] = "test-value"51 expectedDiskTypeURI := gceComputeAPIEndpoint + "projects/" + fmt.Sprintf(52 diskTypeURITemplateSingleZone, gceProjectID, zone, diskType)53 expectedDescription := "{\"test-tag\":\"test-value\"}"54 /* Act */55 _, err := gce.CreateDisk(diskName, diskType, zone, sizeGb, tags)56 /* Assert */57 if err != nil {58 t.Error(err)59 }60 if !fakeManager.createDiskCalled {61 t.Error("Never called GCE disk create.")62 }63 // Partial check of equality between disk description sent to GCE and parameters of method.64 diskToCreate := fakeManager.diskToCreateStable65 if diskToCreate.Name != diskName {66 t.Errorf("Expected disk name: %s; Actual: %s", diskName, diskToCreate.Name)67 }68 if diskToCreate.Type != expectedDiskTypeURI {69 t.Errorf("Expected disk type: %s; Actual: %s", expectedDiskTypeURI, diskToCreate.Type)70 }71 if diskToCreate.SizeGb != sizeGb {72 t.Errorf("Expected disk size: %d; Actual: %d", sizeGb, diskToCreate.SizeGb)73 }74 if diskToCreate.Description != expectedDescription {75 t.Errorf("Expected tag string: %s; Actual: %s", expectedDescription, diskToCreate.Description)76 }77}78func TestCreateRegionalDisk_Basic(t *testing.T) {79 /* Arrange */80 gceProjectID := "test-project"81 gceRegion := "fake-region"82 zonesWithNodes := []string{"zone1", "zone3", "zone2"}83 fakeManager := newFakeManager(gceProjectID, gceRegion)84 gce := Cloud{85 manager: fakeManager,86 managedZones: zonesWithNodes,87 projectID: gceProjectID,88 nodeZones: createNodeZones(zonesWithNodes),89 nodeInformerSynced: func() bool { return true },90 }91 diskName := "disk"92 diskType := DiskTypeSSD93 replicaZones := sets.NewString("zone1", "zone2")94 const sizeGb int64 = 12895 tags := make(map[string]string)96 tags["test-tag"] = "test-value"97 expectedDiskTypeURI := gceComputeAPIEndpoint + "projects/" + fmt.Sprintf(98 diskTypeURITemplateRegional, gceProjectID, gceRegion, diskType)99 expectedDescription := "{\"test-tag\":\"test-value\"}"100 /* Act */101 _, err := gce.CreateRegionalDisk(diskName, diskType, replicaZones, sizeGb, tags)102 /* Assert */103 if err != nil {104 t.Error(err)105 }106 if !fakeManager.createDiskCalled {107 t.Error("Never called GCE disk create.")108 }109 // Partial check of equality between disk description sent to GCE and parameters of method.110 diskToCreate := fakeManager.diskToCreateStable111 if diskToCreate.Name != diskName {112 t.Errorf("Expected disk name: %s; Actual: %s", diskName, diskToCreate.Name)113 }114 if diskToCreate.Type != expectedDiskTypeURI {115 t.Errorf("Expected disk type: %s; Actual: %s", expectedDiskTypeURI, diskToCreate.Type)116 }117 if diskToCreate.SizeGb != sizeGb {118 t.Errorf("Expected disk size: %d; Actual: %d", sizeGb, diskToCreate.SizeGb)119 }120 if diskToCreate.Description != expectedDescription {121 t.Errorf("Expected tag string: %s; Actual: %s", expectedDescription, diskToCreate.Description)122 }123}124func TestCreateDisk_DiskAlreadyExists(t *testing.T) {125 /* Arrange */126 gceProjectID := "test-project"127 gceRegion := "fake-region"128 zonesWithNodes := []string{"zone1"}129 fakeManager := newFakeManager(gceProjectID, gceRegion)130 alphaFeatureGate := NewAlphaFeatureGate([]string{})131 gce := Cloud{132 manager: fakeManager,133 managedZones: zonesWithNodes,134 AlphaFeatureGate: alphaFeatureGate,135 nodeZones: createNodeZones(zonesWithNodes),136 nodeInformerSynced: func() bool { return true },137 }138 // Inject disk AlreadyExists error.139 alreadyExistsError := googleapi.ErrorItem{Reason: "alreadyExists"}140 fakeManager.opError = &googleapi.Error{141 Errors: []googleapi.ErrorItem{alreadyExistsError},142 }143 /* Act */144 _, err := gce.CreateDisk("disk", DiskTypeSSD, "zone1", 128, nil)145 /* Assert */146 if err != nil {147 t.Error(148 "Expected success when a disk with the given name already exists, but an error is returned.")149 }150}151func TestCreateDisk_WrongZone(t *testing.T) {152 /* Arrange */153 gceProjectID := "test-project"154 gceRegion := "fake-region"155 zonesWithNodes := []string{"zone1"}156 fakeManager := newFakeManager(gceProjectID, gceRegion)157 gce := Cloud{158 manager: fakeManager,159 managedZones: zonesWithNodes,160 nodeZones: createNodeZones(zonesWithNodes),161 nodeInformerSynced: func() bool { return true }}162 diskName := "disk"163 diskType := DiskTypeSSD164 const sizeGb int64 = 128165 /* Act */166 _, err := gce.CreateDisk(diskName, diskType, "zone2", sizeGb, nil)167 /* Assert */168 if err == nil {169 t.Error("Expected error when zone is not managed, but none returned.")170 }171}172func TestCreateDisk_NoManagedZone(t *testing.T) {173 /* Arrange */174 gceProjectID := "test-project"175 gceRegion := "fake-region"176 zonesWithNodes := []string{}177 fakeManager := newFakeManager(gceProjectID, gceRegion)178 gce := Cloud{179 manager: fakeManager,180 managedZones: zonesWithNodes,181 nodeZones: createNodeZones(zonesWithNodes),182 nodeInformerSynced: func() bool { return true }}183 diskName := "disk"184 diskType := DiskTypeSSD185 const sizeGb int64 = 128186 /* Act */187 _, err := gce.CreateDisk(diskName, diskType, "zone1", sizeGb, nil)188 /* Assert */189 if err == nil {190 t.Error("Expected error when managedZones is empty, but none returned.")191 }192}193func TestCreateDisk_BadDiskType(t *testing.T) {194 /* Arrange */195 gceProjectID := "test-project"196 gceRegion := "fake-region"197 zonesWithNodes := []string{"zone1"}198 fakeManager := newFakeManager(gceProjectID, gceRegion)199 gce := Cloud{manager: fakeManager,200 managedZones: zonesWithNodes,201 nodeZones: createNodeZones(zonesWithNodes),202 nodeInformerSynced: func() bool { return true }}203 diskName := "disk"204 diskType := "arbitrary-disk"205 zone := "zone1"206 const sizeGb int64 = 128207 /* Act */208 _, err := gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)209 /* Assert */210 if err == nil {211 t.Error("Expected error when disk type is not supported, but none returned.")212 }213}214func TestCreateDisk_MultiZone(t *testing.T) {215 /* Arrange */216 gceProjectID := "test-project"217 gceRegion := "fake-region"218 zonesWithNodes := []string{"zone1", "zone2", "zone3"}219 fakeManager := newFakeManager(gceProjectID, gceRegion)220 alphaFeatureGate := NewAlphaFeatureGate([]string{})221 gce := Cloud{222 manager: fakeManager,223 managedZones: zonesWithNodes,224 AlphaFeatureGate: alphaFeatureGate,225 nodeZones: createNodeZones(zonesWithNodes),226 nodeInformerSynced: func() bool { return true },227 }228 diskName := "disk"229 diskType := DiskTypeStandard230 const sizeGb int64 = 128231 /* Act & Assert */232 for _, zone := range gce.managedZones {233 diskName = zone + "disk"234 _, err := gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)235 if err != nil {236 t.Errorf("Error creating disk in zone '%v'; error: \"%v\"", zone, err)237 }238 }239}240func TestDeleteDisk_Basic(t *testing.T) {241 /* Arrange */242 gceProjectID := "test-project"243 gceRegion := "fake-region"244 zonesWithNodes := []string{"zone1"}245 fakeManager := newFakeManager(gceProjectID, gceRegion)246 alphaFeatureGate := NewAlphaFeatureGate([]string{})247 gce := Cloud{248 manager: fakeManager,249 managedZones: zonesWithNodes,250 AlphaFeatureGate: alphaFeatureGate,251 nodeZones: createNodeZones(zonesWithNodes),252 nodeInformerSynced: func() bool { return true },253 }254 diskName := "disk"255 diskType := DiskTypeSSD256 zone := "zone1"257 const sizeGb int64 = 128258 gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)259 /* Act */260 err := gce.DeleteDisk(diskName)261 /* Assert */262 if err != nil {263 t.Error(err)264 }265 if !fakeManager.deleteDiskCalled {266 t.Error("Never called GCE disk delete.")267 }268}269func TestDeleteDisk_NotFound(t *testing.T) {270 /* Arrange */271 gceProjectID := "test-project"272 gceRegion := "fake-region"273 zonesWithNodes := []string{"zone1"}274 fakeManager := newFakeManager(gceProjectID, gceRegion)275 alphaFeatureGate := NewAlphaFeatureGate([]string{})276 gce := Cloud{277 manager: fakeManager,278 managedZones: zonesWithNodes,279 AlphaFeatureGate: alphaFeatureGate,280 nodeZones: createNodeZones(zonesWithNodes),281 nodeInformerSynced: func() bool { return true },282 }283 diskName := "disk"284 /* Act */285 err := gce.DeleteDisk(diskName)286 /* Assert */287 if err != nil {288 t.Error("Expected successful operation when disk is not found, but an error is returned.")289 }290}291func TestDeleteDisk_ResourceBeingUsed(t *testing.T) {292 /* Arrange */293 gceProjectID := "test-project"294 gceRegion := "fake-region"295 zonesWithNodes := []string{"zone1"}296 fakeManager := newFakeManager(gceProjectID, gceRegion)297 alphaFeatureGate := NewAlphaFeatureGate([]string{})298 gce := Cloud{299 manager: fakeManager,300 managedZones: zonesWithNodes,301 AlphaFeatureGate: alphaFeatureGate,302 nodeZones: createNodeZones(zonesWithNodes),303 nodeInformerSynced: func() bool { return true },304 }305 diskName := "disk"306 diskType := DiskTypeSSD307 zone := "zone1"308 const sizeGb int64 = 128309 gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)310 fakeManager.resourceInUse = true311 /* Act */312 err := gce.DeleteDisk(diskName)313 /* Assert */314 if err == nil {315 t.Error("Expected error when disk is in use, but none returned.")316 }317}318func TestDeleteDisk_SameDiskMultiZone(t *testing.T) {319 /* Assert */320 gceProjectID := "test-project"321 gceRegion := "fake-region"322 zonesWithNodes := []string{"zone1", "zone2", "zone3"}323 fakeManager := newFakeManager(gceProjectID, gceRegion)324 alphaFeatureGate := NewAlphaFeatureGate([]string{})325 gce := Cloud{326 manager: fakeManager,327 managedZones: zonesWithNodes,328 AlphaFeatureGate: alphaFeatureGate,329 nodeZones: createNodeZones(zonesWithNodes),330 nodeInformerSynced: func() bool { return true },331 }332 diskName := "disk"333 diskType := DiskTypeSSD334 const sizeGb int64 = 128335 for _, zone := range gce.managedZones {336 gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)337 }338 /* Act */339 // DeleteDisk will call FakeServiceManager.GetDiskFromCloudProvider() with all zones,340 // and FakeServiceManager.GetDiskFromCloudProvider() always returns a disk,341 // so DeleteDisk thinks a disk with diskName exists in all zones.342 err := gce.DeleteDisk(diskName)343 /* Assert */344 if err == nil {345 t.Error("Expected error when disk is found in multiple zones, but none returned.")346 }347}348func TestDeleteDisk_DiffDiskMultiZone(t *testing.T) {349 /* Arrange */350 gceProjectID := "test-project"351 gceRegion := "fake-region"352 zonesWithNodes := []string{"zone1"}353 fakeManager := newFakeManager(gceProjectID, gceRegion)354 alphaFeatureGate := NewAlphaFeatureGate([]string{})355 gce := Cloud{356 manager: fakeManager,357 managedZones: zonesWithNodes,358 AlphaFeatureGate: alphaFeatureGate,359 nodeZones: createNodeZones(zonesWithNodes),360 nodeInformerSynced: func() bool { return true },361 }362 diskName := "disk"363 diskType := DiskTypeSSD364 const sizeGb int64 = 128365 for _, zone := range gce.managedZones {366 diskName = zone + "disk"367 gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)368 }369 /* Act & Assert */370 var err error371 for _, zone := range gce.managedZones {372 diskName = zone + "disk"373 err = gce.DeleteDisk(diskName)374 if err != nil {375 t.Errorf("Error deleting disk in zone '%v'; error: \"%v\"", zone, err)376 }377 }378}379func pv(name, zone string) *v1.PersistentVolume {380 return &v1.PersistentVolume{381 ObjectMeta: metav1.ObjectMeta{382 Labels: map[string]string{383 v1.LabelFailureDomainBetaZone: zone,384 },385 },386 Spec: v1.PersistentVolumeSpec{387 PersistentVolumeSource: v1.PersistentVolumeSource{388 GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{389 PDName: name,390 },391 },392 },393 }394}395func TestGetLabelsForVolume_Basic(t *testing.T) {396 ctx := context.Background()397 /* Arrange */398 gceProjectID := "test-project"399 gceRegion := "us-central1"400 zone := "us-central1-c"401 zonesWithNodes := []string{zone}402 fakeManager := newFakeManager(gceProjectID, gceRegion)403 diskName := "disk"404 diskType := DiskTypeSSD405 const sizeGb int64 = 128406 alphaFeatureGate := NewAlphaFeatureGate([]string{})407 gce := Cloud{408 manager: fakeManager,409 managedZones: zonesWithNodes,410 AlphaFeatureGate: alphaFeatureGate,411 nodeZones: createNodeZones(zonesWithNodes),412 nodeInformerSynced: func() bool { return true },413 }414 gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)415 /* Act */416 labels, err := gce.GetLabelsForVolume(ctx, pv(diskName, zone))417 /* Assert */418 if err != nil {419 t.Error(err)420 }421 if labels[v1.LabelFailureDomainBetaZone] != zone {422 t.Errorf("Failure domain is '%v', but zone is '%v'",423 labels[v1.LabelFailureDomainBetaZone], zone)424 }425 if labels[v1.LabelFailureDomainBetaRegion] != gceRegion {426 t.Errorf("Region is '%v', but region is 'us-central1'", labels[v1.LabelFailureDomainBetaRegion])427 }428}429func TestGetLabelsForVolume_NoZone(t *testing.T) {430 ctx := context.Background()431 /* Arrange */432 gceProjectID := "test-project"433 gceRegion := "europe-west1"434 zone := "europe-west1-d"435 zonesWithNodes := []string{zone}436 fakeManager := newFakeManager(gceProjectID, gceRegion)437 diskName := "disk"438 diskType := DiskTypeStandard439 const sizeGb int64 = 128440 alphaFeatureGate := NewAlphaFeatureGate([]string{})441 gce := Cloud{442 manager: fakeManager,443 managedZones: zonesWithNodes,444 AlphaFeatureGate: alphaFeatureGate,445 nodeZones: createNodeZones(zonesWithNodes),446 nodeInformerSynced: func() bool { return true },447 }448 gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)449 pv := pv(diskName, zone)450 delete(pv.Labels, v1.LabelFailureDomainBetaZone)451 /* Act */452 labels, err := gce.GetLabelsForVolume(ctx, pv)453 /* Assert */454 if err != nil {455 t.Error(err)456 }457 if labels[v1.LabelFailureDomainBetaZone] != zone {458 t.Errorf("Failure domain is '%v', but zone is '%v'",459 labels[v1.LabelFailureDomainBetaZone], zone)460 }461 if labels[v1.LabelFailureDomainBetaRegion] != gceRegion {462 t.Errorf("Region is '%v', but region is 'europe-west1'", labels[v1.LabelFailureDomainBetaRegion])463 }464}465func TestGetLabelsForVolume_DiskNotFound(t *testing.T) {466 ctx := context.Background()467 /* Arrange */468 gceProjectID := "test-project"469 gceRegion := "fake-region"470 zone := "asia-northeast1-a"471 zonesWithNodes := []string{zone}472 fakeManager := newFakeManager(gceProjectID, gceRegion)473 diskName := "disk"474 gce := Cloud{manager: fakeManager,475 managedZones: zonesWithNodes,476 nodeZones: createNodeZones(zonesWithNodes),477 nodeInformerSynced: func() bool { return true }}478 /* Act */479 _, err := gce.GetLabelsForVolume(ctx, pv(diskName, zone))480 /* Assert */481 if err == nil {482 t.Error("Expected error when the specified disk does not exist, but none returned.")483 }484}485func TestGetLabelsForVolume_DiskNotFoundAndNoZone(t *testing.T) {486 ctx := context.Background()487 /* Arrange */488 gceProjectID := "test-project"489 gceRegion := "fake-region"490 zone := "asia-northeast1-a"491 zonesWithNodes := []string{}492 fakeManager := newFakeManager(gceProjectID, gceRegion)493 diskName := "disk"494 alphaFeatureGate := NewAlphaFeatureGate([]string{})495 gce := Cloud{496 manager: fakeManager,497 managedZones: zonesWithNodes,498 AlphaFeatureGate: alphaFeatureGate,499 nodeZones: createNodeZones(zonesWithNodes),500 nodeInformerSynced: func() bool { return true },501 }502 pv := pv(diskName, zone)503 delete(pv.Labels, v1.LabelFailureDomainBetaZone)504 /* Act */505 _, err := gce.GetLabelsForVolume(ctx, pv)506 /* Assert */507 if err == nil {508 t.Error("Expected error when the specified disk does not exist, but none returned.")509 }510}511func TestGetLabelsForVolume_DupDisk(t *testing.T) {512 ctx := context.Background()513 /* Arrange */514 gceProjectID := "test-project"515 gceRegion := "us-west1"516 zonesWithNodes := []string{"us-west1-b", "asia-southeast1-a"}517 fakeManager := newFakeManager(gceProjectID, gceRegion)518 diskName := "disk"519 diskType := DiskTypeStandard520 zone := "us-west1-b"521 const sizeGb int64 = 128522 alphaFeatureGate := NewAlphaFeatureGate([]string{})523 gce := Cloud{524 manager: fakeManager,525 managedZones: zonesWithNodes,526 AlphaFeatureGate: alphaFeatureGate,527 nodeZones: createNodeZones(zonesWithNodes),528 nodeInformerSynced: func() bool { return true },529 }530 for _, zone := range gce.managedZones {531 gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)532 }533 /* Act */534 labels, err := gce.GetLabelsForVolume(ctx, pv(diskName, zone))535 /* Assert */536 if err != nil {537 t.Error("Disk name and zone uniquely identifies a disk, yet an error is returned.")538 }539 if labels[v1.LabelFailureDomainBetaZone] != zone {540 t.Errorf("Failure domain is '%v', but zone is '%v'",541 labels[v1.LabelFailureDomainBetaZone], zone)542 }543 if labels[v1.LabelFailureDomainBetaRegion] != gceRegion {544 t.Errorf("Region is '%v', but region is 'us-west1'", labels[v1.LabelFailureDomainBetaRegion])545 }546}547func TestGetLabelsForVolume_DupDiskNoZone(t *testing.T) {548 ctx := context.Background()549 /* Arrange */550 gceProjectID := "test-project"551 gceRegion := "fake-region"552 zonesWithNodes := []string{"us-west1-b", "asia-southeast1-a"}553 fakeManager := newFakeManager(gceProjectID, gceRegion)554 diskName := "disk"555 zone := "us-west1-b"556 diskType := DiskTypeStandard557 const sizeGb int64 = 128558 alphaFeatureGate := NewAlphaFeatureGate([]string{})559 gce := Cloud{560 manager: fakeManager,561 managedZones: zonesWithNodes,562 AlphaFeatureGate: alphaFeatureGate,563 nodeZones: createNodeZones(zonesWithNodes),564 nodeInformerSynced: func() bool { return true },565 }566 for _, zone := range gce.managedZones {567 gce.CreateDisk(diskName, diskType, zone, sizeGb, nil)568 }569 pv := pv(diskName, zone)570 delete(pv.Labels, v1.LabelFailureDomainBetaZone)571 /* Act */572 _, err := gce.GetLabelsForVolume(ctx, pv)573 /* Assert */574 if err == nil {575 t.Error("Expected error when the disk is duplicated and zone is not specified, but none returned.")576 }577}578func TestGetAutoLabelsForPD(t *testing.T) {579 zonesWithNodes := []string{"us-west1-b", "asia-southeast1-a"}580 gceRegion := "us-west1"581 gceProjectID := "test-project"582 fakeManager := newFakeManager(gceProjectID, gceRegion)583 alphaFeatureGate := NewAlphaFeatureGate([]string{})584 diskName := "disk"585 zone1 := "us-west1-b"586 zone2 := "us-west1-a"587 const sizeGb int64 = 128588 gce := Cloud{589 manager: fakeManager,590 managedZones: zonesWithNodes,591 AlphaFeatureGate: alphaFeatureGate,592 nodeZones: createNodeZones(zonesWithNodes),593 nodeInformerSynced: func() bool { return true },594 }595 testCases := []struct {596 name string597 zoneInfo zoneType598 region string599 wantZoneLabel sets.String600 wantErr bool601 }{602 {603 name: "basic singleZone",604 zoneInfo: singleZone{zone1},605 region: gceRegion,606 wantZoneLabel: sets.NewString(zone1),607 },608 {609 name: "basic multiZone",610 zoneInfo: multiZone{sets.NewString(zone1, zone2)},611 region: gceRegion,612 // Order of zones in label is nondeterministic.613 wantZoneLabel: sets.NewString("us-west1-a__us-west1-b", "us-west1-b__us-west1-a"),614 },615 {616 name: "empty singleZone",617 zoneInfo: singleZone{},618 region: gceRegion,619 wantErr: true,620 },621 {622 name: "empty region singleZone",623 zoneInfo: singleZone{zone1},624 region: "",625 wantErr: true,626 },627 {628 name: "empty zone set multiZone",629 zoneInfo: multiZone{sets.NewString()},630 region: gceRegion,631 wantErr: true,632 },633 {634 name: "no Zoneinfo",635 zoneInfo: nil,636 region: gceRegion,637 wantErr: true,638 },639 }640 for _, tc := range testCases {641 t.Run(tc.name, func(t *testing.T) {642 disk := &Disk{643 ZoneInfo: tc.zoneInfo,644 Region: tc.region,645 Name: diskName,646 SizeGb: sizeGb,647 }648 labels, err := gce.GetAutoLabelsForPD(disk)649 if gotErr := err != nil; gotErr != tc.wantErr {650 t.Errorf("gce.GetAutoLabelsForPD(%+v) = %v; wantErr: %v", disk, err, tc.wantErr)651 }652 if err != nil {653 return654 }655 if got := labels[v1.LabelFailureDomainBetaZone]; !tc.wantZoneLabel.Has(got) {656 t.Errorf("labels[v1.LabelFailureDomainBetaZone] = %v; want one of: %v", got, tc.wantZoneLabel.List())657 }658 // Validate labels659 if got := labels[v1.LabelFailureDomainBetaRegion]; got != gceRegion {660 t.Errorf("labels[v1.LabelFailureDomainBetaRegion] = %v; want: %v", got, gceRegion)661 }662 })663 }664}665type targetClientAPI int666const (667 targetStable targetClientAPI = iota668 targetBeta669 targetAlpha670)671type FakeServiceManager struct {672 // Common fields shared among tests673 targetAPI targetClientAPI674 gceProjectID string675 gceRegion string676 zonalDisks map[string]string // zone: diskName677 regionalDisks map[string]sets.String // diskName: zones678 opError error679 // Fields for TestCreateDisk680 createDiskCalled bool681 diskToCreateAlpha *computealpha.Disk682 diskToCreateBeta *computebeta.Disk683 diskToCreateStable *compute.Disk684 // Fields for TestDeleteDisk685 deleteDiskCalled bool686 resourceInUse bool // Marks the disk as in-use687}688func newFakeManager(gceProjectID string, gceRegion string) *FakeServiceManager {689 return &FakeServiceManager{690 zonalDisks: make(map[string]string),691 regionalDisks: make(map[string]sets.String),692 gceProjectID: gceProjectID,693 gceRegion: gceRegion,694 }695}696/**697 * Upon disk creation, disk info is stored in FakeServiceManager698 * to be used by other tested methods.699 */700func (manager *FakeServiceManager) CreateDiskOnCloudProvider(701 name string,702 sizeGb int64,703 tagsStr string,704 diskType string,705 zone string) (*Disk, error) {706 manager.createDiskCalled = true707 switch t := manager.targetAPI; t {708 case targetStable:709 diskTypeURI := gceComputeAPIEndpoint + "projects/" + fmt.Sprintf(diskTypeURITemplateSingleZone, manager.gceProjectID, zone, diskType)710 diskToCreateV1 := &compute.Disk{711 Name: name,712 SizeGb: sizeGb,713 Description: tagsStr,714 Type: diskTypeURI,715 }716 manager.diskToCreateStable = diskToCreateV1717 manager.zonalDisks[zone] = diskToCreateV1.Name718 return nil, nil719 case targetBeta:720 diskTypeURI := gceComputeAPIEndpoint + "projects/" + fmt.Sprintf(diskTypeURITemplateSingleZone, manager.gceProjectID, zone, diskType)721 diskToCreateBeta := &computebeta.Disk{722 Name: name,723 SizeGb: sizeGb,724 Description: tagsStr,725 Type: diskTypeURI,726 }727 manager.diskToCreateBeta = diskToCreateBeta728 manager.zonalDisks[zone] = diskToCreateBeta.Name729 return nil, nil730 case targetAlpha:731 diskTypeURI := gceComputeAPIEndpointBeta + "projects/" + fmt.Sprintf(diskTypeURITemplateSingleZone, manager.gceProjectID, zone, diskType)732 diskToCreateAlpha := &computealpha.Disk{733 Name: name,734 SizeGb: sizeGb,735 Description: tagsStr,736 Type: diskTypeURI,737 }738 manager.diskToCreateAlpha = diskToCreateAlpha739 manager.zonalDisks[zone] = diskToCreateAlpha.Name740 return nil, nil741 default:742 return nil, fmt.Errorf("unexpected type: %T", t)743 }744}745/**746 * Upon disk creation, disk info is stored in FakeServiceManager747 * to be used by other tested methods.748 */749func (manager *FakeServiceManager) CreateRegionalDiskOnCloudProvider(750 name string,751 sizeGb int64,752 tagsStr string,753 diskType string,754 zones sets.String) (*Disk, error) {755 manager.createDiskCalled = true756 diskTypeURI := gceComputeAPIEndpoint + "projects/" + fmt.Sprintf(diskTypeURITemplateRegional, manager.gceProjectID, manager.gceRegion, diskType)757 switch t := manager.targetAPI; t {758 case targetStable:759 diskToCreateV1 := &compute.Disk{760 Name: name,761 SizeGb: sizeGb,762 Description: tagsStr,763 Type: diskTypeURI,764 }765 manager.diskToCreateStable = diskToCreateV1766 manager.regionalDisks[diskToCreateV1.Name] = zones767 return nil, nil768 default:769 return nil, fmt.Errorf("unexpected type: %T", t)770 }771}772func (manager *FakeServiceManager) AttachDiskOnCloudProvider(773 disk *Disk,774 readWrite string,775 instanceZone string,776 instanceName string) error {777 switch t := manager.targetAPI; t {778 case targetStable:779 return nil780 case targetBeta:...

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gce.Start(instance)4 gce.Stop(instance)5 gce.Delete(instance)6}7import (8type GCE struct {9}10func Create(instance, zone, machineType, image string) *GCE {11 gce := GCE{Project: "gce-project-123", Zone: zone, Instance: instance, MachineType: machineType, Image: image}12 gce.createInstance()13}14func Start(gce *GCE) {15 gce.startInstance()16}17func Stop(gce *GCE) {18 gce.stopInstance()19}20func Delete(gce *GCE) {21 gce.deleteInstance()22}23func (gce *GCE) createInstance() {24 client := oauth2.NewClient(oauth2.NoContext, google.ComputeTokenSource(""))25 service, err := compute.New(client)26 if err != nil {27 fmt.Println(err)28 }29 instance := &compute.Instance{30 MachineType: fmt.Sprintf("zones/%s/machineTypes/%s", gce.Zone, gce.MachineType),31 Disks: []*compute.AttachedDisk{32 &compute.AttachedDisk{

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 client, err := google.DefaultClient(ctx, compute.ComputeScope)5 if err != nil {6 fmt.Println("Error while creating client:", err)7 os.Exit(1)8 }9 service, err := compute.New(client)10 if err != nil {11 fmt.Println("Error while creating service:", err)12 os.Exit(1)13 }14 disk := &compute.Disk{15 }16 op, err := service.Disks.Insert(projectID, zone, disk).Do()17 if err != nil {18 fmt.Println("Error while creating disk:", err)19 os.Exit(1)20 }21 fmt.Println("Disk is being created. Operation:", op.Name)22}23import (24func main() {25 ctx := context.Background()26 client, err := google.DefaultClient(ctx, compute.ComputeScope)27 if err != nil {28 fmt.Println("Error while creating client:", err)29 os.Exit(1)30 }31 service, err := compute.New(client)32 if err != nil {33 fmt.Println("Error while creating service:", err)34 os.Exit(1)35 }36 op, err := service.Disks.Delete(projectID, zone, diskName).Do()37 if err != nil {38 fmt.Println("Error while deleting disk:", err)39 os.Exit(1)40 }41 fmt.Println("Disk is

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2type gce struct {3}4func (g gce) Create() {5 fmt.Println("code to use Create method of gce class")6}7type aws struct {8}9func (a aws) Create() {10 fmt.Println("code to use Create method of aws class")11}12type cloud interface {13 Create()14}15func main() {16 c = gce{}17 c.Create()18 c = aws{}19 c.Create()20}

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 gce := gce{}5 gce.Create()6}7import (8type gce struct {9}10func (gce gce) Create() {11 fmt.Println("Hello, playground")12}13cannot use gce (type gce) as type compute.InstancesService in argument to compute.NewInstancesService:14 gce does not implement compute.InstancesService (missing Create method)

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := compute.NewClient(nil)4 i := &compute.Instance{5 Disks: []*compute.AttachedDisk{6 {7 },8 },9 NetworkInterfaces: []*compute.NetworkInterface{10 {11 AccessConfigs: []*compute.AccessConfig{12 {13 },14 },15 },16 },17 }18 if err := c.Instances.Create("my-project", "us-central1-a", i).Do(); err != nil {19 fmt.Println(err)20 }21}

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1gce := gce.NewComputeService()2gce.Create()3gce := gce.NewComputeService()4gce.Delete()5gce := gce.NewComputeService()6gce.List()7gce := gce.NewComputeService()8gce.Start()9gce := gce.NewComputeService()10gce.Stop()11gce := gce.NewComputeService()12gce.Update()13gce := gce.NewComputeService()14gce.Get()15gce := gce.NewComputeService()16gce.GetStatus()17gce := gce.NewComputeService()18gce.GetIP()19gce := gce.NewComputeService()20gce.GetSSH()21gce := gce.NewComputeService()22gce.GetConsole()

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gceObj := gce.GCE{}4 gceObj.Create()5}6import (7func main() {8 gceObj := gce.GCE{}9 gceObj.Create()10}11import (12func main() {13 gceObj := gce.GCE{}14 gceObj.Create()15}16import (17func main() {18 gceObj := gce.GCE{}19 gceObj.Create()20}21import (22func main() {23 gceObj := gce.GCE{}24 gceObj.Create()25}26import (

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 g := gce.New()4 g.Create()5 fmt.Println(g)6}7import (8func main() {9 g := gce.New()10 g.Delete()11 fmt.Println(g)12}13import (14func main() {15 g := gce.New()16 g.Delete()17 fmt.Println(g)18}19import (20func main() {21 g := gce.New()22 g.Delete()23 fmt.Println(g)24}25import (26func main() {27 g := gce.New()28 g.Delete()29 fmt.Println(g)30}31import (32func main() {33 g := gce.New()34 g.Delete()35 fmt.Println(g)36}37import (38func main() {39 g := gce.New()40 g.Delete()41 fmt.Println(g)42}43import (44func main() {45 g := gce.New()46 g.Delete()47 fmt.Println(g)48}49import (

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gce := gce.NewGce()4 gce.Create("gce1","us-central1-a","n1-standard-1","centos-6-v20160127","/home/centos/centos6.pem")5}6import (7func main() {8 gce := gce.NewGce()9 gce.List()10}11import (12func main() {13 gce := gce.NewGce()14 gce.Delete("gce1")15}16import (17func main() {18 gce := gce.NewGce()19 gce.Start("gce1")20}21import (22func main() {23 gce := gce.NewGce()24 gce.Stop("gce1")25}26import (27func main() {28 gce := gce.NewGce()29 gce.Restart("gce1")30}31import (32func main() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful