Simplify reconciler unit test setup

Pass initial state objects to fake.NewSimpleClientSet() rather than
calling Create() by hand.

(This will make it easier to have an initial state that is a mix of
Endpoints and EndpointSlices later on.)
This commit is contained in:
Dan Winship 2022-04-27 09:22:58 -04:00
parent 007ca4f69d
commit b07fe3a974
3 changed files with 146 additions and 202 deletions

View File

@ -26,6 +26,7 @@ import (
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/fake"
)
@ -37,7 +38,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled bool
expectedError error
expectedEndpoints *corev1.Endpoints
endpoints []*corev1.Endpoints
initialState []runtime.Object
namespaceParam string
nameParam string
}{
@ -45,7 +46,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled: false,
expectedError: nil,
expectedEndpoints: endpoints1,
endpoints: []*corev1.Endpoints{endpoints1},
initialState: []runtime.Object{endpoints1},
namespaceParam: "testing",
nameParam: "foo",
},
@ -53,7 +54,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled: true,
expectedError: nil,
expectedEndpoints: endpoints1,
endpoints: []*corev1.Endpoints{endpoints1},
initialState: []runtime.Object{endpoints1},
namespaceParam: "testing",
nameParam: "foo",
},
@ -61,7 +62,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled: false,
expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "foo"),
expectedEndpoints: nil,
endpoints: []*corev1.Endpoints{endpoints1},
initialState: []runtime.Object{endpoints1},
namespaceParam: "foo",
nameParam: "foo",
},
@ -69,7 +70,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled: false,
expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "bar"),
expectedEndpoints: nil,
endpoints: []*corev1.Endpoints{endpoints1},
initialState: []runtime.Object{endpoints1},
namespaceParam: "testing",
nameParam: "bar",
},
@ -77,19 +78,12 @@ func TestEndpointsAdapterGet(t *testing.T) {
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
client := fake.NewSimpleClientset()
client := fake.NewSimpleClientset(testCase.initialState...)
epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
if testCase.endpointSlicesEnabled {
epAdapter.endpointSliceClient = client.DiscoveryV1()
}
for _, endpoints := range testCase.endpoints {
_, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating Endpoints: %v", err)
}
}
endpoints, err := epAdapter.Get(testCase.namespaceParam, testCase.nameParam, metav1.GetOptions{})
if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
@ -121,7 +115,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError error
expectedEndpoints *corev1.Endpoints
expectedEndpointSlice *discovery.EndpointSlice
endpoints []*corev1.Endpoints
initialState []runtime.Object
endpointSlices []*discovery.EndpointSlice
namespaceParam string
endpointsParam *corev1.Endpoints
@ -131,7 +125,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: nil,
expectedEndpoints: endpoints1,
expectedEndpointSlice: epSlice1,
endpoints: []*corev1.Endpoints{},
initialState: []runtime.Object{},
namespaceParam: endpoints1.Namespace,
endpointsParam: endpoints1,
},
@ -140,7 +134,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: nil,
expectedEndpoints: endpoints2,
expectedEndpointSlice: epSlice2,
endpoints: []*corev1.Endpoints{},
initialState: []runtime.Object{},
namespaceParam: endpoints2.Namespace,
endpointsParam: endpoints2,
},
@ -149,7 +143,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: nil,
expectedEndpoints: endpoints3,
expectedEndpointSlice: epSlice3,
endpoints: []*corev1.Endpoints{},
initialState: []runtime.Object{},
namespaceParam: endpoints3.Namespace,
endpointsParam: endpoints3,
},
@ -158,7 +152,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: nil,
expectedEndpoints: endpoints1,
expectedEndpointSlice: nil,
endpoints: []*corev1.Endpoints{},
initialState: []runtime.Object{},
namespaceParam: endpoints1.Namespace,
endpointsParam: endpoints1,
},
@ -167,7 +161,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: errors.NewAlreadyExists(schema.GroupResource{Group: "", Resource: "endpoints"}, "foo"),
expectedEndpoints: nil,
expectedEndpointSlice: nil,
endpoints: []*corev1.Endpoints{endpoints1},
initialState: []runtime.Object{endpoints1},
namespaceParam: endpoints1.Namespace,
endpointsParam: endpoints1,
},
@ -175,19 +169,12 @@ func TestEndpointsAdapterCreate(t *testing.T) {
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
client := fake.NewSimpleClientset()
client := fake.NewSimpleClientset(testCase.initialState...)
epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
if testCase.endpointSlicesEnabled {
epAdapter.endpointSliceClient = client.DiscoveryV1()
}
for _, endpoints := range testCase.endpoints {
_, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating Endpoints: %v", err)
}
}
endpoints, err := epAdapter.Create(testCase.namespaceParam, testCase.endpointsParam)
if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
@ -241,7 +228,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError error
expectedEndpoints *corev1.Endpoints
expectedEndpointSlice *discovery.EndpointSlice
endpoints []*corev1.Endpoints
initialState []runtime.Object
endpointSlices []*discovery.EndpointSlice
namespaceParam string
endpointsParam *corev1.Endpoints
@ -251,7 +238,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError: nil,
expectedEndpoints: endpoints1,
expectedEndpointSlice: nil,
endpoints: []*corev1.Endpoints{endpoints1},
initialState: []runtime.Object{endpoints1},
namespaceParam: "testing",
endpointsParam: endpoints1,
},
@ -260,7 +247,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError: nil,
expectedEndpoints: endpoints4,
expectedEndpointSlice: epSlice4IPv4,
endpoints: []*corev1.Endpoints{endpoints4},
initialState: []runtime.Object{endpoints4},
endpointSlices: []*discovery.EndpointSlice{epSlice4IP},
namespaceParam: "testing",
endpointsParam: endpoints4,
@ -270,7 +257,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError: nil,
expectedEndpoints: endpoints2,
expectedEndpointSlice: epSlice2,
endpoints: []*corev1.Endpoints{endpoints1},
initialState: []runtime.Object{endpoints1},
namespaceParam: "testing",
endpointsParam: endpoints2,
},
@ -279,7 +266,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "bar"),
expectedEndpoints: nil,
expectedEndpointSlice: nil,
endpoints: []*corev1.Endpoints{endpoints1},
initialState: []runtime.Object{endpoints1},
namespaceParam: "testing",
endpointsParam: endpoints3,
},
@ -287,19 +274,12 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
client := fake.NewSimpleClientset()
client := fake.NewSimpleClientset(testCase.initialState...)
epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
if testCase.endpointSlicesEnabled {
epAdapter.endpointSliceClient = client.DiscoveryV1()
}
for _, endpoints := range testCase.endpoints {
_, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating Endpoints: %v", err)
}
}
endpoints, err := epAdapter.Update(testCase.namespaceParam, testCase.endpointsParam)
if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
@ -389,7 +369,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled bool
expectedError error
expectedEndpointSlice *discovery.EndpointSlice
endpointSlices []*discovery.EndpointSlice
initialState []runtime.Object
namespaceParam string
endpointsParam *corev1.Endpoints
}{
@ -397,7 +377,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled: true,
expectedError: nil,
expectedEndpointSlice: epSlice1,
endpointSlices: []*discovery.EndpointSlice{epSlice1},
initialState: []runtime.Object{epSlice1},
namespaceParam: "testing",
endpointsParam: endpoints1,
},
@ -405,7 +385,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled: true,
expectedError: nil,
expectedEndpointSlice: epSlice2,
endpointSlices: []*discovery.EndpointSlice{epSlice1},
initialState: []runtime.Object{epSlice1},
namespaceParam: "testing",
endpointsParam: endpoints2,
},
@ -413,7 +393,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled: true,
expectedError: nil,
expectedEndpointSlice: epSlice1,
endpointSlices: []*discovery.EndpointSlice{},
initialState: []runtime.Object{},
namespaceParam: "testing",
endpointsParam: endpoints1,
},
@ -421,7 +401,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled: false,
expectedError: nil,
expectedEndpointSlice: nil,
endpointSlices: []*discovery.EndpointSlice{},
initialState: []runtime.Object{},
namespaceParam: "testing",
endpointsParam: endpoints1,
},
@ -429,19 +409,12 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
client := fake.NewSimpleClientset()
client := fake.NewSimpleClientset(testCase.initialState...)
epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
if testCase.endpointSlicesEnabled {
epAdapter.endpointSliceClient = client.DiscoveryV1()
}
for _, endpointSlice := range testCase.endpointSlices {
_, err := client.DiscoveryV1().EndpointSlices(endpointSlice.Namespace).Create(context.TODO(), endpointSlice, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating EndpointSlice: %v", err)
}
}
err := epAdapter.EnsureEndpointSliceFromEndpoints(testCase.namespaceParam, testCase.endpointsParam)
if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
t.Errorf("Expected error: %v, got: %v", testCase.expectedError, err)

View File

@ -23,6 +23,7 @@ import (
corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
core "k8s.io/client-go/testing"
netutils "k8s.io/utils/net"
@ -45,7 +46,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip string
endpointPorts []corev1.EndpointPort
additionalMasters int
endpoints *corev1.EndpointsList
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
expectCreate *corev1.Endpoints // nil means none expected
}{
@ -54,7 +55,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: nil,
initialState: nil,
expectCreate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
@ -68,14 +69,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
},
{
@ -83,14 +84,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -106,8 +107,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3,
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -119,7 +120,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -140,8 +141,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3,
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -153,7 +154,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -174,8 +175,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.2",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3,
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -184,7 +185,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: nil,
},
@ -194,8 +195,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.2",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3,
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -203,7 +204,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -221,14 +222,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("bar", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectCreate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -243,14 +244,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -265,14 +266,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -287,14 +288,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -309,14 +310,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -335,8 +336,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
@ -346,7 +347,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
}},
}},
},
},
},
{
@ -357,14 +358,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -382,7 +383,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "boo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}},
endpoints: nil,
initialState: nil,
expectCreate: &corev1.Endpoints{
ObjectMeta: om("boo", true),
Subsets: []corev1.EndpointSubset{{
@ -394,10 +395,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)
}
fakeClient := fake.NewSimpleClientset(test.initialState...)
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
@ -449,7 +447,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip string
endpointPorts []corev1.EndpointPort
additionalMasters int
endpoints *corev1.EndpointsList
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
expectCreate *corev1.Endpoints // nil means none expected
}{
@ -461,14 +459,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: nil,
},
@ -480,14 +478,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -502,7 +500,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: nil,
initialState: nil,
expectCreate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
@ -514,10 +512,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)
}
fakeClient := fake.NewSimpleClientset(test.initialState...)
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
@ -576,10 +571,7 @@ func TestEmptySubsets(t *testing.T) {
Subsets: nil,
}},
}
fakeClient := fake.NewSimpleClientset()
if endpoints != nil {
fakeClient = fake.NewSimpleClientset(endpoints)
}
fakeClient := fake.NewSimpleClientset(endpoints)
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
reconciler := NewMasterCountEndpointReconciler(1, epAdapter)
endpointPorts := []corev1.EndpointPort{

View File

@ -29,6 +29,7 @@ import (
corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
netutils "k8s.io/utils/net"
)
@ -97,7 +98,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip string
endpointPorts []corev1.EndpointPort
endpointKeys []string
endpoints *corev1.EndpointsList
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
}{
{
@ -105,7 +106,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: nil,
initialState: nil,
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
@ -119,14 +120,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
},
{
@ -135,14 +136,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4"},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
},
{
@ -150,14 +151,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -173,8 +174,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -186,7 +187,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -207,8 +208,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "4.3.2.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -220,7 +221,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -241,8 +242,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "4.3.2.2",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"4.3.2.1"},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -250,7 +251,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -268,14 +269,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("bar", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -290,14 +291,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -312,14 +313,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -334,14 +335,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -356,14 +357,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -378,14 +379,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", false),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -404,8 +405,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
@ -415,7 +416,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
}},
}},
},
},
},
{
@ -426,14 +427,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -451,15 +452,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
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("unexpected error: %v", err)
continue
}
}
}
clientset := fake.NewSimpleClientset(test.initialState...)
epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()}
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
@ -488,7 +481,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip string
endpointPorts []corev1.EndpointPort
endpointKeys []string
endpoints *corev1.EndpointsList
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
}{
{
@ -499,14 +492,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: nil,
},
@ -518,14 +511,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -540,7 +533,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: nil,
initialState: nil,
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
@ -554,15 +547,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
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("unexpected error: %v", err)
continue
}
}
}
clientset := fake.NewSimpleClientset(test.initialState...)
epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()}
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
@ -602,7 +587,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
ip string
endpointPorts []corev1.EndpointPort
endpointKeys []string
endpoints *corev1.EndpointsList
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
}{
{
@ -611,8 +596,8 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -623,7 +608,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
@ -643,8 +628,8 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
ip: "5.6.7.8",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
@ -655,7 +640,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
}},
},
},
},
{
@ -664,11 +649,11 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
ip: "5.6.7.8",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: nil,
}},
},
},
},
}
@ -676,13 +661,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
t.Run(test.testName, func(t *testing.T) {
fakeLeases := newFakeLeases()
fakeLeases.SetKeys(test.endpointKeys)
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("unexpected error: %v", err)
continue
}
}
clientset := fake.NewSimpleClientset(test.initialState...)
epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()}
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.RemoveEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts)