Simplify endpoint creation in reconciler unit tests

Also make the expectCreate / expectUpdate fields into arrays while
we're rewriting their values anyway, to avoid additional churn in the
next commit.
This commit is contained in:
Dan Winship 2022-04-27 08:27:41 -04:00
parent b07fe3a974
commit 4033de2034
3 changed files with 154 additions and 637 deletions

View File

@ -0,0 +1,52 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reconcilers
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"
)
func makeEndpointsArray(name string, ips []string, ports []corev1.EndpointPort) []runtime.Object {
return []runtime.Object{
makeEndpoints(name, ips, ports),
}
}
func makeEndpoints(name string, ips []string, ports []corev1.EndpointPort) *corev1.Endpoints {
endpoints := &corev1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceDefault,
Name: name,
Labels: map[string]string{
discoveryv1.LabelSkipMirror: "true",
},
},
}
if len(ips) > 0 || len(ports) > 0 {
endpoints.Subsets = []corev1.EndpointSubset{{
Addresses: make([]corev1.EndpointAddress, len(ips)),
Ports: ports,
}}
for i := range ips {
endpoints.Subsets[0].Addresses[i].IP = ips[i]
}
}
return endpoints
}

View File

@ -21,8 +21,6 @@ import (
"testing"
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"
@ -30,16 +28,6 @@ import (
)
func TestMasterCountEndpointReconciler(t *testing.T) {
ns := metav1.NamespaceDefault
om := func(name string, skipMirrorLabel bool) metav1.ObjectMeta {
o := metav1.ObjectMeta{Namespace: ns, Name: name}
if skipMirrorLabel {
o.Labels = map[string]string{
discoveryv1.LabelSkipMirror: "true",
}
}
return o
}
reconcileTests := []struct {
testName string
serviceName string
@ -47,8 +35,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
endpointPorts []corev1.EndpointPort
additionalMasters int
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
expectCreate *corev1.Endpoints // nil means none expected
expectUpdate []runtime.Object
expectCreate []runtime.Object
}{
{
testName: "no existing endpoints",
@ -56,50 +44,22 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
initialState: nil,
expectCreate: &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"}},
}},
},
expectCreate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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"}},
}},
},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy but too many",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy but too many + extra masters",
@ -107,33 +67,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3,
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"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "1.2.3.4"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy but too many + extra masters + delete first",
@ -141,33 +76,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3,
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"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "4.3.2.1"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy and endpoint addresses length less than master count",
@ -175,19 +85,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.2",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3,
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "4.3.2.1"},
{IP: "4.3.2.2"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
},
expectUpdate: nil,
initialState: makeEndpointsArray("foo", []string{"4.3.2.1", "4.3.2.2"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: nil,
},
{
testName: "existing endpoints current IP missing and address length less than master count",
@ -195,137 +94,48 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.2",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3,
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "4.3.2.1"},
{IP: "4.3.2.2"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"4.3.2.1", "4.3.2.2"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong name",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("bar", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectCreate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong IP",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong port",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong protocol",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong port name",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints extra service ports satisfy",
@ -336,19 +146,13 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
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"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
}},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"},
[]corev1.EndpointPort{
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
},
),
},
{
testName: "existing endpoints extra service ports missing port",
@ -358,25 +162,13 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
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"}},
}},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"},
[]corev1.EndpointPort{
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
},
expectUpdate: &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"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
}},
},
),
},
{
testName: "no existing sctp endpoints",
@ -384,13 +176,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}},
initialState: nil,
expectCreate: &corev1.Endpoints{
ObjectMeta: om("boo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}},
}},
},
expectCreate: makeEndpointsArray("boo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}}),
},
}
for _, test := range reconcileTests {
@ -413,7 +199,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
if test.expectUpdate != nil {
if len(updates) != 1 {
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[0], updates[0].GetObject(); !reflect.DeepEqual(e, a) {
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
@ -431,7 +217,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
if test.expectCreate != nil {
if len(creates) != 1 {
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[0], creates[0].GetObject(); !reflect.DeepEqual(e, a) {
t.Errorf("expected create:\n%#v\ngot:\n%#v\n", e, a)
}
}
@ -448,8 +234,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
endpointPorts []corev1.EndpointPort
additionalMasters int
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
expectCreate *corev1.Endpoints // nil means none expected
expectUpdate []runtime.Object
expectCreate []runtime.Object
}{
{
testName: "existing endpoints extra service ports missing port no update",
@ -459,15 +245,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
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"}},
}},
},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: nil,
},
{
@ -478,22 +256,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "no existing endpoints",
@ -501,13 +265,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
initialState: nil,
expectCreate: &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"}},
}},
},
expectCreate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
}
for _, test := range nonReconcileTests {
@ -530,7 +288,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
if test.expectUpdate != nil {
if len(updates) != 1 {
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[0], updates[0].GetObject(); !reflect.DeepEqual(e, a) {
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
@ -548,7 +306,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
if test.expectCreate != nil {
if len(creates) != 1 {
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[0], creates[0].GetObject(); !reflect.DeepEqual(e, a) {
t.Errorf("expected create:\n%#v\ngot:\n%#v\n", e, a)
}
}
@ -561,16 +319,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}
func TestEmptySubsets(t *testing.T) {
ns := metav1.NamespaceDefault
om := func(name string) metav1.ObjectMeta {
return metav1.ObjectMeta{Namespace: ns, Name: name}
}
endpoints := &corev1.EndpointsList{
Items: []corev1.Endpoints{{
ObjectMeta: om("foo"),
Subsets: nil,
}},
}
endpoints := makeEndpoints("foo", nil, nil)
fakeClient := fake.NewSimpleClientset(endpoints)
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
reconciler := NewMasterCountEndpointReconciler(1, epAdapter)

View File

@ -27,7 +27,6 @@ import (
"testing"
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"
@ -82,16 +81,6 @@ func (f *fakeLeases) Destroy() {
}
func TestLeaseEndpointReconciler(t *testing.T) {
ns := corev1.NamespaceDefault
om := func(name string, skipMirrorLabel bool) metav1.ObjectMeta {
o := metav1.ObjectMeta{Namespace: ns, Name: name}
if skipMirrorLabel {
o.Labels = map[string]string{
discoveryv1.LabelSkipMirror: "true",
}
}
return o
}
reconcileTests := []struct {
testName string
serviceName string
@ -99,7 +88,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
endpointPorts []corev1.EndpointPort
endpointKeys []string
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
expectUpdate []runtime.Object
}{
{
testName: "no existing endpoints",
@ -107,28 +96,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
initialState: nil,
expectUpdate: &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: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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"}},
}},
},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy + refresh existing key",
@ -136,37 +111,15 @@ 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"},
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"}},
}},
},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy but too many",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy but too many + extra masters",
@ -174,33 +127,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"},
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"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "1.2.3.4"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints satisfy but too many + extra masters + delete first",
@ -208,33 +136,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"},
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"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "4.3.2.1"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints current IP missing",
@ -242,137 +145,48 @@ 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"},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "4.3.2.1"},
{IP: "4.3.2.2"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"4.3.2.1", "4.3.2.2"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong name",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("bar", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong IP",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong port",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong protocol",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints wrong port name",
serviceName: "foo",
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints without skip mirror label",
@ -380,21 +194,20 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
initialState: []runtime.Object{
// can't use makeEndpointsArray() here because we don't want the
// skip-mirror label
&corev1.Endpoints{
ObjectMeta: om("foo", false),
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceDefault,
Name: "foo",
},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "existing endpoints extra service ports satisfy",
@ -405,19 +218,13 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
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"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
}},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"},
[]corev1.EndpointPort{
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"},
},
},
),
},
{
testName: "existing endpoints extra service ports missing port",
@ -427,25 +234,13 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
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"}},
}},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"},
[]corev1.EndpointPort{
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
},
expectUpdate: &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"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
}},
},
),
},
}
for _, test := range reconcileTests {
@ -465,7 +260,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) {
if e, a := test.expectUpdate[0], actualEndpoints; !reflect.DeepEqual(e, a) {
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
@ -482,7 +277,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
endpointPorts []corev1.EndpointPort
endpointKeys []string
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
expectUpdate []runtime.Object
}{
{
testName: "existing endpoints extra service ports missing port no update",
@ -492,15 +287,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
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"}},
}},
},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: nil,
},
{
@ -511,22 +298,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"},
},
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),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "no existing endpoints",
@ -534,13 +307,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
initialState: nil,
expectUpdate: &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: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
}
for _, test := range nonReconcileTests {
@ -559,7 +326,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) {
if e, a := test.expectUpdate[0], actualEndpoints; !reflect.DeepEqual(e, a) {
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}
@ -571,16 +338,6 @@ func TestLeaseEndpointReconciler(t *testing.T) {
}
func TestLeaseRemoveEndpoints(t *testing.T) {
ns := corev1.NamespaceDefault
om := func(name string, skipMirrorLabel bool) metav1.ObjectMeta {
o := metav1.ObjectMeta{Namespace: ns, Name: name}
if skipMirrorLabel {
o.Labels = map[string]string{
discoveryv1.LabelSkipMirror: "true",
}
}
return o
}
stopTests := []struct {
testName string
serviceName string
@ -588,7 +345,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
endpointPorts []corev1.EndpointPort
endpointKeys []string
initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected
expectUpdate []runtime.Object
}{
{
testName: "successful stop reconciling",
@ -596,31 +353,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"},
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "1.2.3.4"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
},
expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
expectUpdate: makeEndpointsArray("foo", []string{"4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "stop reconciling with ip not in endpoint ip list",
@ -628,20 +362,7 @@ 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"},
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{
{IP: "1.2.3.4"},
{IP: "4.3.2.2"},
{IP: "4.3.2.3"},
{IP: "4.3.2.4"},
},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}},
},
},
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
},
{
testName: "endpoint with no subset",
@ -649,12 +370,7 @@ 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"},
initialState: []runtime.Object{
&corev1.Endpoints{
ObjectMeta: om("foo", true),
Subsets: nil,
},
},
initialState: makeEndpointsArray("foo", nil, nil),
},
}
for _, test := range stopTests {
@ -673,7 +389,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
if test.expectUpdate != nil {
if e, a := test.expectUpdate, actualEndpoints; !reflect.DeepEqual(e, a) {
if e, a := test.expectUpdate[0], actualEndpoints; !reflect.DeepEqual(e, a) {
t.Errorf("expected update:\n%#v\ngot:\n%#v\n", e, a)
}
}