Skip to content
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

🌱 Added unit tests for state_test.go #9468

Closed
wants to merge 3 commits into from
Closed
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
130 changes: 128 additions & 2 deletions internal/controllers/topology/cluster/scope/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"sigs.k8s.io/cluster-api/internal/test/builder"
)

func TestIsUpgrading(t *testing.T) {
func TestMDIsUpgrading(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
Expand Down Expand Up @@ -115,7 +115,92 @@ func TestIsUpgrading(t *testing.T) {
}
}

func TestUpgrading(t *testing.T) {
func TestMPIsUpgrading(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(expv1.AddToScheme(scheme)).To(Succeed())
tests := []struct {
name string
mp *expv1.MachinePool
machines []*clusterv1.Machine
Copy link
Contributor

Choose a reason for hiding this comment

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

We will check the noderef's kubelet version instead of the machine's version. Thus, we'll need to add a list of *corev1.Node instead of *clusterv1.Machine. The logic for this is here: https://github.com/kubernetes-sigs/cluster-api/blob/main/internal/controllers/topology/cluster/scope/state.go#L174

want bool
wantErr bool
}{
{
name: "should return false if all the machines of MachinePool have the same version as the MachinePool",
mp: builder.MachinePool("ns", "mp1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
machines: []*clusterv1.Machine{
builder.Machine("ns", "machine1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
builder.Machine("ns", "machine2").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
},
want: false,
wantErr: false,
},
{
name: "should return true if at least one of the machines of MachinePool has a different version",
mp: builder.MachinePool("ns", "mp1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
machines: []*clusterv1.Machine{
builder.Machine("ns", "machine1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
builder.Machine("ns", "machine2").
WithClusterName("cluster1").
WithVersion("v1.2.2").
Build(),
},
want: true,
wantErr: false,
},
{
name: "should return false if the MachinePool has no machines (creation phase)",
mp: builder.MachinePool("ns", "mp1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
machines: []*clusterv1.Machine{},
want: false,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
objs := []client.Object{}
objs = append(objs, tt.mp)
for _, m := range tt.machines {
objs = append(objs, m)
}
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
mpState := &MachinePoolState{
Object: tt.mp,
}
got, err := mpState.IsUpgrading(ctx, fakeClient)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).ToNot(HaveOccurred())
g.Expect(got).To(Equal(tt.want))
}
})
}
}

func TestMDUpgrading(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
Expand Down Expand Up @@ -155,3 +240,44 @@ func TestUpgrading(t *testing.T) {
g.Expect(got).To(BeComparableTo(want))
})
}

func TestMPUpgrading(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(expv1.AddToScheme(scheme)).To(Succeed())

ctx := context.Background()

t.Run("should return the names of the upgrading MachinePools", func(t *testing.T) {
stableMP := builder.MachinePool("ns", "stableMP").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build()
stableMPMachine := builder.Machine("ns", "stableMP-machine1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build()

upgradingMP := builder.MachinePool("ns", "upgradingMP").
WithClusterName("cluster2").
WithVersion("v1.2.3").
Build()
upgradingMPMachine := builder.Machine("ns", "upgradingMP-machine1").
WithClusterName("cluster2").
WithVersion("v1.2.2").
Build()

objs := []client.Object{stableMP, stableMPMachine, upgradingMP, upgradingMPMachine}
fakeClient := fake.NewClientBuilder().WithObjects(objs...).WithScheme(scheme).Build()

mpsStateMap := MachinePoolsStateMap{
"stableMP": {Object: stableMP},
"upgradingMP": {Object: upgradingMP},
}
want := []string{"upgradingMP"}

got, err := mpsStateMap.Upgrading(ctx, fakeClient)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(got).To(BeComparableTo(want))
})
}
Loading