2014-06-06 23:40:48 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-02-27 16:38:59 +00:00
|
|
|
package config
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2014-07-08 16:55:11 +00:00
|
|
|
"sort"
|
2014-06-06 23:40:48 +00:00
|
|
|
"testing"
|
2016-02-04 17:26:08 +00:00
|
|
|
"time"
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2017-03-17 12:53:46 +00:00
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2014-07-08 16:55:11 +00:00
|
|
|
type sortedServices []api.Service
|
|
|
|
|
|
|
|
func (s sortedServices) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
func (s sortedServices) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
func (s sortedServices) Less(i, j int) bool {
|
2014-10-22 17:02:02 +00:00
|
|
|
return s[i].Name < s[j].Name
|
2014-07-08 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
type ServiceHandlerMock struct {
|
2016-02-02 03:02:23 +00:00
|
|
|
updated chan []api.Service
|
|
|
|
waits int
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:55:11 +00:00
|
|
|
func NewServiceHandlerMock() *ServiceHandlerMock {
|
2016-02-02 03:02:23 +00:00
|
|
|
return &ServiceHandlerMock{updated: make(chan []api.Service, 5)}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 19:16:55 +00:00
|
|
|
func (h *ServiceHandlerMock) OnServiceUpdate(services []api.Service) {
|
2014-07-08 16:55:11 +00:00
|
|
|
sort.Sort(sortedServices(services))
|
2016-02-02 03:02:23 +00:00
|
|
|
h.updated <- services
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:55:11 +00:00
|
|
|
func (h *ServiceHandlerMock) ValidateServices(t *testing.T, expectedServices []api.Service) {
|
2016-02-02 03:02:23 +00:00
|
|
|
// We might get 1 or more updates for N service updates, because we
|
|
|
|
// over write older snapshots of services from the producer go-routine
|
2016-02-04 17:26:08 +00:00
|
|
|
// if the consumer falls behind.
|
2016-02-02 03:02:23 +00:00
|
|
|
var services []api.Service
|
2016-02-04 17:26:08 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case services = <-h.updated:
|
|
|
|
if reflect.DeepEqual(services, expectedServices) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Unittests will hard timeout in 5m with a stack trace, prevent that
|
|
|
|
// and surface a clearer reason for failure.
|
2016-02-02 10:57:06 +00:00
|
|
|
case <-time.After(wait.ForeverTestTimeout):
|
2016-02-04 17:26:08 +00:00
|
|
|
t.Errorf("Timed out. Expected %#v, Got %#v", expectedServices, services)
|
2016-02-02 03:02:23 +00:00
|
|
|
return
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-08 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 15:42:45 +00:00
|
|
|
type sortedEndpoints []*api.Endpoints
|
2014-07-08 16:55:11 +00:00
|
|
|
|
|
|
|
func (s sortedEndpoints) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
func (s sortedEndpoints) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
func (s sortedEndpoints) Less(i, j int) bool {
|
2014-10-22 17:02:02 +00:00
|
|
|
return s[i].Name < s[j].Name
|
2014-07-08 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
type EndpointsHandlerMock struct {
|
2017-03-09 15:42:45 +00:00
|
|
|
updated chan []*api.Endpoints
|
2016-02-02 03:02:23 +00:00
|
|
|
waits int
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:55:11 +00:00
|
|
|
func NewEndpointsHandlerMock() *EndpointsHandlerMock {
|
2017-03-09 15:42:45 +00:00
|
|
|
return &EndpointsHandlerMock{updated: make(chan []*api.Endpoints, 5)}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 15:42:45 +00:00
|
|
|
func (h *EndpointsHandlerMock) OnEndpointsUpdate(endpoints []*api.Endpoints) {
|
2014-07-08 16:55:11 +00:00
|
|
|
sort.Sort(sortedEndpoints(endpoints))
|
2016-02-02 03:02:23 +00:00
|
|
|
h.updated <- endpoints
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 15:42:45 +00:00
|
|
|
func (h *EndpointsHandlerMock) ValidateEndpoints(t *testing.T, expectedEndpoints []*api.Endpoints) {
|
2016-02-02 03:02:23 +00:00
|
|
|
// We might get 1 or more updates for N endpoint updates, because we
|
|
|
|
// over write older snapshots of endpoints from the producer go-routine
|
|
|
|
// if the consumer falls behind. Unittests will hard timeout in 5m.
|
2017-03-09 15:42:45 +00:00
|
|
|
var endpoints []*api.Endpoints
|
2016-02-04 17:26:08 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case endpoints = <-h.updated:
|
|
|
|
if reflect.DeepEqual(endpoints, expectedEndpoints) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Unittests will hard timeout in 5m with a stack trace, prevent that
|
|
|
|
// and surface a clearer reason for failure.
|
2016-02-02 10:57:06 +00:00
|
|
|
case <-time.After(wait.ForeverTestTimeout):
|
2016-02-04 17:26:08 +00:00
|
|
|
t.Errorf("Timed out. Expected %#v, Got %#v", expectedEndpoints, endpoints)
|
2016-02-02 03:02:23 +00:00
|
|
|
return
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-08 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestNewServiceAddedAndNotified(t *testing.T) {
|
2017-03-17 14:15:51 +00:00
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
lw := fakeLW{
|
|
|
|
listResp: &api.ServiceList{Items: []api.Service{}},
|
|
|
|
watchResp: fakeWatch,
|
|
|
|
}
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
|
|
|
|
|
|
|
config := newServiceConfig(lw, time.Minute)
|
2014-06-06 23:40:48 +00:00
|
|
|
handler := NewServiceHandlerMock()
|
2014-07-08 16:55:11 +00:00
|
|
|
config.RegisterHandler(handler)
|
2017-03-17 14:15:51 +00:00
|
|
|
go config.Run(stopCh)
|
|
|
|
|
|
|
|
service := &api.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
|
2015-03-13 15:16:41 +00:00
|
|
|
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 10}}},
|
2017-03-17 14:15:51 +00:00
|
|
|
}
|
|
|
|
fakeWatch.Add(service)
|
|
|
|
handler.ValidateServices(t, []api.Service{*service})
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceAddedRemovedSetAndNotified(t *testing.T) {
|
2017-03-17 14:15:51 +00:00
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
lw := fakeLW{
|
|
|
|
listResp: &api.ServiceList{Items: []api.Service{}},
|
|
|
|
watchResp: fakeWatch,
|
|
|
|
}
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
|
|
|
|
|
|
|
config := newServiceConfig(lw, time.Minute)
|
2014-06-06 23:40:48 +00:00
|
|
|
handler := NewServiceHandlerMock()
|
2014-07-08 16:55:11 +00:00
|
|
|
config.RegisterHandler(handler)
|
2017-03-17 14:15:51 +00:00
|
|
|
go config.Run(stopCh)
|
|
|
|
|
|
|
|
service1 := &api.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
|
2015-03-13 15:16:41 +00:00
|
|
|
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 10}}},
|
2017-03-17 14:15:51 +00:00
|
|
|
}
|
|
|
|
fakeWatch.Add(service1)
|
|
|
|
handler.ValidateServices(t, []api.Service{*service1})
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2017-03-17 14:15:51 +00:00
|
|
|
service2 := &api.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "bar"},
|
2015-03-13 15:16:41 +00:00
|
|
|
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 20}}},
|
2017-03-17 14:15:51 +00:00
|
|
|
}
|
|
|
|
fakeWatch.Add(service2)
|
|
|
|
services := []api.Service{*service2, *service1}
|
2014-06-06 23:40:48 +00:00
|
|
|
handler.ValidateServices(t, services)
|
|
|
|
|
2017-03-17 14:15:51 +00:00
|
|
|
fakeWatch.Delete(service1)
|
|
|
|
services = []api.Service{*service2}
|
2014-06-06 23:40:48 +00:00
|
|
|
handler.ValidateServices(t, services)
|
|
|
|
}
|
|
|
|
|
2017-03-17 14:15:51 +00:00
|
|
|
func TestNewServicesMultipleHandlersAddedAndNotified(t *testing.T) {
|
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
lw := fakeLW{
|
|
|
|
listResp: &api.ServiceList{Items: []api.Service{}},
|
|
|
|
watchResp: fakeWatch,
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2017-03-17 14:15:51 +00:00
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2017-03-17 14:15:51 +00:00
|
|
|
config := newServiceConfig(lw, time.Minute)
|
2014-06-06 23:40:48 +00:00
|
|
|
handler := NewServiceHandlerMock()
|
|
|
|
handler2 := NewServiceHandlerMock()
|
2014-07-08 16:55:11 +00:00
|
|
|
config.RegisterHandler(handler)
|
|
|
|
config.RegisterHandler(handler2)
|
2017-03-17 14:15:51 +00:00
|
|
|
go config.Run(stopCh)
|
|
|
|
|
|
|
|
service1 := &api.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
|
2015-03-13 15:16:41 +00:00
|
|
|
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 10}}},
|
2017-03-17 14:15:51 +00:00
|
|
|
}
|
|
|
|
service2 := &api.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "bar"},
|
2015-03-13 15:16:41 +00:00
|
|
|
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 20}}},
|
2017-03-17 14:15:51 +00:00
|
|
|
}
|
|
|
|
fakeWatch.Add(service1)
|
|
|
|
fakeWatch.Add(service2)
|
|
|
|
|
|
|
|
services := []api.Service{*service2, *service1}
|
2014-06-06 23:40:48 +00:00
|
|
|
handler.ValidateServices(t, services)
|
|
|
|
handler2.ValidateServices(t, services)
|
|
|
|
}
|
|
|
|
|
2017-03-17 12:53:46 +00:00
|
|
|
func TestNewEndpointsMultipleHandlersAddedAndNotified(t *testing.T) {
|
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
lw := fakeLW{
|
|
|
|
listResp: &api.EndpointsList{Items: []api.Endpoints{}},
|
|
|
|
watchResp: fakeWatch,
|
|
|
|
}
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
|
|
|
|
|
|
|
config := newEndpointsConfig(lw, time.Minute)
|
2014-06-06 23:40:48 +00:00
|
|
|
handler := NewEndpointsHandlerMock()
|
|
|
|
handler2 := NewEndpointsHandlerMock()
|
2014-07-08 16:55:11 +00:00
|
|
|
config.RegisterHandler(handler)
|
|
|
|
config.RegisterHandler(handler2)
|
2017-03-17 12:53:46 +00:00
|
|
|
go config.Run(stopCh)
|
|
|
|
|
|
|
|
endpoints1 := &api.Endpoints{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
|
2015-03-20 21:24:43 +00:00
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "1.1.1.1"}, {IP: "2.2.2.2"}},
|
|
|
|
Ports: []api.EndpointPort{{Port: 80}},
|
|
|
|
}},
|
2017-03-17 12:53:46 +00:00
|
|
|
}
|
|
|
|
endpoints2 := &api.Endpoints{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "bar"},
|
2015-03-20 21:24:43 +00:00
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "3.3.3.3"}, {IP: "4.4.4.4"}},
|
|
|
|
Ports: []api.EndpointPort{{Port: 80}},
|
|
|
|
}},
|
2017-03-17 12:53:46 +00:00
|
|
|
}
|
|
|
|
fakeWatch.Add(endpoints1)
|
|
|
|
fakeWatch.Add(endpoints2)
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2017-03-17 12:53:46 +00:00
|
|
|
endpoints := []*api.Endpoints{endpoints2, endpoints1}
|
2014-06-06 23:40:48 +00:00
|
|
|
handler.ValidateEndpoints(t, endpoints)
|
|
|
|
handler2.ValidateEndpoints(t, endpoints)
|
|
|
|
}
|
|
|
|
|
2017-03-17 12:53:46 +00:00
|
|
|
func TestNewEndpointsMultipleHandlersAddRemoveSetAndNotified(t *testing.T) {
|
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
lw := fakeLW{
|
|
|
|
listResp: &api.EndpointsList{Items: []api.Endpoints{}},
|
|
|
|
watchResp: fakeWatch,
|
|
|
|
}
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
|
|
|
|
|
|
|
config := newEndpointsConfig(lw, time.Minute)
|
2014-06-06 23:40:48 +00:00
|
|
|
handler := NewEndpointsHandlerMock()
|
|
|
|
handler2 := NewEndpointsHandlerMock()
|
2014-07-08 16:55:11 +00:00
|
|
|
config.RegisterHandler(handler)
|
|
|
|
config.RegisterHandler(handler2)
|
2017-03-17 12:53:46 +00:00
|
|
|
go config.Run(stopCh)
|
|
|
|
|
|
|
|
endpoints1 := &api.Endpoints{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
|
2015-03-20 21:24:43 +00:00
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "1.1.1.1"}, {IP: "2.2.2.2"}},
|
|
|
|
Ports: []api.EndpointPort{{Port: 80}},
|
|
|
|
}},
|
2017-03-17 12:53:46 +00:00
|
|
|
}
|
|
|
|
endpoints2 := &api.Endpoints{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "bar"},
|
2015-03-20 21:24:43 +00:00
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "3.3.3.3"}, {IP: "4.4.4.4"}},
|
|
|
|
Ports: []api.EndpointPort{{Port: 80}},
|
|
|
|
}},
|
2017-03-17 12:53:46 +00:00
|
|
|
}
|
|
|
|
fakeWatch.Add(endpoints1)
|
|
|
|
fakeWatch.Add(endpoints2)
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2017-03-17 12:53:46 +00:00
|
|
|
endpoints := []*api.Endpoints{endpoints2, endpoints1}
|
2014-06-06 23:40:48 +00:00
|
|
|
handler.ValidateEndpoints(t, endpoints)
|
|
|
|
handler2.ValidateEndpoints(t, endpoints)
|
|
|
|
|
|
|
|
// Add one more
|
2017-03-17 12:53:46 +00:00
|
|
|
endpoints3 := &api.Endpoints{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "foobar"},
|
2015-03-20 21:24:43 +00:00
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "5.5.5.5"}, {IP: "6.6.6.6"}},
|
|
|
|
Ports: []api.EndpointPort{{Port: 80}},
|
|
|
|
}},
|
2017-03-17 12:53:46 +00:00
|
|
|
}
|
|
|
|
fakeWatch.Add(endpoints3)
|
|
|
|
endpoints = []*api.Endpoints{endpoints2, endpoints1, endpoints3}
|
2014-06-06 23:40:48 +00:00
|
|
|
handler.ValidateEndpoints(t, endpoints)
|
|
|
|
handler2.ValidateEndpoints(t, endpoints)
|
|
|
|
|
|
|
|
// Update the "foo" service with new endpoints
|
2017-03-17 12:53:46 +00:00
|
|
|
endpoints1v2 := &api.Endpoints{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
|
2015-03-20 21:24:43 +00:00
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "7.7.7.7"}},
|
|
|
|
Ports: []api.EndpointPort{{Port: 80}},
|
|
|
|
}},
|
2017-03-17 12:53:46 +00:00
|
|
|
}
|
|
|
|
fakeWatch.Modify(endpoints1v2)
|
|
|
|
endpoints = []*api.Endpoints{endpoints2, endpoints1v2, endpoints3}
|
2014-06-06 23:40:48 +00:00
|
|
|
handler.ValidateEndpoints(t, endpoints)
|
|
|
|
handler2.ValidateEndpoints(t, endpoints)
|
|
|
|
|
2017-03-17 12:53:46 +00:00
|
|
|
// Remove "bar" endpoints
|
|
|
|
fakeWatch.Delete(endpoints2)
|
|
|
|
endpoints = []*api.Endpoints{endpoints1v2, endpoints3}
|
2014-06-06 23:40:48 +00:00
|
|
|
handler.ValidateEndpoints(t, endpoints)
|
|
|
|
handler2.ValidateEndpoints(t, endpoints)
|
|
|
|
}
|
2016-02-04 03:19:59 +00:00
|
|
|
|
|
|
|
// TODO: Add a unittest for interrupts getting processed in a timely manner.
|
|
|
|
// Currently this module has a circular dependency with config, and so it's
|
|
|
|
// named config_test, which means even test methods need to be public. This
|
|
|
|
// is refactoring that we can avoid by resolving the dependency.
|