|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + opv1 "github.com/openshift/api/operator/v1" |
| 11 | + operatorv1alpha1 "github.com/openshift/cert-manager-operator/api/operator/v1alpha1" |
| 12 | + fakecertmanclient "github.com/openshift/cert-manager-operator/pkg/operator/clientset/versioned/fake" |
| 13 | + |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/runtime" |
| 16 | +) |
| 17 | + |
| 18 | +// TestVerifyOperatorStatusCondition unit test for verifyOperatorStatusCondition func |
| 19 | +func TestVerifyOperatorStatusCondition(t *testing.T) { |
| 20 | + controllerPrefix := "foo-controller" |
| 21 | + |
| 22 | + newCertManagerObjectWithConditions := func(conditions ...opv1.OperatorCondition) *operatorv1alpha1.CertManager { |
| 23 | + return &operatorv1alpha1.CertManager{ |
| 24 | + ObjectMeta: metav1.ObjectMeta{ |
| 25 | + Name: "cluster", |
| 26 | + }, |
| 27 | + Status: operatorv1alpha1.CertManagerStatus{ |
| 28 | + OperatorStatus: opv1.OperatorStatus{ |
| 29 | + Conditions: conditions, |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + initialObjects []runtime.Object |
| 38 | + expectedConditions map[string]opv1.ConditionStatus |
| 39 | + matchAny bool |
| 40 | + expectError bool |
| 41 | + errorContains string |
| 42 | + }{ |
| 43 | + { |
| 44 | + name: "One degraded is true but another is false using Any", |
| 45 | + expectedConditions: map[string]opv1.ConditionStatus{ |
| 46 | + "Available": opv1.ConditionTrue, |
| 47 | + "Degraded": opv1.ConditionFalse, |
| 48 | + "Progressing": opv1.ConditionFalse, |
| 49 | + }, |
| 50 | + initialObjects: []runtime.Object{ |
| 51 | + newCertManagerObjectWithConditions( |
| 52 | + opv1.OperatorCondition{Type: controllerPrefix + "Available", Status: opv1.ConditionTrue}, |
| 53 | + |
| 54 | + opv1.OperatorCondition{Type: controllerPrefix + "Degraded", Status: opv1.ConditionFalse}, |
| 55 | + opv1.OperatorCondition{Type: controllerPrefix + "-rand-Degraded", Status: opv1.ConditionTrue}, |
| 56 | + |
| 57 | + opv1.OperatorCondition{Type: controllerPrefix + "Progressing", Status: opv1.ConditionFalse}, |
| 58 | + ), |
| 59 | + }, |
| 60 | + matchAny: true, |
| 61 | + expectError: false, |
| 62 | + errorContains: "context deadline exceeded", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "Both degraded is false", |
| 66 | + expectedConditions: map[string]opv1.ConditionStatus{ |
| 67 | + "Available": opv1.ConditionTrue, |
| 68 | + "Degraded": opv1.ConditionFalse, |
| 69 | + "Progressing": opv1.ConditionFalse, |
| 70 | + }, |
| 71 | + initialObjects: []runtime.Object{ |
| 72 | + newCertManagerObjectWithConditions( |
| 73 | + opv1.OperatorCondition{Type: controllerPrefix + "Available", Status: opv1.ConditionTrue}, |
| 74 | + |
| 75 | + opv1.OperatorCondition{Type: controllerPrefix + "Degraded", Status: opv1.ConditionFalse}, |
| 76 | + opv1.OperatorCondition{Type: controllerPrefix + "-bar-Degraded", Status: opv1.ConditionFalse}, |
| 77 | + |
| 78 | + opv1.OperatorCondition{Type: controllerPrefix + "Progressing", Status: opv1.ConditionFalse}, |
| 79 | + ), |
| 80 | + }, |
| 81 | + expectError: false, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "One available is true but another is false using All", |
| 85 | + expectedConditions: map[string]opv1.ConditionStatus{ |
| 86 | + "Available": opv1.ConditionTrue, |
| 87 | + "Degraded": opv1.ConditionFalse, |
| 88 | + "Progressing": opv1.ConditionFalse, |
| 89 | + }, |
| 90 | + initialObjects: []runtime.Object{ |
| 91 | + newCertManagerObjectWithConditions( |
| 92 | + opv1.OperatorCondition{Type: controllerPrefix + "Available", Status: opv1.ConditionTrue}, |
| 93 | + opv1.OperatorCondition{Type: controllerPrefix + "-bar-Available", Status: opv1.ConditionFalse}, |
| 94 | + |
| 95 | + opv1.OperatorCondition{Type: controllerPrefix + "Degraded", Status: opv1.ConditionFalse}, |
| 96 | + opv1.OperatorCondition{Type: controllerPrefix + "-bar-Degraded", Status: opv1.ConditionFalse}, |
| 97 | + |
| 98 | + opv1.OperatorCondition{Type: controllerPrefix + "Progressing", Status: opv1.ConditionFalse}, |
| 99 | + ), |
| 100 | + }, |
| 101 | + expectError: true, |
| 102 | + errorContains: "context deadline exceeded", |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "A missing condition for Progressing", |
| 106 | + expectedConditions: map[string]opv1.ConditionStatus{ |
| 107 | + "Available": opv1.ConditionTrue, |
| 108 | + "Degraded": opv1.ConditionFalse, |
| 109 | + "Progressing": opv1.ConditionFalse, |
| 110 | + }, |
| 111 | + initialObjects: []runtime.Object{ |
| 112 | + newCertManagerObjectWithConditions( |
| 113 | + opv1.OperatorCondition{Type: controllerPrefix + "Available", Status: opv1.ConditionTrue}, |
| 114 | + opv1.OperatorCondition{Type: controllerPrefix + "-bar-Available", Status: opv1.ConditionFalse}, |
| 115 | + |
| 116 | + opv1.OperatorCondition{Type: controllerPrefix + "Degraded", Status: opv1.ConditionFalse}, |
| 117 | + opv1.OperatorCondition{Type: controllerPrefix + "-bar-Degraded", Status: opv1.ConditionFalse}, |
| 118 | + ), |
| 119 | + }, |
| 120 | + expectError: true, |
| 121 | + errorContains: "could not verify all status conditions as expected", |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + t.Parallel() |
| 126 | + for _, tt := range tests { |
| 127 | + t.Run(tt.name, func(t *testing.T) { |
| 128 | + fakeClient := fakecertmanclient.NewSimpleClientset(tt.initialObjects...) |
| 129 | + |
| 130 | + matchers := GenerateConditionMatchers([]PrefixAndMatchTypeTuple{ |
| 131 | + {controllerPrefix, tt.matchAny}, |
| 132 | + }, tt.expectedConditions) |
| 133 | + |
| 134 | + err := verifyOperatorStatusCondition(fakeClient.OperatorV1alpha1(), matchers) |
| 135 | + |
| 136 | + if tt.expectError && err == nil { |
| 137 | + t.Errorf("Expected an error, but got nil") |
| 138 | + } |
| 139 | + |
| 140 | + if !tt.expectError && err != nil { |
| 141 | + t.Errorf("Expected no error, but got: %v", err) |
| 142 | + } |
| 143 | + |
| 144 | + if tt.expectError && err != nil { |
| 145 | + if tt.errorContains != "" && !strings.Contains(err.Error(), tt.errorContains) { |
| 146 | + t.Errorf("Expected error to contain '%s', but got: %v", tt.errorContains, err) |
| 147 | + } |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
0 commit comments