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

     1  /*
     2  Copyright 2021 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 azure
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"k8s.io/utils/ptr"
    24  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    25  )
    26  
    27  func TestVMSS_HasModelChanges(t *testing.T) {
    28  	cases := []struct {
    29  		Name            string
    30  		Factory         func() (VMSS, VMSS)
    31  		HasModelChanges bool
    32  	}{
    33  		{
    34  			Name: "two empty VMSS",
    35  			Factory: func() (VMSS, VMSS) {
    36  				return VMSS{}, VMSS{}
    37  			},
    38  			HasModelChanges: false,
    39  		},
    40  		{
    41  			Name: "one empty and other with image changes",
    42  			Factory: func() (VMSS, VMSS) {
    43  				return VMSS{}, VMSS{
    44  					Image: infrav1.Image{
    45  						Marketplace: &infrav1.AzureMarketplaceImage{
    46  							Version: "foo",
    47  						},
    48  					},
    49  				}
    50  			},
    51  			HasModelChanges: true,
    52  		},
    53  		{
    54  			Name: "one empty and other with image changes",
    55  			Factory: func() (VMSS, VMSS) {
    56  				return VMSS{}, VMSS{
    57  					Image: infrav1.Image{
    58  						Marketplace: &infrav1.AzureMarketplaceImage{
    59  							Version: "foo",
    60  						},
    61  					},
    62  				}
    63  			},
    64  			HasModelChanges: true,
    65  		},
    66  		{
    67  			Name: "same default VMSS",
    68  			Factory: func() (VMSS, VMSS) {
    69  				l := getDefaultVMSSForModelTesting()
    70  				r := getDefaultVMSSForModelTesting()
    71  				return r, l
    72  			},
    73  			HasModelChanges: false,
    74  		},
    75  		{
    76  			Name: "with different identity",
    77  			Factory: func() (VMSS, VMSS) {
    78  				l := getDefaultVMSSForModelTesting()
    79  				l.Identity = infrav1.VMIdentityNone
    80  				r := getDefaultVMSSForModelTesting()
    81  				return r, l
    82  			},
    83  			HasModelChanges: true,
    84  		},
    85  		{
    86  			Name: "with different Zones",
    87  			Factory: func() (VMSS, VMSS) {
    88  				l := getDefaultVMSSForModelTesting()
    89  				l.Zones = []string{"0"}
    90  				r := getDefaultVMSSForModelTesting()
    91  				return r, l
    92  			},
    93  			HasModelChanges: true,
    94  		},
    95  		{
    96  			Name: "with empty image",
    97  			Factory: func() (VMSS, VMSS) {
    98  				l := getDefaultVMSSForModelTesting()
    99  				l.Image = infrav1.Image{}
   100  				r := getDefaultVMSSForModelTesting()
   101  				return r, l
   102  			},
   103  			HasModelChanges: true,
   104  		},
   105  		{
   106  			Name: "with different image reference ID",
   107  			Factory: func() (VMSS, VMSS) {
   108  				l := getDefaultVMSSForModelTesting()
   109  				l.Image = infrav1.Image{
   110  					ID: ptr.To("foo"),
   111  				}
   112  				r := getDefaultVMSSForModelTesting()
   113  				return r, l
   114  			},
   115  			HasModelChanges: true,
   116  		},
   117  		{
   118  			Name: "with different SKU",
   119  			Factory: func() (VMSS, VMSS) {
   120  				l := getDefaultVMSSForModelTesting()
   121  				l.Sku = "reallySmallVM"
   122  				r := getDefaultVMSSForModelTesting()
   123  				return r, l
   124  			},
   125  			HasModelChanges: true,
   126  		},
   127  		{
   128  			Name: "with different Tags",
   129  			Factory: func() (VMSS, VMSS) {
   130  				l := getDefaultVMSSForModelTesting()
   131  				l.Tags = infrav1.Tags{
   132  					"bin": "baz",
   133  				}
   134  				r := getDefaultVMSSForModelTesting()
   135  				return r, l
   136  			},
   137  			HasModelChanges: true,
   138  		},
   139  	}
   140  
   141  	for _, c := range cases {
   142  		c := c
   143  		t.Run(c.Name, func(t *testing.T) {
   144  			l, r := c.Factory()
   145  			g := NewWithT(t)
   146  			g.Expect(l.HasModelChanges(r)).To(Equal(c.HasModelChanges))
   147  		})
   148  	}
   149  }
   150  
   151  func getDefaultVMSSForModelTesting() VMSS {
   152  	return VMSS{
   153  		Zones: []string{"0", "1"},
   154  		Image: infrav1.Image{
   155  			Marketplace: &infrav1.AzureMarketplaceImage{
   156  				Version: "foo",
   157  			},
   158  		},
   159  		Sku:      "reallyBigVM",
   160  		Identity: infrav1.VMIdentitySystemAssigned,
   161  		Tags: infrav1.Tags{
   162  			"foo": "baz",
   163  		},
   164  	}
   165  }