Skip to content

Commit 6056d61

Browse files
committed
enable usetesting linter
Signed-off-by: sivchari <[email protected]>
1 parent 5eee55a commit 6056d61

File tree

88 files changed

+991
-325
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+991
-325
lines changed

.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ linters:
4747
- usestdlibvars # using variables/constants from the standard library
4848
- usetesting # report function to be replace by testing
4949
- whitespace # unnecessary newlines
50-
disable:
51-
# TODO: It will be dropped when the Go version migration is done.
52-
- usetesting
5350
settings:
5451
ginkgolinter:
5552
forbid-focus-container: true

cmd/clusterctl/client/alpha/rollout_pauser_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package alpha
1818

1919
import (
20-
"context"
2120
"testing"
2221

2322
. "github.com/onsi/gomega"
@@ -146,25 +145,25 @@ func Test_ObjectPauser(t *testing.T) {
146145
g := NewWithT(t)
147146
r := newRolloutClient()
148147
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...)
149-
err := r.ObjectPauser(context.Background(), proxy, tt.fields.ref)
148+
err := r.ObjectPauser(t.Context(), proxy, tt.fields.ref)
150149
if tt.wantErr {
151150
g.Expect(err).To(HaveOccurred())
152151
return
153152
}
154153
g.Expect(err).ToNot(HaveOccurred())
155154
for _, obj := range tt.fields.objs {
156-
cl, err := proxy.NewClient(context.Background())
155+
cl, err := proxy.NewClient(t.Context())
157156
g.Expect(err).ToNot(HaveOccurred())
158157
key := client.ObjectKeyFromObject(obj)
159158
switch obj.(type) {
160159
case *clusterv1.MachineDeployment:
161160
md := &clusterv1.MachineDeployment{}
162-
err = cl.Get(context.TODO(), key, md)
161+
err = cl.Get(t.Context(), key, md)
163162
g.Expect(err).ToNot(HaveOccurred())
164163
g.Expect(md.Spec.Paused).To(Equal(tt.wantPaused))
165164
case *controlplanev1.KubeadmControlPlane:
166165
kcp := &controlplanev1.KubeadmControlPlane{}
167-
err = cl.Get(context.TODO(), key, kcp)
166+
err = cl.Get(t.Context(), key, kcp)
168167
g.Expect(err).ToNot(HaveOccurred())
169168
g.Expect(annotations.HasPaused(kcp.GetObjectMeta())).To(Equal(tt.wantPaused))
170169
}

cmd/clusterctl/client/alpha/rollout_restarter_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package alpha
1818

1919
import (
20-
"context"
2120
"testing"
2221
"time"
2322

@@ -204,20 +203,20 @@ func Test_ObjectRestarter(t *testing.T) {
204203
g := NewWithT(t)
205204
r := newRolloutClient()
206205
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...)
207-
err := r.ObjectRestarter(context.Background(), proxy, tt.fields.ref)
206+
err := r.ObjectRestarter(t.Context(), proxy, tt.fields.ref)
208207
if tt.wantErr {
209208
g.Expect(err).To(HaveOccurred())
210209
return
211210
}
212211
g.Expect(err).ToNot(HaveOccurred())
213212
for _, obj := range tt.fields.objs {
214-
cl, err := proxy.NewClient(context.Background())
213+
cl, err := proxy.NewClient(t.Context())
215214
g.Expect(err).ToNot(HaveOccurred())
216215
key := client.ObjectKeyFromObject(obj)
217216
switch obj.(type) {
218217
case *clusterv1.MachineDeployment:
219218
md := &clusterv1.MachineDeployment{}
220-
err = cl.Get(context.TODO(), key, md)
219+
err = cl.Get(t.Context(), key, md)
221220
g.Expect(err).ToNot(HaveOccurred())
222221
if tt.wantRollout {
223222
g.Expect(md.Spec.RolloutAfter).NotTo(BeNil())
@@ -226,7 +225,7 @@ func Test_ObjectRestarter(t *testing.T) {
226225
}
227226
case *controlplanev1.KubeadmControlPlane:
228227
kcp := &controlplanev1.KubeadmControlPlane{}
229-
err = cl.Get(context.TODO(), key, kcp)
228+
err = cl.Get(t.Context(), key, kcp)
230229
g.Expect(err).ToNot(HaveOccurred())
231230
if tt.wantRollout {
232231
g.Expect(kcp.Spec.RolloutAfter).NotTo(BeNil())

cmd/clusterctl/client/alpha/rollout_resumer_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package alpha
1818

1919
import (
20-
"context"
2120
"testing"
2221

2322
. "github.com/onsi/gomega"
@@ -149,25 +148,25 @@ func Test_ObjectResumer(t *testing.T) {
149148
g := NewWithT(t)
150149
r := newRolloutClient()
151150
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...)
152-
err := r.ObjectResumer(context.Background(), proxy, tt.fields.ref)
151+
err := r.ObjectResumer(t.Context(), proxy, tt.fields.ref)
153152
if tt.wantErr {
154153
g.Expect(err).To(HaveOccurred())
155154
return
156155
}
157156
g.Expect(err).ToNot(HaveOccurred())
158157
for _, obj := range tt.fields.objs {
159-
cl, err := proxy.NewClient(context.Background())
158+
cl, err := proxy.NewClient(t.Context())
160159
g.Expect(err).ToNot(HaveOccurred())
161160
key := client.ObjectKeyFromObject(obj)
162161
switch obj.(type) {
163162
case *clusterv1.MachineDeployment:
164163
md := &clusterv1.MachineDeployment{}
165-
err = cl.Get(context.TODO(), key, md)
164+
err = cl.Get(t.Context(), key, md)
166165
g.Expect(err).ToNot(HaveOccurred())
167166
g.Expect(md.Spec.Paused).To(Equal(tt.wantPaused))
168167
case *controlplanev1.KubeadmControlPlane:
169168
kcp := &controlplanev1.KubeadmControlPlane{}
170-
err = cl.Get(context.TODO(), key, kcp)
169+
err = cl.Get(t.Context(), key, kcp)
171170
g.Expect(err).ToNot(HaveOccurred())
172171
g.Expect(annotations.HasPaused(kcp.GetObjectMeta())).To(Equal(tt.wantPaused))
173172
}
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
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: &currentVersion,
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

Comments
 (0)