-
Notifications
You must be signed in to change notification settings - Fork 1.4k
🌱 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
sivchari
wants to merge
1
commit into
kubernetes-sigs:main
Choose a base branch
from
sivchari:enable-usetesting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+991
−325
Open
🌱 Enable usetesting linter #12219
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ¤tVersion, | ||
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
|
||
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)) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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