github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/adminvdc_test.go (about) 1 //go:build org || functional || ALL 2 3 /* 4 * Copyright 2020 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. 5 */ 6 7 package govcd 8 9 import ( 10 "fmt" 11 "math" 12 13 "github.com/vmware/go-vcloud-director/v2/types/v56" 14 . "gopkg.in/check.v1" 15 ) 16 17 // Tests org function GetVDCByName with the vdc specified 18 // in the config file. Then tests with a vdc that doesn't exist. 19 // Fails if the config file name doesn't match with the found VDC, or 20 // if the invalid vdc is found by the function. Also tests a VDC 21 // that doesn't exist. Asserts an error if the function finds it or 22 // if the error is not nil. 23 func (vcd *TestVCD) Test_CreateOrgVdcWithFlex(check *C) { 24 if vcd.skipAdminTests { 25 check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName())) 26 } 27 28 if vcd.config.VCD.ProviderVdc.Name == "" { 29 check.Skip("No Provider VDC name given for VDC tests") 30 } 31 if vcd.config.VCD.ProviderVdc.StorageProfile == "" { 32 check.Skip("No Storage Profile given for VDC tests") 33 } 34 if vcd.config.VCD.ProviderVdc.NetworkPool == "" { 35 check.Skip("No Network Pool given for VDC tests") 36 } 37 adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 38 check.Assert(err, IsNil) 39 check.Assert(adminOrg, NotNil) 40 41 providerVdcHref := getVdcProviderVdcHref(vcd, check) 42 43 storageProfile, err := vcd.client.QueryProviderVdcStorageProfileByName(vcd.config.VCD.StorageProfile.SP1, providerVdcHref) 44 check.Assert(err, IsNil) 45 firstStorageProfileHref := storageProfile.HREF 46 networkPoolHref := getVdcNetworkPoolHref(vcd, check) 47 48 secondStorageProfileHref := "" 49 // Make test more robust and tests additionally disabled storage profile 50 if vcd.config.VCD.StorageProfile.SP2 != "" { 51 storageProfile, err := vcd.client.QueryProviderVdcStorageProfileByName(vcd.config.VCD.StorageProfile.SP2, providerVdcHref) 52 check.Assert(err, IsNil) 53 secondStorageProfileHref = storageProfile.HREF 54 } 55 56 allocationModels := []string{"AllocationVApp", "AllocationPool", "ReservationPool", "Flex"} 57 trueValue := true 58 for i, allocationModel := range allocationModels { 59 vdcConfiguration := &types.VdcConfiguration{ 60 Name: fmt.Sprintf("%s%d", TestCreateOrgVdc, i), 61 Xmlns: types.XMLNamespaceVCloud, 62 AllocationModel: allocationModel, 63 ComputeCapacity: []*types.ComputeCapacity{ 64 { 65 CPU: &types.CapacityWithUsage{ 66 Units: "MHz", 67 Allocated: 1024, 68 Limit: 1024, 69 }, 70 Memory: &types.CapacityWithUsage{ 71 Allocated: 1024, 72 Limit: 1024, 73 }, 74 }, 75 }, 76 VdcStorageProfile: []*types.VdcStorageProfileConfiguration{{ 77 Enabled: addrOf(true), 78 Units: "MB", 79 Limit: 1024, 80 Default: true, 81 ProviderVdcStorageProfile: &types.Reference{ 82 HREF: firstStorageProfileHref, 83 }, 84 }, 85 }, 86 NetworkPoolReference: &types.Reference{ 87 HREF: networkPoolHref, 88 }, 89 ProviderVdcReference: &types.Reference{ 90 HREF: providerVdcHref, 91 }, 92 IsEnabled: true, 93 IsThinProvision: true, 94 UsesFastProvisioning: true, 95 } 96 97 if allocationModel == "Flex" { 98 vdcConfiguration.IsElastic = &trueValue 99 vdcConfiguration.IncludeMemoryOverhead = &trueValue 100 } 101 102 if secondStorageProfileHref != "" { 103 vdcConfiguration.VdcStorageProfile = append(vdcConfiguration.VdcStorageProfile, &types.VdcStorageProfileConfiguration{ 104 Enabled: addrOf(false), 105 Units: "MB", 106 Limit: 1024, 107 Default: false, 108 ProviderVdcStorageProfile: &types.Reference{ 109 HREF: secondStorageProfileHref, 110 }, 111 }) 112 } 113 114 vdc, _ := adminOrg.GetVDCByName(vdcConfiguration.Name, false) 115 if vdc != nil { 116 err = vdc.DeleteWait(true, true) 117 check.Assert(err, IsNil) 118 } 119 120 // expected to fail due to missing value 121 task, err := adminOrg.CreateOrgVdcAsync(vdcConfiguration) 122 check.Assert(err, Not(IsNil)) 123 check.Assert(task, Equals, Task{}) 124 // checks function validation 125 check.Assert(err.Error(), Equals, "VdcConfiguration missing required field: ComputeCapacity[0].Memory.Units") 126 127 vdcConfiguration.ComputeCapacity[0].Memory.Units = "MB" 128 129 vdc, err = adminOrg.CreateOrgVdc(vdcConfiguration) 130 check.Assert(err, IsNil) 131 check.Assert(vdc, NotNil) 132 133 AddToCleanupList(vdcConfiguration.Name, "vdc", vcd.org.Org.Name, "Test_CreateVdcWithFlex") 134 135 vdc, err = adminOrg.GetVDCByName(vdcConfiguration.Name, true) 136 check.Assert(err, IsNil) 137 check.Assert(vdc, NotNil) 138 check.Assert(vdc.Vdc.Name, Equals, vdcConfiguration.Name) 139 check.Assert(vdc.Vdc.IsEnabled, Equals, vdcConfiguration.IsEnabled) 140 check.Assert(vdc.Vdc.AllocationModel, Equals, vdcConfiguration.AllocationModel) 141 check.Assert(vdc.Vdc.VdcStorageProfiles, NotNil) 142 check.Assert(vdc.Vdc.VdcStorageProfiles.VdcStorageProfile, NotNil) 143 if secondStorageProfileHref == "" { 144 check.Assert(len(vdc.Vdc.VdcStorageProfiles.VdcStorageProfile), Equals, 1) 145 } else { 146 check.Assert(len(vdc.Vdc.VdcStorageProfiles.VdcStorageProfile), Equals, 2) 147 } 148 149 // As storage profiles may come unordered, we check them in a generic way with a loop 150 for _, spReference := range vdc.Vdc.VdcStorageProfiles.VdcStorageProfile { 151 check.Assert(spReference, NotNil) 152 vdcStorageProfileDetails, err := adminOrg.client.GetStorageProfileByHref(spReference.HREF) 153 check.Assert(err, IsNil) 154 switch spReference.Name { 155 case vcd.config.VCD.StorageProfile.SP1: 156 check.Assert(*vdcStorageProfileDetails.Enabled, Equals, true) 157 case vcd.config.VCD.StorageProfile.SP2: 158 check.Assert(*vdcStorageProfileDetails.Enabled, Equals, false) 159 default: 160 check.Errorf("didn't expect a storage profile with ID '%s' and name '%s'", spReference.ID, spReference.Name) 161 } 162 } 163 164 err = vdc.DeleteWait(true, true) 165 check.Assert(err, IsNil) 166 167 vdc, err = adminOrg.GetVDCByName(vdcConfiguration.Name, true) 168 check.Assert(err, NotNil) 169 check.Assert(vdc, IsNil) 170 } 171 } 172 173 // Tests VDC by updating it and then asserting if the 174 // variable is updated. 175 func (vcd *TestVCD) Test_UpdateVdcFlex(check *C) { 176 if vcd.skipAdminTests { 177 check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName())) 178 } 179 180 adminOrg, vdcConfiguration, err := setupVdc(vcd, check, "Flex") 181 check.Assert(err, IsNil) 182 183 adminVdc, err := adminOrg.GetAdminVDCByName(vdcConfiguration.Name, true) 184 185 check.Assert(err, IsNil) 186 check.Assert(adminVdc, NotNil) 187 check.Assert(adminVdc.AdminVdc.Name, Equals, vdcConfiguration.Name) 188 check.Assert(adminVdc.AdminVdc.IsEnabled, Equals, vdcConfiguration.IsEnabled) 189 check.Assert(adminVdc.AdminVdc.AllocationModel, Equals, vdcConfiguration.AllocationModel) 190 191 // test part to reproduce https://github.com/vmware/go-vcloud-director/issues/431 192 // this part manages to create task error which later on VDC update fails if type properties order is bad 193 providerVdcHref := getVdcProviderVdcHref(vcd, check) 194 pvdcStorageProfile, err := vcd.client.QueryProviderVdcStorageProfileByName(vcd.config.VCD.StorageProfile.SP2, providerVdcHref) 195 check.Assert(err, IsNil) 196 197 err = adminVdc.AddStorageProfileWait(&types.VdcStorageProfileConfiguration{ 198 Enabled: addrOf(true), 199 Default: false, 200 Units: "MB", 201 ProviderVdcStorageProfile: &types.Reference{HREF: pvdcStorageProfile.HREF}, 202 }, 203 "") 204 check.Assert(err, IsNil) 205 206 vdc, err := adminOrg.GetVDCByName(vdcConfiguration.Name, true) 207 check.Assert(err, IsNil) 208 209 vappName := check.TestName() 210 vmName := check.TestName() 211 vapp, err := makeEmptyVapp(vdc, vappName, "") 212 check.Assert(err, IsNil) 213 _, err = makeEmptyVm(vapp, vmName) 214 check.Assert(err, IsNil) 215 AddToCleanupList(vappName, "vapp", "", vappName) 216 217 err = adminVdc.SetDefaultStorageProfile(vcd.config.VCD.StorageProfile.SP2) 218 check.Assert(err, IsNil) 219 err = adminVdc.RemoveStorageProfileWait(vcd.config.VCD.StorageProfile.SP1) 220 // fails with error in task which stays referenced in VDC as `history` element 221 check.Assert(err, NotNil) 222 err = adminVdc.Refresh() 223 check.Assert(err, IsNil) 224 // end 225 226 updateDescription := "updateDescription" 227 computeCapacity := []*types.ComputeCapacity{ 228 &types.ComputeCapacity{ 229 CPU: &types.CapacityWithUsage{ 230 Units: "MHz", 231 Allocated: 2024, 232 Limit: 2024, 233 }, 234 Memory: &types.CapacityWithUsage{ 235 Allocated: 2024, 236 Limit: 2024, 237 Units: "MB", 238 }, 239 }, 240 } 241 quota := 111 242 vCpu := int64(1000) 243 guaranteed := float64(0.6) 244 adminVdc.AdminVdc.Description = updateDescription 245 adminVdc.AdminVdc.ComputeCapacity = computeCapacity 246 adminVdc.AdminVdc.IsEnabled = false 247 falseRef := false 248 trueRef := true 249 adminVdc.AdminVdc.IsThinProvision = &falseRef 250 adminVdc.AdminVdc.NetworkQuota = quota 251 adminVdc.AdminVdc.VMQuota = quota 252 adminVdc.AdminVdc.OverCommitAllowed = false 253 adminVdc.AdminVdc.VCpuInMhz = &vCpu 254 adminVdc.AdminVdc.UsesFastProvisioning = &falseRef 255 adminVdc.AdminVdc.ResourceGuaranteedCpu = &guaranteed 256 adminVdc.AdminVdc.ResourceGuaranteedMemory = &guaranteed 257 adminVdc.AdminVdc.IsElastic = &trueRef 258 adminVdc.AdminVdc.IncludeMemoryOverhead = &falseRef 259 260 updatedVdc, err := adminVdc.Update() 261 check.Assert(err, IsNil) 262 check.Assert(updatedVdc, Not(IsNil)) 263 check.Assert(updatedVdc.AdminVdc.Description, Equals, updateDescription) 264 check.Assert(updatedVdc.AdminVdc.ComputeCapacity[0].CPU.Allocated, Equals, computeCapacity[0].CPU.Allocated) 265 check.Assert(updatedVdc.AdminVdc.IsEnabled, Equals, false) 266 check.Assert(*updatedVdc.AdminVdc.IsThinProvision, Equals, false) 267 check.Assert(updatedVdc.AdminVdc.NetworkQuota, Equals, quota) 268 check.Assert(updatedVdc.AdminVdc.VMQuota, Equals, quota) 269 check.Assert(updatedVdc.AdminVdc.OverCommitAllowed, Equals, false) 270 check.Assert(*updatedVdc.AdminVdc.VCpuInMhz, Equals, vCpu) 271 check.Assert(*updatedVdc.AdminVdc.UsesFastProvisioning, Equals, false) 272 check.Assert(math.Abs(*updatedVdc.AdminVdc.ResourceGuaranteedCpu-guaranteed) < 0.001, Equals, true) 273 check.Assert(math.Abs(*updatedVdc.AdminVdc.ResourceGuaranteedMemory-guaranteed) < 0.001, Equals, true) 274 check.Assert(*updatedVdc.AdminVdc.IsElastic, Equals, true) 275 check.Assert(*updatedVdc.AdminVdc.IncludeMemoryOverhead, Equals, false) 276 vdc, err = adminOrg.GetVDCByName(updatedVdc.AdminVdc.Name, true) 277 check.Assert(err, IsNil) 278 task, err := vdc.Delete(true, true) 279 check.Assert(err, IsNil) 280 err = task.WaitTaskCompletion() 281 check.Assert(err, IsNil) 282 } 283 284 // Tests VDC storage profile update 285 func (vcd *TestVCD) Test_VdcUpdateStorageProfile(check *C) { 286 if vcd.skipAdminTests { 287 check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName())) 288 } 289 290 adminOrg, vdcConfiguration, err := setupVdc(vcd, check, "Flex") 291 check.Assert(err, IsNil) 292 293 adminVdc, err := adminOrg.GetAdminVDCByName(vdcConfiguration.Name, true) 294 check.Assert(err, IsNil) 295 check.Assert(adminVdc, NotNil) 296 vdc, err := adminOrg.GetVDCByName(vdcConfiguration.Name, true) 297 check.Assert(err, IsNil) 298 check.Assert(adminVdc, NotNil) 299 300 foundStorageProfile, err := vcd.client.Client.GetStorageProfileByHref(adminVdc.AdminVdc.VdcStorageProfiles.VdcStorageProfile[0].HREF) 301 check.Assert(err, IsNil) 302 check.Assert(foundStorageProfile, Not(Equals), types.VdcStorageProfile{}) 303 check.Assert(foundStorageProfile, NotNil) 304 305 storageProfileId, err := GetUuidFromHref(adminVdc.AdminVdc.VdcStorageProfiles.VdcStorageProfile[0].HREF, true) 306 check.Assert(err, IsNil) 307 check.Assert(storageProfileId, NotNil) 308 309 updatedVdc, err := adminVdc.UpdateStorageProfile(storageProfileId, &types.AdminVdcStorageProfile{ 310 Name: foundStorageProfile.Name, 311 Default: true, 312 Limit: 9081, 313 Enabled: addrOf(true), 314 Units: "MB", 315 IopsSettings: nil, 316 ProviderVdcStorageProfile: &types.Reference{HREF: foundStorageProfile.ProviderVdcStorageProfile.HREF}, 317 }) 318 check.Assert(err, IsNil) 319 check.Assert(updatedVdc, Not(IsNil)) 320 321 updatedStorageProfile, err := vcd.client.Client.GetStorageProfileByHref(adminVdc.AdminVdc.VdcStorageProfiles.VdcStorageProfile[0].HREF) 322 check.Assert(err, IsNil) 323 check.Assert(updatedStorageProfile, Not(Equals), types.VdcStorageProfile{}) 324 check.Assert(updatedStorageProfile, NotNil) 325 326 check.Assert(*updatedStorageProfile.Enabled, Equals, true) 327 check.Assert(updatedStorageProfile.Limit, Equals, int64(9081)) 328 check.Assert(updatedStorageProfile.Default, Equals, true) 329 check.Assert(updatedStorageProfile.Units, Equals, "MB") 330 task, err := vdc.Delete(true, true) 331 check.Assert(err, IsNil) 332 err = task.WaitTaskCompletion() 333 check.Assert(err, IsNil) 334 }