sigs.k8s.io/cluster-api@v1.7.1/exp/topology/scope/state_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 scope
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    28  
    29  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    30  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    31  	"sigs.k8s.io/cluster-api/internal/test/builder"
    32  )
    33  
    34  func TestMDUpgrading(t *testing.T) {
    35  	g := NewWithT(t)
    36  	scheme := runtime.NewScheme()
    37  	g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
    38  
    39  	ctx := context.Background()
    40  
    41  	t.Run("should return the names of the upgrading MachineDeployments", func(*testing.T) {
    42  		stableMD := builder.MachineDeployment("ns", "stableMD").
    43  			WithClusterName("cluster1").
    44  			WithVersion("v1.2.3").
    45  			Build()
    46  		stableMDMachine := builder.Machine("ns", "stableMD-machine1").
    47  			WithClusterName("cluster1").
    48  			WithVersion("v1.2.3").
    49  			Build()
    50  
    51  		upgradingMD := builder.MachineDeployment("ns", "upgradingMD").
    52  			WithClusterName("cluster2").
    53  			WithVersion("v1.2.3").
    54  			Build()
    55  		upgradingMDMachine := builder.Machine("ns", "upgradingMD-machine1").
    56  			WithClusterName("cluster2").
    57  			WithVersion("v1.2.2").
    58  			Build()
    59  
    60  		objs := []client.Object{stableMD, stableMDMachine, upgradingMD, upgradingMDMachine}
    61  		fakeClient := fake.NewClientBuilder().WithObjects(objs...).WithScheme(scheme).Build()
    62  
    63  		mdsStateMap := MachineDeploymentsStateMap{
    64  			"stableMD":    {Object: stableMD},
    65  			"upgradingMD": {Object: upgradingMD},
    66  		}
    67  		want := []string{"upgradingMD"}
    68  
    69  		got, err := mdsStateMap.Upgrading(ctx, fakeClient)
    70  		g.Expect(err).ToNot(HaveOccurred())
    71  		g.Expect(got).To(BeComparableTo(want))
    72  	})
    73  }
    74  
    75  func TestMPUpgrading(t *testing.T) {
    76  	g := NewWithT(t)
    77  	scheme := runtime.NewScheme()
    78  	g.Expect(expv1.AddToScheme(scheme)).To(Succeed())
    79  	g.Expect(corev1.AddToScheme(scheme)).To(Succeed())
    80  
    81  	ctx := context.Background()
    82  
    83  	t.Run("should return the names of the upgrading MachinePools", func(*testing.T) {
    84  		stableMP := builder.MachinePool("ns", "stableMP").
    85  			WithClusterName("cluster1").
    86  			WithVersion("v1.2.3").
    87  			WithStatus(expv1.MachinePoolStatus{
    88  				NodeRefs: []corev1.ObjectReference{
    89  					{
    90  						Name: "stableMP-node1",
    91  					},
    92  				},
    93  			}).
    94  			Build()
    95  		stableMPNode := builder.Node("stableMP-node1").
    96  			WithStatus(corev1.NodeStatus{
    97  				NodeInfo: corev1.NodeSystemInfo{
    98  					KubeletVersion: "v1.2.3",
    99  				},
   100  			}).
   101  			Build()
   102  
   103  		upgradingMP := builder.MachinePool("ns", "upgradingMP").
   104  			WithClusterName("cluster2").
   105  			WithVersion("v1.2.3").
   106  			WithStatus(expv1.MachinePoolStatus{
   107  				NodeRefs: []corev1.ObjectReference{
   108  					{
   109  						Name: "upgradingMP-node1",
   110  					},
   111  				},
   112  			}).
   113  			Build()
   114  		upgradingMPNode := builder.Node("upgradingMP-node1").
   115  			WithStatus(corev1.NodeStatus{
   116  				NodeInfo: corev1.NodeSystemInfo{
   117  					KubeletVersion: "v1.2.2",
   118  				},
   119  			}).
   120  			Build()
   121  
   122  		objs := []client.Object{stableMP, stableMPNode, upgradingMP, upgradingMPNode}
   123  		fakeClient := fake.NewClientBuilder().WithObjects(objs...).WithScheme(scheme).Build()
   124  
   125  		mpsStateMap := MachinePoolsStateMap{
   126  			"stableMP":    {Object: stableMP},
   127  			"upgradingMP": {Object: upgradingMP},
   128  		}
   129  		want := []string{"upgradingMP"}
   130  
   131  		got, err := mpsStateMap.Upgrading(ctx, fakeClient)
   132  		g.Expect(err).ToNot(HaveOccurred())
   133  		g.Expect(got).To(BeComparableTo(want))
   134  	})
   135  }