|
| 1 | +/* |
| 2 | +Copyright 2020 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 alpha |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/utils/ptr" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + |
| 28 | + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" |
| 29 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test" |
| 30 | +) |
| 31 | + |
| 32 | +func Test_ObjectRollbacker(t *testing.T) { |
| 33 | + labels := map[string]string{ |
| 34 | + clusterv1.ClusterNameLabel: "test", |
| 35 | + clusterv1.MachineDeploymentNameLabel: "test-md-0", |
| 36 | + } |
| 37 | + currentVersion := "v1.19.3" |
| 38 | + rollbackVersion := "v1.19.1" |
| 39 | + deployment := &clusterv1.MachineDeployment{ |
| 40 | + TypeMeta: metav1.TypeMeta{ |
| 41 | + Kind: "MachineDeployment", |
| 42 | + }, |
| 43 | + ObjectMeta: metav1.ObjectMeta{ |
| 44 | + Name: "test-md-0", |
| 45 | + Namespace: "default", |
| 46 | + Labels: map[string]string{ |
| 47 | + clusterv1.ClusterNameLabel: "test", |
| 48 | + }, |
| 49 | + Annotations: map[string]string{ |
| 50 | + clusterv1.RevisionAnnotation: "2", |
| 51 | + }, |
| 52 | + }, |
| 53 | + Spec: clusterv1.MachineDeploymentSpec{ |
| 54 | + ClusterName: "test", |
| 55 | + Selector: metav1.LabelSelector{ |
| 56 | + MatchLabels: map[string]string{ |
| 57 | + clusterv1.ClusterNameLabel: "test", |
| 58 | + }, |
| 59 | + }, |
| 60 | + Template: clusterv1.MachineTemplateSpec{ |
| 61 | + ObjectMeta: clusterv1.ObjectMeta{ |
| 62 | + Labels: labels, |
| 63 | + }, |
| 64 | + Spec: clusterv1.MachineSpec{ |
| 65 | + ClusterName: "test", |
| 66 | + Version: ¤tVersion, |
| 67 | + InfrastructureRef: corev1.ObjectReference{ |
| 68 | + APIVersion: clusterv1.GroupVersionInfrastructure.String(), |
| 69 | + Kind: "InfrastructureMachineTemplate", |
| 70 | + Name: "md-template", |
| 71 | + }, |
| 72 | + Bootstrap: clusterv1.Bootstrap{ |
| 73 | + DataSecretName: ptr.To("data-secret-name"), |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + type fields struct { |
| 80 | + objs []client.Object |
| 81 | + ref corev1.ObjectReference |
| 82 | + toRevision int64 |
| 83 | + } |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + fields fields |
| 87 | + wantErr bool |
| 88 | + wantVersion string |
| 89 | + wantInfraTemplate string |
| 90 | + wantBootsrapSecretName string |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "machinedeployment should rollback to revision=1", |
| 94 | + fields: fields{ |
| 95 | + objs: []client.Object{ |
| 96 | + deployment, |
| 97 | + &clusterv1.MachineSet{ |
| 98 | + TypeMeta: metav1.TypeMeta{ |
| 99 | + Kind: "MachineSet", |
| 100 | + }, |
| 101 | + ObjectMeta: metav1.ObjectMeta{ |
| 102 | + Name: "ms-rev-2", |
| 103 | + Namespace: "default", |
| 104 | + OwnerReferences: []metav1.OwnerReference{ |
| 105 | + *metav1.NewControllerRef(deployment, clusterv1.GroupVersion.WithKind("MachineDeployment")), |
| 106 | + }, |
| 107 | + Labels: map[string]string{ |
| 108 | + clusterv1.ClusterNameLabel: "test", |
| 109 | + }, |
| 110 | + Annotations: map[string]string{ |
| 111 | + clusterv1.RevisionAnnotation: "2", |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + &clusterv1.MachineSet{ |
| 116 | + TypeMeta: metav1.TypeMeta{ |
| 117 | + Kind: "MachineSet", |
| 118 | + }, |
| 119 | + ObjectMeta: metav1.ObjectMeta{ |
| 120 | + Namespace: "default", |
| 121 | + Name: "ms-rev-1", |
| 122 | + OwnerReferences: []metav1.OwnerReference{ |
| 123 | + *metav1.NewControllerRef(deployment, clusterv1.GroupVersion.WithKind("MachineDeployment")), |
| 124 | + }, |
| 125 | + Labels: map[string]string{ |
| 126 | + clusterv1.ClusterNameLabel: "test", |
| 127 | + }, |
| 128 | + Annotations: map[string]string{ |
| 129 | + clusterv1.RevisionAnnotation: "999", |
| 130 | + }, |
| 131 | + }, |
| 132 | + Spec: clusterv1.MachineSetSpec{ |
| 133 | + ClusterName: "test", |
| 134 | + Selector: metav1.LabelSelector{ |
| 135 | + MatchLabels: map[string]string{ |
| 136 | + clusterv1.ClusterNameLabel: "test", |
| 137 | + }, |
| 138 | + }, |
| 139 | + Template: clusterv1.MachineTemplateSpec{ |
| 140 | + ObjectMeta: clusterv1.ObjectMeta{ |
| 141 | + Labels: labels, |
| 142 | + }, |
| 143 | + Spec: clusterv1.MachineSpec{ |
| 144 | + ClusterName: "test", |
| 145 | + Version: &rollbackVersion, |
| 146 | + InfrastructureRef: corev1.ObjectReference{ |
| 147 | + APIVersion: clusterv1.GroupVersionInfrastructure.String(), |
| 148 | + Kind: "InfrastructureMachineTemplate", |
| 149 | + Name: "md-template-rollback", |
| 150 | + }, |
| 151 | + Bootstrap: clusterv1.Bootstrap{ |
| 152 | + DataSecretName: ptr.To("data-secret-name-rollback"), |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + ref: corev1.ObjectReference{ |
| 160 | + Kind: MachineDeployment, |
| 161 | + Name: "test-md-0", |
| 162 | + Namespace: "default", |
| 163 | + }, |
| 164 | + toRevision: int64(999), |
| 165 | + }, |
| 166 | + wantErr: false, |
| 167 | + wantVersion: rollbackVersion, |
| 168 | + wantInfraTemplate: "md-template-rollback", |
| 169 | + wantBootsrapSecretName: "data-secret-name-rollback", |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "machinedeployment should not rollback because there is no previous revision", |
| 173 | + fields: fields{ |
| 174 | + objs: []client.Object{ |
| 175 | + deployment, |
| 176 | + &clusterv1.MachineSet{ |
| 177 | + TypeMeta: metav1.TypeMeta{ |
| 178 | + Kind: "MachineSet", |
| 179 | + }, |
| 180 | + ObjectMeta: metav1.ObjectMeta{ |
| 181 | + Name: "ms-rev-2", |
| 182 | + Namespace: "default", |
| 183 | + OwnerReferences: []metav1.OwnerReference{ |
| 184 | + *metav1.NewControllerRef(deployment, clusterv1.GroupVersion.WithKind("MachineDeployment")), |
| 185 | + }, |
| 186 | + Labels: map[string]string{ |
| 187 | + clusterv1.ClusterNameLabel: "test", |
| 188 | + }, |
| 189 | + Annotations: map[string]string{ |
| 190 | + clusterv1.RevisionAnnotation: "2", |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + ref: corev1.ObjectReference{ |
| 196 | + Kind: MachineDeployment, |
| 197 | + Name: "test-md-0", |
| 198 | + Namespace: "default", |
| 199 | + }, |
| 200 | + toRevision: int64(0), |
| 201 | + }, |
| 202 | + wantErr: true, |
| 203 | + }, |
| 204 | + { |
| 205 | + name: "machinedeployment should not rollback because the specified version does not exist", |
| 206 | + fields: fields{ |
| 207 | + objs: []client.Object{ |
| 208 | + deployment, |
| 209 | + &clusterv1.MachineSet{ |
| 210 | + TypeMeta: metav1.TypeMeta{ |
| 211 | + Kind: "MachineSet", |
| 212 | + }, |
| 213 | + ObjectMeta: metav1.ObjectMeta{ |
| 214 | + Name: "ms-rev-2", |
| 215 | + Namespace: "default", |
| 216 | + OwnerReferences: []metav1.OwnerReference{ |
| 217 | + *metav1.NewControllerRef(deployment, clusterv1.GroupVersion.WithKind("MachineDeployment")), |
| 218 | + }, |
| 219 | + Labels: map[string]string{ |
| 220 | + clusterv1.ClusterNameLabel: "test", |
| 221 | + }, |
| 222 | + Annotations: map[string]string{ |
| 223 | + clusterv1.RevisionAnnotation: "2", |
| 224 | + }, |
| 225 | + }, |
| 226 | + }, |
| 227 | + }, |
| 228 | + ref: corev1.ObjectReference{ |
| 229 | + Kind: MachineDeployment, |
| 230 | + Name: "test-md-0", |
| 231 | + Namespace: "default", |
| 232 | + }, |
| 233 | + toRevision: int64(999), |
| 234 | + }, |
| 235 | + wantErr: true, |
| 236 | + }, |
| 237 | + } |
| 238 | + for _, tt := range tests { |
| 239 | + t.Run(tt.name, func(t *testing.T) { |
| 240 | + g := NewWithT(t) |
| 241 | + r := newRolloutClient() |
| 242 | + proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...) |
| 243 | + err := r.ObjectRollbacker(t.Context(), proxy, tt.fields.ref, tt.fields.toRevision) |
| 244 | + if tt.wantErr { |
| 245 | + g.Expect(err).To(HaveOccurred()) |
| 246 | + return |
| 247 | + } |
| 248 | + g.Expect(err).ToNot(HaveOccurred()) |
| 249 | + cl, err := proxy.NewClient(t.Context()) |
| 250 | + g.Expect(err).ToNot(HaveOccurred()) |
| 251 | + key := client.ObjectKeyFromObject(deployment) |
| 252 | + md := &clusterv1.MachineDeployment{} |
| 253 | + err = cl.Get(t.Context(), key, md) |
| 254 | + g.Expect(err).ToNot(HaveOccurred()) |
| 255 | + g.Expect(*md.Spec.Template.Spec.Version).To(Equal(tt.wantVersion)) |
| 256 | + g.Expect(md.Spec.Template.Spec.InfrastructureRef.Name).To(Equal(tt.wantInfraTemplate)) |
| 257 | + g.Expect(*md.Spec.Template.Spec.Bootstrap.DataSecretName).To(Equal(tt.wantBootsrapSecretName)) |
| 258 | + }) |
| 259 | + } |
| 260 | +} |
0 commit comments