mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Use t.Run() consistently in reconciler unit tests
This commit is contained in:
parent
ad06854e5e
commit
007ca4f69d
@ -393,53 +393,54 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range reconcileTests {
|
for _, test := range reconcileTests {
|
||||||
fakeClient := fake.NewSimpleClientset()
|
t.Run(test.testName, func(t *testing.T) {
|
||||||
if test.endpoints != nil {
|
fakeClient := fake.NewSimpleClientset()
|
||||||
fakeClient = fake.NewSimpleClientset(test.endpoints)
|
if test.endpoints != nil {
|
||||||
}
|
fakeClient = fake.NewSimpleClientset(test.endpoints)
|
||||||
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
|
}
|
||||||
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
|
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
|
||||||
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
|
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
|
||||||
if err != nil {
|
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
|
||||||
t.Errorf("case %q: unexpected error: %v", test.testName, err)
|
if err != nil {
|
||||||
}
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
updates := []core.UpdateAction{}
|
updates := []core.UpdateAction{}
|
||||||
for _, action := range fakeClient.Actions() {
|
for _, action := range fakeClient.Actions() {
|
||||||
if action.GetVerb() != "update" {
|
if action.GetVerb() != "update" {
|
||||||
continue
|
continue
|
||||||
|
}
|
||||||
|
updates = append(updates, action.(core.UpdateAction))
|
||||||
}
|
}
|
||||||
updates = append(updates, action.(core.UpdateAction))
|
if test.expectUpdate != nil {
|
||||||
}
|
if len(updates) != 1 {
|
||||||
if test.expectUpdate != nil {
|
t.Errorf("unexpected updates: %v", updates)
|
||||||
if len(updates) != 1 {
|
} else if e, a := test.expectUpdate, updates[0].GetObject(); !reflect.DeepEqual(e, a) {
|
||||||
t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
|
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", 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)
|
}
|
||||||
|
if test.expectUpdate == nil && len(updates) > 0 {
|
||||||
|
t.Errorf("no update expected, yet saw: %v", updates)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if test.expectUpdate == nil && len(updates) > 0 {
|
|
||||||
t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
|
|
||||||
}
|
|
||||||
|
|
||||||
creates := []core.CreateAction{}
|
creates := []core.CreateAction{}
|
||||||
for _, action := range fakeClient.Actions() {
|
for _, action := range fakeClient.Actions() {
|
||||||
if action.GetVerb() != "create" {
|
if action.GetVerb() != "create" {
|
||||||
continue
|
continue
|
||||||
|
}
|
||||||
|
creates = append(creates, action.(core.CreateAction))
|
||||||
}
|
}
|
||||||
creates = append(creates, action.(core.CreateAction))
|
if test.expectCreate != nil {
|
||||||
}
|
if len(creates) != 1 {
|
||||||
if test.expectCreate != nil {
|
t.Errorf("unexpected creates: %v", creates)
|
||||||
if len(creates) != 1 {
|
} else if e, a := test.expectCreate, creates[0].GetObject(); !reflect.DeepEqual(e, a) {
|
||||||
t.Errorf("case %q: unexpected creates: %v", test.testName, creates)
|
t.Errorf("expected create:\n%#v\ngot:\n%#v\n", 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)
|
|
||||||
}
|
}
|
||||||
}
|
if test.expectCreate == nil && len(creates) > 0 {
|
||||||
if test.expectCreate == nil && len(creates) > 0 {
|
t.Errorf("no create expected, yet saw: %v", creates)
|
||||||
t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nonReconcileTests := []struct {
|
nonReconcileTests := []struct {
|
||||||
@ -512,53 +513,54 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range nonReconcileTests {
|
for _, test := range nonReconcileTests {
|
||||||
fakeClient := fake.NewSimpleClientset()
|
t.Run(test.testName, func(t *testing.T) {
|
||||||
if test.endpoints != nil {
|
fakeClient := fake.NewSimpleClientset()
|
||||||
fakeClient = fake.NewSimpleClientset(test.endpoints)
|
if test.endpoints != nil {
|
||||||
}
|
fakeClient = fake.NewSimpleClientset(test.endpoints)
|
||||||
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
|
}
|
||||||
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
|
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
|
||||||
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
|
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
|
||||||
if err != nil {
|
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
|
||||||
t.Errorf("case %q: unexpected error: %v", test.testName, err)
|
if err != nil {
|
||||||
}
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
updates := []core.UpdateAction{}
|
updates := []core.UpdateAction{}
|
||||||
for _, action := range fakeClient.Actions() {
|
for _, action := range fakeClient.Actions() {
|
||||||
if action.GetVerb() != "update" {
|
if action.GetVerb() != "update" {
|
||||||
continue
|
continue
|
||||||
|
}
|
||||||
|
updates = append(updates, action.(core.UpdateAction))
|
||||||
}
|
}
|
||||||
updates = append(updates, action.(core.UpdateAction))
|
if test.expectUpdate != nil {
|
||||||
}
|
if len(updates) != 1 {
|
||||||
if test.expectUpdate != nil {
|
t.Errorf("unexpected updates: %v", updates)
|
||||||
if len(updates) != 1 {
|
} else if e, a := test.expectUpdate, updates[0].GetObject(); !reflect.DeepEqual(e, a) {
|
||||||
t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
|
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", 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)
|
}
|
||||||
|
if test.expectUpdate == nil && len(updates) > 0 {
|
||||||
|
t.Errorf("no update expected, yet saw: %v", updates)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if test.expectUpdate == nil && len(updates) > 0 {
|
|
||||||
t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
|
|
||||||
}
|
|
||||||
|
|
||||||
creates := []core.CreateAction{}
|
creates := []core.CreateAction{}
|
||||||
for _, action := range fakeClient.Actions() {
|
for _, action := range fakeClient.Actions() {
|
||||||
if action.GetVerb() != "create" {
|
if action.GetVerb() != "create" {
|
||||||
continue
|
continue
|
||||||
|
}
|
||||||
|
creates = append(creates, action.(core.CreateAction))
|
||||||
}
|
}
|
||||||
creates = append(creates, action.(core.CreateAction))
|
if test.expectCreate != nil {
|
||||||
}
|
if len(creates) != 1 {
|
||||||
if test.expectCreate != nil {
|
t.Errorf("unexpected creates: %v", creates)
|
||||||
if len(creates) != 1 {
|
} else if e, a := test.expectCreate, creates[0].GetObject(); !reflect.DeepEqual(e, a) {
|
||||||
t.Errorf("case %q: unexpected creates: %v", test.testName, creates)
|
t.Errorf("expected create:\n%#v\ngot:\n%#v\n", 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)
|
|
||||||
}
|
}
|
||||||
}
|
if test.expectCreate == nil && len(creates) > 0 {
|
||||||
if test.expectCreate == nil && len(creates) > 0 {
|
t.Errorf("no create expected, yet saw: %v", creates)
|
||||||
t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -448,36 +448,38 @@ func TestLeaseEndpointReconciler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range reconcileTests {
|
for _, test := range reconcileTests {
|
||||||
fakeLeases := newFakeLeases()
|
t.Run(test.testName, func(t *testing.T) {
|
||||||
fakeLeases.SetKeys(test.endpointKeys)
|
fakeLeases := newFakeLeases()
|
||||||
clientset := fake.NewSimpleClientset()
|
fakeLeases.SetKeys(test.endpointKeys)
|
||||||
if test.endpoints != nil {
|
clientset := fake.NewSimpleClientset()
|
||||||
for _, ep := range test.endpoints.Items {
|
if test.endpoints != nil {
|
||||||
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil {
|
for _, ep := range test.endpoints.Items {
|
||||||
t.Errorf("case %q: unexpected error: %v", test.testName, err)
|
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil {
|
||||||
continue
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()}
|
epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()}
|
||||||
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{})
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("case %q: unexpected error: %v", test.testName, 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)
|
|
||||||
}
|
}
|
||||||
}
|
actualEndpoints, err := clientset.CoreV1().Endpoints(corev1.NamespaceDefault).Get(context.TODO(), test.serviceName, metav1.GetOptions{})
|
||||||
if updatedKeys := fakeLeases.GetUpdatedKeys(); len(updatedKeys) != 1 || updatedKeys[0] != test.ip {
|
if err != nil {
|
||||||
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("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
if test.expectUpdate != nil {
|
||||||
|
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(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("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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user