sigs.k8s.io/cluster-api@v1.7.1/internal/controllers/topology/machineset/util_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 machineset
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"github.com/pkg/errors"
    24  	corev1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  
    27  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    28  	"sigs.k8s.io/cluster-api/internal/test/builder"
    29  )
    30  
    31  func TestCalculateTemplatesInUse(t *testing.T) {
    32  	t.Run("Calculate templates in use with regular MachineDeployment and MachineSet", func(t *testing.T) {
    33  		g := NewWithT(t)
    34  
    35  		md := builder.MachineDeployment(metav1.NamespaceDefault, "md").
    36  			WithBootstrapTemplate(builder.BootstrapTemplate(metav1.NamespaceDefault, "mdBT").Build()).
    37  			WithInfrastructureTemplate(builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "mdIMT").Build()).
    38  			Build()
    39  		ms := builder.MachineSet(metav1.NamespaceDefault, "ms").
    40  			WithBootstrapTemplate(builder.BootstrapTemplate(metav1.NamespaceDefault, "msBT").Build()).
    41  			WithInfrastructureTemplate(builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "msIMT").Build()).
    42  			Build()
    43  
    44  		actual, err := CalculateTemplatesInUse(md, []*clusterv1.MachineSet{ms})
    45  		g.Expect(err).ToNot(HaveOccurred())
    46  		g.Expect(actual).To(HaveLen(4))
    47  
    48  		g.Expect(actual).To(HaveKey(mustTemplateRefID(md.Spec.Template.Spec.Bootstrap.ConfigRef)))
    49  		g.Expect(actual).To(HaveKey(mustTemplateRefID(&md.Spec.Template.Spec.InfrastructureRef)))
    50  
    51  		g.Expect(actual).To(HaveKey(mustTemplateRefID(ms.Spec.Template.Spec.Bootstrap.ConfigRef)))
    52  		g.Expect(actual).To(HaveKey(mustTemplateRefID(&ms.Spec.Template.Spec.InfrastructureRef)))
    53  	})
    54  
    55  	t.Run("Calculate templates in use with MachineDeployment and MachineSet without BootstrapTemplate", func(t *testing.T) {
    56  		g := NewWithT(t)
    57  
    58  		mdWithoutBootstrapTemplate := builder.MachineDeployment(metav1.NamespaceDefault, "md").
    59  			WithInfrastructureTemplate(builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "mdIMT").Build()).
    60  			Build()
    61  		msWithoutBootstrapTemplate := builder.MachineSet(metav1.NamespaceDefault, "ms").
    62  			WithInfrastructureTemplate(builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "msIMT").Build()).
    63  			Build()
    64  
    65  		actual, err := CalculateTemplatesInUse(mdWithoutBootstrapTemplate, []*clusterv1.MachineSet{msWithoutBootstrapTemplate})
    66  		g.Expect(err).ToNot(HaveOccurred())
    67  		g.Expect(actual).To(HaveLen(2))
    68  
    69  		g.Expect(actual).To(HaveKey(mustTemplateRefID(&mdWithoutBootstrapTemplate.Spec.Template.Spec.InfrastructureRef)))
    70  
    71  		g.Expect(actual).To(HaveKey(mustTemplateRefID(&msWithoutBootstrapTemplate.Spec.Template.Spec.InfrastructureRef)))
    72  	})
    73  
    74  	t.Run("Calculate templates in use with MachineDeployment and MachineSet ignore templates when resources in deleting", func(t *testing.T) {
    75  		g := NewWithT(t)
    76  
    77  		deletionTimeStamp := metav1.Now()
    78  
    79  		mdInDeleting := builder.MachineDeployment(metav1.NamespaceDefault, "md").
    80  			WithInfrastructureTemplate(builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "mdIMT").Build()).
    81  			Build()
    82  		mdInDeleting.SetDeletionTimestamp(&deletionTimeStamp)
    83  
    84  		msInDeleting := builder.MachineSet(metav1.NamespaceDefault, "ms").
    85  			WithInfrastructureTemplate(builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "msIMT").Build()).
    86  			Build()
    87  		msInDeleting.SetDeletionTimestamp(&deletionTimeStamp)
    88  
    89  		actual, err := CalculateTemplatesInUse(mdInDeleting, []*clusterv1.MachineSet{msInDeleting})
    90  		g.Expect(err).ToNot(HaveOccurred())
    91  		g.Expect(actual).To(BeEmpty())
    92  
    93  		g.Expect(actual).ToNot(HaveKey(mustTemplateRefID(&mdInDeleting.Spec.Template.Spec.InfrastructureRef)))
    94  
    95  		g.Expect(actual).ToNot(HaveKey(mustTemplateRefID(&msInDeleting.Spec.Template.Spec.InfrastructureRef)))
    96  	})
    97  
    98  	t.Run("Calculate templates in use without MachineDeployment and with MachineSet", func(t *testing.T) {
    99  		g := NewWithT(t)
   100  
   101  		ms := builder.MachineSet(metav1.NamespaceDefault, "ms").
   102  			WithBootstrapTemplate(builder.BootstrapTemplate(metav1.NamespaceDefault, "msBT").Build()).
   103  			WithInfrastructureTemplate(builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "msIMT").Build()).
   104  			Build()
   105  
   106  		actual, err := CalculateTemplatesInUse(nil, []*clusterv1.MachineSet{ms})
   107  		g.Expect(err).ToNot(HaveOccurred())
   108  		g.Expect(actual).To(HaveLen(2))
   109  
   110  		g.Expect(actual).To(HaveKey(mustTemplateRefID(ms.Spec.Template.Spec.Bootstrap.ConfigRef)))
   111  		g.Expect(actual).To(HaveKey(mustTemplateRefID(&ms.Spec.Template.Spec.InfrastructureRef)))
   112  	})
   113  }
   114  
   115  // mustTemplateRefID returns the templateRefID as calculated by templateRefID, but panics
   116  // if templateRefID returns an error.
   117  func mustTemplateRefID(ref *corev1.ObjectReference) string {
   118  	refID, err := templateRefID(ref)
   119  	if err != nil {
   120  		panic(errors.Wrap(err, "failed to calculate templateRefID"))
   121  	}
   122  	return refID
   123  }