-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
🌱 Add unit test coverage for machineDeployments. reconcileNewMachineSet #4495
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:master
from
enxebre:unit-test-md-rolling
Apr 21, 2021
Merged
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 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 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,221 @@ | ||
/* | ||
Copyright 2021 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 controllers | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"github.com/pkg/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/record" | ||
"k8s.io/utils/pointer" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" | ||
"sigs.k8s.io/cluster-api/controllers/mdutil" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
func TestReconcileNewMachineSet(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
machineDeployment *clusterv1.MachineDeployment | ||
newMachineSet *clusterv1.MachineSet | ||
oldMachineSets []*clusterv1.MachineSet | ||
expectedNewMachineSetReplicas int | ||
error error | ||
}{ | ||
{ | ||
name: "It fails when machineDeployment has no replicas", | ||
machineDeployment: &clusterv1.MachineDeployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
}, | ||
newMachineSet: &clusterv1.MachineSet{ | ||
Spec: clusterv1.MachineSetSpec{ | ||
Replicas: pointer.Int32Ptr(2), | ||
}, | ||
}, | ||
error: errors.Errorf("spec replicas for MachineDeployment foo/bar is nil, this is unexpected"), | ||
}, | ||
{ | ||
name: "It fails when new machineSet has no replicas", | ||
machineDeployment: &clusterv1.MachineDeployment{ | ||
Spec: clusterv1.MachineDeploymentSpec{ | ||
Replicas: pointer.Int32Ptr(2), | ||
}, | ||
}, | ||
newMachineSet: &clusterv1.MachineSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
}, | ||
error: errors.Errorf("spec replicas for MachineSet foo/bar is nil, this is unexpected"), | ||
}, | ||
{ | ||
name: "RollingUpdate strategy: Scale up: 0 -> 2", | ||
machineDeployment: &clusterv1.MachineDeployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
Spec: clusterv1.MachineDeploymentSpec{ | ||
Strategy: &clusterv1.MachineDeploymentStrategy{ | ||
Type: clusterv1.RollingUpdateMachineDeploymentStrategyType, | ||
RollingUpdate: &clusterv1.MachineRollingUpdateDeployment{ | ||
MaxUnavailable: intOrStrPtr(0), | ||
MaxSurge: intOrStrPtr(2), | ||
}, | ||
}, | ||
Replicas: pointer.Int32Ptr(2), | ||
}, | ||
}, | ||
newMachineSet: &clusterv1.MachineSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
Spec: clusterv1.MachineSetSpec{ | ||
Replicas: pointer.Int32Ptr(0), | ||
}, | ||
}, | ||
expectedNewMachineSetReplicas: 2, | ||
}, | ||
{ | ||
name: "RollingUpdate strategy: Scale down: 2 -> 0", | ||
machineDeployment: &clusterv1.MachineDeployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
Spec: clusterv1.MachineDeploymentSpec{ | ||
Strategy: &clusterv1.MachineDeploymentStrategy{ | ||
Type: clusterv1.RollingUpdateMachineDeploymentStrategyType, | ||
RollingUpdate: &clusterv1.MachineRollingUpdateDeployment{ | ||
MaxUnavailable: intOrStrPtr(0), | ||
MaxSurge: intOrStrPtr(2), | ||
}, | ||
}, | ||
Replicas: pointer.Int32Ptr(0), | ||
}, | ||
}, | ||
newMachineSet: &clusterv1.MachineSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
Spec: clusterv1.MachineSetSpec{ | ||
Replicas: pointer.Int32Ptr(2), | ||
}, | ||
}, | ||
expectedNewMachineSetReplicas: 0, | ||
}, | ||
{ | ||
name: "RollingUpdate strategy: Scale up does not go above maxSurge (3+2)", | ||
machineDeployment: &clusterv1.MachineDeployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
Spec: clusterv1.MachineDeploymentSpec{ | ||
Strategy: &clusterv1.MachineDeploymentStrategy{ | ||
Type: clusterv1.RollingUpdateMachineDeploymentStrategyType, | ||
RollingUpdate: &clusterv1.MachineRollingUpdateDeployment{ | ||
MaxUnavailable: intOrStrPtr(0), | ||
MaxSurge: intOrStrPtr(2), | ||
}, | ||
}, | ||
Replicas: pointer.Int32Ptr(3), | ||
}, | ||
}, | ||
newMachineSet: &clusterv1.MachineSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
Spec: clusterv1.MachineSetSpec{ | ||
Replicas: pointer.Int32Ptr(1), | ||
}, | ||
}, | ||
expectedNewMachineSetReplicas: 2, | ||
oldMachineSets: []*clusterv1.MachineSet{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "3replicas", | ||
}, | ||
Spec: clusterv1.MachineSetSpec{ | ||
Replicas: pointer.Int32Ptr(3), | ||
}, | ||
Status: clusterv1.MachineSetStatus{ | ||
Replicas: 3, | ||
}, | ||
}, | ||
}, | ||
error: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
g.Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed()) | ||
|
||
resources := []client.Object{ | ||
tc.machineDeployment, | ||
} | ||
|
||
allMachineSets := append(tc.oldMachineSets, tc.newMachineSet) | ||
for key := range allMachineSets { | ||
resources = append(resources, allMachineSets[key]) | ||
} | ||
|
||
r := &MachineDeploymentReconciler{ | ||
Client: fake.NewClientBuilder().WithObjects(resources...).Build(), | ||
recorder: record.NewFakeRecorder(32), | ||
} | ||
|
||
err := r.reconcileNewMachineSet(ctx, allMachineSets, tc.newMachineSet, tc.machineDeployment) | ||
if tc.error != nil { | ||
g.Expect(err.Error()).To(BeEquivalentTo(tc.error.Error())) | ||
return | ||
} | ||
|
||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
freshNewMachineSet := &clusterv1.MachineSet{} | ||
err = r.Client.Get(ctx, client.ObjectKeyFromObject(tc.newMachineSet), freshNewMachineSet) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
g.Expect(*freshNewMachineSet.Spec.Replicas).To(BeEquivalentTo(tc.expectedNewMachineSetReplicas)) | ||
|
||
desiredReplicasAnnotation, ok := freshNewMachineSet.GetAnnotations()[clusterv1.DesiredReplicasAnnotation] | ||
g.Expect(ok).To(BeTrue()) | ||
g.Expect(strconv.Atoi(desiredReplicasAnnotation)).To(BeEquivalentTo(*tc.machineDeployment.Spec.Replicas)) | ||
|
||
maxReplicasAnnotation, ok := freshNewMachineSet.GetAnnotations()[clusterv1.MaxReplicasAnnotation] | ||
g.Expect(ok).To(BeTrue()) | ||
g.Expect(strconv.Atoi(maxReplicasAnnotation)).To(BeEquivalentTo(*tc.machineDeployment.Spec.Replicas + mdutil.MaxSurge(*tc.machineDeployment))) | ||
}) | ||
} | ||
} |
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.
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.
I'm pretty unfamiliar with this code, so I might be missing something entirely.
To me it looks like allMS is always
append(oldMSs, newMS)
cluster-api/controllers/machinedeployment_rolling.go
Lines 45 to 48 in e949440
I think we should also have test cases for this. As far as I can see the current tests all have disjunct newMS/allMS, is this intended?
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.
This is actually already being ensured for every testCase, see https://github.com/kubernetes-sigs/cluster-api/pull/4495/files#diff-a70da8885988c24ea4a35f7c1291f24a99f8c5ffdedf2f27864b90575afc2312R188
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.
I just renamed the field allMachineSets -> OldMachineSets to make this more obvious.
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.
Oh, shouldn't have missed that :/
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.
you raised a good point which resulted in better naming, thanks!