|
| 1 | +/* |
| 2 | +Copyright 2025 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 | + "github.com/Azure/azure-service-operator/v2/pkg/genruntime" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/utils/ptr" |
| 29 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 30 | + expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" |
| 31 | + ctrl "sigs.k8s.io/controller-runtime" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 33 | + |
| 34 | + infrav1alpha "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha1" |
| 35 | +) |
| 36 | + |
| 37 | +func TestAgentPoolAdoptController(t *testing.T) { |
| 38 | + g := NewWithT(t) |
| 39 | + req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "fake-agent-pool", Namespace: "fake-ns"}} |
| 40 | + ctx := context.Background() |
| 41 | + scheme, err := newScheme() |
| 42 | + g.Expect(err).ToNot(HaveOccurred()) |
| 43 | + |
| 44 | + agentPool := &asocontainerservicev1.ManagedClustersAgentPool{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{ |
| 46 | + Name: "fake-agent-pool", |
| 47 | + Namespace: "fake-ns", |
| 48 | + Annotations: map[string]string{ |
| 49 | + adoptAnnotation: adoptAnnotationValue, |
| 50 | + }, |
| 51 | + }, |
| 52 | + Spec: asocontainerservicev1.ManagedClustersAgentPool_Spec{ |
| 53 | + Count: ptr.To(1), |
| 54 | + Owner: &genruntime.KnownResourceReference{ |
| 55 | + Name: "fake-managed-cluster", |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + mc := &asocontainerservicev1.ManagedCluster{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{ |
| 61 | + Name: "fake-managed-cluster", |
| 62 | + Namespace: "fake-ns", |
| 63 | + OwnerReferences: []metav1.OwnerReference{ |
| 64 | + { |
| 65 | + Kind: infrav1alpha.AzureASOManagedControlPlaneKind, |
| 66 | + APIVersion: infrav1alpha.GroupVersion.Identifier(), |
| 67 | + Name: "fake-managed-cluster", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | + asoManagedControlPlane := &infrav1alpha.AzureASOManagedControlPlane{ |
| 73 | + ObjectMeta: metav1.ObjectMeta{ |
| 74 | + Name: "fake-managed-cluster", |
| 75 | + Namespace: "fake-ns", |
| 76 | + Labels: map[string]string{ |
| 77 | + clusterv1.ClusterNameLabel: "cluster-name", |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + err = asocontainerservicev1.AddToScheme(scheme) |
| 83 | + g.Expect(err).ToNot(HaveOccurred()) |
| 84 | + err = infrav1alpha.AddToScheme(scheme) |
| 85 | + g.Expect(err).ToNot(HaveOccurred()) |
| 86 | + client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(agentPool, mc, asoManagedControlPlane).WithStatusSubresource(mc, agentPool, asoManagedControlPlane).Build() |
| 87 | + aprec := &AgentPoolAdoptReconciler{ |
| 88 | + Client: client, |
| 89 | + } |
| 90 | + _, err = aprec.Reconcile(ctx, req) |
| 91 | + g.Expect(err).ToNot(HaveOccurred()) |
| 92 | + mp := &expv1.MachinePool{} |
| 93 | + err = aprec.Get(ctx, types.NamespacedName{Name: agentPool.Name, Namespace: "fake-ns"}, mp) |
| 94 | + g.Expect(err).ToNot(HaveOccurred()) |
| 95 | + asoMP := &infrav1alpha.AzureASOManagedMachinePool{} |
| 96 | + err = aprec.Get(ctx, types.NamespacedName{Name: agentPool.Name, Namespace: "fake-ns"}, asoMP) |
| 97 | + g.Expect(err).ToNot(HaveOccurred()) |
| 98 | +} |
0 commit comments