@@ -28,6 +28,7 @@ import (
28
28
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
29
29
apiextensionsfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
30
30
k8serrors "k8s.io/apimachinery/pkg/api/errors"
31
+ meta "k8s.io/apimachinery/pkg/api/meta"
31
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
33
"k8s.io/apimachinery/pkg/labels"
33
34
"k8s.io/apimachinery/pkg/runtime"
@@ -4537,6 +4538,217 @@ func TestSyncOperatorGroups(t *testing.T) {
4537
4538
}
4538
4539
}
4539
4540
4541
+ func TestOperatorGroupConditions (t * testing.T ) {
4542
+ logrus .SetLevel (logrus .DebugLevel )
4543
+ clockFake := utilclock .NewFakeClock (time .Date (2006 , time .January , 2 , 15 , 4 , 5 , 0 , time .FixedZone ("MST" , - 7 * 3600 )))
4544
+
4545
+ operatorNamespace := "operator-ns"
4546
+ opNamespace := & corev1.Namespace {
4547
+ ObjectMeta : metav1.ObjectMeta {
4548
+ Name : operatorNamespace ,
4549
+ },
4550
+ }
4551
+ serviceAccount := serviceAccount ("sa" , operatorNamespace )
4552
+
4553
+ type initial struct {
4554
+ operatorGroup * v1.OperatorGroup
4555
+ clientObjs []runtime.Object
4556
+ k8sObjs []runtime.Object
4557
+ }
4558
+
4559
+ tests := []struct {
4560
+ initial initial
4561
+ name string
4562
+ expectedConditions []metav1.Condition
4563
+ expectError bool
4564
+ }{
4565
+ {
4566
+ name : "ValidOperatorGroup/NoServiceAccount" ,
4567
+ initial : initial {
4568
+ operatorGroup : & v1.OperatorGroup {
4569
+ ObjectMeta : metav1.ObjectMeta {
4570
+ Name : "operator-group-1" ,
4571
+ Namespace : operatorNamespace ,
4572
+ UID : "135e02a5-a7e2-44e7-abaa-88c63838993c" ,
4573
+ },
4574
+ Spec : v1.OperatorGroupSpec {
4575
+ TargetNamespaces : []string {operatorNamespace },
4576
+ },
4577
+ },
4578
+ k8sObjs : []runtime.Object {
4579
+ & corev1.Namespace {
4580
+ ObjectMeta : metav1.ObjectMeta {
4581
+ Name : operatorNamespace ,
4582
+ },
4583
+ },
4584
+ },
4585
+ },
4586
+ expectError : false ,
4587
+ expectedConditions : []metav1.Condition {},
4588
+ },
4589
+ {
4590
+ name : "ValidOperatorGroup/ValidServiceAccount" ,
4591
+ initial : initial {
4592
+ operatorGroup : & v1.OperatorGroup {
4593
+ ObjectMeta : metav1.ObjectMeta {
4594
+ Name : "operator-group-1" ,
4595
+ Namespace : operatorNamespace ,
4596
+ UID : "135e02a5-a7e2-44e7-abaa-88c63838993c" ,
4597
+ },
4598
+ Spec : v1.OperatorGroupSpec {
4599
+ ServiceAccountName : "sa" ,
4600
+ TargetNamespaces : []string {operatorNamespace },
4601
+ },
4602
+ },
4603
+ k8sObjs : []runtime.Object {
4604
+ & corev1.Namespace {
4605
+ ObjectMeta : metav1.ObjectMeta {
4606
+ Name : operatorNamespace ,
4607
+ },
4608
+ },
4609
+ serviceAccount ,
4610
+ },
4611
+ },
4612
+ expectError : false ,
4613
+ expectedConditions : []metav1.Condition {},
4614
+ },
4615
+ {
4616
+ name : "BadOperatorGroup/MissingServiceAccount" ,
4617
+ initial : initial {
4618
+ operatorGroup : & v1.OperatorGroup {
4619
+ ObjectMeta : metav1.ObjectMeta {
4620
+ Name : "operator-group-1" ,
4621
+ Namespace : operatorNamespace ,
4622
+ UID : "135e02a5-a7e2-44e7-abaa-88c63838993c" ,
4623
+ },
4624
+ Spec : v1.OperatorGroupSpec {
4625
+ ServiceAccountName : "nonexistingSA" ,
4626
+ TargetNamespaces : []string {operatorNamespace },
4627
+ },
4628
+ },
4629
+ k8sObjs : []runtime.Object {
4630
+ & corev1.Namespace {
4631
+ ObjectMeta : metav1.ObjectMeta {
4632
+ Name : operatorNamespace ,
4633
+ },
4634
+ },
4635
+ },
4636
+ },
4637
+ expectError : true ,
4638
+ expectedConditions : []metav1.Condition {
4639
+ metav1.Condition {
4640
+ Type : v1 .OperatorGroupServiceAccountCondition ,
4641
+ Status : metav1 .ConditionTrue ,
4642
+ Reason : v1 .OperatorGroupServiceAccountReason ,
4643
+ Message : "ServiceAccount nonexistingSA not found" ,
4644
+ },
4645
+ },
4646
+ },
4647
+ {
4648
+ name : "BadOperatorGroup/MultipleOperatorGroups" ,
4649
+ initial : initial {
4650
+ operatorGroup : & v1.OperatorGroup {
4651
+ ObjectMeta : metav1.ObjectMeta {
4652
+ Name : "operator-group-1" ,
4653
+ Namespace : operatorNamespace ,
4654
+ UID : "135e02a5-a7e2-44e7-abaa-88c63838993c" ,
4655
+ },
4656
+ Spec : v1.OperatorGroupSpec {
4657
+ TargetNamespaces : []string {operatorNamespace },
4658
+ },
4659
+ },
4660
+ clientObjs : []runtime.Object {
4661
+ & v1.OperatorGroup {
4662
+ ObjectMeta : metav1.ObjectMeta {
4663
+ Name : "operator-group-2" ,
4664
+ Namespace : operatorNamespace ,
4665
+ UID : "cdc9643e-7c52-4f7c-ae75-28ccb6aec97d" ,
4666
+ },
4667
+ Spec : v1.OperatorGroupSpec {
4668
+ TargetNamespaces : []string {operatorNamespace },
4669
+ },
4670
+ },
4671
+ },
4672
+ k8sObjs : []runtime.Object {
4673
+ & corev1.Namespace {
4674
+ ObjectMeta : metav1.ObjectMeta {
4675
+ Name : operatorNamespace ,
4676
+ },
4677
+ },
4678
+ },
4679
+ },
4680
+ expectError : true ,
4681
+ expectedConditions : []metav1.Condition {
4682
+ metav1.Condition {
4683
+ Type : v1 .MutlipleOperatorGroupCondition ,
4684
+ Status : metav1 .ConditionTrue ,
4685
+ Reason : v1 .MultipleOperatorGroupsReason ,
4686
+ Message : "Multiple OperatorGroup found in the same namespace" ,
4687
+ },
4688
+ },
4689
+ },
4690
+ }
4691
+
4692
+ for _ , tt := range tests {
4693
+ t .Run (tt .name , func (t * testing.T ) {
4694
+ namespaces := []string {}
4695
+ // Pick out Namespaces
4696
+ for _ , obj := range tt .initial .k8sObjs {
4697
+ if ns , ok := obj .(* corev1.Namespace ); ok {
4698
+ namespaces = append (namespaces , ns .GetName ())
4699
+ }
4700
+ }
4701
+
4702
+ // Append operatorGroup to initialObjs
4703
+ tt .initial .clientObjs = append (tt .initial .clientObjs , tt .initial .operatorGroup )
4704
+
4705
+ // Create test operator
4706
+ ctx , cancel := context .WithCancel (context .TODO ())
4707
+ defer cancel ()
4708
+ op , err := NewFakeOperator (
4709
+ ctx ,
4710
+ withClock (clockFake ),
4711
+ withNamespaces (namespaces ... ),
4712
+ withOperatorNamespace (operatorNamespace ),
4713
+ withClientObjs (tt .initial .clientObjs ... ),
4714
+ withK8sObjs (tt .initial .k8sObjs ... ),
4715
+ )
4716
+ require .NoError (t , err )
4717
+
4718
+ err = op .syncOperatorGroups (tt .initial .operatorGroup )
4719
+ if ! tt .expectError {
4720
+ require .NoError (t , err )
4721
+ }
4722
+
4723
+ // wait on operator group updated status to be in the cache
4724
+ err = wait .PollImmediate (1 * time .Millisecond , 5 * time .Second , func () (bool , error ) {
4725
+ og , err := op .lister .OperatorsV1 ().OperatorGroupLister ().OperatorGroups (tt .initial .operatorGroup .GetNamespace ()).Get (tt .initial .operatorGroup .GetName ())
4726
+ if err != nil || og == nil {
4727
+ return false , err
4728
+ }
4729
+ return true , nil
4730
+ })
4731
+ require .NoError (t , err )
4732
+
4733
+ // sync namespace
4734
+ err = op .syncNamespace (opNamespace )
4735
+ require .NoError (t , err )
4736
+
4737
+ operatorGroup , err := op .client .OperatorsV1 ().OperatorGroups (tt .initial .operatorGroup .GetNamespace ()).Get (context .TODO (), tt .initial .operatorGroup .GetName (), metav1.GetOptions {})
4738
+ require .NoError (t , err )
4739
+ assert .Equal (t , len (tt .expectedConditions ), len (operatorGroup .Status .Conditions ))
4740
+ if len (tt .expectedConditions ) > 0 {
4741
+ for _ , cond := range tt .expectedConditions {
4742
+ c := meta .FindStatusCondition (operatorGroup .Status .Conditions , cond .Type )
4743
+ assert .Equal (t , cond .Status , c .Status )
4744
+ assert .Equal (t , cond .Reason , c .Reason )
4745
+ assert .Equal (t , cond .Message , c .Message )
4746
+ }
4747
+ }
4748
+ })
4749
+ }
4750
+ }
4751
+
4540
4752
func RequireObjectsInCache (t * testing.T , lister operatorlister.OperatorLister , namespace string , objects []runtime.Object , doCompare bool ) error {
4541
4753
for _ , object := range objects {
4542
4754
var err error
0 commit comments