@@ -4,7 +4,10 @@ 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"
8
11
apierrors "k8s.io/apimachinery/pkg/api/errors"
9
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
13
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -375,7 +378,163 @@ func TestSyncBaremetalControllers(t *testing.T) {
375
378
376
379
err := optr .syncBaremetalControllers (operatorConfig , tc .configCRName )
377
380
g .Expect (err ).To (Equal (tc .expectedError ))
381
+ })
382
+ }
383
+ }
384
+
385
+ func TestCheckMetal3DeploymentOwned (t * testing.T ) {
386
+ kubeClient := fakekube .NewSimpleClientset (nil ... )
387
+ operatorConfig := newOperatorWithBaremetalConfig ()
388
+ client := kubeClient .AppsV1 ()
378
389
390
+ testCases := []struct {
391
+ testCase string
392
+ deployment * appsv1.Deployment
393
+ expected bool
394
+ expectedError bool
395
+ }{
396
+ {
397
+ testCase : "Only maoOwnedAnnotation" ,
398
+ deployment : & appsv1.Deployment {
399
+ TypeMeta : metav1.TypeMeta {
400
+ Kind : "Deployment" ,
401
+ APIVersion : "apps/v1" ,
402
+ },
403
+ ObjectMeta : metav1.ObjectMeta {
404
+ Name : baremetalDeploymentName ,
405
+ Annotations : map [string ]string {
406
+ maoOwnedAnnotation : "" ,
407
+ },
408
+ },
409
+ },
410
+ expected : true ,
411
+ },
412
+ {
413
+ testCase : "Only cboOwnedAnnotation" ,
414
+ deployment : & appsv1.Deployment {
415
+ TypeMeta : metav1.TypeMeta {
416
+ Kind : "Deployment" ,
417
+ APIVersion : "apps/v1" ,
418
+ },
419
+ ObjectMeta : metav1.ObjectMeta {
420
+ Name : baremetalDeploymentName ,
421
+ Annotations : map [string ]string {
422
+ cboOwnedAnnotation : "" ,
423
+ },
424
+ },
425
+ },
426
+ expected : false ,
427
+ },
428
+ {
429
+ testCase : "Both cboOwnedAnnotation and maoOwnedAnnotation" ,
430
+ deployment : & appsv1.Deployment {
431
+ TypeMeta : metav1.TypeMeta {
432
+ Kind : "Deployment" ,
433
+ APIVersion : "apps/v1" ,
434
+ },
435
+ ObjectMeta : metav1.ObjectMeta {
436
+ Name : baremetalDeploymentName ,
437
+ Annotations : map [string ]string {
438
+ cboOwnedAnnotation : "" ,
439
+ maoOwnedAnnotation : "" ,
440
+ },
441
+ },
442
+ },
443
+ expected : false ,
444
+ },
445
+ {
446
+ testCase : "No cboOwnedAnnotation or maoOwnedAnnotation" ,
447
+ deployment : & appsv1.Deployment {
448
+ TypeMeta : metav1.TypeMeta {
449
+ Kind : "Deployment" ,
450
+ APIVersion : "apps/v1" ,
451
+ },
452
+ ObjectMeta : metav1.ObjectMeta {
453
+ Name : baremetalDeploymentName ,
454
+ Annotations : map [string ]string {},
455
+ },
456
+ },
457
+ expected : true ,
458
+ },
459
+ }
460
+ for _ , tc := range testCases {
461
+ t .Run (string (tc .testCase ), func (t * testing.T ) {
462
+
463
+ _ , err := client .Deployments ("test-namespace" ).Create (context .Background (), tc .deployment , metav1.CreateOptions {})
464
+ if err != nil {
465
+ t .Fatalf ("Could not create metal3 test deployment.\n " )
466
+ }
467
+ maoOwned , err := checkMetal3DeploymentMAOOwned (client , operatorConfig )
468
+ if maoOwned != tc .expected {
469
+ t .Errorf ("Expected: %v, got: %v" , tc .expected , maoOwned )
470
+ }
471
+ if tc .expectedError != (err != nil ) {
472
+ t .Errorf ("ExpectedError: %v, got: %v" , tc .expectedError , err )
473
+ }
474
+ err = client .Deployments ("test-namespace" ).Delete (context .Background (), baremetalDeploymentName , metav1.DeleteOptions {})
475
+ if err != nil {
476
+ t .Errorf ("Could not delete metal3 test deployment.\n " )
477
+ }
478
+ })
479
+ }
480
+
481
+ }
482
+
483
+ func TestCheckForBaremetalClusterOperator (t * testing.T ) {
484
+ testCases := []struct {
485
+ testCase string
486
+ clusterOperator * osconfigv1.ClusterOperator
487
+ expected bool
488
+ expectedError bool
489
+ }{
490
+ {
491
+ testCase : cboClusterOperatorName ,
492
+ clusterOperator : & osconfigv1.ClusterOperator {
493
+ TypeMeta : metav1.TypeMeta {
494
+ Kind : "ClusterOperator" ,
495
+ APIVersion : "config.openshift.io/v1" ,
496
+ },
497
+ ObjectMeta : metav1.ObjectMeta {
498
+ Name : cboClusterOperatorName ,
499
+ },
500
+ Status : osconfigv1.ClusterOperatorStatus {
501
+ RelatedObjects : []osconfigv1.ObjectReference {
502
+ {
503
+ Group : "" ,
504
+ Resource : "namespaces" ,
505
+ Name : "openshift-machine-api" ,
506
+ },
507
+ },
508
+ },
509
+ },
510
+ expected : true ,
511
+ },
512
+ {
513
+ testCase : "invalidCO" ,
514
+ clusterOperator : & osconfigv1.ClusterOperator {
515
+ ObjectMeta : metav1.ObjectMeta {
516
+ Name : "invalidCO" ,
517
+ },
518
+ },
519
+ expected : false ,
520
+ },
521
+ }
522
+ for _ , tc := range testCases {
523
+ t .Run (string (tc .testCase ), func (t * testing.T ) {
524
+ var osClient * fakeos.Clientset
525
+ osClient = fakeos .NewSimpleClientset (tc .clusterOperator )
526
+ _ , err := osClient .ConfigV1 ().ClusterOperators ().Create (context .Background (), tc .clusterOperator , metav1.CreateOptions {})
527
+ if err != nil && ! apierrors .IsAlreadyExists (err ) {
528
+ t .Fatalf ("Unable to create ClusterOperator for test: %v" , err )
529
+ }
530
+ exists , err := checkForBaremetalClusterOperator (osClient )
531
+ if exists != tc .expected {
532
+ t .Errorf ("Expected: %v, got: %v" , tc .expected , exists )
533
+ }
534
+ if tc .expectedError != (err != nil ) {
535
+ t .Errorf ("ExpectedError: %v, got: %v" , tc .expectedError , err )
536
+ }
537
+ err = osClient .ConfigV1 ().ClusterOperators ().Delete (context .Background (), tc .testCase , metav1.DeleteOptions {})
379
538
})
380
539
}
381
540
}
0 commit comments