@@ -4,7 +4,11 @@ import (
4
4
"testing"
5
5
6
6
. "github.com/onsi/gomega"
7
+ osconfigv1 "github.com/openshift/api/config/v1"
8
+ fakeos "github.com/openshift/client-go/config/clientset/versioned/fake"
7
9
"golang.org/x/net/context"
10
+ appsv1 "k8s.io/api/apps/v1"
11
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
8
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
13
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10
14
"k8s.io/apimachinery/pkg/runtime"
@@ -341,3 +345,160 @@ func TestBaremetalProvisionigConfig(t *testing.T) {
341
345
})
342
346
}
343
347
}
348
+
349
+ func TestCheckMetal3DeploymentOwned (t * testing.T ) {
350
+ kubeClient := fakekube .NewSimpleClientset (nil ... )
351
+ operatorConfig := newOperatorWithBaremetalConfig ()
352
+ client := kubeClient .AppsV1 ()
353
+
354
+ testCases := []struct {
355
+ testCase string
356
+ deployment * appsv1.Deployment
357
+ expected bool
358
+ expectedError bool
359
+ }{
360
+ {
361
+ testCase : "Only maoOwnedAnnotation" ,
362
+ deployment : & appsv1.Deployment {
363
+ TypeMeta : metav1.TypeMeta {
364
+ Kind : "Deployment" ,
365
+ APIVersion : "apps/v1" ,
366
+ },
367
+ ObjectMeta : metav1.ObjectMeta {
368
+ Name : "metal3" ,
369
+ Annotations : map [string ]string {
370
+ maoOwnedAnnotation : "" ,
371
+ },
372
+ },
373
+ },
374
+ expected : true ,
375
+ },
376
+ {
377
+ testCase : "Only cboOwnedAnnotation" ,
378
+ deployment : & appsv1.Deployment {
379
+ TypeMeta : metav1.TypeMeta {
380
+ Kind : "Deployment" ,
381
+ APIVersion : "apps/v1" ,
382
+ },
383
+ ObjectMeta : metav1.ObjectMeta {
384
+ Name : "metal3" ,
385
+ Annotations : map [string ]string {
386
+ cboOwnedAnnotation : "" ,
387
+ },
388
+ },
389
+ },
390
+ expected : false ,
391
+ },
392
+ {
393
+ testCase : "Both cboOwnedAnnotation and maoOwnedAnnotation" ,
394
+ deployment : & appsv1.Deployment {
395
+ TypeMeta : metav1.TypeMeta {
396
+ Kind : "Deployment" ,
397
+ APIVersion : "apps/v1" ,
398
+ },
399
+ ObjectMeta : metav1.ObjectMeta {
400
+ Name : "metal3" ,
401
+ Annotations : map [string ]string {
402
+ cboOwnedAnnotation : "" ,
403
+ maoOwnedAnnotation : "" ,
404
+ },
405
+ },
406
+ },
407
+ expected : false ,
408
+ },
409
+ {
410
+ testCase : "No cboOwnedAnnotation or maoOwnedAnnotation" ,
411
+ deployment : & appsv1.Deployment {
412
+ TypeMeta : metav1.TypeMeta {
413
+ Kind : "Deployment" ,
414
+ APIVersion : "apps/v1" ,
415
+ },
416
+ ObjectMeta : metav1.ObjectMeta {
417
+ Name : "metal3" ,
418
+ Annotations : map [string ]string {},
419
+ },
420
+ },
421
+ expected : true ,
422
+ },
423
+ }
424
+ for _ , tc := range testCases {
425
+ t .Run (string (tc .testCase ), func (t * testing.T ) {
426
+
427
+ _ , err := client .Deployments ("test-namespace" ).Create (context .Background (), tc .deployment , metav1.CreateOptions {})
428
+ if err != nil {
429
+ t .Fatalf ("Could not create metal3 test deployment.\n " )
430
+ }
431
+ maoOwned , err := checkMetal3DeploymentMAOOwned (client , operatorConfig )
432
+ if maoOwned != tc .expected {
433
+ t .Errorf ("Expected: %v, got: %v" , tc .expected , maoOwned )
434
+ }
435
+ if tc .expectedError != (err != nil ) {
436
+ t .Errorf ("ExpectedError: %v, got: %v" , tc .expectedError , err )
437
+ }
438
+ err = client .Deployments ("test-namespace" ).Delete (context .Background (), "metal3" , metav1.DeleteOptions {})
439
+ if err != nil {
440
+ t .Errorf ("Could not delete metal3 test deployment.\n " )
441
+ }
442
+ })
443
+ }
444
+
445
+ }
446
+
447
+ func TestCheckForBaremetalClusterOperator (t * testing.T ) {
448
+ testCases := []struct {
449
+ testCase string
450
+ clusterOperator * osconfigv1.ClusterOperator
451
+ expected bool
452
+ expectedError bool
453
+ }{
454
+ {
455
+ testCase : cboClusterOperatorName ,
456
+ clusterOperator : & osconfigv1.ClusterOperator {
457
+ TypeMeta : metav1.TypeMeta {
458
+ Kind : "ClusterOperator" ,
459
+ APIVersion : "config.openshift.io/v1" ,
460
+ },
461
+ ObjectMeta : metav1.ObjectMeta {
462
+ Name : cboClusterOperatorName ,
463
+ },
464
+ Status : osconfigv1.ClusterOperatorStatus {
465
+ RelatedObjects : []osconfigv1.ObjectReference {
466
+ {
467
+ Group : "" ,
468
+ Resource : "namespaces" ,
469
+ Name : "openshift-machine-api" ,
470
+ },
471
+ },
472
+ },
473
+ },
474
+ expected : true ,
475
+ },
476
+ {
477
+ testCase : "invalidCO" ,
478
+ clusterOperator : & osconfigv1.ClusterOperator {
479
+ ObjectMeta : metav1.ObjectMeta {
480
+ Name : "invalidCO" ,
481
+ },
482
+ },
483
+ expected : false ,
484
+ },
485
+ }
486
+ for _ , tc := range testCases {
487
+ t .Run (string (tc .testCase ), func (t * testing.T ) {
488
+ var osClient * fakeos.Clientset
489
+ osClient = fakeos .NewSimpleClientset (tc .clusterOperator )
490
+ _ , err := osClient .ConfigV1 ().ClusterOperators ().Create (context .Background (), tc .clusterOperator , metav1.CreateOptions {})
491
+ if err != nil && ! apierrors .IsAlreadyExists (err ) {
492
+ t .Fatalf ("Unable to create ClusterOperator for test: %v" , err )
493
+ }
494
+ exists , err := checkForBaremetalClusterOperator (osClient )
495
+ if exists != tc .expected {
496
+ t .Errorf ("Expected: %v, got: %v" , tc .expected , exists )
497
+ }
498
+ if tc .expectedError != (err != nil ) {
499
+ t .Errorf ("ExpectedError: %v, got: %v" , tc .expectedError , err )
500
+ }
501
+ err = osClient .ConfigV1 ().ClusterOperators ().Delete (context .Background (), tc .testCase , metav1.DeleteOptions {})
502
+ })
503
+ }
504
+ }
0 commit comments