sigs.k8s.io/cluster-api@v1.6.3/internal/controllers/topology/cluster/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 "k8s.io/apimachinery/pkg/runtime" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 "sigs.k8s.io/controller-runtime/pkg/client/fake" 27 28 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 29 "sigs.k8s.io/cluster-api/internal/test/builder" 30 ) 31 32 func TestIsUpgrading(t *testing.T) { 33 g := NewWithT(t) 34 scheme := runtime.NewScheme() 35 g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed()) 36 37 tests := []struct { 38 name string 39 md *clusterv1.MachineDeployment 40 machines []*clusterv1.Machine 41 want bool 42 wantErr bool 43 }{ 44 { 45 name: "should return false if all the machines of MachineDeployment have the same version as the MachineDeployment", 46 md: builder.MachineDeployment("ns", "md1"). 47 WithClusterName("cluster1"). 48 WithVersion("v1.2.3"). 49 Build(), 50 machines: []*clusterv1.Machine{ 51 builder.Machine("ns", "machine1"). 52 WithClusterName("cluster1"). 53 WithVersion("v1.2.3"). 54 Build(), 55 builder.Machine("ns", "machine2"). 56 WithClusterName("cluster1"). 57 WithVersion("v1.2.3"). 58 Build(), 59 }, 60 want: false, 61 wantErr: false, 62 }, 63 { 64 name: "should return true if at least one of the machines of MachineDeployment has a different version", 65 md: builder.MachineDeployment("ns", "md1"). 66 WithClusterName("cluster1"). 67 WithVersion("v1.2.3"). 68 Build(), 69 machines: []*clusterv1.Machine{ 70 builder.Machine("ns", "machine1"). 71 WithClusterName("cluster1"). 72 WithVersion("v1.2.3"). 73 Build(), 74 builder.Machine("ns", "machine2"). 75 WithClusterName("cluster1"). 76 WithVersion("v1.2.2"). 77 Build(), 78 }, 79 want: true, 80 wantErr: false, 81 }, 82 { 83 name: "should return false if the MachineDeployment has no machines (creation phase)", 84 md: builder.MachineDeployment("ns", "md1"). 85 WithClusterName("cluster1"). 86 WithVersion("v1.2.3"). 87 Build(), 88 machines: []*clusterv1.Machine{}, 89 want: false, 90 wantErr: false, 91 }, 92 } 93 94 for _, tt := range tests { 95 t.Run(tt.name, func(t *testing.T) { 96 g := NewWithT(t) 97 ctx := context.Background() 98 objs := []client.Object{} 99 objs = append(objs, tt.md) 100 for _, m := range tt.machines { 101 objs = append(objs, m) 102 } 103 fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build() 104 mdState := &MachineDeploymentState{ 105 Object: tt.md, 106 } 107 got, err := mdState.IsUpgrading(ctx, fakeClient) 108 if tt.wantErr { 109 g.Expect(err).To(HaveOccurred()) 110 } else { 111 g.Expect(err).ToNot(HaveOccurred()) 112 g.Expect(got).To(Equal(tt.want)) 113 } 114 }) 115 } 116 } 117 118 func TestUpgrading(t *testing.T) { 119 g := NewWithT(t) 120 scheme := runtime.NewScheme() 121 g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed()) 122 123 ctx := context.Background() 124 125 t.Run("should return the names of the upgrading MachineDeployments", func(t *testing.T) { 126 stableMD := builder.MachineDeployment("ns", "stableMD"). 127 WithClusterName("cluster1"). 128 WithVersion("v1.2.3"). 129 Build() 130 stableMDMachine := builder.Machine("ns", "stableMD-machine1"). 131 WithClusterName("cluster1"). 132 WithVersion("v1.2.3"). 133 Build() 134 135 upgradingMD := builder.MachineDeployment("ns", "upgradingMD"). 136 WithClusterName("cluster2"). 137 WithVersion("v1.2.3"). 138 Build() 139 upgradingMDMachine := builder.Machine("ns", "upgradingMD-machine1"). 140 WithClusterName("cluster2"). 141 WithVersion("v1.2.2"). 142 Build() 143 144 objs := []client.Object{stableMD, stableMDMachine, upgradingMD, upgradingMDMachine} 145 fakeClient := fake.NewClientBuilder().WithObjects(objs...).WithScheme(scheme).Build() 146 147 mdsStateMap := MachineDeploymentsStateMap{ 148 "stableMD": {Object: stableMD}, 149 "upgradingMD": {Object: upgradingMD}, 150 } 151 want := []string{"upgradingMD"} 152 153 got, err := mdsStateMap.Upgrading(ctx, fakeClient) 154 g.Expect(err).ToNot(HaveOccurred()) 155 g.Expect(got).To(BeComparableTo(want)) 156 }) 157 }