|
| 1 | +package configuration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 10 | + |
| 11 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + |
| 14 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 15 | + operatorv1alpha1 "github.com/openshift/api/operator/v1alpha1" |
| 16 | + operatorclientsetfake "github.com/openshift/client-go/operator/clientset/versioned/fake" |
| 17 | + operatorexternalversions "github.com/openshift/client-go/operator/informers/externalversions" |
| 18 | +) |
| 19 | + |
| 20 | +func TestClusterVersionOperatorConfiguration_sync(t *testing.T) { |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + config operatorv1alpha1.ClusterVersionOperator |
| 24 | + expectedConfig operatorv1alpha1.ClusterVersionOperator |
| 25 | + internalConfig ClusterVersionOperatorConfiguration |
| 26 | + expectedInternalConfig ClusterVersionOperatorConfiguration |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "first sync run correctly updates the status", |
| 30 | + config: operatorv1alpha1.ClusterVersionOperator{ |
| 31 | + ObjectMeta: metav1.ObjectMeta{ |
| 32 | + Generation: 1, |
| 33 | + }, |
| 34 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 35 | + OperatorLogLevel: operatorv1.Normal, |
| 36 | + }, |
| 37 | + }, |
| 38 | + expectedConfig: operatorv1alpha1.ClusterVersionOperator{ |
| 39 | + ObjectMeta: metav1.ObjectMeta{ |
| 40 | + Generation: 1, |
| 41 | + }, |
| 42 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 43 | + OperatorLogLevel: operatorv1.Normal, |
| 44 | + }, |
| 45 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 46 | + ObservedGeneration: 1, |
| 47 | + }, |
| 48 | + }, |
| 49 | + internalConfig: ClusterVersionOperatorConfiguration{ |
| 50 | + desiredLogLevel: operatorv1.Normal, |
| 51 | + lastObservedGeneration: 0, |
| 52 | + }, |
| 53 | + expectedInternalConfig: ClusterVersionOperatorConfiguration{ |
| 54 | + desiredLogLevel: operatorv1.Normal, |
| 55 | + lastObservedGeneration: 1, |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "sync updates observed generation correctly", |
| 60 | + config: operatorv1alpha1.ClusterVersionOperator{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{ |
| 62 | + Generation: 3, |
| 63 | + }, |
| 64 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 65 | + OperatorLogLevel: operatorv1.Normal, |
| 66 | + }, |
| 67 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 68 | + ObservedGeneration: 2, |
| 69 | + }, |
| 70 | + }, |
| 71 | + expectedConfig: operatorv1alpha1.ClusterVersionOperator{ |
| 72 | + ObjectMeta: metav1.ObjectMeta{ |
| 73 | + Generation: 3, |
| 74 | + }, |
| 75 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 76 | + OperatorLogLevel: operatorv1.Normal, |
| 77 | + }, |
| 78 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 79 | + ObservedGeneration: 3, |
| 80 | + }, |
| 81 | + }, |
| 82 | + internalConfig: ClusterVersionOperatorConfiguration{ |
| 83 | + desiredLogLevel: operatorv1.Normal, |
| 84 | + lastObservedGeneration: 2, |
| 85 | + }, |
| 86 | + expectedInternalConfig: ClusterVersionOperatorConfiguration{ |
| 87 | + desiredLogLevel: operatorv1.Normal, |
| 88 | + lastObservedGeneration: 3, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "sync updates desired log level correctly", |
| 93 | + config: operatorv1alpha1.ClusterVersionOperator{ |
| 94 | + ObjectMeta: metav1.ObjectMeta{ |
| 95 | + Generation: 4, |
| 96 | + }, |
| 97 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 98 | + OperatorLogLevel: operatorv1.Trace, |
| 99 | + }, |
| 100 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 101 | + ObservedGeneration: 3, |
| 102 | + }, |
| 103 | + }, |
| 104 | + expectedConfig: operatorv1alpha1.ClusterVersionOperator{ |
| 105 | + ObjectMeta: metav1.ObjectMeta{ |
| 106 | + Generation: 4, |
| 107 | + }, |
| 108 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 109 | + OperatorLogLevel: operatorv1.Trace, |
| 110 | + }, |
| 111 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 112 | + ObservedGeneration: 4, |
| 113 | + }, |
| 114 | + }, |
| 115 | + internalConfig: ClusterVersionOperatorConfiguration{ |
| 116 | + desiredLogLevel: operatorv1.Normal, |
| 117 | + lastObservedGeneration: 3, |
| 118 | + }, |
| 119 | + expectedInternalConfig: ClusterVersionOperatorConfiguration{ |
| 120 | + desiredLogLevel: operatorv1.Trace, |
| 121 | + lastObservedGeneration: 4, |
| 122 | + }, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "number of not observed generations does not impact sync", |
| 126 | + config: operatorv1alpha1.ClusterVersionOperator{ |
| 127 | + ObjectMeta: metav1.ObjectMeta{ |
| 128 | + Generation: 40, |
| 129 | + }, |
| 130 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 131 | + OperatorLogLevel: operatorv1.TraceAll, |
| 132 | + }, |
| 133 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 134 | + ObservedGeneration: 3, |
| 135 | + }, |
| 136 | + }, |
| 137 | + expectedConfig: operatorv1alpha1.ClusterVersionOperator{ |
| 138 | + ObjectMeta: metav1.ObjectMeta{ |
| 139 | + Generation: 40, |
| 140 | + }, |
| 141 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 142 | + OperatorLogLevel: operatorv1.TraceAll, |
| 143 | + }, |
| 144 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 145 | + ObservedGeneration: 40, |
| 146 | + }, |
| 147 | + }, |
| 148 | + internalConfig: ClusterVersionOperatorConfiguration{ |
| 149 | + desiredLogLevel: operatorv1.Normal, |
| 150 | + lastObservedGeneration: 3, |
| 151 | + }, |
| 152 | + expectedInternalConfig: ClusterVersionOperatorConfiguration{ |
| 153 | + desiredLogLevel: operatorv1.TraceAll, |
| 154 | + lastObservedGeneration: 40, |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + for _, tt := range tests { |
| 159 | + t.Run(tt.name, func(t *testing.T) { |
| 160 | + // Initialize testing logic |
| 161 | + client := operatorclientsetfake.NewClientset(&tt.config) |
| 162 | + tt.internalConfig.client = client.OperatorV1alpha1().ClusterVersionOperators() |
| 163 | + ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Minute)) |
| 164 | + |
| 165 | + // Run tested functionality |
| 166 | + if err := tt.internalConfig.sync(ctx, &tt.config); err != nil { |
| 167 | + t.Errorf("unexpected error %v", err) |
| 168 | + } |
| 169 | + |
| 170 | + // Verify results |
| 171 | + if tt.internalConfig.lastObservedGeneration != tt.expectedInternalConfig.lastObservedGeneration { |
| 172 | + t.Errorf("unexpected 'lastObservedGeneration' value; wanted=%v, got=%v", tt.expectedInternalConfig.lastObservedGeneration, tt.internalConfig.lastObservedGeneration) |
| 173 | + } |
| 174 | + if tt.internalConfig.desiredLogLevel != tt.expectedInternalConfig.desiredLogLevel { |
| 175 | + t.Errorf("unexpected 'desiredLogLevel' value; wanted=%v, got=%v", tt.expectedInternalConfig.desiredLogLevel, tt.internalConfig.desiredLogLevel) |
| 176 | + } |
| 177 | + |
| 178 | + config, err := client.OperatorV1alpha1().ClusterVersionOperators().Get(ctx, "", metav1.GetOptions{}) |
| 179 | + if err != nil { |
| 180 | + t.Errorf("unexpected error %v", err) |
| 181 | + } |
| 182 | + if diff := cmp.Diff(tt.expectedConfig, *config, cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ManagedFields")); diff != "" { |
| 183 | + t.Errorf("unexpected config (-want, +got) = %v", diff) |
| 184 | + } |
| 185 | + |
| 186 | + // Shutdown created resources |
| 187 | + cancelFunc() |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +func TestClusterVersionOperatorConfiguration_Sync(t *testing.T) { |
| 193 | + tests := []struct { |
| 194 | + name string |
| 195 | + config *operatorv1alpha1.ClusterVersionOperator |
| 196 | + expectedConfig *operatorv1alpha1.ClusterVersionOperator |
| 197 | + }{ |
| 198 | + { |
| 199 | + name: "the configuration resource does not exist in the cluster -> ignore", |
| 200 | + config: nil, |
| 201 | + expectedConfig: nil, |
| 202 | + }, |
| 203 | + { |
| 204 | + name: "Sync updates the ClusterVersionOperator resource", |
| 205 | + config: &operatorv1alpha1.ClusterVersionOperator{ |
| 206 | + ObjectMeta: metav1.ObjectMeta{ |
| 207 | + Name: "cluster", |
| 208 | + Generation: 4, |
| 209 | + }, |
| 210 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 211 | + OperatorLogLevel: operatorv1.Trace, |
| 212 | + }, |
| 213 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 214 | + ObservedGeneration: 3, |
| 215 | + }, |
| 216 | + }, |
| 217 | + expectedConfig: &operatorv1alpha1.ClusterVersionOperator{ |
| 218 | + ObjectMeta: metav1.ObjectMeta{ |
| 219 | + Name: "cluster", |
| 220 | + Generation: 4, |
| 221 | + }, |
| 222 | + Spec: operatorv1alpha1.ClusterVersionOperatorSpec{ |
| 223 | + OperatorLogLevel: operatorv1.Trace, |
| 224 | + }, |
| 225 | + Status: operatorv1alpha1.ClusterVersionOperatorStatus{ |
| 226 | + ObservedGeneration: 4, |
| 227 | + }, |
| 228 | + }, |
| 229 | + }, |
| 230 | + } |
| 231 | + for _, tt := range tests { |
| 232 | + t.Run(tt.name, func(t *testing.T) { |
| 233 | + // Initialize testing logic |
| 234 | + var client *operatorclientsetfake.Clientset |
| 235 | + if tt.config != nil { |
| 236 | + client = operatorclientsetfake.NewClientset(tt.config) |
| 237 | + } else { |
| 238 | + client = operatorclientsetfake.NewClientset() |
| 239 | + } |
| 240 | + |
| 241 | + factory := operatorexternalversions.NewSharedInformerFactoryWithOptions(client, time.Minute) |
| 242 | + cvoConfiguration := NewClusterVersionOperatorConfiguration(client, factory) |
| 243 | + defer cvoConfiguration.queue.ShutDown() |
| 244 | + |
| 245 | + ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Minute)) |
| 246 | + defer cancelFunc() |
| 247 | + |
| 248 | + err := cvoConfiguration.Start(ctx) |
| 249 | + if err != nil { |
| 250 | + t.Errorf("unexpected error %v", err) |
| 251 | + } |
| 252 | + |
| 253 | + // Run tested functionality |
| 254 | + err = cvoConfiguration.Sync(ctx, "ClusterVersionOperator/cluster") |
| 255 | + if err != nil { |
| 256 | + t.Errorf("unexpected error %v", err) |
| 257 | + } |
| 258 | + |
| 259 | + // Verify results |
| 260 | + config, err := client.OperatorV1alpha1().ClusterVersionOperators().Get(ctx, "cluster", metav1.GetOptions{}) |
| 261 | + if err != nil && !apierrors.IsNotFound(err) { |
| 262 | + t.Errorf("unexpected error %v", err) |
| 263 | + } |
| 264 | + |
| 265 | + switch { |
| 266 | + case apierrors.IsNotFound(err) && tt.expectedConfig != nil: |
| 267 | + t.Errorf("expected config to be '%v', got NotFound", *tt.expectedConfig) |
| 268 | + case err == nil: |
| 269 | + if diff := cmp.Diff(*tt.expectedConfig, *config, cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ManagedFields")); diff != "" { |
| 270 | + t.Errorf("unexpected config (-want, +got) = %v", diff) |
| 271 | + } |
| 272 | + } |
| 273 | + }) |
| 274 | + } |
| 275 | +} |
0 commit comments