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 test for state_test.go #9789

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
143 changes: 141 additions & 2 deletions internal/controllers/topology/cluster/scope/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
"context"
"testing"

. "github.com/onsi/gomega"

Check failure on line 23 in internal/controllers/topology/cluster/scope/state_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)

Check failure on line 23 in internal/controllers/topology/cluster/scope/state_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

corev1 "k8s.io/api/core/v1"

Check failure on line 28 in internal/controllers/topology/cluster/scope/state_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)

Check failure on line 28 in internal/controllers/topology/cluster/scope/state_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)
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 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 +117,105 @@
}
}

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
nodes []*corev1.Node
want bool
wantErr bool
}{
{
name: "should return false if all the nodes have the same version as the MachinePool",
mp: builder.MachinePool("ns", "mp1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
nodes: []*corev1.Node{
builder.Node("node1").
WithStatus(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.3"}}).
Build(),
builder.Node("node2").
WithStatus(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.3"}}).
Build(),
},
want: false,
wantErr: false,
},
{
name: "should return true if at least one of the nodes of MachinePool has a different version",
mp: builder.MachinePool("ns", "mp1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
nodes: []*corev1.Node{
builder.Node("node1").
WithStatus(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.3"}}).
Build(),
builder.Node("node2").
WithStatus(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.4"}}).
Build(),
},
want: true,
wantErr: false,
},
{
name: "should return false if there is no version for MachinePool",
mp: builder.MachinePool("ns", "mp1").
WithClusterName("cluster1").
Build(),
nodes: []*corev1.Node{
builder.Node("node1").
WithStatus(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.3"}}).
Build(),
builder.Node("node2").
WithStatus(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.4"}}).
Build(),
},
want: false,
wantErr: false,
},
{
name: "should return false if the MachinePool has no nodes (creation phase)",
mp: builder.MachinePool("ns", "mp1").
WithClusterName("cluster1").
WithVersion("v1.2.3").
Build(),
nodes: []*corev1.Node{},

Check failure on line 188 in internal/controllers/topology/cluster/scope/state_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)

Check failure on line 188 in internal/controllers/topology/cluster/scope/state_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)
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.nodes {
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 +255,42 @@
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()
stableMPNnode := 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").
Build()
upgradingMPNode := builder.Node("upgradingMP-node1").
WithStatus(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{KubeletVersion: "v1.2.3"}}).
Build()

objs := []client.Object{stableMP, stableMPNnode, 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))
})
}
33 changes: 33 additions & 0 deletions internal/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,39 @@
return obj
}

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

Check failure on line 1579 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)

Check failure on line 1579 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)
status corev1.NodeStatus
}

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

Check failure on line 1586 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)

Check failure on line 1586 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)
}
}

// WithStatus adds Status to the NodeBuilder

Check failure on line 1590 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 1590 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 1590 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 1590 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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

Check failure on line 1596 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 1596 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 1596 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 1596 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
func (n *NodeBuilder) Build() *corev1.Node {
obj := &corev1.Node{
TypeMeta: metav1.TypeMeta{
Kind: "Node",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: n.name,

Check failure on line 1604 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)

Check failure on line 1604 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(sigs.k8s.io/cluster-api) --custom-order (gci)
},
}
return obj
}

// MachineDeploymentBuilder holds the variables and objects needed to build a generic MachineDeployment.
type MachineDeploymentBuilder struct {
namespace string
Expand Down
Loading