github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/argocd/custommapper_test.go (about) 1 package argocd 2 3 import ( 4 "context" 5 "reflect" 6 "testing" 7 8 configv1 "github.com/openshift/api/config/v1" 9 routev1 "github.com/openshift/api/route/v1" 10 "github.com/stretchr/testify/assert" 11 12 argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1" 13 "github.com/argoproj-labs/argocd-operator/common" 14 15 corev1 "k8s.io/api/core/v1" 16 17 rbacv1 "k8s.io/api/rbac/v1" 18 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 19 20 "k8s.io/apimachinery/pkg/runtime" 21 "k8s.io/apimachinery/pkg/types" 22 "sigs.k8s.io/controller-runtime/pkg/client" 23 "sigs.k8s.io/controller-runtime/pkg/reconcile" 24 ) 25 26 func TestReconcileArgoCD_clusterRoleBindingMapper(t *testing.T) { 27 28 type fields struct { 29 client client.Client 30 scheme *runtime.Scheme 31 } 32 type args struct { 33 o client.Object 34 } 35 tests := []struct { 36 name string 37 fields fields 38 args args 39 want []reconcile.Request 40 }{ 41 { 42 name: "crb incorrectly annotated", 43 fields: fields{}, 44 args: args{ 45 &rbacv1.ClusterRoleBinding{ 46 ObjectMeta: metav1.ObjectMeta{ 47 Annotations: map[string]string{ 48 "argocds.argoproj.io/name": "foo", 49 "foo/namespace": "foo-ns", 50 }, 51 }, 52 }, 53 }, 54 want: []reconcile.Request{}, 55 }, 56 { 57 name: "crb associated with ArgoCD", 58 fields: fields{}, 59 args: args{ 60 &rbacv1.ClusterRoleBinding{ 61 ObjectMeta: metav1.ObjectMeta{ 62 Annotations: map[string]string{ 63 "argocds.argoproj.io/name": "foo", 64 "argocds.argoproj.io/namespace": "foo-ns", 65 }, 66 }, 67 }, 68 }, 69 want: []reconcile.Request{ 70 { 71 NamespacedName: types.NamespacedName{ 72 Name: "foo", 73 Namespace: "foo-ns", 74 }, 75 }, 76 }, 77 }, 78 { 79 name: "crb not associated with ArgoCD", 80 fields: fields{}, 81 args: args{ 82 &rbacv1.ClusterRoleBinding{ 83 ObjectMeta: metav1.ObjectMeta{ 84 Annotations: map[string]string{ 85 "foo/name": "foo", 86 "foo/namespace": "foo-ns", 87 }, 88 }, 89 }, 90 }, 91 92 want: []reconcile.Request{}, 93 }, 94 } 95 for _, tt := range tests { 96 t.Run(tt.name, func(t *testing.T) { 97 r := &ReconcileArgoCD{ 98 Client: tt.fields.client, 99 Scheme: tt.fields.scheme, 100 } 101 if got := r.clusterResourceMapper(context.TODO(), tt.args.o); !reflect.DeepEqual(got, tt.want) { 102 t.Errorf("ReconcileArgoCD.clusterRoleBindingMapper() = %v, want %v", got, tt.want) 103 } 104 }) 105 } 106 } 107 108 func TestReconcileArgoCD_tlsSecretMapperRepoServer(t *testing.T) { 109 argocd := &argoproj.ArgoCD{ 110 ObjectMeta: metav1.ObjectMeta{ 111 Name: "argocd", 112 Namespace: "argocd-operator", 113 UID: "abcd", 114 }, 115 } 116 117 t.Run("Map with proper ownerReference", func(t *testing.T) { 118 service := &corev1.Service{ 119 ObjectMeta: metav1.ObjectMeta{ 120 Name: "argocd-repo-server", 121 Namespace: "argocd-operator", 122 OwnerReferences: []metav1.OwnerReference{ 123 { 124 APIVersion: "argoproj.io/v1alpha1", 125 Kind: "ArgoCD", 126 Name: "argocd", 127 UID: argocd.GetUID(), 128 }, 129 }, 130 UID: "service-123", 131 }, 132 } 133 secret := &corev1.Secret{ 134 ObjectMeta: metav1.ObjectMeta{ 135 Name: "argocd-repo-server-tls", 136 Namespace: "argocd-operator", 137 OwnerReferences: []metav1.OwnerReference{ 138 { 139 APIVersion: "v1", 140 Kind: "Service", 141 Name: "argocd-repo-server", 142 UID: service.GetUID(), 143 }, 144 }, 145 }, 146 Type: corev1.SecretTypeTLS, 147 Data: map[string][]byte{ 148 corev1.TLSCertKey: []byte("foo"), 149 corev1.TLSPrivateKeyKey: []byte("bar"), 150 }, 151 } 152 153 resObjs := []client.Object{argocd, secret, service} 154 subresObjs := []client.Object{argocd} 155 runtimeObjs := []runtime.Object{} 156 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 157 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 158 r := makeTestReconciler(cl, sch) 159 160 want := []reconcile.Request{ 161 { 162 NamespacedName: types.NamespacedName{ 163 Name: "argocd", 164 Namespace: "argocd-operator", 165 }, 166 }, 167 } 168 got := r.tlsSecretMapper(context.TODO(), secret) 169 if !reflect.DeepEqual(got, want) { 170 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 171 } 172 }) 173 174 t.Run("Map with ownerReference on non-existing owner", func(t *testing.T) { 175 service := &corev1.Service{ 176 ObjectMeta: metav1.ObjectMeta{ 177 Name: "argocd-repo-server", 178 Namespace: "argocd-operator", 179 OwnerReferences: []metav1.OwnerReference{ 180 { 181 APIVersion: "argoproj.io/v1alpha1", 182 Kind: "ArgoCD", 183 Name: "argocd", 184 UID: argocd.GetUID(), 185 }, 186 }, 187 UID: "service-123", 188 }, 189 } 190 secret := &corev1.Secret{ 191 ObjectMeta: metav1.ObjectMeta{ 192 Name: "argocd-repo-server-tls", 193 Namespace: "argocd-operator", 194 OwnerReferences: []metav1.OwnerReference{ 195 { 196 APIVersion: "v1", 197 Kind: "Service", 198 Name: "argocd-repo-server", 199 UID: service.GetUID(), 200 }, 201 }, 202 }, 203 Type: corev1.SecretTypeTLS, 204 Data: map[string][]byte{ 205 corev1.TLSCertKey: []byte("foo"), 206 corev1.TLSPrivateKeyKey: []byte("bar"), 207 }, 208 } 209 210 resObjs := []client.Object{argocd, secret} 211 subresObjs := []client.Object{argocd} 212 runtimeObjs := []runtime.Object{} 213 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 214 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 215 r := makeTestReconciler(cl, sch) 216 217 want := []reconcile.Request{} 218 got := r.tlsSecretMapper(context.TODO(), secret) 219 if !reflect.DeepEqual(got, want) { 220 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 221 } 222 }) 223 224 t.Run("Map with invalid owner", func(t *testing.T) { 225 service := &corev1.Service{ 226 ObjectMeta: metav1.ObjectMeta{ 227 Name: "argocd-repo-server", 228 Namespace: "argocd-operator", 229 OwnerReferences: []metav1.OwnerReference{ 230 { 231 APIVersion: "argoproj.io/v1alpha1", 232 Kind: "ArgoCD", 233 Name: "argocd", 234 UID: argocd.GetUID(), 235 }, 236 }, 237 UID: "service-123", 238 }, 239 } 240 secret := &corev1.Secret{ 241 ObjectMeta: metav1.ObjectMeta{ 242 Name: "argocd-repo-server-tls", 243 Namespace: "argocd-operator", 244 OwnerReferences: []metav1.OwnerReference{ 245 { 246 APIVersion: "v1", 247 Kind: "Service", 248 Name: "argocd-server", 249 UID: service.GetUID(), 250 }, 251 }, 252 }, 253 Type: corev1.SecretTypeTLS, 254 Data: map[string][]byte{ 255 corev1.TLSCertKey: []byte("foo"), 256 corev1.TLSPrivateKeyKey: []byte("bar"), 257 }, 258 } 259 260 resObjs := []client.Object{argocd, secret, service} 261 subresObjs := []client.Object{argocd} 262 runtimeObjs := []runtime.Object{} 263 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 264 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 265 r := makeTestReconciler(cl, sch) 266 267 want := []reconcile.Request{} 268 got := r.tlsSecretMapper(context.TODO(), secret) 269 if !reflect.DeepEqual(got, want) { 270 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 271 } 272 }) 273 274 t.Run("Map with owner annotation", func(t *testing.T) { 275 secret := &corev1.Secret{ 276 ObjectMeta: metav1.ObjectMeta{ 277 Name: "argocd-repo-server-tls", 278 Namespace: "argocd-operator", 279 Annotations: map[string]string{ 280 common.AnnotationName: "argocd", 281 }, 282 }, 283 Type: corev1.SecretTypeTLS, 284 Data: map[string][]byte{ 285 corev1.TLSCertKey: []byte("foo"), 286 corev1.TLSPrivateKeyKey: []byte("bar"), 287 }, 288 } 289 290 resObjs := []client.Object{argocd, secret} 291 subresObjs := []client.Object{argocd} 292 runtimeObjs := []runtime.Object{} 293 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 294 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 295 r := makeTestReconciler(cl, sch) 296 297 want := []reconcile.Request{ 298 { 299 NamespacedName: types.NamespacedName{ 300 Name: "argocd", 301 Namespace: "argocd-operator", 302 }, 303 }, 304 } 305 got := r.tlsSecretMapper(context.TODO(), secret) 306 if !reflect.DeepEqual(got, want) { 307 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 308 } 309 }) 310 311 t.Run("Map without owner and without annotation", func(t *testing.T) { 312 secret := &corev1.Secret{ 313 ObjectMeta: metav1.ObjectMeta{ 314 Name: "argocd-repo-server-tls", 315 Namespace: "argocd-operator", 316 }, 317 Type: corev1.SecretTypeTLS, 318 Data: map[string][]byte{ 319 corev1.TLSCertKey: []byte("foo"), 320 corev1.TLSPrivateKeyKey: []byte("bar"), 321 }, 322 } 323 324 resObjs := []client.Object{argocd, secret} 325 subresObjs := []client.Object{argocd} 326 runtimeObjs := []runtime.Object{} 327 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 328 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 329 r := makeTestReconciler(cl, sch) 330 331 want := []reconcile.Request{} 332 got := r.tlsSecretMapper(context.TODO(), secret) 333 if !reflect.DeepEqual(got, want) { 334 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 335 } 336 }) 337 338 } 339 340 func TestReconcileArgoCD_tlsSecretMapperRedis(t *testing.T) { 341 argocd := &argoproj.ArgoCD{ 342 ObjectMeta: metav1.ObjectMeta{ 343 Name: "argocd", 344 Namespace: "argocd-operator", 345 UID: "abcd", 346 }, 347 } 348 349 t.Run("Map with proper ownerReference", func(t *testing.T) { 350 service := &corev1.Service{ 351 ObjectMeta: metav1.ObjectMeta{ 352 Name: "argocd-redis", 353 Namespace: "argocd-operator", 354 OwnerReferences: []metav1.OwnerReference{ 355 { 356 APIVersion: "argoproj.io/v1alpha1", 357 Kind: "ArgoCD", 358 Name: "argocd", 359 UID: argocd.GetUID(), 360 }, 361 }, 362 UID: "service-123", 363 }, 364 } 365 secret := &corev1.Secret{ 366 ObjectMeta: metav1.ObjectMeta{ 367 Name: "argocd-operator-redis-tls", 368 Namespace: "argocd-operator", 369 OwnerReferences: []metav1.OwnerReference{ 370 { 371 APIVersion: "v1", 372 Kind: "Service", 373 Name: "argocd-redis", 374 UID: service.GetUID(), 375 }, 376 }, 377 }, 378 Type: corev1.SecretTypeTLS, 379 Data: map[string][]byte{ 380 corev1.TLSCertKey: []byte("foo"), 381 corev1.TLSPrivateKeyKey: []byte("bar"), 382 }, 383 } 384 385 resObjs := []client.Object{argocd, secret, service} 386 subresObjs := []client.Object{argocd} 387 runtimeObjs := []runtime.Object{} 388 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 389 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 390 r := makeTestReconciler(cl, sch) 391 392 want := []reconcile.Request{ 393 { 394 NamespacedName: types.NamespacedName{ 395 Name: "argocd", 396 Namespace: "argocd-operator", 397 }, 398 }, 399 } 400 got := r.tlsSecretMapper(context.TODO(), secret) 401 if !reflect.DeepEqual(got, want) { 402 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 403 } 404 }) 405 406 t.Run("Map with ownerReference on non-existing owner", func(t *testing.T) { 407 service := &corev1.Service{ 408 ObjectMeta: metav1.ObjectMeta{ 409 Name: "argocd-redis", 410 Namespace: "argocd-operator", 411 OwnerReferences: []metav1.OwnerReference{ 412 { 413 APIVersion: "argoproj.io/v1alpha1", 414 Kind: "ArgoCD", 415 Name: "argocd", 416 UID: argocd.GetUID(), 417 }, 418 }, 419 UID: "service-123", 420 }, 421 } 422 secret := &corev1.Secret{ 423 ObjectMeta: metav1.ObjectMeta{ 424 Name: "argocd-operator-redis-tls", 425 Namespace: "argocd-operator", 426 OwnerReferences: []metav1.OwnerReference{ 427 { 428 APIVersion: "v1", 429 Kind: "Service", 430 Name: "argocd-redis", 431 UID: service.GetUID(), 432 }, 433 }, 434 }, 435 Type: corev1.SecretTypeTLS, 436 Data: map[string][]byte{ 437 corev1.TLSCertKey: []byte("foo"), 438 corev1.TLSPrivateKeyKey: []byte("bar"), 439 }, 440 } 441 442 resObjs := []client.Object{argocd, secret} 443 subresObjs := []client.Object{argocd} 444 runtimeObjs := []runtime.Object{} 445 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 446 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 447 r := makeTestReconciler(cl, sch) 448 449 want := []reconcile.Request{} 450 got := r.tlsSecretMapper(context.TODO(), secret) 451 if !reflect.DeepEqual(got, want) { 452 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 453 } 454 }) 455 456 t.Run("Map with invalid owner", func(t *testing.T) { 457 service := &corev1.Service{ 458 ObjectMeta: metav1.ObjectMeta{ 459 Name: "argocd-redis", 460 Namespace: "argocd-operator", 461 OwnerReferences: []metav1.OwnerReference{ 462 { 463 APIVersion: "argoproj.io/v1alpha1", 464 Kind: "ArgoCD", 465 Name: "argocd", 466 UID: argocd.GetUID(), 467 }, 468 }, 469 UID: "service-123", 470 }, 471 } 472 secret := &corev1.Secret{ 473 ObjectMeta: metav1.ObjectMeta{ 474 Name: "argocd-operator-redis-tls", 475 Namespace: "argocd-operator", 476 OwnerReferences: []metav1.OwnerReference{ 477 { 478 APIVersion: "v1", 479 Kind: "Service", 480 Name: "argocd-server", 481 UID: service.GetUID(), 482 }, 483 }, 484 }, 485 Type: corev1.SecretTypeTLS, 486 Data: map[string][]byte{ 487 corev1.TLSCertKey: []byte("foo"), 488 corev1.TLSPrivateKeyKey: []byte("bar"), 489 }, 490 } 491 492 resObjs := []client.Object{argocd, secret, service} 493 subresObjs := []client.Object{argocd} 494 runtimeObjs := []runtime.Object{} 495 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 496 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 497 r := makeTestReconciler(cl, sch) 498 499 want := []reconcile.Request{} 500 got := r.tlsSecretMapper(context.TODO(), secret) 501 if !reflect.DeepEqual(got, want) { 502 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 503 } 504 }) 505 506 t.Run("Map with owner annotation", func(t *testing.T) { 507 secret := &corev1.Secret{ 508 ObjectMeta: metav1.ObjectMeta{ 509 Name: "argocd-operator-redis-tls", 510 Namespace: "argocd-operator", 511 Annotations: map[string]string{ 512 common.AnnotationName: "argocd", 513 }, 514 }, 515 Type: corev1.SecretTypeTLS, 516 Data: map[string][]byte{ 517 corev1.TLSCertKey: []byte("foo"), 518 corev1.TLSPrivateKeyKey: []byte("bar"), 519 }, 520 } 521 522 resObjs := []client.Object{argocd, secret} 523 subresObjs := []client.Object{argocd} 524 runtimeObjs := []runtime.Object{} 525 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 526 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 527 r := makeTestReconciler(cl, sch) 528 529 want := []reconcile.Request{ 530 { 531 NamespacedName: types.NamespacedName{ 532 Name: "argocd", 533 Namespace: "argocd-operator", 534 }, 535 }, 536 } 537 got := r.tlsSecretMapper(context.TODO(), secret) 538 if !reflect.DeepEqual(got, want) { 539 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 540 } 541 }) 542 543 t.Run("Map without owner and without annotation", func(t *testing.T) { 544 secret := &corev1.Secret{ 545 ObjectMeta: metav1.ObjectMeta{ 546 Name: "argocd-operator-redis-tls", 547 Namespace: "argocd-operator", 548 }, 549 Type: corev1.SecretTypeTLS, 550 Data: map[string][]byte{ 551 corev1.TLSCertKey: []byte("foo"), 552 corev1.TLSPrivateKeyKey: []byte("bar"), 553 }, 554 } 555 556 resObjs := []client.Object{argocd, secret} 557 subresObjs := []client.Object{argocd} 558 runtimeObjs := []runtime.Object{} 559 sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) 560 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 561 r := makeTestReconciler(cl, sch) 562 563 want := []reconcile.Request{} 564 got := r.tlsSecretMapper(context.TODO(), secret) 565 if !reflect.DeepEqual(got, want) { 566 t.Errorf("Reconciliation unsucessful: got: %v, want: %v", got, want) 567 } 568 }) 569 570 } 571 572 func TestReconcileArgoCD_namespaceResourceMapperWithManagedByLabel(t *testing.T) { 573 a := makeTestArgoCD() 574 575 resObjs := []client.Object{a} 576 subresObjs := []client.Object{a} 577 runtimeObjs := []runtime.Object{} 578 sch := makeTestReconcilerScheme(argoproj.AddToScheme) 579 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 580 r := makeTestReconciler(cl, sch) 581 582 a.Namespace = "newTestNamespace" 583 584 // Fake client returns an error if ResourceVersion is not nil 585 a.ResourceVersion = "" 586 assert.NoError(t, r.Client.Create(context.TODO(), a)) 587 588 type test struct { 589 name string 590 o client.Object 591 want []reconcile.Request 592 } 593 594 tests := []test{ 595 { 596 name: "test when namespace is labelled", 597 o: &corev1.Namespace{ 598 ObjectMeta: metav1.ObjectMeta{ 599 Name: "testNamespace", 600 Labels: map[string]string{ 601 common.ArgoCDManagedByLabel: a.Namespace, 602 }, 603 }, 604 }, 605 want: []reconcile.Request{ 606 { 607 NamespacedName: types.NamespacedName{ 608 Name: a.Name, 609 Namespace: a.Namespace, 610 }, 611 }, 612 }, 613 }, 614 { 615 name: "test when namespace is not labelled", 616 o: &corev1.Namespace{ 617 ObjectMeta: metav1.ObjectMeta{ 618 Name: "testNamespace", 619 Labels: make(map[string]string), 620 }, 621 }, 622 want: []reconcile.Request{}, 623 }, 624 } 625 626 for _, tt := range tests { 627 t.Run(tt.name, func(t *testing.T) { 628 if got := r.namespaceResourceMapper(context.TODO(), tt.o); !reflect.DeepEqual(got, tt.want) { 629 t.Errorf("ReconcileArgoCD.namespaceResourceMapper(), got = %v, want = %v", got, tt.want) 630 } 631 }) 632 } 633 } 634 635 func TestReconcileArgoCD_namespaceResourceMapperForSpecificNamespaceWithoutManagedByLabel(t *testing.T) { 636 argocd1 := makeTestArgoCD() 637 resObjs := []client.Object{argocd1} 638 subresObjs := []client.Object{argocd1} 639 runtimeObjs := []runtime.Object{} 640 sch := makeTestReconcilerScheme(argoproj.AddToScheme) 641 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 642 r := makeTestReconciler(cl, sch) 643 644 argocd1.Name = "argocd1" 645 argocd1.Namespace = "argo-test-1" 646 argocd1.Spec.SourceNamespaces = append(argocd1.Spec.SourceNamespaces, "test-namespace-1") 647 // Fake client returns an error if ResourceVersion is not nil 648 argocd1.ResourceVersion = "" 649 650 assert.NoError(t, r.Client.Create(context.TODO(), argocd1)) 651 652 type test struct { 653 name string 654 o client.Object 655 want []reconcile.Request 656 } 657 658 tests := []test{ 659 { 660 name: "Reconcile for Namespace 'test-namespace-1'", 661 o: &corev1.Namespace{ 662 ObjectMeta: metav1.ObjectMeta{ 663 Name: "test-namespace-1", 664 }, 665 }, 666 want: []reconcile.Request{ 667 { 668 NamespacedName: types.NamespacedName{ 669 Name: argocd1.Name, 670 Namespace: argocd1.Namespace, 671 }, 672 }, 673 }, 674 }, 675 { 676 name: "No Reconcile for Namespace 'test-namespace-2'", 677 o: &corev1.Namespace{ 678 ObjectMeta: metav1.ObjectMeta{ 679 Name: "test-namespace-2", 680 }, 681 }, 682 want: []reconcile.Request{}, 683 }, 684 } 685 686 for _, tt := range tests { 687 t.Run(tt.name, func(t *testing.T) { 688 if got := r.namespaceResourceMapper(context.TODO(), tt.o); !assert.ElementsMatch(t, got, tt.want) { 689 t.Errorf("ReconcileArgoCD.sourceNamespaceMapper(), got = %v, want = %v", got, tt.want) 690 } 691 }) 692 } 693 } 694 695 func TestReconcileArgoCD_namespaceResourceMapperForWildCardPatternNamespaceWithoutManagedByLabel(t *testing.T) { 696 argocd1 := makeTestArgoCD() 697 resObjs := []client.Object{argocd1} 698 subresObjs := []client.Object{argocd1} 699 runtimeObjs := []runtime.Object{} 700 sch := makeTestReconcilerScheme(argoproj.AddToScheme) 701 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 702 r := makeTestReconciler(cl, sch) 703 704 argocd1.Name = "argocd1" 705 argocd1.Namespace = "argo-test-1" 706 argocd1.Spec.SourceNamespaces = append(argocd1.Spec.SourceNamespaces, "test*") 707 // Fake client returns an error if ResourceVersion is not nil 708 argocd1.ResourceVersion = "" 709 710 assert.NoError(t, r.Client.Create(context.TODO(), argocd1)) 711 712 type test struct { 713 name string 714 o client.Object 715 want []reconcile.Request 716 } 717 718 tests := []test{ 719 { 720 name: "Reconcile for Namespace 'test-namespace-1'", 721 o: &corev1.Namespace{ 722 ObjectMeta: metav1.ObjectMeta{ 723 Name: "test-namespace-1", 724 }, 725 }, 726 want: []reconcile.Request{ 727 { 728 NamespacedName: types.NamespacedName{ 729 Name: argocd1.Name, 730 Namespace: argocd1.Namespace, 731 }, 732 }, 733 }, 734 }, 735 { 736 name: "Reconcile for Namespace 'test-namespace-2'", 737 o: &corev1.Namespace{ 738 ObjectMeta: metav1.ObjectMeta{ 739 Name: "test-namespace-2", 740 }, 741 }, 742 want: []reconcile.Request{ 743 { 744 NamespacedName: types.NamespacedName{ 745 Name: argocd1.Name, 746 Namespace: argocd1.Namespace, 747 }, 748 }, 749 }, 750 }, 751 { 752 name: "No Reconcile for Namespace 'prod-namespace-1'", 753 o: &corev1.Namespace{ 754 ObjectMeta: metav1.ObjectMeta{ 755 Name: "prod-namespace-1", 756 }, 757 }, 758 want: []reconcile.Request{}, 759 }, 760 } 761 762 for _, tt := range tests { 763 t.Run(tt.name, func(t *testing.T) { 764 if got := r.namespaceResourceMapper(context.TODO(), tt.o); !assert.ElementsMatch(t, got, tt.want) { 765 t.Errorf("ReconcileArgoCD.sourceNamespaceMapper(), got = %v, want = %v", got, tt.want) 766 } 767 }) 768 } 769 } 770 771 func TestReconcileArgoCD_namespaceResourceMapperForMultipleSourceNamespacesWithoutManagedByLabel(t *testing.T) { 772 argocd1 := makeTestArgoCD() 773 resObjs := []client.Object{argocd1} 774 subresObjs := []client.Object{argocd1} 775 runtimeObjs := []runtime.Object{} 776 sch := makeTestReconcilerScheme(argoproj.AddToScheme) 777 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 778 r := makeTestReconciler(cl, sch) 779 780 argocd1.Name = "argocd1" 781 argocd1.Namespace = "argo-test-1" 782 argocd1.Spec.SourceNamespaces = append(argocd1.Spec.SourceNamespaces, "test*", "dev*") 783 // Fake client returns an error if ResourceVersion is not nil 784 argocd1.ResourceVersion = "" 785 786 assert.NoError(t, r.Client.Create(context.TODO(), argocd1)) 787 788 type test struct { 789 name string 790 o client.Object 791 want []reconcile.Request 792 } 793 794 tests := []test{ 795 { 796 name: "Reconcile for Namespace 'test-namespace-1'", 797 o: &corev1.Namespace{ 798 ObjectMeta: metav1.ObjectMeta{ 799 Name: "test-namespace-1", 800 }, 801 }, 802 want: []reconcile.Request{ 803 { 804 NamespacedName: types.NamespacedName{ 805 Name: argocd1.Name, 806 Namespace: argocd1.Namespace, 807 }, 808 }, 809 }, 810 }, 811 { 812 name: "Reconcile for Namespace 'test-namespace-2'", 813 o: &corev1.Namespace{ 814 ObjectMeta: metav1.ObjectMeta{ 815 Name: "test-namespace-2", 816 }, 817 }, 818 want: []reconcile.Request{ 819 { 820 NamespacedName: types.NamespacedName{ 821 Name: argocd1.Name, 822 Namespace: argocd1.Namespace, 823 }, 824 }, 825 }, 826 }, 827 { 828 name: "Reconcile for Namespace 'dev-namespace-1'", 829 o: &corev1.Namespace{ 830 ObjectMeta: metav1.ObjectMeta{ 831 Name: "dev-namespace-1", 832 }, 833 }, 834 want: []reconcile.Request{ 835 { 836 NamespacedName: types.NamespacedName{ 837 Name: argocd1.Name, 838 Namespace: argocd1.Namespace, 839 }, 840 }, 841 }, 842 }, 843 { 844 name: "No Reconcile for Namespace 'prod-namespace-1'", 845 o: &corev1.Namespace{ 846 ObjectMeta: metav1.ObjectMeta{ 847 Name: "prod-namespace-1", 848 }, 849 }, 850 want: []reconcile.Request{}, 851 }, 852 } 853 854 for _, tt := range tests { 855 t.Run(tt.name, func(t *testing.T) { 856 if got := r.namespaceResourceMapper(context.TODO(), tt.o); !assert.ElementsMatch(t, got, tt.want) { 857 t.Errorf("ReconcileArgoCD.sourceNamespaceMapper(), got = %v, want = %v", got, tt.want) 858 } 859 }) 860 } 861 } 862 863 func TestReconcileArgoCD_namespaceResourceMapperForWildCardNamespaceWithoutManagedByLabel(t *testing.T) { 864 argocd1 := makeTestArgoCD() 865 resObjs := []client.Object{argocd1} 866 subresObjs := []client.Object{argocd1} 867 runtimeObjs := []runtime.Object{} 868 sch := makeTestReconcilerScheme(argoproj.AddToScheme) 869 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 870 r := makeTestReconciler(cl, sch) 871 872 argocd1.Name = "argocd1" 873 argocd1.Namespace = "argo-test-1" 874 argocd1.Spec.SourceNamespaces = append(argocd1.Spec.SourceNamespaces, "*") 875 // Fake client returns an error if ResourceVersion is not nil 876 argocd1.ResourceVersion = "" 877 878 assert.NoError(t, r.Client.Create(context.TODO(), argocd1)) 879 880 type test struct { 881 name string 882 o client.Object 883 want []reconcile.Request 884 } 885 886 tests := []test{ 887 { 888 name: "Reconcile for Namespace 'test-namespace-1'", 889 o: &corev1.Namespace{ 890 ObjectMeta: metav1.ObjectMeta{ 891 Name: "test-namespace-1", 892 }, 893 }, 894 want: []reconcile.Request{ 895 { 896 NamespacedName: types.NamespacedName{ 897 Name: argocd1.Name, 898 Namespace: argocd1.Namespace, 899 }, 900 }, 901 }, 902 }, 903 { 904 name: "Reconcile for Namespace 'test-namespace-2'", 905 o: &corev1.Namespace{ 906 ObjectMeta: metav1.ObjectMeta{ 907 Name: "test-namespace-2", 908 }, 909 }, 910 want: []reconcile.Request{ 911 { 912 NamespacedName: types.NamespacedName{ 913 Name: argocd1.Name, 914 Namespace: argocd1.Namespace, 915 }, 916 }, 917 }, 918 }, 919 { 920 name: "Reconcile for Namespace 'prod-namespace-1'", 921 o: &corev1.Namespace{ 922 ObjectMeta: metav1.ObjectMeta{ 923 Name: "prod-namespace-1", 924 }, 925 }, 926 want: []reconcile.Request{ 927 { 928 NamespacedName: types.NamespacedName{ 929 Name: argocd1.Name, 930 Namespace: argocd1.Namespace, 931 }, 932 }, 933 }, 934 }, 935 } 936 937 for _, tt := range tests { 938 t.Run(tt.name, func(t *testing.T) { 939 if got := r.namespaceResourceMapper(context.TODO(), tt.o); !assert.ElementsMatch(t, got, tt.want) { 940 t.Errorf("ReconcileArgoCD.sourceNamespaceMapper(), got = %v, want = %v", got, tt.want) 941 } 942 }) 943 } 944 }