|
| 1 | +package resourcesynccontroller |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + labels "k8s.io/apimachinery/pkg/labels" |
| 14 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 15 | + corev1listers "k8s.io/client-go/listers/core/v1" |
| 16 | + |
| 17 | + "github.com/openshift/api/annotations" |
| 18 | + "github.com/openshift/library-go/pkg/crypto" |
| 19 | + "github.com/openshift/library-go/pkg/operator/certrotation" |
| 20 | +) |
| 21 | + |
| 22 | +// mockConfigMapLister is a mock implementation of the ConfigMapLister interface for testing |
| 23 | +type mockConfigMapLister struct { |
| 24 | + configMaps map[string]map[string]*corev1.ConfigMap |
| 25 | +} |
| 26 | + |
| 27 | +func (m *mockConfigMapLister) List(selector labels.Selector) ([]*corev1.ConfigMap, error) { |
| 28 | + panic("not implemented") |
| 29 | +} |
| 30 | + |
| 31 | +func (m *mockConfigMapLister) ConfigMaps(namespace string) corev1listers.ConfigMapNamespaceLister { |
| 32 | + return &mockConfigMapNamespaceLister{ |
| 33 | + namespace: namespace, |
| 34 | + configMaps: m.configMaps, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +type mockConfigMapNamespaceLister struct { |
| 39 | + namespace string |
| 40 | + configMaps map[string]map[string]*corev1.ConfigMap |
| 41 | +} |
| 42 | + |
| 43 | +func (m *mockConfigMapNamespaceLister) List(selector labels.Selector) ([]*corev1.ConfigMap, error) { |
| 44 | + panic("not implemented") |
| 45 | +} |
| 46 | + |
| 47 | +func (m *mockConfigMapNamespaceLister) Get(name string) (*corev1.ConfigMap, error) { |
| 48 | + if m.configMaps == nil { |
| 49 | + return nil, apierrors.NewNotFound(schema.GroupResource{Resource: "configmaps"}, name) |
| 50 | + } |
| 51 | + if namespace, ok := m.configMaps[m.namespace]; ok { |
| 52 | + if cm, ok := namespace[name]; ok { |
| 53 | + return cm, nil |
| 54 | + } |
| 55 | + } |
| 56 | + return nil, apierrors.NewNotFound(schema.GroupResource{Resource: "configmaps"}, name) |
| 57 | +} |
| 58 | + |
| 59 | +func TestCombineCABundleConfigMapsOptimistically(t *testing.T) { |
| 60 | + // Generate test certificates |
| 61 | + validCert, err := crypto.MakeSelfSignedCAConfig("Test CA", time.Hour) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("Failed to create test certificate: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + validCertPEM, err := crypto.EncodeCertificates(validCert.Certs[0]) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Failed to encode certificate: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + validCert2, err := crypto.MakeSelfSignedCAConfig("Test CA", time.Hour) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("Failed to create test certificate: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + validCertPEM2, err := crypto.EncodeCertificates(validCert2.Certs[0]) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("Failed to encode certificate: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + expiredCert, err := crypto.MakeSelfSignedCAConfig("Expired CA", -1*time.Hour) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("Failed to create expired certificate: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + expiredCertPEM, err := crypto.EncodeCertificates(expiredCert.Certs[0]) |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("Failed to encode expired certificate: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + jiraComponent := "foobar" |
| 92 | + destinationConfigMap := &corev1.ConfigMap{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: "cm1", |
| 95 | + Namespace: "ns1", |
| 96 | + Annotations: map[string]string{ |
| 97 | + annotations.OpenShiftComponent: jiraComponent, |
| 98 | + }, |
| 99 | + }, |
| 100 | + Data: map[string]string{ |
| 101 | + "ca-bundle.crt": string(validCertPEM), |
| 102 | + }, |
| 103 | + } |
| 104 | + tests := []struct { |
| 105 | + name string |
| 106 | + destinationConfigMap *corev1.ConfigMap |
| 107 | + mockConfigMaps map[string]map[string]*corev1.ConfigMap |
| 108 | + inputLocations []ResourceLocation |
| 109 | + additionalAnnotations certrotation.AdditionalAnnotations |
| 110 | + expectModified bool |
| 111 | + expectedCABundle *corev1.ConfigMap |
| 112 | + }{ |
| 113 | + { |
| 114 | + name: "combine valid certificates", |
| 115 | + mockConfigMaps: map[string]map[string]*corev1.ConfigMap{ |
| 116 | + "ns1": { |
| 117 | + "cm1": { |
| 118 | + Data: map[string]string{ |
| 119 | + "ca-bundle.crt": string(validCertPEM), |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + "ns2": { |
| 124 | + "cm2": { |
| 125 | + Data: map[string]string{ |
| 126 | + "ca-bundle.crt": string(validCertPEM2), |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + inputLocations: []ResourceLocation{ |
| 132 | + {Namespace: "ns1", Name: "cm1"}, |
| 133 | + {Namespace: "ns2", Name: "cm2"}, |
| 134 | + }, |
| 135 | + additionalAnnotations: certrotation.AdditionalAnnotations{}, |
| 136 | + expectModified: true, |
| 137 | + expectedCABundle: &corev1.ConfigMap{ |
| 138 | + ObjectMeta: metav1.ObjectMeta{ |
| 139 | + Annotations: map[string]string{}, |
| 140 | + }, |
| 141 | + Data: map[string]string{ |
| 142 | + "ca-bundle.crt": fmt.Sprintf("%s%s", string(validCertPEM), string(validCertPEM2)), |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "filter expired certificates", |
| 148 | + destinationConfigMap: &corev1.ConfigMap{ |
| 149 | + ObjectMeta: metav1.ObjectMeta{ |
| 150 | + Name: "cm1", |
| 151 | + Namespace: "ns1", |
| 152 | + Annotations: map[string]string{ |
| 153 | + annotations.OpenShiftComponent: jiraComponent, |
| 154 | + }, |
| 155 | + }, |
| 156 | + Data: map[string]string{ |
| 157 | + "ca-bundle.crt": string(expiredCertPEM), |
| 158 | + }, |
| 159 | + }, |
| 160 | + mockConfigMaps: map[string]map[string]*corev1.ConfigMap{ |
| 161 | + "ns1": { |
| 162 | + "cm1": { |
| 163 | + Data: map[string]string{ |
| 164 | + "ca-bundle.crt": string(validCertPEM), |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + inputLocations: []ResourceLocation{ |
| 170 | + {Namespace: "ns1", Name: "cm1"}, |
| 171 | + }, |
| 172 | + additionalAnnotations: certrotation.AdditionalAnnotations{}, |
| 173 | + expectModified: true, |
| 174 | + expectedCABundle: &corev1.ConfigMap{ |
| 175 | + ObjectMeta: metav1.ObjectMeta{ |
| 176 | + Name: "cm1", |
| 177 | + Namespace: "ns1", |
| 178 | + Annotations: map[string]string{ |
| 179 | + annotations.OpenShiftComponent: jiraComponent, |
| 180 | + }, |
| 181 | + }, |
| 182 | + Data: map[string]string{ |
| 183 | + "ca-bundle.crt": string(validCertPEM), |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "not modified", |
| 189 | + destinationConfigMap: destinationConfigMap, |
| 190 | + mockConfigMaps: map[string]map[string]*corev1.ConfigMap{ |
| 191 | + "ns1": { |
| 192 | + "cm1": { |
| 193 | + Data: map[string]string{ |
| 194 | + "ca-bundle.crt": string(validCertPEM), |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + inputLocations: []ResourceLocation{ |
| 200 | + {Namespace: "ns1", Name: "cm1"}, |
| 201 | + }, |
| 202 | + additionalAnnotations: certrotation.AdditionalAnnotations{ |
| 203 | + JiraComponent: jiraComponent, |
| 204 | + }, |
| 205 | + expectModified: false, |
| 206 | + expectedCABundle: destinationConfigMap, |
| 207 | + }, |
| 208 | + { |
| 209 | + name: "metadata modified only", |
| 210 | + destinationConfigMap: &corev1.ConfigMap{ |
| 211 | + ObjectMeta: metav1.ObjectMeta{ |
| 212 | + Name: "cm1", |
| 213 | + Namespace: "ns1", |
| 214 | + }, |
| 215 | + Data: map[string]string{ |
| 216 | + "ca-bundle.crt": string(validCertPEM), |
| 217 | + }, |
| 218 | + }, |
| 219 | + mockConfigMaps: map[string]map[string]*corev1.ConfigMap{ |
| 220 | + "ns1": { |
| 221 | + "cm1": { |
| 222 | + Data: map[string]string{ |
| 223 | + "ca-bundle.crt": string(validCertPEM), |
| 224 | + }, |
| 225 | + }, |
| 226 | + }, |
| 227 | + }, |
| 228 | + inputLocations: []ResourceLocation{ |
| 229 | + {Namespace: "ns1", Name: "cm1"}, |
| 230 | + }, |
| 231 | + additionalAnnotations: certrotation.AdditionalAnnotations{ |
| 232 | + JiraComponent: jiraComponent, |
| 233 | + }, |
| 234 | + expectModified: true, |
| 235 | + expectedCABundle: destinationConfigMap, |
| 236 | + }, |
| 237 | + { |
| 238 | + name: "contents modified only", |
| 239 | + destinationConfigMap: destinationConfigMap, |
| 240 | + mockConfigMaps: map[string]map[string]*corev1.ConfigMap{ |
| 241 | + "ns1": { |
| 242 | + "cm1": { |
| 243 | + Data: map[string]string{ |
| 244 | + "ca-bundle.crt": string(validCertPEM2), |
| 245 | + }, |
| 246 | + }, |
| 247 | + }, |
| 248 | + }, |
| 249 | + inputLocations: []ResourceLocation{ |
| 250 | + {Namespace: "ns1", Name: "cm1"}, |
| 251 | + }, |
| 252 | + additionalAnnotations: certrotation.AdditionalAnnotations{ |
| 253 | + JiraComponent: jiraComponent, |
| 254 | + }, |
| 255 | + expectModified: true, |
| 256 | + expectedCABundle: &corev1.ConfigMap{ |
| 257 | + ObjectMeta: metav1.ObjectMeta{ |
| 258 | + Name: "cm1", |
| 259 | + Namespace: "ns1", |
| 260 | + Annotations: map[string]string{ |
| 261 | + annotations.OpenShiftComponent: jiraComponent, |
| 262 | + }, |
| 263 | + }, |
| 264 | + Data: map[string]string{ |
| 265 | + "ca-bundle.crt": string(validCertPEM2), |
| 266 | + }, |
| 267 | + }, |
| 268 | + }, |
| 269 | + } |
| 270 | + |
| 271 | + for _, test := range tests { |
| 272 | + t.Run(test.name, func(t *testing.T) { |
| 273 | + lister := &mockConfigMapLister{ |
| 274 | + configMaps: test.mockConfigMaps, |
| 275 | + } |
| 276 | + |
| 277 | + result, modified, err := CombineCABundleConfigMapsOptimistically(test.destinationConfigMap, lister, test.additionalAnnotations, test.inputLocations...) |
| 278 | + |
| 279 | + if err != nil { |
| 280 | + t.Errorf("Unexpected error: %v", err) |
| 281 | + } |
| 282 | + if test.expectModified != modified { |
| 283 | + t.Errorf("Expected modified=%v but got %v", test.expectModified, modified) |
| 284 | + } |
| 285 | + if err == nil && result == nil { |
| 286 | + t.Errorf("Expected result to not be nil when no error occurred") |
| 287 | + } |
| 288 | + |
| 289 | + if result != nil && test.expectedCABundle != nil { |
| 290 | + diff := cmp.Diff(&test.expectedCABundle, &result) |
| 291 | + if diff != "" { |
| 292 | + t.Errorf("Unexpected configmap (-want +got):\n%s", diff) |
| 293 | + } |
| 294 | + } |
| 295 | + }) |
| 296 | + } |
| 297 | +} |
0 commit comments