sigs.k8s.io/cluster-api@v1.7.1/internal/topology/check/upgrade_test.go (about) 1 /* 2 Copyright 2024 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 check 18 19 import ( 20 "context" 21 "testing" 22 23 . "github.com/onsi/gomega" 24 corev1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/runtime" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 "sigs.k8s.io/controller-runtime/pkg/client/fake" 29 30 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 31 expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" 32 "sigs.k8s.io/cluster-api/internal/test/builder" 33 ) 34 35 func TestIsMachineDeploymentUpgrading(t *testing.T) { 36 g := NewWithT(t) 37 scheme := runtime.NewScheme() 38 g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed()) 39 40 tests := []struct { 41 name string 42 md *clusterv1.MachineDeployment 43 machines []*clusterv1.Machine 44 want bool 45 wantErr bool 46 }{ 47 { 48 name: "should return false if all the machines of MachineDeployment have the same version as the MachineDeployment", 49 md: builder.MachineDeployment("ns", "md1"). 50 WithClusterName("cluster1"). 51 WithVersion("v1.2.3"). 52 Build(), 53 machines: []*clusterv1.Machine{ 54 builder.Machine("ns", "machine1"). 55 WithClusterName("cluster1"). 56 WithVersion("v1.2.3"). 57 Build(), 58 builder.Machine("ns", "machine2"). 59 WithClusterName("cluster1"). 60 WithVersion("v1.2.3"). 61 Build(), 62 }, 63 want: false, 64 wantErr: false, 65 }, 66 { 67 name: "should return true if at least one of the machines of MachineDeployment has a different version", 68 md: builder.MachineDeployment("ns", "md1"). 69 WithClusterName("cluster1"). 70 WithVersion("v1.2.3"). 71 Build(), 72 machines: []*clusterv1.Machine{ 73 builder.Machine("ns", "machine1"). 74 WithClusterName("cluster1"). 75 WithVersion("v1.2.3"). 76 Build(), 77 builder.Machine("ns", "machine2"). 78 WithClusterName("cluster1"). 79 WithVersion("v1.2.2"). 80 Build(), 81 }, 82 want: true, 83 wantErr: false, 84 }, 85 { 86 name: "should return false if the MachineDeployment has no machines (creation phase)", 87 md: builder.MachineDeployment("ns", "md1"). 88 WithClusterName("cluster1"). 89 WithVersion("v1.2.3"). 90 Build(), 91 machines: []*clusterv1.Machine{}, 92 want: false, 93 wantErr: false, 94 }, 95 } 96 for _, tt := range tests { 97 t.Run(tt.name, func(t *testing.T) { 98 g := NewWithT(t) 99 100 ctx := context.Background() 101 102 objs := []client.Object{} 103 objs = append(objs, tt.md) 104 for _, m := range tt.machines { 105 objs = append(objs, m) 106 } 107 fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build() 108 109 got, err := IsMachineDeploymentUpgrading(ctx, fakeClient, tt.md) 110 if tt.wantErr { 111 g.Expect(err).To(HaveOccurred()) 112 } else { 113 g.Expect(err).ToNot(HaveOccurred()) 114 g.Expect(got).To(Equal(tt.want)) 115 } 116 }) 117 } 118 } 119 120 func TestIsMachinePoolUpgrading(t *testing.T) { 121 g := NewWithT(t) 122 scheme := runtime.NewScheme() 123 g.Expect(corev1.AddToScheme(scheme)).To(Succeed()) 124 125 tests := []struct { 126 name string 127 mp *expv1.MachinePool 128 nodes []*corev1.Node 129 want bool 130 wantErr bool 131 }{ 132 { 133 name: "should return false if all the nodes of MachinePool have the same version as the MachinePool", 134 mp: builder.MachinePool("ns", "mp1"). 135 WithClusterName("cluster1"). 136 WithVersion("v1.2.3"). 137 WithStatus(expv1.MachinePoolStatus{ 138 NodeRefs: []corev1.ObjectReference{ 139 {Name: "node1"}, 140 {Name: "node2"}, 141 }}). 142 Build(), 143 nodes: []*corev1.Node{ 144 { 145 ObjectMeta: metav1.ObjectMeta{Name: "node1"}, 146 Status: corev1.NodeStatus{ 147 NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.3"}, 148 }, 149 }, 150 { 151 ObjectMeta: metav1.ObjectMeta{Name: "node2"}, 152 Status: corev1.NodeStatus{ 153 NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.3"}, 154 }, 155 }, 156 }, 157 want: false, 158 wantErr: false, 159 }, 160 { 161 name: "should return true if at least one of the nodes of MachinePool has a different version", 162 mp: builder.MachinePool("ns", "mp1"). 163 WithClusterName("cluster1"). 164 WithVersion("v1.2.3"). 165 WithStatus(expv1.MachinePoolStatus{ 166 NodeRefs: []corev1.ObjectReference{ 167 {Name: "node1"}, 168 {Name: "node2"}, 169 }}). 170 Build(), 171 nodes: []*corev1.Node{ 172 { 173 ObjectMeta: metav1.ObjectMeta{Name: "node1"}, 174 Status: corev1.NodeStatus{ 175 NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.3"}, 176 }, 177 }, 178 { 179 ObjectMeta: metav1.ObjectMeta{Name: "node2"}, 180 Status: corev1.NodeStatus{ 181 NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.2"}, 182 }, 183 }, 184 }, 185 want: true, 186 wantErr: false, 187 }, 188 { 189 name: "should return false if the MachinePool has no nodes (creation phase)", 190 mp: builder.MachinePool("ns", "mp1"). 191 WithClusterName("cluster1"). 192 WithVersion("v1.2.3"). 193 WithStatus(expv1.MachinePoolStatus{ 194 NodeRefs: []corev1.ObjectReference{}}). 195 Build(), 196 nodes: []*corev1.Node{}, 197 want: false, 198 wantErr: false, 199 }, 200 } 201 for _, tt := range tests { 202 t.Run(tt.name, func(t *testing.T) { 203 g := NewWithT(t) 204 205 ctx := context.Background() 206 207 objs := []client.Object{} 208 for _, m := range tt.nodes { 209 objs = append(objs, m) 210 } 211 fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build() 212 213 got, err := IsMachinePoolUpgrading(ctx, fakeClient, tt.mp) 214 if tt.wantErr { 215 g.Expect(err).To(HaveOccurred()) 216 } else { 217 g.Expect(err).ToNot(HaveOccurred()) 218 g.Expect(got).To(Equal(tt.want)) 219 } 220 }) 221 } 222 }