@@ -23,15 +23,29 @@ import (
23
23
"os"
24
24
"testing"
25
25
26
+ "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
27
+ "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
28
+ "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
29
+ "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
30
+ "github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
26
31
. "github.com/onsi/ginkgo/v2"
27
32
. "github.com/onsi/gomega"
28
33
"k8s.io/klog/v2"
34
+ "k8s.io/utils/ptr"
29
35
ctrl "sigs.k8s.io/controller-runtime"
30
36
31
37
"sigs.k8s.io/cluster-api-provider-openstack/test/e2e/shared"
32
38
)
33
39
34
- var e2eCtx * shared.E2EContext
40
+ var (
41
+ e2eCtx * shared.E2EContext
42
+ initialServers []servers.Server
43
+ initialNetworks []networks.Network
44
+ initialSecurityGroups []groups.SecGroup
45
+ initialLoadBalancers []loadbalancers.LoadBalancer
46
+ initialVolumes []volumes.Volume
47
+ err error
48
+ )
35
49
36
50
func init () {
37
51
e2eCtx = shared .NewE2EContext ()
@@ -55,10 +69,60 @@ var _ = SynchronizedBeforeSuite(func() []byte {
55
69
return data
56
70
}, func (data []byte ) {
57
71
shared .AllNodesBeforeSuite (e2eCtx , data )
72
+ initialServers , err = shared .DumpOpenStackServers (e2eCtx , servers.ListOpts {})
73
+ Expect (err ).NotTo (HaveOccurred ())
74
+ initialNetworks , err = shared .DumpOpenStackNetworks (e2eCtx , networks.ListOpts {})
75
+ Expect (err ).NotTo (HaveOccurred ())
76
+ initialSecurityGroups , err = shared .DumpOpenStackSecurityGroups (e2eCtx , groups.ListOpts {})
77
+ Expect (err ).NotTo (HaveOccurred ())
78
+ initialLoadBalancers , err = shared .DumpOpenStackLoadBalancers (e2eCtx , loadbalancers.ListOpts {})
79
+ Expect (err ).NotTo (HaveOccurred ())
80
+ initialVolumes , err = shared .DumpOpenStackVolumes (e2eCtx , volumes.ListOpts {})
81
+ Expect (err ).NotTo (HaveOccurred ())
58
82
})
59
83
84
+ // CheckResourceCleanup checks if all resources created during the test are cleaned up by comparing the resources
85
+ // before and after the test.
86
+ // The function f is used to list the resources of type T, whose list opts is of type L.
87
+ // The list of resources is then compared to the initialResources, using the ConsistOfIDs custom matcher.
88
+ func CheckResourceCleanup [T any , L any ](f func (* shared.E2EContext , L ) ([]T , error ), l L , initialResources []T ) * string {
89
+ endResources , err := f (e2eCtx , l )
90
+ if err != nil {
91
+ return ptr .To (err .Error ())
92
+ }
93
+
94
+ matcher := ConsistOfIDs (initialResources )
95
+ success , err := matcher .Match (endResources )
96
+ if err != nil {
97
+ return ptr .To (err .Error ())
98
+ }
99
+ if ! success {
100
+ return ptr .To (matcher .FailureMessage (endResources ))
101
+ }
102
+
103
+ return nil
104
+ }
105
+
60
106
var _ = SynchronizedAfterSuite (func () {
61
107
shared .AllNodesAfterSuite (e2eCtx )
62
108
}, func () {
109
+ failed := false
110
+ for _ , error := range []* string {
111
+ CheckResourceCleanup (shared .DumpOpenStackServers , servers.ListOpts {}, initialServers ),
112
+ CheckResourceCleanup (shared .DumpOpenStackNetworks , networks.ListOpts {}, initialNetworks ),
113
+ CheckResourceCleanup (shared .DumpOpenStackSecurityGroups , groups.ListOpts {}, initialSecurityGroups ),
114
+ CheckResourceCleanup (shared .DumpOpenStackLoadBalancers , loadbalancers.ListOpts {}, initialLoadBalancers ),
115
+ CheckResourceCleanup (shared .DumpOpenStackVolumes , volumes.ListOpts {}, initialVolumes ),
116
+ } {
117
+ if error != nil {
118
+ GinkgoWriter .Println (* error )
119
+ failed = true
120
+ }
121
+ }
122
+
63
123
shared .Node1AfterSuite (e2eCtx )
124
+
125
+ if failed {
126
+ Fail ("Not all resources were cleaned up" )
127
+ }
64
128
})
0 commit comments