Skip to content

Commit

Permalink
Merge pull request #2564 from willie-yao/add-vm-converter-test
Browse files Browse the repository at this point in the history
Add unit tests for VM converter and remove unused err from SDKToVM
  • Loading branch information
k8s-ci-robot authored Aug 23, 2022
2 parents 11199dd + 8b53f68 commit 997b40a
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 6 deletions.
4 changes: 2 additions & 2 deletions azure/converters/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type VM struct {
}

// SDKToVM converts an Azure SDK VirtualMachine to the CAPZ VM type.
func SDKToVM(v compute.VirtualMachine) (*VM, error) {
func SDKToVM(v compute.VirtualMachine) *VM {
vm := &VM{
ID: to.String(v.ID),
Name: to.String(v.Name),
Expand All @@ -63,5 +63,5 @@ func SDKToVM(v compute.VirtualMachine) (*VM, error) {
vm.Tags = MapToTags(v.Tags)
}

return vm, nil
return vm
}
137 changes: 137 additions & 0 deletions azure/converters/vm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package converters

import (
"reflect"
"testing"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/go-autorest/autorest/to"
"github.com/google/go-cmp/cmp"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)

func TestSDKToVM(t *testing.T) {
tests := []struct {
name string
sdk compute.VirtualMachine
want *VM
}{
{
name: "Basic conversion with required fields",
sdk: compute.VirtualMachine{
ID: to.StringPtr("test-vm-id"),
Name: to.StringPtr("test-vm-name"),
VirtualMachineProperties: &compute.VirtualMachineProperties{
ProvisioningState: to.StringPtr("Succeeded"),
},
},
want: &VM{
ID: "test-vm-id",
Name: "test-vm-name",
State: infrav1.ProvisioningState(compute.ProvisioningStateSucceeded),
},
},
{
name: "Should convert and populate with VMSize",
sdk: compute.VirtualMachine{
ID: to.StringPtr("test-vm-id"),
Name: to.StringPtr("test-vm-name"),
VirtualMachineProperties: &compute.VirtualMachineProperties{
ProvisioningState: to.StringPtr("Succeeded"),
HardwareProfile: &compute.HardwareProfile{
VMSize: compute.VirtualMachineSizeTypesStandardA1,
},
},
},
want: &VM{
ID: "test-vm-id",
Name: "test-vm-name",
State: infrav1.ProvisioningState(compute.ProvisioningStateSucceeded),
VMSize: "Standard_A1",
},
},
{
name: "Should convert and populate with availability zones",
sdk: compute.VirtualMachine{
ID: to.StringPtr("test-vm-id"),
Name: to.StringPtr("test-vm-name"),
VirtualMachineProperties: &compute.VirtualMachineProperties{
ProvisioningState: to.StringPtr("Succeeded"),
},
Zones: &[]string{"1", "2"},
},
want: &VM{
ID: "test-vm-id",
Name: "test-vm-name",
State: infrav1.ProvisioningState(compute.ProvisioningStateSucceeded),
AvailabilityZone: "1",
},
},
{
name: "Should convert and populate with tags",
sdk: compute.VirtualMachine{
ID: to.StringPtr("test-vm-id"),
Name: to.StringPtr("test-vm-name"),
VirtualMachineProperties: &compute.VirtualMachineProperties{
ProvisioningState: to.StringPtr("Succeeded"),
},
Tags: map[string]*string{"foo": to.StringPtr("bar")},
},
want: &VM{
ID: "test-vm-id",
Name: "test-vm-name",
State: infrav1.ProvisioningState(compute.ProvisioningStateSucceeded),
Tags: infrav1.Tags{"foo": "bar"},
},
},
{
name: "Should convert and populate with all fields",
sdk: compute.VirtualMachine{
ID: to.StringPtr("test-vm-id"),
Name: to.StringPtr("test-vm-name"),
VirtualMachineProperties: &compute.VirtualMachineProperties{
ProvisioningState: to.StringPtr("Succeeded"),
HardwareProfile: &compute.HardwareProfile{
VMSize: compute.VirtualMachineSizeTypesStandardA1,
},
},
Zones: &[]string{"1"},
Tags: map[string]*string{"foo": to.StringPtr("bar")},
},
want: &VM{
ID: "test-vm-id",
Name: "test-vm-name",
State: infrav1.ProvisioningState(compute.ProvisioningStateSucceeded),
VMSize: "Standard_A1",
AvailabilityZone: "1",
Tags: infrav1.Tags{"foo": "bar"},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := SDKToVM(tt.sdk)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Diff between expected result and actual result:\n%s", cmp.Diff(tt.want, got))
}
})
}
}
5 changes: 1 addition & 4 deletions azure/services/virtualmachines/virtualmachines.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
if !ok {
return errors.Errorf("%T is not a compute.VirtualMachine", result)
}
infraVM, err := converters.SDKToVM(vm)
if err != nil {
return err
}
infraVM := converters.SDKToVM(vm)
// Transform the VM resource representation to conform to the cloud-provider-azure representation
providerID, err := azureutil.ConvertResourceGroupNameToLower(azure.ProviderIDPrefix + infraVM.ID)
if err != nil {
Expand Down

0 comments on commit 997b40a

Please sign in to comment.