Use t.Run() consistently in reconciler unit tests

This commit is contained in:
Dan Winship 2022-04-27 10:05:09 -04:00
parent ad06854e5e
commit 007ca4f69d
2 changed files with 122 additions and 118 deletions

View File

@ -393,6 +393,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
},
}
for _, test := range reconcileTests {
t.Run(test.testName, func(t *testing.T) {
fakeClient := fake.NewSimpleClientset()
if test.endpoints != nil {
fakeClient = fake.NewSimpleClientset(test.endpoints)
@ -401,7 +402,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
}
updates := []core.UpdateAction{}
@ -413,13 +414,13 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}
if test.expectUpdate != nil {
if len(updates) != 1 {
t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
t.Errorf("unexpected updates: %v", updates)
} else if e, a := test.expectUpdate, updates[0].GetObject(); !reflect.DeepEqual(e, a) {
t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
if test.expectUpdate == nil && len(updates) > 0 {
t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
t.Errorf("no update expected, yet saw: %v", updates)
}
creates := []core.CreateAction{}
@ -431,15 +432,15 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}
if test.expectCreate != nil {
if len(creates) != 1 {
t.Errorf("case %q: unexpected creates: %v", test.testName, creates)
t.Errorf("unexpected creates: %v", creates)
} else if e, a := test.expectCreate, creates[0].GetObject(); !reflect.DeepEqual(e, a) {
t.Errorf("case %q: expected create:\n%#v\ngot:\n%#v\n", test.testName, e, a)
t.Errorf("expected create:\n%#v\ngot:\n%#v\n", e, a)
}
}
if test.expectCreate == nil && len(creates) > 0 {
t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
t.Errorf("no create expected, yet saw: %v", creates)
}
})
}
nonReconcileTests := []struct {
@ -512,6 +513,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
},
}
for _, test := range nonReconcileTests {
t.Run(test.testName, func(t *testing.T) {
fakeClient := fake.NewSimpleClientset()
if test.endpoints != nil {
fakeClient = fake.NewSimpleClientset(test.endpoints)
@ -520,7 +522,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
}
updates := []core.UpdateAction{}
@ -532,13 +534,13 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}
if test.expectUpdate != nil {
if len(updates) != 1 {
t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
t.Errorf("unexpected updates: %v", updates)
} else if e, a := test.expectUpdate, updates[0].GetObject(); !reflect.DeepEqual(e, a) {
t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
if test.expectUpdate == nil && len(updates) > 0 {
t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
t.Errorf("no update expected, yet saw: %v", updates)
}
creates := []core.CreateAction{}
@ -550,15 +552,15 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}
if test.expectCreate != nil {
if len(creates) != 1 {
t.Errorf("case %q: unexpected creates: %v", test.testName, creates)
t.Errorf("unexpected creates: %v", creates)
} else if e, a := test.expectCreate, creates[0].GetObject(); !reflect.DeepEqual(e, a) {
t.Errorf("case %q: expected create:\n%#v\ngot:\n%#v\n", test.testName, e, a)
t.Errorf("expected create:\n%#v\ngot:\n%#v\n", e, a)
}
}
if test.expectCreate == nil && len(creates) > 0 {
t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
t.Errorf("no create expected, yet saw: %v", creates)
}
})
}
}

View File

@ -448,13 +448,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
},
}
for _, test := range reconcileTests {
t.Run(test.testName, func(t *testing.T) {
fakeLeases := newFakeLeases()
fakeLeases.SetKeys(test.endpointKeys)
clientset := fake.NewSimpleClientset()
if test.endpoints != nil {
for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
continue
}
}
@ -464,20 +465,21 @@ func TestLeaseEndpointReconciler(t *testing.T) {
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
}
actualEndpoints, err := clientset.CoreV1().Endpoints(corev1.NamespaceDefault).Get(context.TODO(), test.serviceName, metav1.GetOptions{})
if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
}
if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) {
t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
if updatedKeys := fakeLeases.GetUpdatedKeys(); len(updatedKeys) != 1 || updatedKeys[0] != test.ip {
t.Errorf("case %q: expected the master's IP to be refreshed, but the following IPs were refreshed instead: %v", test.testName, updatedKeys)
t.Errorf("expected the master's IP to be refreshed, but the following IPs were refreshed instead: %v", updatedKeys)
}
})
}
nonReconcileTests := []struct {
@ -556,7 +558,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
if test.endpoints != nil {
for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
continue
}
}
@ -565,19 +567,19 @@ func TestLeaseEndpointReconciler(t *testing.T) {
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
}
actualEndpoints, err := clientset.CoreV1().Endpoints(corev1.NamespaceDefault).Get(context.TODO(), test.serviceName, metav1.GetOptions{})
if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
}
if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) {
t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
if updatedKeys := fakeLeases.GetUpdatedKeys(); len(updatedKeys) != 1 || updatedKeys[0] != test.ip {
t.Errorf("case %q: expected the master's IP to be refreshed, but the following IPs were refreshed instead: %v", test.testName, updatedKeys)
t.Errorf("expected the master's IP to be refreshed, but the following IPs were refreshed instead: %v", updatedKeys)
}
})
}
@ -677,7 +679,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
clientset := fake.NewSimpleClientset()
for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
continue
}
}
@ -685,20 +687,20 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.RemoveEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts)
if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
}
actualEndpoints, err := clientset.CoreV1().Endpoints(corev1.NamespaceDefault).Get(context.TODO(), test.serviceName, metav1.GetOptions{})
if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err)
t.Errorf("unexpected error: %v", err)
}
if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) {
t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
for _, key := range fakeLeases.GetUpdatedKeys() {
if key == test.ip {
t.Errorf("case %q: Found ip %s in leases but shouldn't be there", test.testName, key)
t.Errorf("Found ip %s in leases but shouldn't be there", key)
}
}
})