|
| 1 | +// +build e2e |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2020 The Kubernetes Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package e2e |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "path/filepath" |
| 25 | + |
| 26 | + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute" |
| 27 | + "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network" |
| 28 | + "github.com/Azure/go-autorest/autorest/azure/auth" |
| 29 | + . "github.com/onsi/ginkgo" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | +) |
| 32 | + |
| 33 | +// AzureAcceleratedNetworkingSpecInput is the input for AzureAcceleratedNetworkingSpec. |
| 34 | +type AzureAcceleratedNetworkingSpecInput struct { |
| 35 | + ClusterName string |
| 36 | +} |
| 37 | + |
| 38 | +// AzureAcceleratedNetworkingSpec implements a test that verifies Azure VMs in a workload |
| 39 | +// cluster provisioned by CAPZ have accelerated networking enabled if they're capable of it. |
| 40 | +func AzureAcceleratedNetworkingSpec(ctx context.Context, inputGetter func() AzureAcceleratedNetworkingSpecInput) { |
| 41 | + var ( |
| 42 | + specName = "azure-accelerated-networking" |
| 43 | + input AzureAcceleratedNetworkingSpecInput |
| 44 | + ) |
| 45 | + |
| 46 | + input = inputGetter() |
| 47 | + Expect(input.ClusterName).NotTo(BeEmpty(), "Invalid argument. input.ClusterName can't be empty when calling %s spec", specName) |
| 48 | + |
| 49 | + By("creating Azure clients with the workload cluster's subscription") |
| 50 | + settings, err := auth.GetSettingsFromEnvironment() |
| 51 | + Expect(err).NotTo(HaveOccurred()) |
| 52 | + subscriptionID := settings.GetSubscriptionID() |
| 53 | + authorizer, err := settings.GetAuthorizer() |
| 54 | + Expect(err).NotTo(HaveOccurred()) |
| 55 | + vmsClient := compute.NewVirtualMachinesClient(subscriptionID) |
| 56 | + vmsClient.Authorizer = authorizer |
| 57 | + nicsClient := network.NewInterfacesClient(subscriptionID) |
| 58 | + nicsClient.Authorizer = authorizer |
| 59 | + |
| 60 | + By("verifying EnableAcceleratedNetworking for the primary NIC of each VM") |
| 61 | + // NOTE: add SKUs being tested to this lookup table. |
| 62 | + acceleratedNetworking := map[compute.VirtualMachineSizeTypes]bool{ |
| 63 | + compute.VirtualMachineSizeTypesStandardB2ms: false, |
| 64 | + compute.VirtualMachineSizeTypesStandardD2V2: true, |
| 65 | + compute.VirtualMachineSizeTypesStandardD2V3: false, |
| 66 | + compute.VirtualMachineSizeTypesStandardD2sV3: false, |
| 67 | + compute.VirtualMachineSizeTypesStandardD4V2: true, |
| 68 | + compute.VirtualMachineSizeTypesStandardD4V3: true, |
| 69 | + compute.VirtualMachineSizeTypesStandardD8sV3: true, |
| 70 | + } |
| 71 | + rgName := input.ClusterName |
| 72 | + page, err := vmsClient.List(ctx, rgName) |
| 73 | + Expect(err).NotTo(HaveOccurred()) |
| 74 | + Expect(len(page.Values())).To(BeNumerically(">", 0)) |
| 75 | + for page.NotDone() { |
| 76 | + for _, vm := range page.Values() { |
| 77 | + sku := vm.HardwareProfile.VMSize |
| 78 | + for _, nic := range *vm.NetworkProfile.NetworkInterfaces { |
| 79 | + if nic.Primary != nil && *nic.Primary { |
| 80 | + // verify that accelerated networking is enabled if the SKU is capable |
| 81 | + nicInfo, err := nicsClient.Get(ctx, rgName, filepath.Base(*nic.ID), "") |
| 82 | + Expect(err).NotTo(HaveOccurred()) |
| 83 | + capable, found := acceleratedNetworking[sku] |
| 84 | + if !found { |
| 85 | + fmt.Fprintf(GinkgoWriter, "SKU %s was not found, please add to the acceleratedNetworking lookup table.\n", sku) |
| 86 | + } else { |
| 87 | + Expect(capable).To(Equal(*nicInfo.EnableAcceleratedNetworking)) |
| 88 | + } |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + err = page.NextWithContext(ctx) |
| 94 | + Expect(err).NotTo(HaveOccurred()) |
| 95 | + } |
| 96 | +} |
0 commit comments