sigs.k8s.io/cluster-api-provider-azure@v1.17.0/exp/api/v1beta1/azuremachinepool_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 v1beta1_test
    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  	infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
    26  )
    27  
    28  func TestAzureMachinePool_Validate(t *testing.T) {
    29  	cases := []struct {
    30  		Name    string
    31  		Factory func(g *gomega.GomegaWithT) *infrav1exp.AzureMachinePool
    32  		Expect  func(g *gomega.GomegaWithT, actual error)
    33  	}{
    34  		{
    35  			Name: "HasNoImage",
    36  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
    37  				return &infrav1exp.AzureMachinePool{
    38  					Spec: infrav1exp.AzureMachinePoolSpec{
    39  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
    40  							OSDisk: infrav1.OSDisk{
    41  								OSType:      "Linux",
    42  								CachingType: "None",
    43  							},
    44  						},
    45  					},
    46  				}
    47  			},
    48  			Expect: func(g *gomega.GomegaWithT, actual error) {
    49  				g.Expect(actual).NotTo(gomega.HaveOccurred())
    50  			},
    51  		},
    52  		{
    53  			Name: "HasValidImage",
    54  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
    55  				return &infrav1exp.AzureMachinePool{
    56  					Spec: infrav1exp.AzureMachinePoolSpec{
    57  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
    58  							Image: &infrav1.Image{
    59  								SharedGallery: &infrav1.AzureSharedGalleryImage{
    60  									SubscriptionID: "foo",
    61  									ResourceGroup:  "blah",
    62  									Name:           "bin",
    63  									Gallery:        "bazz",
    64  									Version:        "1.2.3",
    65  								},
    66  							},
    67  							OSDisk: infrav1.OSDisk{
    68  								OSType:      "Linux",
    69  								CachingType: "None",
    70  							},
    71  						},
    72  					},
    73  				}
    74  			},
    75  			Expect: func(g *gomega.GomegaWithT, actual error) {
    76  				g.Expect(actual).NotTo(gomega.HaveOccurred())
    77  			},
    78  		},
    79  		{
    80  			Name: "HasInvalidImage",
    81  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
    82  				return &infrav1exp.AzureMachinePool{
    83  					Spec: infrav1exp.AzureMachinePoolSpec{
    84  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
    85  							Image: new(infrav1.Image),
    86  							OSDisk: infrav1.OSDisk{
    87  								OSType:      "Linux",
    88  								CachingType: "None",
    89  							},
    90  						},
    91  					},
    92  				}
    93  			},
    94  			Expect: func(g *gomega.GomegaWithT, actual error) {
    95  				g.Expect(actual).To(gomega.HaveOccurred())
    96  				g.Expect(actual.Error()).To(gomega.ContainSubstring("You must supply an ID, Marketplace or ComputeGallery image details"))
    97  			},
    98  		},
    99  		{
   100  			Name: "HasValidTerminateNotificationTimeout",
   101  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
   102  				return &infrav1exp.AzureMachinePool{
   103  					Spec: infrav1exp.AzureMachinePoolSpec{
   104  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
   105  							TerminateNotificationTimeout: ptr.To(7),
   106  							OSDisk: infrav1.OSDisk{
   107  								OSType:      "Linux",
   108  								CachingType: "None",
   109  							},
   110  						},
   111  					},
   112  				}
   113  			},
   114  			Expect: func(g *gomega.GomegaWithT, actual error) {
   115  				g.Expect(actual).NotTo(gomega.HaveOccurred())
   116  			},
   117  		},
   118  		{
   119  			Name: "HasInvalidMaximumTerminateNotificationTimeout",
   120  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
   121  				return &infrav1exp.AzureMachinePool{
   122  					Spec: infrav1exp.AzureMachinePoolSpec{
   123  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
   124  							TerminateNotificationTimeout: ptr.To(20),
   125  							OSDisk: infrav1.OSDisk{
   126  								OSType:      "Linux",
   127  								CachingType: "None",
   128  							},
   129  						},
   130  					},
   131  				}
   132  			},
   133  			Expect: func(g *gomega.GomegaWithT, actual error) {
   134  				g.Expect(actual).To(gomega.HaveOccurred())
   135  				g.Expect(actual.Error()).To(gomega.ContainSubstring("maximum timeout 15 is allowed for TerminateNotificationTimeout"))
   136  			},
   137  		},
   138  		{
   139  			Name: "HasInvalidMinimumTerminateNotificationTimeout",
   140  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
   141  				return &infrav1exp.AzureMachinePool{
   142  					Spec: infrav1exp.AzureMachinePoolSpec{
   143  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
   144  							TerminateNotificationTimeout: ptr.To(3),
   145  							OSDisk: infrav1.OSDisk{
   146  								OSType:      "Linux",
   147  								CachingType: "None",
   148  							},
   149  						},
   150  					},
   151  				}
   152  			},
   153  			Expect: func(g *gomega.GomegaWithT, actual error) {
   154  				g.Expect(actual).To(gomega.HaveOccurred())
   155  				g.Expect(actual.Error()).To(gomega.ContainSubstring("minimum timeout 5 is allowed for TerminateNotificationTimeout"))
   156  			},
   157  		},
   158  		{
   159  			Name: "HasNoDiagnostics",
   160  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
   161  				return &infrav1exp.AzureMachinePool{
   162  					Spec: infrav1exp.AzureMachinePoolSpec{
   163  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
   164  							Diagnostics: nil,
   165  							OSDisk: infrav1.OSDisk{
   166  								OSType:      "Linux",
   167  								CachingType: "None",
   168  							},
   169  						},
   170  					},
   171  				}
   172  			},
   173  			Expect: func(g *gomega.GomegaWithT, actual error) {
   174  				g.Expect(actual).NotTo(gomega.HaveOccurred())
   175  			},
   176  		},
   177  		{
   178  			Name: "HasValidDiagnostics",
   179  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
   180  				return &infrav1exp.AzureMachinePool{
   181  					Spec: infrav1exp.AzureMachinePoolSpec{
   182  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
   183  							Diagnostics: &infrav1.Diagnostics{
   184  								Boot: &infrav1.BootDiagnostics{
   185  									StorageAccountType: infrav1.ManagedDiagnosticsStorage,
   186  								},
   187  							},
   188  							OSDisk: infrav1.OSDisk{
   189  								OSType:      "Linux",
   190  								CachingType: "None",
   191  							},
   192  						},
   193  					},
   194  				}
   195  			},
   196  			Expect: func(g *gomega.GomegaWithT, actual error) {
   197  				g.Expect(actual).NotTo(gomega.HaveOccurred())
   198  			},
   199  		},
   200  		{
   201  			Name: "HasMismatcingManagedDiagnosticsWithStorageAccountURI",
   202  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
   203  				return &infrav1exp.AzureMachinePool{
   204  					Spec: infrav1exp.AzureMachinePoolSpec{
   205  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
   206  							Diagnostics: &infrav1.Diagnostics{
   207  								Boot: &infrav1.BootDiagnostics{
   208  									StorageAccountType: infrav1.ManagedDiagnosticsStorage,
   209  									UserManaged: &infrav1.UserManagedBootDiagnostics{
   210  										StorageAccountURI: "https://fake",
   211  									},
   212  								},
   213  							},
   214  						},
   215  					},
   216  				}
   217  			},
   218  			Expect: func(g *gomega.GomegaWithT, actual error) {
   219  				g.Expect(actual).To(gomega.HaveOccurred())
   220  			},
   221  		},
   222  		{
   223  			Name: "HasMismatcingDisabledDiagnosticsWithStorageAccountURI",
   224  			Factory: func(_ *gomega.GomegaWithT) *infrav1exp.AzureMachinePool {
   225  				return &infrav1exp.AzureMachinePool{
   226  					Spec: infrav1exp.AzureMachinePoolSpec{
   227  						Template: infrav1exp.AzureMachinePoolMachineTemplate{
   228  							Diagnostics: &infrav1.Diagnostics{
   229  								Boot: &infrav1.BootDiagnostics{
   230  									StorageAccountType: infrav1.DisabledDiagnosticsStorage,
   231  									UserManaged: &infrav1.UserManagedBootDiagnostics{
   232  										StorageAccountURI: "https://fake",
   233  									},
   234  								},
   235  							},
   236  						},
   237  					},
   238  				}
   239  			},
   240  			Expect: func(g *gomega.GomegaWithT, actual error) {
   241  				g.Expect(actual).To(gomega.HaveOccurred())
   242  			},
   243  		},
   244  	}
   245  
   246  	for _, c := range cases {
   247  		c := c
   248  		t.Run(c.Name, func(t *testing.T) {
   249  			// Don't add t.Parallel() here or the test will fail.
   250  			g := gomega.NewGomegaWithT(t)
   251  			amp := c.Factory(g)
   252  			actualErr := amp.Validate(nil, nil)
   253  			c.Expect(g, actualErr)
   254  		})
   255  	}
   256  }