sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/converters/vm_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package converters
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
    24  	"github.com/google/go-cmp/cmp"
    25  	"k8s.io/utils/ptr"
    26  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    27  )
    28  
    29  func TestSDKToVM(t *testing.T) {
    30  	tests := []struct {
    31  		name string
    32  		sdk  armcompute.VirtualMachine
    33  		want *VM
    34  	}{
    35  		{
    36  			name: "Basic conversion with required fields",
    37  			sdk: armcompute.VirtualMachine{
    38  				ID:   ptr.To("test-vm-id"),
    39  				Name: ptr.To("test-vm-name"),
    40  				Properties: &armcompute.VirtualMachineProperties{
    41  					ProvisioningState: ptr.To("Succeeded"),
    42  				},
    43  			},
    44  			want: &VM{
    45  				ID:    "test-vm-id",
    46  				Name:  "test-vm-name",
    47  				State: infrav1.ProvisioningState("Succeeded"),
    48  			},
    49  		},
    50  		{
    51  			name: "Should convert and populate with VMSize",
    52  			sdk: armcompute.VirtualMachine{
    53  				ID:   ptr.To("test-vm-id"),
    54  				Name: ptr.To("test-vm-name"),
    55  				Properties: &armcompute.VirtualMachineProperties{
    56  					ProvisioningState: ptr.To("Succeeded"),
    57  					HardwareProfile: &armcompute.HardwareProfile{
    58  						VMSize: ptr.To(armcompute.VirtualMachineSizeTypesStandardA1),
    59  					},
    60  				},
    61  			},
    62  			want: &VM{
    63  				ID:     "test-vm-id",
    64  				Name:   "test-vm-name",
    65  				State:  infrav1.ProvisioningState("Succeeded"),
    66  				VMSize: "Standard_A1",
    67  			},
    68  		},
    69  		{
    70  			name: "Should convert and populate with availability zones",
    71  			sdk: armcompute.VirtualMachine{
    72  				ID:   ptr.To("test-vm-id"),
    73  				Name: ptr.To("test-vm-name"),
    74  				Properties: &armcompute.VirtualMachineProperties{
    75  					ProvisioningState: ptr.To("Succeeded"),
    76  				},
    77  				Zones: []*string{ptr.To("1"), ptr.To("2")},
    78  			},
    79  			want: &VM{
    80  				ID:               "test-vm-id",
    81  				Name:             "test-vm-name",
    82  				State:            infrav1.ProvisioningState("Succeeded"),
    83  				AvailabilityZone: "1",
    84  			},
    85  		},
    86  		{
    87  			name: "Should convert and populate with tags",
    88  			sdk: armcompute.VirtualMachine{
    89  				ID:   ptr.To("test-vm-id"),
    90  				Name: ptr.To("test-vm-name"),
    91  				Properties: &armcompute.VirtualMachineProperties{
    92  					ProvisioningState: ptr.To("Succeeded"),
    93  				},
    94  				Tags: map[string]*string{"foo": ptr.To("bar")},
    95  			},
    96  			want: &VM{
    97  				ID:    "test-vm-id",
    98  				Name:  "test-vm-name",
    99  				State: infrav1.ProvisioningState("Succeeded"),
   100  				Tags:  infrav1.Tags{"foo": "bar"},
   101  			},
   102  		},
   103  		{
   104  			name: "Should convert and populate with all fields",
   105  			sdk: armcompute.VirtualMachine{
   106  				ID:   ptr.To("test-vm-id"),
   107  				Name: ptr.To("test-vm-name"),
   108  				Properties: &armcompute.VirtualMachineProperties{
   109  					ProvisioningState: ptr.To("Succeeded"),
   110  					HardwareProfile: &armcompute.HardwareProfile{
   111  						VMSize: ptr.To(armcompute.VirtualMachineSizeTypesStandardA1),
   112  					},
   113  				},
   114  				Zones: []*string{ptr.To("1")},
   115  				Tags:  map[string]*string{"foo": ptr.To("bar")},
   116  			},
   117  			want: &VM{
   118  				ID:               "test-vm-id",
   119  				Name:             "test-vm-name",
   120  				State:            infrav1.ProvisioningState("Succeeded"),
   121  				VMSize:           "Standard_A1",
   122  				AvailabilityZone: "1",
   123  				Tags:             infrav1.Tags{"foo": "bar"},
   124  			},
   125  		},
   126  	}
   127  	for _, tt := range tests {
   128  		tt := tt
   129  		t.Run(tt.name, func(t *testing.T) {
   130  			t.Parallel()
   131  			got := SDKToVM(tt.sdk)
   132  			if !reflect.DeepEqual(got, tt.want) {
   133  				t.Errorf("Diff between expected result and actual result:\n%s", cmp.Diff(tt.want, got))
   134  			}
   135  		})
   136  	}
   137  }