@@ -28,6 +28,7 @@ import (
28
28
"sigs.k8s.io/controller-runtime/pkg/client"
29
29
30
30
netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
31
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
31
32
)
32
33
33
34
var _ = Describe ("IpAddress Controller" , Ordered , func () {
@@ -51,6 +52,7 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
51
52
cr * netboxv1.IpAddress , // our CR as typed object
52
53
IpamMocksIpAddress []func (* mock_interfaces.MockIpamInterface , chan error ),
53
54
TenancyMocks []func (* mock_interfaces.MockTenancyInterface , chan error ),
55
+ restorationHashMismatch bool , // To check for deletion if restoration hash does not match
54
56
expectedConditionReady bool , // Expected state of the ConditionReady condition
55
57
expectedCRStatus netboxv1.IpAddressStatus , // Expected status of the CR
56
58
) {
@@ -81,31 +83,40 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
81
83
By ("Creating IpAddress CR" )
82
84
Eventually (k8sClient .Create (ctx , cr ), timeout , interval ).Should (Succeed ())
83
85
84
- // check that reconcile loop did run a least once by checking that conditions are set
85
86
createdCR := & netboxv1.IpAddress {}
86
- Eventually (func () bool {
87
- err := k8sClient .Get (ctx , types.NamespacedName {Name : cr .GetName (), Namespace : cr .GetNamespace ()}, createdCR )
88
- return err == nil && len (createdCR .Status .Conditions ) > 0
89
- }, timeout , interval ).Should (BeTrue ())
90
-
91
- // Now check if conditions are set as expected
92
- Eventually (func () bool {
93
- err := k8sClient .Get (ctx , types.NamespacedName {Name : cr .GetName (), Namespace : cr .GetNamespace ()}, createdCR )
94
- return err == nil &&
95
- apismeta .IsStatusConditionTrue (createdCR .Status .Conditions , netboxv1 .ConditionIpaddressReadyTrue .Type ) == expectedConditionReady
96
- }, timeout , interval ).Should (BeTrue ())
97
-
98
- // Check that the expected ip address is present in the status
99
- Expect (createdCR .Status .IpAddressId ).To (Equal (expectedCRStatus .IpAddressId ))
100
-
101
- // Cleanup the netbox resources
102
- Expect (k8sClient .Delete (ctx , createdCR )).Should (Succeed ())
103
-
104
- // Wait until the resource is deleted to make sure that it will not interfere with the next test case
105
- Eventually (func () bool {
106
- err := k8sClient .Get (ctx , types.NamespacedName {Name : cr .GetName (), Namespace : cr .GetNamespace ()}, createdCR )
107
- return err != client .IgnoreNotFound (err )
108
- }, timeout , interval ).Should (BeTrue ())
87
+
88
+ if restorationHashMismatch {
89
+ Eventually (func () bool {
90
+ err := k8sClient .Get (ctx , types.NamespacedName {Name : cr .GetName (), Namespace : cr .GetNamespace ()}, createdCR )
91
+ return apierrors .IsNotFound (err )
92
+ }, timeout , interval ).Should (BeTrue ())
93
+ } else {
94
+
95
+ // check that reconcile loop did run a least once by checking that conditions are set
96
+ Eventually (func () bool {
97
+ err := k8sClient .Get (ctx , types.NamespacedName {Name : cr .GetName (), Namespace : cr .GetNamespace ()}, createdCR )
98
+ return err == nil && len (createdCR .Status .Conditions ) > 0
99
+ }, timeout , interval ).Should (BeTrue ())
100
+
101
+ // Now check if conditions are set as expected
102
+ Eventually (func () bool {
103
+ err := k8sClient .Get (ctx , types.NamespacedName {Name : cr .GetName (), Namespace : cr .GetNamespace ()}, createdCR )
104
+ return err == nil &&
105
+ apismeta .IsStatusConditionTrue (createdCR .Status .Conditions , netboxv1 .ConditionIpaddressReadyTrue .Type ) == expectedConditionReady
106
+ }, timeout , interval ).Should (BeTrue ())
107
+
108
+ // Check that the expected ip address is present in the status
109
+ Expect (createdCR .Status .IpAddressId ).To (Equal (expectedCRStatus .IpAddressId ))
110
+
111
+ // Cleanup the netbox resources
112
+ Expect (k8sClient .Delete (ctx , createdCR )).Should (Succeed ())
113
+
114
+ // Wait until the resource is deleted to make sure that it will not interfere with the next test case
115
+ Eventually (func () bool {
116
+ err := k8sClient .Get (ctx , types.NamespacedName {Name : cr .GetName (), Namespace : cr .GetNamespace ()}, createdCR )
117
+ return err != client .IgnoreNotFound (err )
118
+ }, timeout , interval ).Should (BeTrue ())
119
+ }
109
120
110
121
catchCtxCancel ()
111
122
},
@@ -119,7 +130,7 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
119
130
[]func (* mock_interfaces.MockTenancyInterface , chan error ){
120
131
mockTenancyTenancyTenantsList ,
121
132
},
122
- true , ExpectedIpAddressStatus ),
133
+ false , true , ExpectedIpAddressStatus ),
123
134
Entry ("Create IpAddress CR, ip address already reserved in NetBox, preserved in netbox, " ,
124
135
defaultIpAddressCR (true ),
125
136
[]func (* mock_interfaces.MockIpamInterface , chan error ){
@@ -129,7 +140,7 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
129
140
[]func (* mock_interfaces.MockTenancyInterface , chan error ){
130
141
mockTenancyTenancyTenantsList ,
131
142
},
132
- true , ExpectedIpAddressStatus ),
143
+ false , true , ExpectedIpAddressStatus ),
133
144
Entry ("Create IpAddress CR, ip address already reserved in NetBox" ,
134
145
defaultIpAddressCR (false ),
135
146
[]func (* mock_interfaces.MockIpamInterface , chan error ){
@@ -140,7 +151,7 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
140
151
[]func (* mock_interfaces.MockTenancyInterface , chan error ){
141
152
mockTenancyTenancyTenantsList ,
142
153
},
143
- true , ExpectedIpAddressStatus ),
154
+ false , true , ExpectedIpAddressStatus ),
144
155
Entry ("Create IpAddress CR, reserve or update failure" ,
145
156
defaultIpAddressCR (false ),
146
157
[]func (* mock_interfaces.MockIpamInterface , chan error ){
@@ -151,6 +162,15 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
151
162
[]func (* mock_interfaces.MockTenancyInterface , chan error ){
152
163
mockTenancyTenancyTenantsList ,
153
164
},
154
- false , ExpectedIpAddressFailedStatus ),
165
+ false , false , ExpectedIpAddressFailedStatus ),
166
+ Entry ("Create IpAddress CR, restoration hash mismatch" ,
167
+ defaultIpAddressCreatedByClaim (true ),
168
+ []func (* mock_interfaces.MockIpamInterface , chan error ){
169
+ mockIpAddressListWithHashFilterMismatch ,
170
+ },
171
+ []func (* mock_interfaces.MockTenancyInterface , chan error ){
172
+ mockTenancyTenancyTenantsList ,
173
+ },
174
+ true , false , nil ),
155
175
)
156
176
})
0 commit comments