sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/converters/spotinstances_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  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
    24  	. "github.com/onsi/gomega"
    25  	"k8s.io/apimachinery/pkg/api/resource"
    26  	"k8s.io/utils/ptr"
    27  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    28  )
    29  
    30  func TestGetSpotVMOptions(t *testing.T) {
    31  	deletePolicy := infrav1.SpotEvictionPolicyDelete
    32  	type resultParams struct {
    33  		vmPriorityTypes       *armcompute.VirtualMachinePriorityTypes
    34  		vmEvictionPolicyTypes *armcompute.VirtualMachineEvictionPolicyTypes
    35  		billingProfile        *armcompute.BillingProfile
    36  	}
    37  	tests := []struct {
    38  		name             string
    39  		spot             *infrav1.SpotVMOptions
    40  		diffDiskSettings *infrav1.DiffDiskSettings
    41  		want             resultParams
    42  	}{
    43  		{
    44  			name:             "nil spot",
    45  			spot:             nil,
    46  			diffDiskSettings: nil,
    47  			want: resultParams{
    48  				vmPriorityTypes:       nil,
    49  				vmEvictionPolicyTypes: nil,
    50  				billingProfile:        nil,
    51  			},
    52  		},
    53  		{
    54  			name: "spot with nil max price",
    55  			spot: &infrav1.SpotVMOptions{
    56  				MaxPrice: nil,
    57  			},
    58  			diffDiskSettings: nil,
    59  			want: resultParams{
    60  				vmPriorityTypes:       ptr.To(armcompute.VirtualMachinePriorityTypesSpot),
    61  				vmEvictionPolicyTypes: nil,
    62  				billingProfile:        nil,
    63  			},
    64  		},
    65  		{
    66  			name: "spot with max price",
    67  			spot: &infrav1.SpotVMOptions{
    68  				MaxPrice: func(price string) *resource.Quantity {
    69  					p := resource.MustParse(price)
    70  					return &p
    71  				}("1000"),
    72  			},
    73  			diffDiskSettings: nil,
    74  			want: resultParams{
    75  				vmPriorityTypes:       ptr.To(armcompute.VirtualMachinePriorityTypesSpot),
    76  				vmEvictionPolicyTypes: nil,
    77  				billingProfile: &armcompute.BillingProfile{
    78  					MaxPrice: ptr.To[float64](1000),
    79  				},
    80  			},
    81  		},
    82  		{
    83  			name: "spot with ephemeral disk",
    84  			spot: &infrav1.SpotVMOptions{
    85  				MaxPrice: nil,
    86  			},
    87  			diffDiskSettings: &infrav1.DiffDiskSettings{
    88  				Option: string(armcompute.DiffDiskOptionsLocal),
    89  			},
    90  			want: resultParams{
    91  				vmPriorityTypes:       ptr.To(armcompute.VirtualMachinePriorityTypesSpot),
    92  				vmEvictionPolicyTypes: nil,
    93  				billingProfile:        nil,
    94  			},
    95  		},
    96  		{
    97  			name: "spot with ResourceDisk",
    98  			spot: &infrav1.SpotVMOptions{
    99  				MaxPrice: nil,
   100  			},
   101  			diffDiskSettings: &infrav1.DiffDiskSettings{
   102  				Option:    string(armcompute.DiffDiskOptionsLocal),
   103  				Placement: ptr.To(infrav1.DiffDiskPlacementResourceDisk),
   104  			},
   105  			want: resultParams{
   106  				vmPriorityTypes:       ptr.To(armcompute.VirtualMachinePriorityTypesSpot),
   107  				vmEvictionPolicyTypes: nil,
   108  				billingProfile:        nil,
   109  			},
   110  		},
   111  		{
   112  			name: "spot with eviction policy",
   113  			spot: &infrav1.SpotVMOptions{
   114  				MaxPrice:       nil,
   115  				EvictionPolicy: &deletePolicy,
   116  			},
   117  			diffDiskSettings: nil,
   118  			want: resultParams{
   119  				vmPriorityTypes:       ptr.To(armcompute.VirtualMachinePriorityTypesSpot),
   120  				vmEvictionPolicyTypes: ptr.To(armcompute.VirtualMachineEvictionPolicyTypesDelete),
   121  				billingProfile:        nil,
   122  			},
   123  		},
   124  	}
   125  	for _, tt := range tests {
   126  		tt := tt
   127  		t.Run(tt.name, func(t *testing.T) {
   128  			t.Parallel()
   129  			g := NewGomegaWithT(t)
   130  			result := resultParams{}
   131  			var err error
   132  			result.vmPriorityTypes, result.vmEvictionPolicyTypes, result.billingProfile, err = GetSpotVMOptions(tt.spot, tt.diffDiskSettings)
   133  			g.Expect(result.vmPriorityTypes).To(Equal(tt.want.vmPriorityTypes), fmt.Sprintf("got: %v, want: %v", result.vmPriorityTypes, tt.want.vmPriorityTypes))
   134  			g.Expect(result.vmEvictionPolicyTypes).To(Equal(tt.want.vmEvictionPolicyTypes), fmt.Sprintf("got: %v, want: %v", result.vmEvictionPolicyTypes, tt.want.vmEvictionPolicyTypes))
   135  			g.Expect(result.billingProfile).To(Equal(tt.want.billingProfile), fmt.Sprintf("got: %v, want: %v", result.billingProfile, tt.want.billingProfile))
   136  			g.Expect(err).NotTo(HaveOccurred())
   137  		})
   138  	}
   139  }