Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
cahillsf committed Jul 12, 2024
1 parent 6de7466 commit ffc797d
Showing 1 changed file with 132 additions and 88 deletions.
220 changes: 132 additions & 88 deletions exp/internal/controllers/machinepool_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2107,91 +2107,135 @@ func getMachinePool(replicas int, mpName, clusterName, nsName string) expv1.Mach
}
}

// func TestMachinePoolReconciler_getNodeRefMap(t *testing.T) {
// r := &MachinePoolReconciler{
// Client: fake.NewClientBuilder().Build(),
// recorder: record.NewFakeRecorder(32),
// }

// nodeList := []client.Object{
// &corev1.Node{
// ObjectMeta: metav1.ObjectMeta{
// Name: "node-1",
// },
// Spec: corev1.NodeSpec{
// ProviderID: "aws://us-east-1/id-node-1",
// },
// Status: corev1.NodeStatus{
// Conditions: []corev1.NodeCondition{
// {
// Type: corev1.NodeReady,
// Status: corev1.ConditionTrue,
// },
// },
// },
// },
// &corev1.Node{
// ObjectMeta: metav1.ObjectMeta{
// Name: "gce-node-2",
// },
// Spec: corev1.NodeSpec{
// ProviderID: "gce://us-central1/gce-id-node-2",
// },
// Status: corev1.NodeStatus{
// Conditions: []corev1.NodeCondition{
// {
// Type: corev1.NodeReady,
// Status: corev1.ConditionTrue,
// },
// },
// },
// },
// &corev1.Node{
// ObjectMeta: metav1.ObjectMeta{
// Name: "azure-node-4",
// },
// Spec: corev1.NodeSpec{
// ProviderID: "azure://westus2/id-node-4",
// },
// Status: corev1.NodeStatus{
// Conditions: []corev1.NodeCondition{
// {
// Type: corev1.NodeReady,
// Status: corev1.ConditionTrue,
// },
// },
// },
// },
// }

// client := fake.NewClientBuilder().WithObjects(nodeList...).Build()
// testCases := []struct {
// name string
// expected map[string]*corev1.Node
// err error
// }{
// {
// name: "valid provider id, valid aws node",
// expected: &getNodeReferencesResult{
// references: []corev1.ObjectReference{
// {Name: "node-1"},
// },
// available: 1,
// ready: 1,
// },
// },
// }
// for _, tt := range testCases {
// t.Run(tt.name, func(t *testing.T) {
// g := NewWithT(t)

// result, err := r.getNodeRefMap(ctx, client)
// if tt.err == nil {
// g.Expect(err).ToNot(HaveOccurred())
// } else {
// g.Expect(err).To(HaveOccurred())
// g.Expect(err).To(Equal(tt.err), "Expected error %v, got %v", tt.err, err)
// }
// })
// }
// }
func TestMachinePoolReconciler_getNodeRefMap(t *testing.T) {
r := &MachinePoolReconciler{
Client: fake.NewClientBuilder().Build(),
recorder: record.NewFakeRecorder(32),
}

nodeList := []client.Object{
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Spec: corev1.NodeSpec{
ProviderID: "aws://us-east-1/id-node-1",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
},
},
},
},
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "gce-node-2",
},
Spec: corev1.NodeSpec{
ProviderID: "gce://us-central1/gce-id-node-2",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
},
},
},
},
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "azure-node-4",
},
Spec: corev1.NodeSpec{
ProviderID: "azure://westus2/id-node-4",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
},
},
},
},
}

client := fake.NewClientBuilder().WithObjects(nodeList...).Build()
testCases := []struct {
name string
expected map[string]*corev1.Node
err error
}{
{
name: "valid provider id, valid aws node",
expected: map[string]*corev1.Node{
"aws://us-east-1/id-node-1": {
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Spec: corev1.NodeSpec{
ProviderID: "aws://us-east-1/id-node-1",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
},
},
},
},
"gce://us-central1/gce-id-node-2": {
ObjectMeta: metav1.ObjectMeta{
Name: "gce-node-2",
},
Spec: corev1.NodeSpec{
ProviderID: "gce://us-central1/gce-id-node-2",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
},
},
},
},
"azure://westus2/id-node-4": {
ObjectMeta: metav1.ObjectMeta{
Name: "azure-node-4",
},
Spec: corev1.NodeSpec{
ProviderID: "azure://westus2/id-node-4",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
},
},
},
},
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

result, err := r.getNodeRefMap(ctx, client)
if tt.err == nil {
g.Expect(err).ToNot(HaveOccurred())
} else {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(Equal(tt.err), "Expected error %v, got %v", tt.err, err)
}
g.Expect(result).To(HaveLen(len(tt.expected)), "Expected NodeRef count to be %v, got %v", len(result), len(tt.expected))
})
}
}

0 comments on commit ffc797d

Please sign in to comment.