Skip to content

🌱 Enable usetesting linter #12219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ linters:
- usestdlibvars # using variables/constants from the standard library
- usetesting # report function to be replace by testing
- whitespace # unnecessary newlines
disable:
# TODO: It will be dropped when the Go version migration is done.
- usetesting
settings:
ginkgolinter:
forbid-focus-container: true
Expand Down
9 changes: 4 additions & 5 deletions cmd/clusterctl/client/alpha/rollout_pauser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package alpha

import (
"context"
"testing"
Copy link
Member

@sbueringer sbueringer May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know how this will work in combination with ctrl.SetupSignalHandler() that we use in many places?

Which context will be cancelled first? Will the shutdown still be somewhat clean?

Copy link
Member

@sbueringer sbueringer May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+ should we use either t.Context() or ctrl.SetupSignalHandler() everywhere? Or continue to have that mix that we currently have? Or do we know in which cases we would want to use which one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.Context() is canceled before t.Cleanup, then ctrl.SetupSignalHandler doesn't cancel until the signal is received. So it might be good to use properly. For example, how about is it ?

  • ctrl.SetupSignalHandler should be only used when it's suite test.
  • t.Context() should be only used other than suite test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry will get back to this eventually. Just a lot of other things going on

Copy link
Member

@sbueringer sbueringer Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(low priority at the moment) I wonder if we could/should get rid of ctrl.SetupSignalHandler() entirely.

I wonder what the difference is between using t.Context and ctrl.SetupSignalHandler in suite_test.go.

Is one more graceful then the other? Maybe let's test this manually?

Context:
ctrl.SetupSignalHandler will cancel the context on the first signal and os.Exit(1) on the second. I don't know how tests with t.Context behave when the test binary receives signals


. "github.com/onsi/gomega"
Expand Down Expand Up @@ -146,25 +145,25 @@ func Test_ObjectPauser(t *testing.T) {
g := NewWithT(t)
r := newRolloutClient()
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...)
err := r.ObjectPauser(context.Background(), proxy, tt.fields.ref)
err := r.ObjectPauser(t.Context(), proxy, tt.fields.ref)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())
for _, obj := range tt.fields.objs {
cl, err := proxy.NewClient(context.Background())
cl, err := proxy.NewClient(t.Context())
g.Expect(err).ToNot(HaveOccurred())
key := client.ObjectKeyFromObject(obj)
switch obj.(type) {
case *clusterv1.MachineDeployment:
md := &clusterv1.MachineDeployment{}
err = cl.Get(context.TODO(), key, md)
err = cl.Get(t.Context(), key, md)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(md.Spec.Paused).To(Equal(tt.wantPaused))
case *controlplanev1.KubeadmControlPlane:
kcp := &controlplanev1.KubeadmControlPlane{}
err = cl.Get(context.TODO(), key, kcp)
err = cl.Get(t.Context(), key, kcp)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(annotations.HasPaused(kcp.GetObjectMeta())).To(Equal(tt.wantPaused))
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/clusterctl/client/alpha/rollout_restarter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package alpha

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -204,20 +203,20 @@ func Test_ObjectRestarter(t *testing.T) {
g := NewWithT(t)
r := newRolloutClient()
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...)
err := r.ObjectRestarter(context.Background(), proxy, tt.fields.ref)
err := r.ObjectRestarter(t.Context(), proxy, tt.fields.ref)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())
for _, obj := range tt.fields.objs {
cl, err := proxy.NewClient(context.Background())
cl, err := proxy.NewClient(t.Context())
g.Expect(err).ToNot(HaveOccurred())
key := client.ObjectKeyFromObject(obj)
switch obj.(type) {
case *clusterv1.MachineDeployment:
md := &clusterv1.MachineDeployment{}
err = cl.Get(context.TODO(), key, md)
err = cl.Get(t.Context(), key, md)
g.Expect(err).ToNot(HaveOccurred())
if tt.wantRollout {
g.Expect(md.Spec.RolloutAfter).NotTo(BeNil())
Expand All @@ -226,7 +225,7 @@ func Test_ObjectRestarter(t *testing.T) {
}
case *controlplanev1.KubeadmControlPlane:
kcp := &controlplanev1.KubeadmControlPlane{}
err = cl.Get(context.TODO(), key, kcp)
err = cl.Get(t.Context(), key, kcp)
g.Expect(err).ToNot(HaveOccurred())
if tt.wantRollout {
g.Expect(kcp.Spec.RolloutAfter).NotTo(BeNil())
Expand Down
9 changes: 4 additions & 5 deletions cmd/clusterctl/client/alpha/rollout_resumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package alpha

import (
"context"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -149,25 +148,25 @@ func Test_ObjectResumer(t *testing.T) {
g := NewWithT(t)
r := newRolloutClient()
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...)
err := r.ObjectResumer(context.Background(), proxy, tt.fields.ref)
err := r.ObjectResumer(t.Context(), proxy, tt.fields.ref)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())
for _, obj := range tt.fields.objs {
cl, err := proxy.NewClient(context.Background())
cl, err := proxy.NewClient(t.Context())
g.Expect(err).ToNot(HaveOccurred())
key := client.ObjectKeyFromObject(obj)
switch obj.(type) {
case *clusterv1.MachineDeployment:
md := &clusterv1.MachineDeployment{}
err = cl.Get(context.TODO(), key, md)
err = cl.Get(t.Context(), key, md)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(md.Spec.Paused).To(Equal(tt.wantPaused))
case *controlplanev1.KubeadmControlPlane:
kcp := &controlplanev1.KubeadmControlPlane{}
err = cl.Get(context.TODO(), key, kcp)
err = cl.Get(t.Context(), key, kcp)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(annotations.HasPaused(kcp.GetObjectMeta())).To(Equal(tt.wantPaused))
}
Expand Down
260 changes: 260 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_rollbacker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package alpha

import (
"testing"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
)

func Test_ObjectRollbacker(t *testing.T) {
labels := map[string]string{
clusterv1.ClusterNameLabel: "test",
clusterv1.MachineDeploymentNameLabel: "test-md-0",
}
currentVersion := "v1.19.3"
rollbackVersion := "v1.19.1"
deployment := &clusterv1.MachineDeployment{
TypeMeta: metav1.TypeMeta{
Kind: "MachineDeployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-md-0",
Namespace: "default",
Labels: map[string]string{
clusterv1.ClusterNameLabel: "test",
},
Annotations: map[string]string{
clusterv1.RevisionAnnotation: "2",
},
},
Spec: clusterv1.MachineDeploymentSpec{
ClusterName: "test",
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
clusterv1.ClusterNameLabel: "test",
},
},
Template: clusterv1.MachineTemplateSpec{
ObjectMeta: clusterv1.ObjectMeta{
Labels: labels,
},
Spec: clusterv1.MachineSpec{
ClusterName: "test",
Version: &currentVersion,
InfrastructureRef: corev1.ObjectReference{
APIVersion: clusterv1.GroupVersionInfrastructure.String(),
Kind: "InfrastructureMachineTemplate",
Name: "md-template",
},
Bootstrap: clusterv1.Bootstrap{
DataSecretName: ptr.To("data-secret-name"),
},
},
},
},
}
type fields struct {
objs []client.Object
ref corev1.ObjectReference
toRevision int64
}
tests := []struct {
name string
fields fields
wantErr bool
wantVersion string
wantInfraTemplate string
wantBootsrapSecretName string
}{
{
name: "machinedeployment should rollback to revision=1",
fields: fields{
objs: []client.Object{
deployment,
&clusterv1.MachineSet{
TypeMeta: metav1.TypeMeta{
Kind: "MachineSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: "ms-rev-2",
Namespace: "default",
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(deployment, clusterv1.GroupVersion.WithKind("MachineDeployment")),
},
Labels: map[string]string{
clusterv1.ClusterNameLabel: "test",
},
Annotations: map[string]string{
clusterv1.RevisionAnnotation: "2",
},
},
},
&clusterv1.MachineSet{
TypeMeta: metav1.TypeMeta{
Kind: "MachineSet",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "ms-rev-1",
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(deployment, clusterv1.GroupVersion.WithKind("MachineDeployment")),
},
Labels: map[string]string{
clusterv1.ClusterNameLabel: "test",
},
Annotations: map[string]string{
clusterv1.RevisionAnnotation: "999",
},
},
Spec: clusterv1.MachineSetSpec{
ClusterName: "test",
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
clusterv1.ClusterNameLabel: "test",
},
},
Template: clusterv1.MachineTemplateSpec{
ObjectMeta: clusterv1.ObjectMeta{
Labels: labels,
},
Spec: clusterv1.MachineSpec{
ClusterName: "test",
Version: &rollbackVersion,
InfrastructureRef: corev1.ObjectReference{
APIVersion: clusterv1.GroupVersionInfrastructure.String(),
Kind: "InfrastructureMachineTemplate",
Name: "md-template-rollback",
},
Bootstrap: clusterv1.Bootstrap{
DataSecretName: ptr.To("data-secret-name-rollback"),
},
},
},
},
},
},
ref: corev1.ObjectReference{
Kind: MachineDeployment,
Name: "test-md-0",
Namespace: "default",
},
toRevision: int64(999),
},
wantErr: false,
wantVersion: rollbackVersion,
wantInfraTemplate: "md-template-rollback",
wantBootsrapSecretName: "data-secret-name-rollback",
},
{
name: "machinedeployment should not rollback because there is no previous revision",
fields: fields{
objs: []client.Object{
deployment,
&clusterv1.MachineSet{
TypeMeta: metav1.TypeMeta{
Kind: "MachineSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: "ms-rev-2",
Namespace: "default",
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(deployment, clusterv1.GroupVersion.WithKind("MachineDeployment")),
},
Labels: map[string]string{
clusterv1.ClusterNameLabel: "test",
},
Annotations: map[string]string{
clusterv1.RevisionAnnotation: "2",
},
},
},
},
ref: corev1.ObjectReference{
Kind: MachineDeployment,
Name: "test-md-0",
Namespace: "default",
},
toRevision: int64(0),
},
wantErr: true,
},
{
name: "machinedeployment should not rollback because the specified version does not exist",
fields: fields{
objs: []client.Object{
deployment,
&clusterv1.MachineSet{
TypeMeta: metav1.TypeMeta{
Kind: "MachineSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: "ms-rev-2",
Namespace: "default",
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(deployment, clusterv1.GroupVersion.WithKind("MachineDeployment")),
},
Labels: map[string]string{
clusterv1.ClusterNameLabel: "test",
},
Annotations: map[string]string{
clusterv1.RevisionAnnotation: "2",
},
},
},
},
ref: corev1.ObjectReference{
Kind: MachineDeployment,
Name: "test-md-0",
Namespace: "default",
},
toRevision: int64(999),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
r := newRolloutClient()
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...)
err := r.ObjectRollbacker(t.Context(), proxy, tt.fields.ref, tt.fields.toRevision)

Check failure on line 243 in cmd/clusterctl/client/alpha/rollout_rollbacker_test.go

View workflow job for this annotation

GitHub Actions / lint

r.ObjectRollbacker undefined (type Rollout has no field or method ObjectRollbacker) (typecheck)

Check failure on line 243 in cmd/clusterctl/client/alpha/rollout_rollbacker_test.go

View workflow job for this annotation

GitHub Actions / lint (hack/tools)

r.ObjectRollbacker undefined (type Rollout has no field or method ObjectRollbacker) (typecheck)

Check failure on line 243 in cmd/clusterctl/client/alpha/rollout_rollbacker_test.go

View workflow job for this annotation

GitHub Actions / lint (test)

r.ObjectRollbacker undefined (type Rollout has no field or method ObjectRollbacker) (typecheck)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())
cl, err := proxy.NewClient(t.Context())
g.Expect(err).ToNot(HaveOccurred())
key := client.ObjectKeyFromObject(deployment)
md := &clusterv1.MachineDeployment{}
err = cl.Get(t.Context(), key, md)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(*md.Spec.Template.Spec.Version).To(Equal(tt.wantVersion))
g.Expect(md.Spec.Template.Spec.InfrastructureRef.Name).To(Equal(tt.wantInfraTemplate))
g.Expect(*md.Spec.Template.Spec.Bootstrap.DataSecretName).To(Equal(tt.wantBootsrapSecretName))
})
}
}
Loading
Loading