|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package controllers |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + "k8s.io/apimachinery/pkg/types" |
| 24 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 25 | + ctrl "sigs.k8s.io/controller-runtime" |
| 26 | +) |
| 27 | + |
| 28 | +func TestClusterToAzureManagedControlPlane(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + controlPlaneRef *corev1.ObjectReference |
| 32 | + expected []ctrl.Request |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "nil", |
| 36 | + controlPlaneRef: nil, |
| 37 | + expected: nil, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "bad kind", |
| 41 | + controlPlaneRef: &corev1.ObjectReference{ |
| 42 | + Kind: "NotAzureManagedControlPlane", |
| 43 | + }, |
| 44 | + expected: nil, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "ok", |
| 48 | + controlPlaneRef: &corev1.ObjectReference{ |
| 49 | + Kind: "AzureManagedControlPlane", |
| 50 | + Name: "name", |
| 51 | + Namespace: "namespace", |
| 52 | + }, |
| 53 | + expected: []ctrl.Request{ |
| 54 | + { |
| 55 | + NamespacedName: types.NamespacedName{ |
| 56 | + Name: "name", |
| 57 | + Namespace: "namespace", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for _, test := range tests { |
| 65 | + t.Run(test.name, func(t *testing.T) { |
| 66 | + g := NewWithT(t) |
| 67 | + actual := (&AzureManagedControlPlaneReconciler{}).ClusterToAzureManagedControlPlane(&clusterv1.Cluster{ |
| 68 | + Spec: clusterv1.ClusterSpec{ |
| 69 | + ControlPlaneRef: test.controlPlaneRef, |
| 70 | + }, |
| 71 | + }) |
| 72 | + if test.expected == nil { |
| 73 | + g.Expect(actual).To(BeNil()) |
| 74 | + } else { |
| 75 | + g.Expect(actual).To(Equal(test.expected)) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
0 commit comments