|
| 1 | +/* |
| 2 | +Copyright 2024 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 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001" |
| 24 | + asoresourcesv1 "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601" |
| 25 | + "github.com/Azure/azure-service-operator/v2/pkg/genruntime" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 31 | + ctrl "sigs.k8s.io/controller-runtime" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 33 | + |
| 34 | + "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha1" |
| 35 | + infrav1alpha "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha1" |
| 36 | +) |
| 37 | + |
| 38 | +func TestManagedClusterAdoptReconcile(t *testing.T) { |
| 39 | + ctx := context.Background() |
| 40 | + g := NewWithT(t) |
| 41 | + req := ctrl.Request{ |
| 42 | + NamespacedName: types.NamespacedName{ |
| 43 | + Name: "fake-mc", |
| 44 | + Namespace: "fake-ns", |
| 45 | + }, |
| 46 | + } |
| 47 | + managedCluster := &asocontainerservicev1.ManagedCluster{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "fake-mc", |
| 50 | + Namespace: "fake-ns", |
| 51 | + Annotations: map[string]string{ |
| 52 | + adoptAnnotation: adoptAnnotationValue, |
| 53 | + }, |
| 54 | + }, |
| 55 | + Spec: asocontainerservicev1.ManagedCluster_Spec{ |
| 56 | + Owner: &genruntime.KnownResourceReference{ |
| 57 | + Name: "fake-mc", |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + rg := &asoresourcesv1.ResourceGroup{ |
| 63 | + ObjectMeta: metav1.ObjectMeta{ |
| 64 | + Name: "fake-mc", |
| 65 | + Namespace: "fake-ns", |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + s := runtime.NewScheme() |
| 70 | + err := asocontainerservicev1.AddToScheme(s) |
| 71 | + g.Expect(err).ToNot(HaveOccurred()) |
| 72 | + err = clusterv1.AddToScheme(s) |
| 73 | + g.Expect(err).ToNot(HaveOccurred()) |
| 74 | + err = asoresourcesv1.AddToScheme(s) |
| 75 | + g.Expect(err).ToNot(HaveOccurred()) |
| 76 | + err = v1alpha1.AddToScheme(s) |
| 77 | + g.Expect(err).ToNot(HaveOccurred()) |
| 78 | + client := fake.NewClientBuilder().WithScheme(s).WithObjects(managedCluster, rg).Build() |
| 79 | + rec := ManagedClusterAdoptReconciler{ |
| 80 | + Client: client, |
| 81 | + } |
| 82 | + _, err = rec.Reconcile(ctx, req) |
| 83 | + g.Expect(err).ToNot(HaveOccurred()) |
| 84 | + mcp := &infrav1alpha.AzureASOManagedControlPlane{} |
| 85 | + err = rec.Get(ctx, types.NamespacedName{Name: managedCluster.Name, Namespace: managedCluster.Namespace}, mcp) |
| 86 | + g.Expect(err).ToNot(HaveOccurred()) |
| 87 | + asomc := &infrav1alpha.AzureASOManagedCluster{} |
| 88 | + err = rec.Get(ctx, types.NamespacedName{Name: managedCluster.Name, Namespace: managedCluster.Namespace}, asomc) |
| 89 | + g.Expect(err).ToNot(HaveOccurred()) |
| 90 | +} |
0 commit comments