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 { for _, test := range reconcileTests {
t.Run(test.testName, func(t *testing.T) {
fakeClient := fake.NewSimpleClientset() fakeClient := fake.NewSimpleClientset()
if test.endpoints != nil { if test.endpoints != nil {
fakeClient = fake.NewSimpleClientset(test.endpoints) fakeClient = fake.NewSimpleClientset(test.endpoints)
@ -401,7 +402,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter) reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true) err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
if err != nil { if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err) t.Errorf("unexpected error: %v", err)
} }
updates := []core.UpdateAction{} updates := []core.UpdateAction{}
@ -413,13 +414,13 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
} }
if test.expectUpdate != nil { if test.expectUpdate != nil {
if len(updates) != 1 { 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) { } 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 { 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{} creates := []core.CreateAction{}
@ -431,15 +432,15 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
} }
if test.expectCreate != nil { if test.expectCreate != nil {
if len(creates) != 1 { 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) { } 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 { 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 { nonReconcileTests := []struct {
@ -512,6 +513,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}, },
} }
for _, test := range nonReconcileTests { for _, test := range nonReconcileTests {
t.Run(test.testName, func(t *testing.T) {
fakeClient := fake.NewSimpleClientset() fakeClient := fake.NewSimpleClientset()
if test.endpoints != nil { if test.endpoints != nil {
fakeClient = fake.NewSimpleClientset(test.endpoints) fakeClient = fake.NewSimpleClientset(test.endpoints)
@ -520,7 +522,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter) reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false) err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
if err != nil { if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err) t.Errorf("unexpected error: %v", err)
} }
updates := []core.UpdateAction{} updates := []core.UpdateAction{}
@ -532,13 +534,13 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
} }
if test.expectUpdate != nil { if test.expectUpdate != nil {
if len(updates) != 1 { 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) { } 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 { 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{} creates := []core.CreateAction{}
@ -550,15 +552,15 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
} }
if test.expectCreate != nil { if test.expectCreate != nil {
if len(creates) != 1 { 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) { } 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 { 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 { for _, test := range reconcileTests {
t.Run(test.testName, func(t *testing.T) {
fakeLeases := newFakeLeases() fakeLeases := newFakeLeases()
fakeLeases.SetKeys(test.endpointKeys) fakeLeases.SetKeys(test.endpointKeys)
clientset := fake.NewSimpleClientset() clientset := fake.NewSimpleClientset()
if test.endpoints != nil { if test.endpoints != nil {
for _, ep := range test.endpoints.Items { for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil { 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 continue
} }
} }
@ -464,20 +465,21 @@ func TestLeaseEndpointReconciler(t *testing.T) {
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases) r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true) err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
if err != nil { 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{}) actualEndpoints, err := clientset.CoreV1().Endpoints(corev1.NamespaceDefault).Get(context.TODO(), test.serviceName, metav1.GetOptions{})
if err != nil { if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err) t.Errorf("unexpected error: %v", err)
} }
if test.expectUpdate != nil { if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) { 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 { 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 { nonReconcileTests := []struct {
@ -556,7 +558,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
if test.endpoints != nil { if test.endpoints != nil {
for _, ep := range test.endpoints.Items { for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil { 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 continue
} }
} }
@ -565,19 +567,19 @@ func TestLeaseEndpointReconciler(t *testing.T) {
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases) r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false) err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
if err != nil { 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{}) actualEndpoints, err := clientset.CoreV1().Endpoints(corev1.NamespaceDefault).Get(context.TODO(), test.serviceName, metav1.GetOptions{})
if err != nil { if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err) t.Errorf("unexpected error: %v", err)
} }
if test.expectUpdate != nil { if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) { 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 { 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() clientset := fake.NewSimpleClientset()
for _, ep := range test.endpoints.Items { for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil { 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 continue
} }
} }
@ -685,20 +687,20 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases) r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.RemoveEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts) err := r.RemoveEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts)
if err != nil { 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{}) actualEndpoints, err := clientset.CoreV1().Endpoints(corev1.NamespaceDefault).Get(context.TODO(), test.serviceName, metav1.GetOptions{})
if err != nil { if err != nil {
t.Errorf("case %q: unexpected error: %v", test.testName, err) t.Errorf("unexpected error: %v", err)
} }
if test.expectUpdate != nil { if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) { 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() { for _, key := range fakeLeases.GetUpdatedKeys() {
if key == test.ip { 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)
} }
} }
}) })