Skip to content

Commit 6688e74

Browse files
Add unit tests for MPs in topology/scope package
Co-authored-by: razashahid107 <[email protected]>
1 parent 28f0335 commit 6688e74

File tree

5 files changed

+803
-6
lines changed

5 files changed

+803
-6
lines changed

internal/controllers/topology/cluster/scope/state_test.go

Lines changed: 148 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ import (
2121
"testing"
2222

2323
. "github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
2425
"k8s.io/apimachinery/pkg/runtime"
2526
"sigs.k8s.io/controller-runtime/pkg/client"
2627
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2728

2829
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
30+
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
2931
"sigs.k8s.io/cluster-api/internal/test/builder"
3032
)
3133

32-
func TestIsUpgrading(t *testing.T) {
34+
func TestMDIsUpgrading(t *testing.T) {
3335
g := NewWithT(t)
3436
scheme := runtime.NewScheme()
3537
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
@@ -115,7 +117,7 @@ func TestIsUpgrading(t *testing.T) {
115117
}
116118
}
117119

118-
func TestUpgrading(t *testing.T) {
120+
func TestMDUpgrading(t *testing.T) {
119121
g := NewWithT(t)
120122
scheme := runtime.NewScheme()
121123
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
@@ -155,3 +157,147 @@ func TestUpgrading(t *testing.T) {
155157
g.Expect(got).To(BeComparableTo(want))
156158
})
157159
}
160+
161+
func TestMPIsUpgrading(t *testing.T) {
162+
g := NewWithT(t)
163+
scheme := runtime.NewScheme()
164+
g.Expect(expv1.AddToScheme(scheme)).To(Succeed())
165+
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())
166+
167+
tests := []struct {
168+
name string
169+
mp *expv1.MachinePool
170+
nodes []*corev1.Node
171+
want bool
172+
wantErr bool
173+
}{
174+
{
175+
name: "should return false if all the nodes of MachinePool have the same version as the MachinePool",
176+
mp: builder.MachinePool("ns", "mp1").
177+
WithClusterName("cluster1").
178+
WithVersion("v1.2.3").
179+
Build(),
180+
nodes: []*corev1.Node{
181+
builder.Node("node1").
182+
WithStatus(corev1.NodeStatus{
183+
NodeInfo: corev1.NodeSystemInfo{
184+
KubeletVersion: "v1.2.3",
185+
},
186+
}).
187+
Build(),
188+
},
189+
want: false,
190+
wantErr: false,
191+
},
192+
{
193+
name: "should return true if the MachinePool's node has a different kubelet version",
194+
mp: builder.MachinePool("ns", "mp1").
195+
WithClusterName("cluster1").
196+
WithVersion("v1.2.3").
197+
WithStatus(expv1.MachinePoolStatus{
198+
NodeRefs: []corev1.ObjectReference{
199+
{
200+
Name: "node1",
201+
},
202+
},
203+
}).
204+
Build(),
205+
nodes: []*corev1.Node{
206+
builder.Node("node1").
207+
WithStatus(corev1.NodeStatus{
208+
NodeInfo: corev1.NodeSystemInfo{
209+
KubeletVersion: "v1.2.2",
210+
},
211+
}).
212+
Build(),
213+
},
214+
want: true,
215+
wantErr: false,
216+
},
217+
}
218+
219+
for _, tt := range tests {
220+
t.Run(tt.name, func(t *testing.T) {
221+
g := NewWithT(t)
222+
ctx := context.Background()
223+
objs := []client.Object{}
224+
objs = append(objs, tt.mp)
225+
for _, n := range tt.nodes {
226+
objs = append(objs, n)
227+
}
228+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
229+
mpState := &MachinePoolState{
230+
Object: tt.mp,
231+
}
232+
got, err := mpState.IsUpgrading(ctx, fakeClient)
233+
if tt.wantErr {
234+
g.Expect(err).To(HaveOccurred())
235+
} else {
236+
g.Expect(err).ToNot(HaveOccurred())
237+
g.Expect(got).To(Equal(tt.want))
238+
}
239+
})
240+
}
241+
}
242+
243+
func TestMPUpgrading(t *testing.T) {
244+
g := NewWithT(t)
245+
scheme := runtime.NewScheme()
246+
g.Expect(expv1.AddToScheme(scheme)).To(Succeed())
247+
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())
248+
249+
ctx := context.Background()
250+
251+
t.Run("should return the names of the upgrading MachinePools", func(t *testing.T) {
252+
stableMP := builder.MachinePool("ns", "stableMP").
253+
WithClusterName("cluster1").
254+
WithVersion("v1.2.3").
255+
WithStatus(expv1.MachinePoolStatus{
256+
NodeRefs: []corev1.ObjectReference{
257+
{
258+
Name: "stableMP-node1",
259+
},
260+
},
261+
}).
262+
Build()
263+
stableMPNode := builder.Node("stableMP-node1").
264+
WithStatus(corev1.NodeStatus{
265+
NodeInfo: corev1.NodeSystemInfo{
266+
KubeletVersion: "v1.2.3",
267+
},
268+
}).
269+
Build()
270+
271+
upgradingMP := builder.MachinePool("ns", "upgradingMP").
272+
WithClusterName("cluster2").
273+
WithVersion("v1.2.3").
274+
WithStatus(expv1.MachinePoolStatus{
275+
NodeRefs: []corev1.ObjectReference{
276+
{
277+
Name: "upgradingMP-node1",
278+
},
279+
},
280+
}).
281+
Build()
282+
upgradingMPNode := builder.Node("upgradingMP-node1").
283+
WithStatus(corev1.NodeStatus{
284+
NodeInfo: corev1.NodeSystemInfo{
285+
KubeletVersion: "v1.2.2",
286+
},
287+
}).
288+
Build()
289+
290+
objs := []client.Object{stableMP, stableMPNode, upgradingMP, upgradingMPNode}
291+
fakeClient := fake.NewClientBuilder().WithObjects(objs...).WithScheme(scheme).Build()
292+
293+
mpsStateMap := MachinePoolsStateMap{
294+
"stableMP": {Object: stableMP},
295+
"upgradingMP": {Object: upgradingMP},
296+
}
297+
want := []string{"upgradingMP"}
298+
299+
got, err := mpsStateMap.Upgrading(ctx, fakeClient)
300+
g.Expect(err).ToNot(HaveOccurred())
301+
g.Expect(got).To(BeComparableTo(want))
302+
})
303+
}

internal/controllers/topology/cluster/scope/upgradetracker_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ func TestNewUpgradeTracker(t *testing.T) {
3636
},
3737
{
3838
name: "should set the value of 1 if given concurrency is less than 1",
39-
options: []UpgradeTrackerOption{MaxMDUpgradeConcurrency(0)},
39+
options: []UpgradeTrackerOption{MaxMDUpgradeConcurrency(0), MaxMPUpgradeConcurrency(0)},
4040
want: 1,
4141
},
4242
{
4343
name: "should set the value to the given concurrency if the value is greater than 0",
44-
options: []UpgradeTrackerOption{MaxMDUpgradeConcurrency(2)},
44+
options: []UpgradeTrackerOption{MaxMDUpgradeConcurrency(2), MaxMPUpgradeConcurrency(2)},
4545
want: 2,
4646
},
4747
}
@@ -50,6 +50,7 @@ func TestNewUpgradeTracker(t *testing.T) {
5050
g := NewWithT(t)
5151
got := NewUpgradeTracker(tt.options...)
5252
g.Expect(got.MachineDeployments.maxUpgradeConcurrency).To(Equal(tt.want))
53+
g.Expect(got.MachinePools.maxUpgradeConcurrency).To(Equal(tt.want))
5354
}
5455
})
5556
}

internal/test/builder/builders.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,36 @@ func (c *TestControlPlaneBuilder) Build() *unstructured.Unstructured {
14671467
return c.obj
14681468
}
14691469

1470+
// NodeBuilder holds the variables required to build a Node.
1471+
type NodeBuilder struct {
1472+
name string
1473+
status corev1.NodeStatus
1474+
}
1475+
1476+
// Node returns a NodeBuilder.
1477+
func Node(name string) *NodeBuilder {
1478+
return &NodeBuilder{
1479+
name: name,
1480+
}
1481+
}
1482+
1483+
// WithStatus adds Status to the NodeBuilder
1484+
func (n *NodeBuilder) WithStatus(status corev1.NodeStatus) *NodeBuilder {
1485+
n.status = status
1486+
return n
1487+
}
1488+
1489+
// Build produces a new Node from the information passed to the NodeBuilder
1490+
func (n *NodeBuilder) Build() *corev1.Node {
1491+
obj := &corev1.Node{
1492+
ObjectMeta: metav1.ObjectMeta{
1493+
Name: n.name,
1494+
},
1495+
Status: n.status,
1496+
}
1497+
return obj
1498+
}
1499+
14701500
// MachinePoolBuilder holds the variables and objects needed to build a generic MachinePool.
14711501
type MachinePoolBuilder struct {
14721502
namespace string

internal/test/builder/zz_generated.deepcopy.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)