-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented initial test for vm converter
Replaced empty test with basic test Update and verify go modules in hack/tools Watch Cluster for changes in AzureJSONMachineTemplate, AzureJSONMachine, and AzureJSONMachinePool controllers Update support link Fix hack/tools imports so "make modules" works Added test without optional fields Add header and remove comment Added expected error field and better diff printing in case of error Added tests for individual fields Fix import order for linter
- Loading branch information
1 parent
be33ec3
commit 20b71b7
Showing
1 changed file
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
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" | ||
. "github.com/onsi/gomega" | ||
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" | ||
) | ||
|
||
func TestSDKToVM(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
sdk compute.VirtualMachine | ||
want *VM | ||
expectedError string | ||
}{ | ||
{ | ||
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"), | ||
}, | ||
}, | ||
expectedError: "", | ||
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, | ||
}, | ||
}, | ||
}, | ||
expectedError: "", | ||
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"}, | ||
}, | ||
expectedError: "", | ||
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")}, | ||
}, | ||
expectedError: "", | ||
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")}, | ||
}, | ||
expectedError: "", | ||
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) { | ||
g := NewWithT(t) | ||
t.Parallel() | ||
got, err := SDKToVM(tt.sdk) | ||
if tt.expectedError != "" { | ||
g.Expect(err).To(HaveOccurred()) | ||
g.Expect(err).To(MatchError(tt.expectedError)) | ||
} else { | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Diff between expected result and actual result:\n%s", cmp.Diff(tt.want, got)) | ||
} | ||
}) | ||
} | ||
} |