github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/nsxv_ipset_test.go (about) 1 //go:build nsxv || functional || ALL 2 3 /* 4 * Copyright 2019 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. 5 */ 6 7 package govcd 8 9 import ( 10 "github.com/vmware/go-vcloud-director/v2/types/v56" 11 . "gopkg.in/check.v1" 12 ) 13 14 // Test_NsxvIpSet performes the following actions: 15 // 1. Creates an IP set and checks it was created 16 // 2. Tries to create duplicate name IP set and expects error 17 // 3. Gets all IP sets and ensures there are some 18 // 4. Validates GetByName, GetByID, GetByNameOrID 19 // 5. Updates IP set and validates only one field is changed 20 // 6. Deletes created IP set by ID 21 // 7. Deletes created IP set by Name 22 func (vcd *TestVCD) Test_NsxvIpSet(check *C) { 23 24 if vcd.config.VCD.Org == "" { 25 check.Skip(check.TestName() + ": Org name not given") 26 return 27 } 28 if vcd.config.VCD.Vdc == "" { 29 check.Skip(check.TestName() + ": VDC name not given") 30 return 31 } 32 org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org) 33 check.Assert(err, IsNil) 34 check.Assert(org, NotNil) 35 36 vdc, err := org.GetVDCByName(vcd.config.VCD.Vdc, false) 37 check.Assert(err, IsNil) 38 check.Assert(vdc, NotNil) 39 40 // 1. Create 41 ipSetConfig := &types.EdgeIpSet{ 42 Name: "test-ipset", 43 Description: "test-ipset-description", 44 // The below example demonstrates a problem when vCD API shuffles the below list and the 45 // answer becomes ordered differently then it was submitted. If submitted as it was 46 // returned, next time it shuffles it again so one can not rely on order of list returned. 47 // IPAddresses: "192.168.200.1-192.168.200.24,192.168.200.1,192.168.200.1/24", 48 IPAddresses: "192.168.200.1/24", 49 InheritanceAllowed: addrOf(false), 50 } 51 52 createdIpSet, err := vdc.CreateNsxvIpSet(ipSetConfig) 53 check.Assert(err, IsNil) 54 check.Assert(createdIpSet.ID, Not(Equals), "") // Validate that ID was set 55 56 // Add to cleanup list 57 parentEntity := vcd.org.Org.Name + "|" + vcd.vdc.Vdc.Name 58 AddToCleanupList(createdIpSet.Name, "ipSet", parentEntity, check.TestName()) 59 60 // Check if the structure after creation is exactly the same, but with ID populated 61 ipSetConfig.ID = createdIpSet.ID 62 ipSetConfig.Revision = createdIpSet.Revision 63 createdIpSet.XMLName.Local = "" 64 check.Assert(createdIpSet, DeepEquals, ipSetConfig) 65 66 // 2. Try to create another IP set with the same name and expect error 67 _, err = vdc.CreateNsxvIpSet(ipSetConfig) 68 check.Assert(err, ErrorMatches, ".*Another object with same name.* already exists in the current scope.*") 69 70 // 3. Get all IP sets 71 ipSets, err := vdc.GetAllNsxvIpSets() 72 check.Assert(err, IsNil) 73 check.Assert(len(ipSets) > 0, Equals, true) 74 75 // 4. Get by Name, Id, NameOrId and check that all results are deeply equal 76 ipSetByName, err := vdc.GetNsxvIpSetByName(createdIpSet.Name) 77 check.Assert(err, IsNil) 78 ipSetById, err := vdc.GetNsxvIpSetById(createdIpSet.ID) 79 check.Assert(err, IsNil) 80 ipSetByName2, err := vdc.GetNsxvIpSetByNameOrId(createdIpSet.Name) 81 check.Assert(err, IsNil) 82 ipSetById2, err := vdc.GetNsxvIpSetByNameOrId(createdIpSet.ID) 83 check.Assert(err, IsNil) 84 check.Assert(ipSetByName, DeepEquals, ipSetById) 85 check.Assert(ipSetByName, DeepEquals, ipSetByName2) 86 check.Assert(ipSetByName, DeepEquals, ipSetById2) 87 88 // 5. Update IP set field 89 createdIpSet.InheritanceAllowed = addrOf(true) 90 updatedIpSet, err := vcd.vdc.UpdateNsxvIpSet(createdIpSet) 91 check.Assert(err, IsNil) 92 93 // Check that only this field was changed 94 updatedIpSet.XMLName.Local = "" 95 // Because revisions are auto-incremented - this must also be incremented so that it matches 96 createdIpSet.Revision = updatedIpSet.Revision 97 check.Assert(updatedIpSet, DeepEquals, createdIpSet) 98 99 // 6. Delete created IP set by Id 100 err = vdc.DeleteNsxvIpSetById(createdIpSet.ID) 101 check.Assert(err, IsNil) 102 103 // Verify that the IP set cannot be found by ID and by Name 104 _, err = vdc.GetNsxvIpSetById(createdIpSet.ID) 105 check.Assert(IsNotFound(err), Equals, true) 106 107 _, err = vdc.GetNsxvIpSetByName(createdIpSet.Name) 108 check.Assert(IsNotFound(err), Equals, true) 109 110 // 7. Create another IP set and try to delete by name 111 ipSet2, err := vdc.CreateNsxvIpSet(ipSetConfig) 112 check.Assert(err, IsNil) 113 114 err = vdc.DeleteNsxvIpSetByName(ipSet2.Name) 115 check.Assert(err, IsNil) 116 } 117 118 // testCreateIpSet creates an IP set with given name and returns it which is useful in other tests 119 // when an IP set is needed to validate inputs. 120 func testCreateIpSet(name string, vdc *Vdc) (*types.EdgeIpSet, error) { 121 ipSetConfig := &types.EdgeIpSet{ 122 Name: name, 123 Description: "test-ipset-description", 124 IPAddresses: "192.168.200.1/24", 125 InheritanceAllowed: addrOf(true), 126 } 127 128 return vdc.CreateNsxvIpSet(ipSetConfig) 129 }