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

     1  /*
     2  Copyright 2023 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
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"k8s.io/utils/ptr"
    24  )
    25  
    26  func TestAzureManagedControlPlane_SetDefaultSSHPublicKey(t *testing.T) {
    27  	g := NewWithT(t)
    28  
    29  	type test struct {
    30  		m *AzureManagedControlPlane
    31  	}
    32  
    33  	existingPublicKey := "testpublickey"
    34  	publicKeyExistTest := test{m: createAzureManagedControlPlaneWithSSHPublicKey(existingPublicKey)}
    35  	publicKeyNotExistTest := test{m: createAzureManagedControlPlaneWithSSHPublicKey("")}
    36  
    37  	err := publicKeyExistTest.m.setDefaultSSHPublicKey()
    38  	g.Expect(err).NotTo(HaveOccurred())
    39  	g.Expect(*publicKeyExistTest.m.Spec.SSHPublicKey).To(Equal(existingPublicKey))
    40  
    41  	err = publicKeyNotExistTest.m.setDefaultSSHPublicKey()
    42  	g.Expect(err).NotTo(HaveOccurred())
    43  	g.Expect(*publicKeyNotExistTest.m.Spec.SSHPublicKey).NotTo(BeEmpty())
    44  }
    45  
    46  func createAzureManagedControlPlaneWithSSHPublicKey(sshPublicKey string) *AzureManagedControlPlane {
    47  	return hardcodedAzureManagedControlPlaneWithSSHKey(sshPublicKey)
    48  }
    49  
    50  func hardcodedAzureManagedControlPlaneWithSSHKey(sshPublicKey string) *AzureManagedControlPlane {
    51  	return &AzureManagedControlPlane{
    52  		Spec: AzureManagedControlPlaneSpec{
    53  			SSHPublicKey: &sshPublicKey,
    54  		},
    55  	}
    56  }
    57  
    58  func TestSetDefaultAutoScalerProfile(t *testing.T) {
    59  	g := NewWithT(t)
    60  
    61  	type test struct {
    62  		amcp *AzureManagedControlPlane
    63  	}
    64  
    65  	defaultAMP := &AzureManagedControlPlane{
    66  		Spec: AzureManagedControlPlaneSpec{
    67  			AzureManagedControlPlaneClassSpec: AzureManagedControlPlaneClassSpec{
    68  				AutoScalerProfile: &AutoScalerProfile{
    69  					BalanceSimilarNodeGroups:      (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsFalse))),
    70  					Expander:                      (*Expander)(ptr.To(string(ExpanderRandom))),
    71  					MaxEmptyBulkDelete:            ptr.To("10"),
    72  					MaxGracefulTerminationSec:     ptr.To("600"),
    73  					MaxNodeProvisionTime:          ptr.To("15m"),
    74  					MaxTotalUnreadyPercentage:     ptr.To("45"),
    75  					NewPodScaleUpDelay:            ptr.To("0s"),
    76  					OkTotalUnreadyCount:           ptr.To("3"),
    77  					ScanInterval:                  ptr.To("10s"),
    78  					ScaleDownDelayAfterAdd:        ptr.To("10m"),
    79  					ScaleDownDelayAfterDelete:     ptr.To("10s"),
    80  					ScaleDownDelayAfterFailure:    ptr.To("3m"),
    81  					ScaleDownUnneededTime:         ptr.To("10m"),
    82  					ScaleDownUnreadyTime:          ptr.To("20m"),
    83  					ScaleDownUtilizationThreshold: ptr.To("0.5"),
    84  					SkipNodesWithLocalStorage:     (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageFalse))),
    85  					SkipNodesWithSystemPods:       (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsTrue))),
    86  				},
    87  			},
    88  		},
    89  	}
    90  
    91  	allFieldsAreNilTest := test{amcp: &AzureManagedControlPlane{
    92  		Spec: AzureManagedControlPlaneSpec{
    93  			AzureManagedControlPlaneClassSpec: AzureManagedControlPlaneClassSpec{
    94  				AutoScalerProfile: &AutoScalerProfile{},
    95  			},
    96  		},
    97  	}}
    98  
    99  	allFieldsAreNilTest.amcp.Spec.AutoScalerProfile = setDefaultAutoScalerProfile(allFieldsAreNilTest.amcp.Spec.AutoScalerProfile)
   100  
   101  	g.Expect(allFieldsAreNilTest.amcp.Spec.AutoScalerProfile).To(Equal(defaultAMP.Spec.AutoScalerProfile))
   102  
   103  	expectedNotNil := &AzureManagedControlPlane{
   104  		Spec: AzureManagedControlPlaneSpec{
   105  			AzureManagedControlPlaneClassSpec: AzureManagedControlPlaneClassSpec{
   106  				AutoScalerProfile: &AutoScalerProfile{
   107  					BalanceSimilarNodeGroups:      (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsTrue))),
   108  					Expander:                      (*Expander)(ptr.To(string(ExpanderLeastWaste))),
   109  					MaxEmptyBulkDelete:            ptr.To("5"),
   110  					MaxGracefulTerminationSec:     ptr.To("300"),
   111  					MaxNodeProvisionTime:          ptr.To("10m"),
   112  					MaxTotalUnreadyPercentage:     ptr.To("30"),
   113  					NewPodScaleUpDelay:            ptr.To("30s"),
   114  					OkTotalUnreadyCount:           ptr.To("5"),
   115  					ScanInterval:                  ptr.To("20s"),
   116  					ScaleDownDelayAfterAdd:        ptr.To("5m"),
   117  					ScaleDownDelayAfterDelete:     ptr.To("1m"),
   118  					ScaleDownDelayAfterFailure:    ptr.To("2m"),
   119  					ScaleDownUnneededTime:         ptr.To("5m"),
   120  					ScaleDownUnreadyTime:          ptr.To("10m"),
   121  					ScaleDownUtilizationThreshold: ptr.To("0.4"),
   122  					SkipNodesWithLocalStorage:     (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageTrue))),
   123  					SkipNodesWithSystemPods:       (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsFalse))),
   124  				},
   125  			},
   126  		},
   127  	}
   128  
   129  	allFieldsAreNotNilTest := test{amcp: &AzureManagedControlPlane{
   130  		Spec: AzureManagedControlPlaneSpec{
   131  			AzureManagedControlPlaneClassSpec: AzureManagedControlPlaneClassSpec{
   132  				AutoScalerProfile: ptr.To(*expectedNotNil.Spec.AutoScalerProfile),
   133  			},
   134  		},
   135  	}}
   136  
   137  	allFieldsAreNotNilTest.amcp.Spec.AutoScalerProfile = setDefaultAutoScalerProfile(allFieldsAreNotNilTest.amcp.Spec.AutoScalerProfile)
   138  
   139  	g.Expect(allFieldsAreNotNilTest.amcp.Spec.AutoScalerProfile).To(Equal(expectedNotNil.Spec.AutoScalerProfile))
   140  }