Skip to content

Commit

Permalink
Convert /test references to SDKv2
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed Sep 12, 2023
1 parent fd27a78 commit b32c20e
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 115 deletions.
22 changes: 14 additions & 8 deletions test/e2e/azure_edgezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ package e2e
import (
"context"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/go-autorest/autorest/azure/auth"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -75,26 +76,31 @@ func AzureEdgeZoneClusterSpec(ctx context.Context, inputGetter func() AzureEdgeZ
settings, err := auth.GetSettingsFromEnvironment()
Expect(err).NotTo(HaveOccurred())
subscriptionID := settings.GetSubscriptionID()
auth, err := settings.GetAuthorizer()
cred, err := azidentity.NewDefaultAzureCredential(nil)
Expect(err).NotTo(HaveOccurred())

if len(machineList.Items) > 0 {
By("Creating a VM client")
// create a VM client
vmClient := compute.NewVirtualMachinesClient(subscriptionID)
vmClient.Authorizer = auth
vmClient, err := armcompute.NewVirtualMachinesClient(subscriptionID, cred, nil)
Expect(err).NotTo(HaveOccurred())

// get the resource group name
resource, err := azureutil.ParseResourceID(*machineList.Items[0].Spec.ProviderID)
Expect(err).NotTo(HaveOccurred())

vmListResults, err := vmClient.List(ctx, resource.ResourceGroupName, "")
Expect(err).NotTo(HaveOccurred())
var vms []*armcompute.VirtualMachine
pager := vmClient.NewListPager(resource.ResourceGroupName, nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
Expect(err).NotTo(HaveOccurred())
vms = append(vms, nextResult.Value...)
}

By("Verifying VMs' extendedLocation property is correct")
for _, machine := range vmListResults.Values() {
for _, machine := range vms {
Expect(*machine.ExtendedLocation.Name).To(Equal(extendedLocationName))
Expect(string(machine.ExtendedLocation.Type)).To(Equal(extendedLocationType))
Expect(string(*machine.ExtendedLocation.Type)).To(Equal(extendedLocationType))
}
}
}
27 changes: 17 additions & 10 deletions test/e2e/azure_logcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -420,18 +421,21 @@ func collectVMBootLog(ctx context.Context, am *infrav1.AzureMachine, outputPath
return errors.Wrap(err, "failed to get settings from environment")
}

vmClient := compute.NewVirtualMachinesClient(settings.GetSubscriptionID())
vmClient.Authorizer, err = azureutil.GetAuthorizer(settings)
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return errors.Wrap(err, "failed to get authorizer")
return errors.Wrap(err, "failed to get default azure credential")
}
vmClient, err := armcompute.NewVirtualMachinesClient(settings.GetSubscriptionID(), cred, nil)
if err != nil {
return errors.Wrap(err, "failed to create virtual machines client")
}

bootDiagnostics, err := vmClient.RetrieveBootDiagnosticsData(ctx, resource.ResourceGroupName, resource.Name, nil)
if err != nil {
return errors.Wrap(err, "failed to get boot diagnostics data")
}

return writeBootLog(bootDiagnostics, outputPath)
return writeBootLog(bootDiagnostics.RetrieveBootDiagnosticsDataResult, outputPath)
}

// collectVMSSBootLog collects boot logs of the scale set by using azure boot diagnostics.
Expand All @@ -452,21 +456,24 @@ func collectVMSSBootLog(ctx context.Context, providerID string, outputPath strin
return errors.Wrap(err, "failed to get settings from environment")
}

vmssClient := compute.NewVirtualMachineScaleSetVMsClient(settings.GetSubscriptionID())
vmssClient.Authorizer, err = azureutil.GetAuthorizer(settings)
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return errors.Wrap(err, "failed to get default azure credential")
}
vmssClient, err := armcompute.NewVirtualMachineScaleSetVMsClient(settings.GetSubscriptionID(), cred, nil)
if err != nil {
return errors.Wrap(err, "failed to get authorizer")
return errors.Wrap(err, "failed to create virtual machine scale set VMs client")
}

bootDiagnostics, err := vmssClient.RetrieveBootDiagnosticsData(ctx, resource.ResourceGroupName, resource.Name, instanceID, nil)
if err != nil {
return errors.Wrap(err, "failed to get boot diagnostics data")
}

return writeBootLog(bootDiagnostics, outputPath)
return writeBootLog(bootDiagnostics.RetrieveBootDiagnosticsDataResult, outputPath)
}

func writeBootLog(bootDiagnostics compute.RetrieveBootDiagnosticsDataResult, outputPath string) error {
func writeBootLog(bootDiagnostics armcompute.RetrieveBootDiagnosticsDataResult, outputPath string) error {
var err error
req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, *bootDiagnostics.SerialConsoleLogBlobURI, http.NoBody)
if err != nil {
Expand Down
32 changes: 18 additions & 14 deletions test/e2e/azure_securitygroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ package e2e
import (
"context"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
"github.com/Azure/go-autorest/autorest/azure/auth"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -91,18 +91,17 @@ func AzureSecurityGroupsSpec(ctx context.Context, inputGetter func() AzureSecuri
settings, err := auth.GetSettingsFromEnvironment()
Expect(err).NotTo(HaveOccurred())
subscriptionID := settings.GetSubscriptionID()
auth, err := azureutil.GetAuthorizer(settings)
Expect(err).NotTo(HaveOccurred())

By("creating a subnets client")
subnetsClient := network.NewSubnetsClient(subscriptionID)
subnetsClient.Authorizer = auth
By("creating securitygroups and securityrules clients")
cred, err := azidentity.NewDefaultAzureCredential(nil)
Expect(err).NotTo(HaveOccurred())

securityGroupsClient := network.NewSecurityGroupsClient(subscriptionID)
securityGroupsClient.Authorizer = auth
securityGroupsClient, err := armnetwork.NewSecurityGroupsClient(subscriptionID, cred, nil)
Expect(err).NotTo(HaveOccurred())

securityRulesClient := network.NewSecurityRulesClient(subscriptionID)
securityRulesClient.Authorizer = auth
securityRulesClient, err := armnetwork.NewSecurityRulesClient(subscriptionID, cred, nil)
Expect(err).NotTo(HaveOccurred())

azureCluster := &infrav1.AzureCluster{}
err = mgmtClient.Get(ctx, client.ObjectKey{
Expand All @@ -114,18 +113,23 @@ func AzureSecurityGroupsSpec(ctx context.Context, inputGetter func() AzureSecuri
var expectedSubnets infrav1.Subnets
checkSubnets := func(g Gomega) {
for _, expectedSubnet := range expectedSubnets {
securityGroup, err := securityGroupsClient.Get(ctx, azureCluster.Spec.ResourceGroup, expectedSubnet.SecurityGroup.Name, "")
securityGroup, err := securityGroupsClient.Get(ctx, azureCluster.Spec.ResourceGroup, expectedSubnet.SecurityGroup.Name, nil)
g.Expect(err).NotTo(HaveOccurred())

securityRules, err := securityRulesClient.List(ctx, azureCluster.Spec.ResourceGroup, *securityGroup.Name)
g.Expect(err).NotTo(HaveOccurred())
var securityRules []*armnetwork.SecurityRule
pager := securityRulesClient.NewListPager(azureCluster.Spec.ResourceGroup, *securityGroup.Name, nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
Expect(err).NotTo(HaveOccurred())
securityRules = append(securityRules, nextResult.Value...)
}

var expectedSecurityRuleNames []string
for _, expectedSecurityRule := range expectedSubnet.SecurityGroup.SecurityRules {
expectedSecurityRuleNames = append(expectedSecurityRuleNames, expectedSecurityRule.Name)
}

for _, securityRule := range securityRules.Values() {
for _, securityRule := range securityRules {
g.Expect(expectedSecurityRuleNames).To(ContainElement(*securityRule.Name))
}
}
Expand Down
69 changes: 44 additions & 25 deletions test/e2e/azure_vmextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ package e2e
import (
"context"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/go-autorest/autorest/azure/auth"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -73,8 +74,6 @@ func AzureVMExtensionsSpec(ctx context.Context, inputGetter func() AzureVMExtens
settings, err := auth.GetSettingsFromEnvironment()
Expect(err).NotTo(HaveOccurred())
subscriptionID := settings.GetSubscriptionID()
auth, err := azureutil.GetAuthorizer(settings)
Expect(err).NotTo(HaveOccurred())

if len(machineList.Items) > 0 {
By("Creating a mapping of machine IDs to array of expected VM extensions")
Expand All @@ -86,31 +85,39 @@ func AzureVMExtensionsSpec(ctx context.Context, inputGetter func() AzureVMExtens
}

By("Creating a VM and VM extension client")
cred, err := azidentity.NewDefaultAzureCredential(nil)
Expect(err).NotTo(HaveOccurred())

// create a VM client
vmClient := compute.NewVirtualMachinesClient(subscriptionID)
vmClient.Authorizer = auth
vmClient, err := armcompute.NewVirtualMachinesClient(subscriptionID, cred, nil)
Expect(err).NotTo(HaveOccurred())

// create a VM extension client
vmExtensionsClient := compute.NewVirtualMachineExtensionsClient(subscriptionID)
vmExtensionsClient.Authorizer = auth
vmExtensionsClient, err := armcompute.NewVirtualMachineExtensionsClient(subscriptionID, cred, nil)
Expect(err).NotTo(HaveOccurred())

// get the resource group name
resource, err := azureutil.ParseResourceID(*machineList.Items[0].Spec.ProviderID)
Expect(err).NotTo(HaveOccurred())

vmListResults, err := vmClient.List(ctx, resource.ResourceGroupName, "")
Expect(err).NotTo(HaveOccurred())
var vms []*armcompute.VirtualMachine
pager := vmClient.NewListPager(resource.ResourceGroupName, nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
Expect(err).NotTo(HaveOccurred())
vms = append(vms, nextResult.Value...)
}

By("Verifying specified VM extensions are created on Azure")
for _, machine := range vmListResults.Values() {
vmExtensionListResult, err := vmExtensionsClient.List(ctx, resource.ResourceGroupName, *machine.Name, "")
for _, machine := range vms {
vmExtensionListResult, err := vmExtensionsClient.List(ctx, resource.ResourceGroupName, *machine.Name, nil)
Expect(err).NotTo(HaveOccurred())
vmExtensionList := *vmExtensionListResult.Value
vmExtensionList := vmExtensionListResult.Value
var vmExtensionNames []string
for _, vmExtension := range vmExtensionList {
vmExtensionNames = append(vmExtensionNames, *vmExtension.Name)
}
osName := string(machine.VirtualMachineProperties.StorageProfile.OsDisk.OsType)
osName := string(*machine.Properties.StorageProfile.OSDisk.OSType)
Expect(vmExtensionNames).To(ContainElements("CAPZ." + osName + ".Bootstrapping"))
Expect(vmExtensionNames).To(ContainElements(expectedVMExtensionMap[*machine.ID]))
}
Expand All @@ -133,31 +140,43 @@ func AzureVMExtensionsSpec(ctx context.Context, inputGetter func() AzureVMExtens
}

By("Creating a VMSS and VMSS extension client")
cred, err := azidentity.NewDefaultAzureCredential(nil)
Expect(err).NotTo(HaveOccurred())

// create a VMSS client
vmssClient := compute.NewVirtualMachineScaleSetsClient(subscriptionID)
vmssClient.Authorizer = auth
vmssClient, err := armcompute.NewVirtualMachineScaleSetsClient(subscriptionID, cred, nil)
Expect(err).NotTo(HaveOccurred())

// create a VMSS extension client
vmssExtensionsClient := compute.NewVirtualMachineScaleSetExtensionsClient(subscriptionID)
vmssExtensionsClient.Authorizer = auth
vmssExtensionsClient, err := armcompute.NewVirtualMachineScaleSetExtensionsClient(subscriptionID, cred, nil)
Expect(err).NotTo(HaveOccurred())

// get the resource group name
resource, err := azureutil.ParseResourceID(machinePoolList.Items[0].Spec.ProviderID)
Expect(err).NotTo(HaveOccurred())

vmssListResults, err := vmssClient.List(ctx, resource.ResourceGroupName)
Expect(err).NotTo(HaveOccurred())
var vmsses []*armcompute.VirtualMachineScaleSet
pager := vmssClient.NewListPager(resource.ResourceGroupName, nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
Expect(err).NotTo(HaveOccurred())
vmsses = append(vmsses, nextResult.Value...)
}

By("Verifying VMSS extensions are created on Azure")
for _, machinePool := range vmssListResults.Values() {
vmssExtensionListResult, err := vmssExtensionsClient.List(ctx, resource.ResourceGroupName, *machinePool.Name)
Expect(err).NotTo(HaveOccurred())
vmssExtensionList := vmssExtensionListResult.Values()
for _, machinePool := range vmsses {
var vmssExts []*armcompute.VirtualMachineScaleSetExtension
pager := vmssExtensionsClient.NewListPager(resource.ResourceGroupName, *machinePool.Name, nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
Expect(err).NotTo(HaveOccurred())
vmssExts = append(vmssExts, nextResult.Value...)
}
var vmssExtensionNames []string
for _, vmssExtension := range vmssExtensionList {
for _, vmssExtension := range vmssExts {
vmssExtensionNames = append(vmssExtensionNames, *vmssExtension.Name)
}
osName := string(machinePool.VirtualMachineScaleSetProperties.VirtualMachineProfile.StorageProfile.OsDisk.OsType)
osName := string(*machinePool.Properties.VirtualMachineProfile.StorageProfile.OSDisk.OSType)
Expect(vmssExtensionNames).To(ContainElements("CAPZ." + osName + ".Bootstrapping"))
Expect(vmssExtensionNames).To(ContainElements(expectedVMSSExtensionMap[*machinePool.ID]))
}
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
"path/filepath"
"strings"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/Azure/go-autorest/autorest/azure/auth"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
Expand All @@ -40,7 +41,6 @@ import (
"k8s.io/utils/ptr"
"sigs.k8s.io/cluster-api-provider-azure/azure"
e2e_namespace "sigs.k8s.io/cluster-api-provider-azure/test/e2e/kubernetes/namespace"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
kubeadmv1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
Expand Down Expand Up @@ -209,11 +209,11 @@ func ExpectResourceGroupToBe404(ctx context.Context) {
settings, err := auth.GetSettingsFromEnvironment()
Expect(err).NotTo(HaveOccurred())
subscriptionID := settings.GetSubscriptionID()
authorizer, err := azureutil.GetAuthorizer(settings)
cred, err := azidentity.NewDefaultAzureCredential(nil)
Expect(err).NotTo(HaveOccurred())
groupsClient := resources.NewGroupsClient(subscriptionID)
groupsClient.Authorizer = authorizer
_, err = groupsClient.Get(ctx, os.Getenv(AzureResourceGroup))
groupsClient, err := armresources.NewResourceGroupsClient(subscriptionID, cred, nil)
Expect(err).NotTo(HaveOccurred())
_, err = groupsClient.Get(ctx, os.Getenv(AzureResourceGroup), nil)
Expect(azure.ResourceNotFound(err)).To(BeTrue(), "The resource group in Azure still exists. After deleting the cluster all of the Azure resources should also be deleted.")
}

Expand Down
Loading

0 comments on commit b32c20e

Please sign in to comment.