Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add missing unit test for covering azure.ParseResourceID() #4165

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions azure/scope/machinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,41 @@ func TestMachinePoolScope_applyAzureMachinePoolMachines(t *testing.T) {
g.Expect(len(list.Items)).Should(Equal(1))
},
},
{
Name: "if existing MachinePool is not present, reduce replicas",
Setup: func(mp *expv1.MachinePool, amp *infrav1exp.AzureMachinePool, vmssState *azure.VMSS, cb *fake.ClientBuilder) {
mp.Spec.Replicas = ptr.To[int32](1)

vmssState.Instances = []azure.VMSSVM{
{
ID: "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
Name: "vm",
},
}
},
Verify: func(g *WithT, amp *infrav1exp.AzureMachinePool, c client.Client, err error) {
g.Expect(err).NotTo(HaveOccurred())
list := infrav1exp.AzureMachinePoolMachineList{}
g.Expect(c.List(ctx, &list)).NotTo(HaveOccurred())
g.Expect(len(list.Items)).Should(Equal(1))
},
},
{
Name: "if existing MachinePool is not present and Instances ID is in wrong format, reduce replicas",
Setup: func(mp *expv1.MachinePool, amp *infrav1exp.AzureMachinePool, vmssState *azure.VMSS, cb *fake.ClientBuilder) {
mp.Spec.Replicas = ptr.To[int32](1)

vmssState.Instances = []azure.VMSSVM{
{
ID: "foo/ampm0",
Name: "ampm0",
},
}
},
Verify: func(g *WithT, amp *infrav1exp.AzureMachinePool, c client.Client, err error) {
g.Expect(err).To(HaveOccurred())
},
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
Expand Down
45 changes: 0 additions & 45 deletions azure/services/virtualmachines/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ package virtualmachines

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/pkg/errors"
"k8s.io/utils/ptr"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/async"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)
Expand All @@ -41,7 +38,6 @@ type (
// Client provides operations on Azure virtual machine resources.
Client interface {
Get(context.Context, azure.ResourceSpecGetter) (interface{}, error)
GetByID(context.Context, string) (armcompute.VirtualMachine, error)
CreateOrUpdateAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string, parameters interface{}) (result interface{}, poller *runtime.Poller[armcompute.VirtualMachinesClientCreateOrUpdateResponse], err error)
DeleteAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string) (poller *runtime.Poller[armcompute.VirtualMachinesClientDeleteResponse], err error)
}
Expand Down Expand Up @@ -74,29 +70,6 @@ func (ac *AzureClient) Get(ctx context.Context, spec azure.ResourceSpecGetter) (
return resp.VirtualMachine, nil
}

// GetByID retrieves information about the model or instance view of a virtual machine.
func (ac *AzureClient) GetByID(ctx context.Context, resourceID string) (armcompute.VirtualMachine, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere

ctx, log, done := tele.StartSpanWithLogger(ctx, "virtualmachines.AzureClient.GetByID")
defer done()

parsed, err := azureutil.ParseResourceID(resourceID)
if err != nil {
return armcompute.VirtualMachine{}, errors.Wrap(err, fmt.Sprintf("failed parsing the VM resource id %q", resourceID))
}

log.V(4).Info("parsed VM resourceID", "parsed", parsed)

result, err := ac.Get(ctx, newResourceAdaptor(parsed))
if err != nil {
return armcompute.VirtualMachine{}, err
}

if vm, ok := result.(armcompute.VirtualMachine); ok {
return vm, nil
}
return armcompute.VirtualMachine{}, errors.Errorf("expected VirtualMachine but got %T", result)
}

// CreateOrUpdateAsync creates or updates a virtual machine asynchronously.
// It sends a PUT request to Azure and if accepted without error, the func will return a Poller which can be used to track the ongoing
// progress of the operation.
Expand Down Expand Up @@ -158,21 +131,3 @@ func (ac *AzureClient) DeleteAsync(ctx context.Context, spec azure.ResourceSpecG
// if the operation completed, return a nil poller.
return nil, err
}

// resourceAdaptor implements the ResourceSpecGetter interface for an arm.ResourceID.
type resourceAdaptor struct {
resource *arm.ResourceID
}

func newResourceAdaptor(resource *arm.ResourceID) *resourceAdaptor {
return &resourceAdaptor{resource: resource}
}

func (r *resourceAdaptor) OwnerResourceName() string { return r.resource.Parent.Name }

func (r *resourceAdaptor) Parameters(ctx context.Context, existing interface{}) (interface{}, error) {
return nil, nil // Not implemented
}
func (r *resourceAdaptor) ResourceGroupName() string { return r.resource.ResourceGroupName }

func (r *resourceAdaptor) ResourceName() string { return r.resource.Name }
15 changes: 0 additions & 15 deletions azure/services/virtualmachines/mock_virtualmachines/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.