github.com/vmware/govmomi@v0.51.0/vapi/vcenter/consumptiondomains/consumptiondomains_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package consumptiondomains_test 6 7 import ( 8 "context" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/vmware/govmomi/simulator" 14 "github.com/vmware/govmomi/vapi/rest" 15 "github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/associations" 16 "github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/zones" 17 "github.com/vmware/govmomi/vim25" 18 19 _ "github.com/vmware/govmomi/vapi/simulator" 20 _ "github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/simulator" 21 ) 22 23 func TestConsumptionDomains(t *testing.T) { 24 simulator.Test(func(ctx context.Context, vc *vim25.Client) { 25 rc := rest.NewClient(vc) 26 27 err := rc.Login(ctx, simulator.DefaultLogin) 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 zm := zones.NewManager(rc) 33 34 // Create a zone 35 zoneId, err := zm.CreateZone(zones.CreateSpec{ 36 Zone: "test-zone-1", 37 Description: "placeholder description", 38 }) 39 40 if err != nil { 41 t.Error(err) 42 } 43 44 // List all zones and find the one created earlier 45 zonesList, err := zm.ListZones() 46 47 if err != nil { 48 t.Error(err) 49 } 50 51 assert.Equal(t, 1, len(zonesList)) 52 assert.Equal(t, "test-zone-1", zonesList[0].Zone) 53 assert.Equal(t, "placeholder description", zonesList[0].Info.Description) 54 55 // Query zone by ID 56 zone, err := zm.GetZone(zoneId) 57 58 if err != nil { 59 t.Error(err) 60 } 61 62 assert.Equal(t, "placeholder description", zone.Description) 63 64 am := associations.NewManager(rc) 65 66 // Create a cluster association 67 err = am.AddAssociations(zoneId, "domain-c9") 68 69 if err != nil { 70 t.Error(err) 71 } 72 73 // Query the associations for the test zone 74 assc, err := am.GetAssociations(zoneId) 75 76 if err != nil { 77 t.Error(err) 78 } 79 80 assert.Equal(t, 1, len(assc)) 81 assert.Equal(t, "domain-c9", assc[0]) 82 83 // Delete the cluster association 84 err = am.RemoveAssociations(zoneId, "domain-c9") 85 86 if err != nil { 87 t.Error(err) 88 } 89 90 // Verify that the association is removed 91 assc, err = am.GetAssociations(zoneId) 92 93 if err != nil { 94 t.Error(err) 95 } 96 97 assert.Equal(t, 0, len(assc)) 98 99 // Delete the zone 100 err = zm.DeleteZone(zoneId) 101 102 if err != nil { 103 t.Error(err) 104 } 105 106 // Verify the zone is removed 107 zone, err = zm.GetZone(zoneId) 108 109 if err == nil { 110 t.Error(err) 111 } 112 113 zonesList, err = zm.ListZones() 114 115 if err != nil { 116 t.Error(err) 117 } 118 119 assert.Equal(t, 0, len(zonesList)) 120 }) 121 }