Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce generic OpenAPI entity cleanup for tests #348

Merged
merged 8 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
BUGS FIXED:
* Made IPAddress field for IPAddresses struct to array [#350](https://github.com/vmware/go-vcloud-director/pull/350)

IMPROVEMENTS:
* Introduce generic OpenAPI entity cleanup for tests [348](https://github.com/vmware/go-vcloud-director/pull/348)

## 2.10.0 (December 18, 2020)
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved

* Added functions to retrieve and use VCD version `client.GetVcdVersion`, `client.GetVcdShortVersion`, `client.GetVcdFullVersion`, `client.VersionEqualOrGreater` [#339](https://github.com/vmware/go-vcloud-director/pull/339)
Expand Down
3 changes: 1 addition & 2 deletions govcd/adminorg_ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@ func createDirectNetwork(vcd *TestVCD, check *C) string {
}
check.Assert(task.Task.HREF, Not(Equals), "")

AddToCleanupList(networkName,
"network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, check.TestName())
AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, check.TestName())

err = task.WaitInspectTaskCompletion(LogTask, 10)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion govcd/adminvdc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ package govcd

import (
"fmt"
"math"

"github.com/vmware/go-vcloud-director/v2/types/v56"
. "gopkg.in/check.v1"
"math"
)

// Tests org function GetVDCByName with the vdc specified
Expand Down
93 changes: 87 additions & 6 deletions govcd/api_vcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,11 @@ type TestVCD struct {
// Cleanup entity structure used by the tear-down procedure
// at the end of the tests to remove leftover entities
type CleanupEntity struct {
Name string
EntityType string
Parent string
CreatedBy string
Name string
EntityType string
Parent string
CreatedBy string
OpenApiEndpoint string
}

// CleanupInfo is the data used to persist an entity list in a file
Expand Down Expand Up @@ -334,7 +335,7 @@ func writeCleanupList(cleanupList []CleanupEntity) error {
return file.Close()
}

// Adds an entity to the cleanup list.
// AddToCleanupList adds an entity to the cleanup list.
// To be called by all tests when a new entity has been created, before
// running any other operation.
// Items in the list will be deleted at the end of the tests if they still exist.
Expand All @@ -352,7 +353,7 @@ func AddToCleanupList(name, entityType, parent, createdBy string) {
}
}

// Prepend an entity to the cleanup list.
// PrependToCleanupList prepends an entity to the cleanup list.
// To be called by all tests when a new entity has been created, before
// running any other operation.
// Items in the list will be deleted at the end of the tests if they still exist.
Expand All @@ -370,6 +371,39 @@ func PrependToCleanupList(name, entityType, parent, createdBy string) {
}
}

// AddToCleanupListOpenApi adds an OpenAPI entity OpenApi objects `entityType=OpenApiEntity` and `openApiEndpoint`should
// be set in format "types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks + ID"
func AddToCleanupListOpenApi(name, createdBy, openApiEndpoint string) {
for _, item := range cleanupEntityList {
// avoid adding the same item twice
if item.OpenApiEndpoint == openApiEndpoint {
return
}
}
cleanupEntityList = append(cleanupEntityList, CleanupEntity{Name: name, EntityType: "OpenApiEntity", CreatedBy: createdBy, OpenApiEndpoint: openApiEndpoint})
err := writeCleanupList(cleanupEntityList)
if err != nil {
fmt.Printf("################ error writing cleanup list %s\n", err)
}
}

// PrependToCleanupListOpenApi prepends an OpenAPI entity OpenApi objects `entityType=OpenApiEntity` and
// `openApiEndpoint`should be set in format "types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks + ID"
//lint:ignore U1000 Not yet used
func PrependToCleanupListOpenApi(name, createdBy, openApiEndpoint string) {
for _, item := range cleanupEntityList {
// avoid adding the same item twice
if item.OpenApiEndpoint == openApiEndpoint {
return
}
}
cleanupEntityList = append([]CleanupEntity{{Name: name, EntityType: "OpenApiEntity", CreatedBy: createdBy, OpenApiEndpoint: openApiEndpoint}}, cleanupEntityList...)
err := writeCleanupList(cleanupEntityList)
if err != nil {
fmt.Printf("################ error writing cleanup list %s\n", err)
}
}

// Users use the environmental variable GOVCD_CONFIG as
// a config file for testing. Otherwise the default is govcd_test_config.yaml
// in the current directory. Throws an error if it cannot find your
Expand Down Expand Up @@ -695,6 +729,53 @@ func (vcd *TestVCD) removeLeftoverEntities(entity CleanupEntity) {
// For this reason, the [ERROR] messages won't be followed by a program termination
vcd.infoCleanup(introMsg, entity.EntityType, entity.Name, entity.CreatedBy)
switch entity.EntityType {

// openApiEntity can be used to delete any OpenAPI entity due to the API being uniform and allowing the same
// low level OpenApiDeleteItem()
case "OpenApiEntity":

// entity.OpenApiEndpoint contains "endpoint/{ID}"
// (in format types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks + ID) but
// to lookup used API version this ID must not be present therefore below we remove suffix ID.
// This is done by splitting whole path by "/" and rebuilding path again without last element in slice (which is
// expected to be the ID)
endpointSlice := strings.Split(entity.OpenApiEndpoint, "/")
endpoint := strings.Join(endpointSlice[:len(endpointSlice)-1], "/") + "/"
apiVersion, _ := vcd.client.Client.checkOpenApiEndpointCompatibility(endpoint)

// Build UP complete endpoint address
urlRef, err := vcd.client.Client.OpenApiBuildEndpoint(entity.OpenApiEndpoint)
if err != nil {
vcd.infoCleanup(notDeletedMsg, entity.EntityType, entity.Name, err)
return
}

// Validate if the resource still exists
err = vcd.client.Client.OpenApiGetItem(apiVersion, urlRef, nil, nil)
if ContainsNotFound(err) {
vcd.infoCleanup(notFoundMsg, entity.EntityType, entity.Name)
return
}

if err != nil {
vcd.infoCleanup(notDeletedMsg, entity.EntityType, entity.Name, err)
return
}

if err != nil {
vcd.infoCleanup(notDeletedMsg, entity.EntityType, entity.Name, err)
return
}

// Attempt to use supplied path in entity.Parent for element deletion
err = vcd.client.Client.OpenApiDeleteItem(apiVersion, urlRef, nil)
if err != nil {
vcd.infoCleanup(notDeletedMsg, entity.EntityType, entity.Name, err)
return
}

vcd.infoCleanup(removedMsg, entity.EntityType, entity.Name, entity.CreatedBy)

case "vapp":
vapp, err := vcd.vdc.GetVAppByName(entity.Name, true)
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions govcd/external_network_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ func (vcd *TestVCD) testCreateExternalNetworkV2Nsxt(check *C, nsxtTier0Router, b
check.Assert(err, IsNil)

// Create network and test CRUD capabilities
netNsxt := testExternalNetworkV2(backingType, tier0RouterVrf.NsxtTier0Router.ID, nsxtManagerId)
netNsxt := testExternalNetworkV2(check.TestName(), backingType, tier0RouterVrf.NsxtTier0Router.ID, nsxtManagerId)
createdNet, err := CreateExternalNetworkV2(vcd.client, netNsxt)
check.Assert(err, IsNil)

createdNet.ExternalNetwork.Name = "changed_name"
// Use generic "OpenApiEntity" resource cleanup type
openApiEndpoint := endpoint + createdNet.ExternalNetwork.ID
AddToCleanupListOpenApi(createdNet.ExternalNetwork.Name, check.TestName(), openApiEndpoint)

createdNet.ExternalNetwork.Name = check.TestName() + "changed_name"
updatedNet, err := createdNet.Update()
check.Assert(err, IsNil)
check.Assert(updatedNet.ExternalNetwork.Name, Equals, createdNet.ExternalNetwork.Name)
Expand Down Expand Up @@ -109,12 +113,16 @@ func (vcd *TestVCD) Test_CreateExternalNetworkV2Nsxv(check *C) {
vcUrn, err := BuildUrnWithUuid("urn:vcloud:vimserver:", vcuuid)
check.Assert(err, IsNil)

neT := testExternalNetworkV2(vcd.config.VCD.ExternalNetworkPortGroupType, pgs[0].MoRef, vcUrn)
neT := testExternalNetworkV2(check.TestName(), vcd.config.VCD.ExternalNetworkPortGroupType, pgs[0].MoRef, vcUrn)

r, err := CreateExternalNetworkV2(vcd.client, neT)
check.Assert(err, IsNil)

r.ExternalNetwork.Name = "changed_name"
// Use generic "OpenApiEntity" resource cleanup type
openApiEndpoint := endpoint + r.ExternalNetwork.ID
AddToCleanupListOpenApi(r.ExternalNetwork.Name, check.TestName(), openApiEndpoint)

r.ExternalNetwork.Name = check.TestName() + "changed_name"
updatedNet, err := r.Update()
check.Assert(err, IsNil)
check.Assert(updatedNet.ExternalNetwork.Name, Equals, r.ExternalNetwork.Name)
Expand All @@ -123,10 +131,10 @@ func (vcd *TestVCD) Test_CreateExternalNetworkV2Nsxv(check *C) {
check.Assert(err, IsNil)
}

func testExternalNetworkV2(backingType, backingId, NetworkProviderId string) *types.ExternalNetworkV2 {
func testExternalNetworkV2(name, backingType, backingId, NetworkProviderId string) *types.ExternalNetworkV2 {
neT := &types.ExternalNetworkV2{
ID: "",
Name: "testNet",
Name: name,
Description: "",
Subnets: types.ExternalNetworkV2Subnets{[]types.ExternalNetworkV2Subnet{
{
Expand Down
1 change: 1 addition & 0 deletions govcd/lbvirtualserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package govcd

import (
"fmt"

"github.com/vmware/go-vcloud-director/v2/types/v56"
. "gopkg.in/check.v1"
)
Expand Down
1 change: 1 addition & 0 deletions govcd/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package govcd

import (
"fmt"

"github.com/vmware/go-vcloud-director/v2/types/v56"

. "gopkg.in/check.v1"
Expand Down
4 changes: 2 additions & 2 deletions govcd/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"time"

"github.com/araddon/dateparse"
"github.com/vmware/go-vcloud-director/v2/types/v56"

. "gopkg.in/check.v1"
Expand Down Expand Up @@ -295,8 +296,7 @@ func getAuditTrailTimestampWithElements(elementCount int, check *C, vcd *TestVCD
singleElement = onePageAuditTrail[(elementCount - 1)]
}

timeFormat, err := time.Parse("2006-01-02T15:04:05.000+0000", singleElement.Timestamp)
check.Assert(err, IsNil)
timeFormat := dateparse.MustParse(singleElement.Timestamp)

return timeFormat.Format(types.FiqlQueryTimestampFormat)
}
28 changes: 6 additions & 22 deletions govcd/orgvdcnetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ func (vcd *TestVCD) testCreateUpdateOrgVdcNetworkRouted(check *C, ipSubnet strin
fmt.Printf("error creating Network <%s>: %s\n", networkName, err)
}
check.Assert(err, IsNil)
AddToCleanupList(networkName,
"network",
vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name,
"Test_CreateOrgVdcNetworkRouted")
AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkRouted")
network, err := vcd.vdc.GetOrgVdcNetworkByName(networkName, true)
check.Assert(err, IsNil)
check.Assert(network, NotNil)
Expand Down Expand Up @@ -171,10 +168,7 @@ func (vcd *TestVCD) testCreateUpdateOrgVdcNetworkRouted(check *C, ipSubnet strin

err = network.Update()
check.Assert(err, IsNil)
AddToCleanupList(updatedNetworkName,
"network",
vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name,
"Test_CreateOrgVdcNetworkRouted")
AddToCleanupList(updatedNetworkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkRouted")

network, err = vcd.vdc.GetOrgVdcNetworkById(networkId, true)
check.Assert(err, IsNil)
Expand Down Expand Up @@ -282,10 +276,7 @@ func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkIso(check *C) {
fmt.Printf("error creating Network <%s>: %s\n", networkName, err)
}
check.Assert(err, IsNil)
AddToCleanupList(networkName,
"network",
vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name,
"Test_CreateOrgVdcNetworkIso")
AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkIso")

network, err := vcd.vdc.GetOrgVdcNetworkByName(networkName, true)
check.Assert(err, IsNil)
Expand All @@ -310,10 +301,7 @@ func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkIso(check *C) {
network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].EndAddress = updatedEndAddress
err = network.Update()
check.Assert(err, IsNil)
AddToCleanupList(updatedNetworkName,
"network",
vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name,
"Test_CreateOrgVdcNetworkIso")
AddToCleanupList(updatedNetworkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkIso")

network, err = vcd.vdc.GetOrgVdcNetworkById(networkId, true)
check.Assert(err, IsNil)
Expand Down Expand Up @@ -381,9 +369,7 @@ func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkDirect(check *C) {
}
check.Assert(task.Task.HREF, Not(Equals), "")

AddToCleanupList(networkName,
"network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name,
"Test_CreateOrgVdcNetworkDirect")
AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkDirect")

// err = task.WaitTaskCompletion()
err = task.WaitInspectTaskCompletion(LogTask, 10)
Expand Down Expand Up @@ -411,9 +397,7 @@ func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkDirect(check *C) {
err = newNetwork.Update()
check.Assert(err, IsNil)

AddToCleanupList(updatedNetworkName,
"network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name,
"Test_CreateOrgVdcNetworkDirect")
AddToCleanupList(updatedNetworkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkDirect")

// Check the values of the updated entities. The new values should be available
// immediately.
Expand Down
4 changes: 1 addition & 3 deletions govcd/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,7 @@ func (vcd *TestVCD) Test_QueryOrgVdcNetworkByNameWithSpace(check *C) {
}
check.Assert(task.Task.HREF, Not(Equals), "")

AddToCleanupList(networkName,
"network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name,
"Test_CreateOrgVdcNetworkDirect")
AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkDirect")

// err = task.WaitTaskCompletion()
err = task.WaitInspectTaskCompletion(LogTask, 10)
Expand Down
5 changes: 3 additions & 2 deletions govcd/vdccomputepolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ package govcd

import (
"fmt"
"github.com/vmware/go-vcloud-director/v2/types/v56"
. "gopkg.in/check.v1"
"net/url"
"strings"

"github.com/vmware/go-vcloud-director/v2/types/v56"
. "gopkg.in/check.v1"
)

func (vcd *TestVCD) Test_VdcComputePolicies(check *C) {
Expand Down
3 changes: 1 addition & 2 deletions govcd/vm_dhcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ func makeOrgVdcNetworkWithDhcp(vcd *TestVCD, check *C, edgeGateway *EdgeGateway)
fmt.Printf("error creating Network <%s>: %s\n", TestCreateOrgVdcNetworkDhcp, err)
}
check.Assert(err, IsNil)
AddToCleanupList(TestCreateOrgVdcNetworkDhcp, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name,
"TestCreateOrgVdcNetworkDhcp")
AddToCleanupList(TestCreateOrgVdcNetworkDhcp, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "TestCreateOrgVdcNetworkDhcp")
network, err := vcd.vdc.GetOrgVdcNetworkByName(TestCreateOrgVdcNetworkDhcp, true)
check.Assert(err, IsNil)

Expand Down