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

🌱 Add unit tests for MachinePools in topology/scope package #10052

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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
66 changes: 65 additions & 1 deletion internal/controllers/topology/cluster/scope/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import (
"testing"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
)

func TestUpgrading(t *testing.T) {
func TestMDUpgrading(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
Expand Down Expand Up @@ -69,3 +71,65 @@ 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())
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())

ctx := context.Background()

t.Run("should return the names of the upgrading MachinePools", func(*testing.T) {
stableMP := builder.MachinePool("ns", "stableMP").
WithClusterName("cluster1").
WithVersion("v1.2.3").
WithStatus(expv1.MachinePoolStatus{
NodeRefs: []corev1.ObjectReference{
{
Name: "stableMP-node1",
},
},
}).
Build()
stableMPNode := builder.Node("stableMP-node1").
WithStatus(corev1.NodeStatus{
NodeInfo: corev1.NodeSystemInfo{
KubeletVersion: "v1.2.3",
},
}).
Build()

upgradingMP := builder.MachinePool("ns", "upgradingMP").
WithClusterName("cluster2").
WithVersion("v1.2.3").
WithStatus(expv1.MachinePoolStatus{
NodeRefs: []corev1.ObjectReference{
{
Name: "upgradingMP-node1",
},
},
}).
Build()
upgradingMPNode := builder.Node("upgradingMP-node1").
WithStatus(corev1.NodeStatus{
NodeInfo: corev1.NodeSystemInfo{
KubeletVersion: "v1.2.2",
},
}).
Build()

objs := []client.Object{stableMP, stableMPNode, upgradingMP, upgradingMPNode}
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))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestNewUpgradeTracker(t *testing.T) {
},
{
name: "should set the value of 1 if given concurrency is less than 1",
options: []UpgradeTrackerOption{MaxMDUpgradeConcurrency(0)},
options: []UpgradeTrackerOption{MaxMDUpgradeConcurrency(0), MaxMPUpgradeConcurrency(0)},
want: 1,
},
{
name: "should set the value to the given concurrency if the value is greater than 0",
options: []UpgradeTrackerOption{MaxMDUpgradeConcurrency(2)},
options: []UpgradeTrackerOption{MaxMDUpgradeConcurrency(2), MaxMPUpgradeConcurrency(2)},
want: 2,
},
}
Expand All @@ -50,6 +50,7 @@ func TestNewUpgradeTracker(t *testing.T) {
g := NewWithT(t)
got := NewUpgradeTracker(tt.options...)
g.Expect(got.MachineDeployments.maxUpgradeConcurrency).To(Equal(tt.want))
g.Expect(got.MachinePools.maxUpgradeConcurrency).To(Equal(tt.want))
}
})
}
30 changes: 30 additions & 0 deletions internal/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,36 @@ func (c *TestControlPlaneBuilder) Build() *unstructured.Unstructured {
return c.obj
}

// NodeBuilder holds the variables required to build a Node.
type NodeBuilder struct {
name string
status corev1.NodeStatus
}

// Node returns a NodeBuilder.
func Node(name string) *NodeBuilder {
return &NodeBuilder{
name: name,
}
}

// WithStatus adds Status to the NodeBuilder.
func (n *NodeBuilder) WithStatus(status corev1.NodeStatus) *NodeBuilder {
n.status = status
return n
}

// Build produces a new Node from the information passed to the NodeBuilder.
func (n *NodeBuilder) Build() *corev1.Node {
obj := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: n.name,
},
Status: n.status,
}
return obj
}

// MachinePoolBuilder holds the variables and objects needed to build a generic MachinePool.
type MachinePoolBuilder struct {
namespace string
Expand Down
16 changes: 16 additions & 0 deletions internal/test/builder/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading