Skip to content

Commit 6fb0b89

Browse files
committed
e2e: Fix deletion of test security group
We were trying to delete the security group in a DeferCleanup() called from the test itself. This will execute before the cluster is deleted, so it can't work as the cluster machines are still using the security group. We need to delete the security group after the cluster has been deleted. In making this change, we also add a context argument to postClusterCleanup and propagate that to the other users of this mechanism.
1 parent afe4a3a commit 6fb0b89

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

test/e2e/shared/openstack.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -477,35 +477,35 @@ func DumpOpenStackPorts(e2eCtx *E2EContext, filter ports.ListOpts) ([]ports.Port
477477
}
478478

479479
// CreateOpenStackSecurityGroup creates a security group to be consumed by a worker node.
480-
func CreateOpenStackSecurityGroup(e2eCtx *E2EContext, securityGroupName, description string) error {
480+
func CreateOpenStackSecurityGroup(ctx context.Context, e2eCtx *E2EContext, securityGroupName, description string) (func(context.Context), error) {
481481
providerClient, clientOpts, _, err := GetTenantProviderClient(e2eCtx)
482482
if err != nil {
483-
return fmt.Errorf("error creating provider client: %s", err)
483+
return nil, fmt.Errorf("error creating provider client: %s", err)
484484
}
485485

486486
networkClient, err := openstack.NewNetworkV2(providerClient, gophercloud.EndpointOpts{
487487
Region: clientOpts.RegionName,
488488
})
489489
if err != nil {
490-
return fmt.Errorf("error creating network client: %s", err)
490+
return nil, fmt.Errorf("error creating network client: %s", err)
491491
}
492492

493493
createOpts := groups.CreateOpts{
494494
Name: securityGroupName,
495495
Description: description,
496496
}
497497

498-
group, err := groups.Create(context.TODO(), networkClient, createOpts).Extract()
498+
group, err := groups.Create(ctx, networkClient, createOpts).Extract()
499499
if err != nil {
500-
return err
500+
return nil, err
501501
}
502-
// Ensure the group is deleted after the test
503-
DeferCleanup(func() error {
502+
503+
cleanup := func(ctx context.Context) {
504504
By(fmt.Sprintf("Deleting test security group %s(%s)", group.Name, group.ID))
505-
return groups.Delete(context.TODO(), networkClient, group.ID).ExtractErr()
506-
})
505+
Expect(groups.Delete(ctx, networkClient, group.ID).ExtractErr()).To(Succeed(), "Delete test security group")
506+
}
507507

508-
return nil
508+
return cleanup, nil
509509
}
510510

511511
// DumpOpenStackTrunks trunks for a given port.
@@ -817,7 +817,7 @@ func CreateOpenStackNetwork(e2eCtx *E2EContext, name, cidr string) (*networks.Ne
817817
return net, nil
818818
}
819819

820-
func DeleteOpenStackNetwork(e2eCtx *E2EContext, id string) error {
820+
func DeleteOpenStackNetwork(ctx context.Context, e2eCtx *E2EContext, id string) error {
821821
providerClient, clientOpts, _, err := GetTenantProviderClient(e2eCtx)
822822
if err != nil {
823823
_, _ = fmt.Fprintf(GinkgoWriter, "error creating provider client: %s\n", err)
@@ -831,7 +831,7 @@ func DeleteOpenStackNetwork(e2eCtx *E2EContext, id string) error {
831831
return fmt.Errorf("error creating network client: %s", err)
832832
}
833833

834-
return networks.Delete(context.TODO(), networkClient, id).ExtractErr()
834+
return networks.Delete(ctx, networkClient, id).ExtractErr()
835835
}
836836

837837
func GetOpenStackVolume(e2eCtx *E2EContext, name string) (*volumes.Volume, error) {

test/e2e/suites/e2e/e2e_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {
6666
ctx context.Context
6767

6868
// Cleanup functions which cannot run until after the cluster has been deleted
69-
postClusterCleanup []func()
69+
postClusterCleanup []func(context.Context)
7070
)
7171

7272
BeforeEach(func() {
@@ -406,11 +406,10 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {
406406
md3Name := clusterName + "-md-3"
407407
testSecurityGroupName := "testSecGroup"
408408
// create required test security group
409-
Eventually(func() int {
410-
err := shared.CreateOpenStackSecurityGroup(e2eCtx, testSecurityGroupName, "Test security group")
411-
Expect(err).To(BeNil())
412-
return 1
413-
}, e2eCtx.E2EConfig.GetIntervals(specName, "wait-worker-nodes")...).Should(Equal(1))
409+
var securityGroupCleanup func(ctx context.Context)
410+
securityGroupCleanup, err = shared.CreateOpenStackSecurityGroup(ctx, e2eCtx, testSecurityGroupName, "Test security group")
411+
Expect(err).To(BeNil())
412+
postClusterCleanup = append(postClusterCleanup, securityGroupCleanup)
414413

415414
customPortOptions := &[]infrav1.PortOpts{
416415
{
@@ -496,17 +495,17 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {
496495

497496
extraNet1, err = shared.CreateOpenStackNetwork(e2eCtx, fmt.Sprintf("%s-extraNet1", namespace.Name), "10.14.0.0/24")
498497
Expect(err).NotTo(HaveOccurred())
499-
postClusterCleanup = append(postClusterCleanup, func() {
498+
postClusterCleanup = append(postClusterCleanup, func(ctx context.Context) {
500499
shared.Logf("Deleting additional network %s", extraNet1.Name)
501-
err := shared.DeleteOpenStackNetwork(e2eCtx, extraNet1.ID)
500+
err := shared.DeleteOpenStackNetwork(ctx, e2eCtx, extraNet1.ID)
502501
Expect(err).NotTo(HaveOccurred())
503502
})
504503

505504
extraNet2, err = shared.CreateOpenStackNetwork(e2eCtx, fmt.Sprintf("%s-extraNet2", namespace.Name), "10.14.1.0/24")
506505
Expect(err).NotTo(HaveOccurred())
507-
postClusterCleanup = append(postClusterCleanup, func() {
506+
postClusterCleanup = append(postClusterCleanup, func(ctx context.Context) {
508507
shared.Logf("Deleting additional network %s", extraNet2.Name)
509-
err := shared.DeleteOpenStackNetwork(e2eCtx, extraNet2.ID)
508+
err := shared.DeleteOpenStackNetwork(ctx, e2eCtx, extraNet2.ID)
510509
Expect(err).NotTo(HaveOccurred())
511510
})
512511

@@ -806,15 +805,15 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {
806805
})
807806
})
808807

809-
AfterEach(func() {
808+
AfterEach(func(ctx context.Context) {
810809
shared.Logf("Attempting to collect logs for cluster %q in namespace %q", clusterResources.Cluster.Name, namespace.Name)
811810
e2eCtx.Environment.BootstrapClusterProxy.CollectWorkloadClusterLogs(ctx, namespace.Name, clusterResources.Cluster.Name, filepath.Join(e2eCtx.Settings.ArtifactFolder, "clusters", e2eCtx.Environment.BootstrapClusterProxy.GetName(), namespace.Name))
812811
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
813812
shared.DumpSpecResourcesAndCleanup(ctx, specName, namespace, e2eCtx)
814813

815814
// Cleanup resources which can't be cleaned up until the cluster has been deleted
816815
for _, cleanup := range postClusterCleanup {
817-
cleanup()
816+
cleanup(ctx)
818817
}
819818
})
820819
})

0 commit comments

Comments
 (0)