@@ -19,12 +19,12 @@ package envtest
19
19
import (
20
20
"context"
21
21
"path/filepath"
22
-
23
22
"time"
24
23
25
24
. "github.com/onsi/ginkgo"
26
25
. "github.com/onsi/gomega"
27
26
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
27
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
28
28
"k8s.io/apimachinery/pkg/runtime"
29
29
"k8s.io/apimachinery/pkg/types"
30
30
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -52,6 +52,17 @@ var _ = Describe("Test", func() {
52
52
// Cleanup CRDs
53
53
AfterEach (func (done Done ) {
54
54
for _ , crd := range crds {
55
+ // Delete only if CRD exists.
56
+ crdObjectKey := client.ObjectKey {
57
+ Name : crd .Name ,
58
+ }
59
+ var placeholder v1beta1.CustomResourceDefinition
60
+ err := c .Get (context .TODO (), crdObjectKey , & placeholder )
61
+ if err != nil && apierrors .IsNotFound (err ) {
62
+ // CRD doesn't need to be deleted.
63
+ continue
64
+ }
65
+ Expect (err ).NotTo (HaveOccurred ())
55
66
Expect (c .Delete (context .TODO (), crd )).To (Succeed ())
56
67
}
57
68
close (done )
@@ -216,4 +227,126 @@ var _ = Describe("Test", func() {
216
227
close (done )
217
228
}, 5 )
218
229
})
230
+
231
+ Describe ("UninstallCRDs" , func () {
232
+ It ("should uninstall the CRDs from the cluster" , func (done Done ) {
233
+
234
+ crds , err = InstallCRDs (env .Config , CRDInstallOptions {
235
+ Paths : []string {filepath .Join ("." , "testdata" )},
236
+ })
237
+ Expect (err ).NotTo (HaveOccurred ())
238
+
239
+ // Expect to find the CRDs
240
+
241
+ crd := & v1beta1.CustomResourceDefinition {}
242
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "foos.bar.example.com" }, crd )
243
+ Expect (err ).NotTo (HaveOccurred ())
244
+ Expect (crd .Spec .Names .Kind ).To (Equal ("Foo" ))
245
+
246
+ crd = & v1beta1.CustomResourceDefinition {}
247
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "bazs.qux.example.com" }, crd )
248
+ Expect (err ).NotTo (HaveOccurred ())
249
+ Expect (crd .Spec .Names .Kind ).To (Equal ("Baz" ))
250
+
251
+ crd = & v1beta1.CustomResourceDefinition {}
252
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "captains.crew.example.com" }, crd )
253
+ Expect (err ).NotTo (HaveOccurred ())
254
+ Expect (crd .Spec .Names .Kind ).To (Equal ("Captain" ))
255
+
256
+ crd = & v1beta1.CustomResourceDefinition {}
257
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "firstmates.crew.example.com" }, crd )
258
+ Expect (err ).NotTo (HaveOccurred ())
259
+ Expect (crd .Spec .Names .Kind ).To (Equal ("FirstMate" ))
260
+
261
+ crd = & v1beta1.CustomResourceDefinition {}
262
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "drivers.crew.example.com" }, crd )
263
+ Expect (err ).NotTo (HaveOccurred ())
264
+ Expect (crd .Spec .Names .Kind ).To (Equal ("Driver" ))
265
+
266
+ err = WaitForCRDs (env .Config , []* v1beta1.CustomResourceDefinition {
267
+ {
268
+ Spec : v1beta1.CustomResourceDefinitionSpec {
269
+ Group : "qux.example.com" ,
270
+ Version : "v1beta1" ,
271
+ Names : v1beta1.CustomResourceDefinitionNames {
272
+ Plural : "bazs" ,
273
+ }},
274
+ },
275
+ {
276
+ Spec : v1beta1.CustomResourceDefinitionSpec {
277
+ Group : "bar.example.com" ,
278
+ Version : "v1beta1" ,
279
+ Names : v1beta1.CustomResourceDefinitionNames {
280
+ Plural : "foos" ,
281
+ }},
282
+ },
283
+ {
284
+ Spec : v1beta1.CustomResourceDefinitionSpec {
285
+ Group : "crew.example.com" ,
286
+ Version : "v1beta1" ,
287
+ Names : v1beta1.CustomResourceDefinitionNames {
288
+ Plural : "captains" ,
289
+ }},
290
+ },
291
+ {
292
+ Spec : v1beta1.CustomResourceDefinitionSpec {
293
+ Group : "crew.example.com" ,
294
+ Version : "v1beta1" ,
295
+ Names : v1beta1.CustomResourceDefinitionNames {
296
+ Plural : "firstmates" ,
297
+ }},
298
+ },
299
+ {
300
+ Spec : v1beta1.CustomResourceDefinitionSpec {
301
+ Group : "crew.example.com" ,
302
+ Names : v1beta1.CustomResourceDefinitionNames {
303
+ Plural : "drivers" ,
304
+ },
305
+ Versions : []v1beta1.CustomResourceDefinitionVersion {
306
+ {
307
+ Name : "v1" ,
308
+ Storage : true ,
309
+ Served : true ,
310
+ },
311
+ {
312
+ Name : "v2" ,
313
+ Storage : false ,
314
+ Served : true ,
315
+ },
316
+ }},
317
+ },
318
+ },
319
+ CRDInstallOptions {MaxTime : 50 * time .Millisecond , PollInterval : 15 * time .Millisecond },
320
+ )
321
+ Expect (err ).NotTo (HaveOccurred ())
322
+
323
+ err = UninstallCRDs (env .Config , CRDInstallOptions {
324
+ Paths : []string {filepath .Join ("." , "testdata" )},
325
+ })
326
+ Expect (err ).NotTo (HaveOccurred ())
327
+
328
+ // Expect to NOT find the CRDs
329
+
330
+ crds := []string {
331
+ "foos.bar.example.com" ,
332
+ "bazs.qux.example.com" ,
333
+ "captains.crew.example.com" ,
334
+ "firstmates.crew.example.com" ,
335
+ "drivers.crew.example.com" ,
336
+ }
337
+ placeholder := & v1beta1.CustomResourceDefinition {}
338
+ Eventually (func () bool {
339
+ for _ , crd := range crds {
340
+ err = c .Get (context .TODO (), types.NamespacedName {Name : crd }, placeholder )
341
+ notFound := err != nil && apierrors .IsNotFound (err )
342
+ if ! notFound {
343
+ return false
344
+ }
345
+ }
346
+ return true
347
+ }, 20 ).Should (BeTrue ())
348
+
349
+ close (done )
350
+ }, 30 )
351
+ })
219
352
})
0 commit comments