|
| 1 | +/* |
| 2 | +Portions Copyright (c) Microsoft Corporation. |
| 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 instancetype_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + "github.com/samber/lo" |
| 27 | + |
| 28 | + "github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute" |
| 29 | + "github.com/Azure/karpenter-provider-azure/pkg/apis" |
| 30 | + instancetypecontroller "github.com/Azure/karpenter-provider-azure/pkg/controllers/instancetype" |
| 31 | + "github.com/Azure/karpenter-provider-azure/pkg/fake" |
| 32 | + "github.com/Azure/karpenter-provider-azure/pkg/operator/options" |
| 33 | + "github.com/Azure/karpenter-provider-azure/pkg/test" |
| 34 | + coreoptions "sigs.k8s.io/karpenter/pkg/operator/options" |
| 35 | + coretest "sigs.k8s.io/karpenter/pkg/test" |
| 36 | + . "sigs.k8s.io/karpenter/pkg/test/expectations" |
| 37 | + "sigs.k8s.io/karpenter/pkg/test/v1alpha1" |
| 38 | + . "sigs.k8s.io/karpenter/pkg/utils/testing" |
| 39 | +) |
| 40 | + |
| 41 | +var ctx context.Context |
| 42 | +var env *coretest.Environment |
| 43 | +var azureEnv *test.Environment |
| 44 | +var controller *instancetypecontroller.Controller |
| 45 | + |
| 46 | +func TestController(t *testing.T) { |
| 47 | + ctx = TestContextWithLogger(t) |
| 48 | + RegisterFailHandler(Fail) |
| 49 | + RunSpecs(t, "InstanceTypeController") |
| 50 | +} |
| 51 | + |
| 52 | +var _ = BeforeSuite(func() { |
| 53 | + ctx = coreoptions.ToContext(ctx, coretest.Options()) |
| 54 | + ctx = options.ToContext(ctx, test.Options()) |
| 55 | + env = coretest.NewEnvironment(coretest.WithCRDs(apis.CRDs...), coretest.WithCRDs(v1alpha1.CRDs...)) |
| 56 | + azureEnv = test.NewEnvironment(ctx, env) |
| 57 | + controller = instancetypecontroller.NewController(azureEnv.InstanceTypesProvider) |
| 58 | +}) |
| 59 | + |
| 60 | +var _ = AfterSuite(func() { |
| 61 | + Expect(env.Stop()).To(Succeed(), "Failed to stop environment") |
| 62 | +}) |
| 63 | + |
| 64 | +var _ = BeforeEach(func() { |
| 65 | + azureEnv.Reset() |
| 66 | +}) |
| 67 | + |
| 68 | +var _ = AfterEach(func() { |
| 69 | + ExpectCleanedUp(ctx, env.Client) |
| 70 | +}) |
| 71 | + |
| 72 | +var _ = Describe("InstanceType Controller", func() { |
| 73 | + It("should return a requeue interval of 12 hours", func() { |
| 74 | + result := ExpectSingletonReconciled(ctx, controller) |
| 75 | + Expect(result.RequeueAfter).To(Equal(instancetypecontroller.InstanceTypesRefreshInterval)) |
| 76 | + }) |
| 77 | + |
| 78 | + It("should List after reconciliation", func() { |
| 79 | + // Flush the cache to simulate a cold start |
| 80 | + azureEnv.InstanceTypesProvider.Reset() |
| 81 | + |
| 82 | + // Reconcile to populate instance types |
| 83 | + ExpectSingletonReconciled(ctx, controller) |
| 84 | + |
| 85 | + nodeClass := test.AKSNodeClass() |
| 86 | + instanceTypes, err := azureEnv.InstanceTypesProvider.List(ctx, nodeClass) |
| 87 | + Expect(err).To(BeNil()) |
| 88 | + Expect(instanceTypes).NotTo(BeEmpty()) |
| 89 | + }) |
| 90 | + |
| 91 | + It("should fail reconciliation when the SKU API returns an error", func() { |
| 92 | + azureEnv.SKUsAPI.Error = fmt.Errorf("simulated SKU API failure") |
| 93 | + |
| 94 | + err := ExpectSingletonReconcileFailed(ctx, controller) |
| 95 | + Expect(err).To(HaveOccurred()) |
| 96 | + Expect(err.Error()).To(ContainSubstring("fetching SKUs using skewer")) |
| 97 | + }) |
| 98 | + |
| 99 | + It("should update instance types on subsequent reconciliations", func() { |
| 100 | + // First reconcile |
| 101 | + ExpectSingletonReconciled(ctx, controller) |
| 102 | + |
| 103 | + nodeClass := test.AKSNodeClass() |
| 104 | + instanceTypes, err := azureEnv.InstanceTypesProvider.List(ctx, nodeClass) |
| 105 | + Expect(err).To(BeNil()) |
| 106 | + Expect(instanceTypes).NotTo(BeEmpty()) |
| 107 | + Expect(instanceTypes).ToNot(ContainElement(HaveField("Name", Equal("Standard_D64s_v6")))) |
| 108 | + |
| 109 | + // create a copy of the slice so we can revert it to its old state afterwards |
| 110 | + copy := append([]compute.ResourceSku{}, fake.ResourceSkus[fake.Region]...) |
| 111 | + fake.ResourceSkus[fake.Region] = append(fake.ResourceSkus[fake.Region], |
| 112 | + compute.ResourceSku{ |
| 113 | + Name: lo.ToPtr("Standard_D64s_v6"), |
| 114 | + Tier: lo.ToPtr("Stanadard"), |
| 115 | + Kind: lo.ToPtr(""), |
| 116 | + Size: lo.ToPtr("D64s_v6"), |
| 117 | + Family: lo.ToPtr("standardD64s_v6Family"), |
| 118 | + ResourceType: lo.ToPtr("virtualMachines"), |
| 119 | + APIVersions: &[]string{}, |
| 120 | + Costs: &[]compute.ResourceSkuCosts{}, |
| 121 | + Restrictions: &[]compute.ResourceSkuRestrictions{}, |
| 122 | + Capabilities: &[]compute.ResourceSkuCapabilities{ |
| 123 | + {Name: lo.ToPtr("vCPUs"), Value: lo.ToPtr("64")}, |
| 124 | + {Name: lo.ToPtr("MemoryGB"), Value: lo.ToPtr("64")}, |
| 125 | + {Name: lo.ToPtr("CpuArchitectureType"), Value: lo.ToPtr("x64")}, |
| 126 | + {Name: lo.ToPtr("vCPUsAvailable"), Value: lo.ToPtr("64")}, |
| 127 | + }, |
| 128 | + Locations: &[]string{"southcentralus"}, |
| 129 | + LocationInfo: &[]compute.ResourceSkuLocationInfo{{Location: lo.ToPtr("southcentralus"), Zones: &[]string{}}}, |
| 130 | + }, |
| 131 | + ) |
| 132 | + defer func() { |
| 133 | + fake.ResourceSkus[fake.Region] = copy |
| 134 | + }() |
| 135 | + |
| 136 | + // Second reconcile should succeed and have new cached data |
| 137 | + ExpectSingletonReconciled(ctx, controller) |
| 138 | + instanceTypes, err = azureEnv.InstanceTypesProvider.List(ctx, nodeClass) |
| 139 | + Expect(err).To(BeNil()) |
| 140 | + Expect(instanceTypes).NotTo(BeEmpty()) |
| 141 | + Expect(instanceTypes).To(ContainElement(HaveField("Name", Equal("Standard_D64s_v6")))) |
| 142 | + }) |
| 143 | +}) |
0 commit comments