sigs.k8s.io/cluster-api-provider-azure@v1.14.3/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 eviction policy",
    98  			spot: &infrav1.SpotVMOptions{
    99  				MaxPrice:       nil,
   100  				EvictionPolicy: &deletePolicy,
   101  			},
   102  			diffDiskSettings: nil,
   103  			want: resultParams{
   104  				vmPriorityTypes:       ptr.To(armcompute.VirtualMachinePriorityTypesSpot),
   105  				vmEvictionPolicyTypes: ptr.To(armcompute.VirtualMachineEvictionPolicyTypesDelete),
   106  				billingProfile:        nil,
   107  			},
   108  		},
   109  	}
   110  	for _, tt := range tests {
   111  		tt := tt
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			t.Parallel()
   114  			g := NewGomegaWithT(t)
   115  			result := resultParams{}
   116  			var err error
   117  			result.vmPriorityTypes, result.vmEvictionPolicyTypes, result.billingProfile, err = GetSpotVMOptions(tt.spot, tt.diffDiskSettings)
   118  			g.Expect(result.vmPriorityTypes).To(Equal(tt.want.vmPriorityTypes), fmt.Sprintf("got: %v, want: %v", result.vmPriorityTypes, tt.want.vmPriorityTypes))
   119  			g.Expect(result.vmEvictionPolicyTypes).To(Equal(tt.want.vmEvictionPolicyTypes), fmt.Sprintf("got: %v, want: %v", result.vmEvictionPolicyTypes, tt.want.vmEvictionPolicyTypes))
   120  			g.Expect(result.billingProfile).To(Equal(tt.want.billingProfile), fmt.Sprintf("got: %v, want: %v", result.billingProfile, tt.want.billingProfile))
   121  			g.Expect(err).NotTo(HaveOccurred())
   122  		})
   123  	}
   124  }