2014-08-15 21:14:22 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 config
|
|
|
|
|
|
|
|
import (
|
2014-08-29 02:31:41 +00:00
|
|
|
"errors"
|
2014-08-15 21:14:22 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServices(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
service := api.Service{ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "2"}}
|
2014-08-15 21:14:22 +00:00
|
|
|
|
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
fakeClient := &client.Fake{Watch: fakeWatch}
|
|
|
|
services := make(chan ServiceUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll)}}
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := "1"
|
2014-08-15 21:14:22 +00:00
|
|
|
go func() {
|
|
|
|
// called twice
|
2015-01-11 05:13:32 +00:00
|
|
|
source.s.run(&resourceVersion)
|
|
|
|
source.s.run(&resourceVersion)
|
2014-08-15 21:14:22 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// test adding a service to the watch
|
|
|
|
fakeWatch.Add(&service)
|
2014-10-07 20:51:28 +00:00
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-services", "1"}}) {
|
2014-08-15 21:14:22 +00:00
|
|
|
t.Errorf("expected call to watch-services, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := <-services
|
2014-08-29 02:31:41 +00:00
|
|
|
expected := ServiceUpdate{Op: ADD, Services: []api.Service{service}}
|
2014-08-15 21:14:22 +00:00
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that a delete results in a config change
|
|
|
|
fakeWatch.Delete(&service)
|
|
|
|
actual = <-services
|
2014-08-29 02:31:41 +00:00
|
|
|
expected = ServiceUpdate{Op: REMOVE, Services: []api.Service{service}}
|
2014-08-15 21:14:22 +00:00
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that closing the channel results in a new call to WatchServices with a higher resource version
|
|
|
|
newFakeWatch := watch.NewFake()
|
|
|
|
fakeClient.Watch = newFakeWatch
|
|
|
|
fakeWatch.Stop()
|
|
|
|
|
|
|
|
newFakeWatch.Add(&service)
|
2014-10-07 22:31:34 +00:00
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-services", "1"}, {"watch-services", "2"}}) {
|
2014-08-15 21:14:22 +00:00
|
|
|
t.Errorf("expected call to watch-endpoints, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 02:31:41 +00:00
|
|
|
func TestServicesFromZero(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
service := api.Service{ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "2"}}
|
2014-08-29 02:31:41 +00:00
|
|
|
|
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
fakeWatch.Stop()
|
|
|
|
fakeClient := &client.Fake{Watch: fakeWatch}
|
|
|
|
fakeClient.ServiceList = api.ServiceList{
|
2014-10-23 20:51:34 +00:00
|
|
|
ListMeta: api.ListMeta{ResourceVersion: "2"},
|
2014-08-29 02:31:41 +00:00
|
|
|
Items: []api.Service{
|
|
|
|
service,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
services := make(chan ServiceUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll)}}
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := ""
|
2014-08-29 02:31:41 +00:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-01-11 05:13:32 +00:00
|
|
|
source.s.run(&resourceVersion)
|
2014-08-29 02:31:41 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// should get services SET
|
|
|
|
actual := <-services
|
|
|
|
expected := ServiceUpdate{Op: SET, Services: []api.Service{service}}
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should have listed, then watched
|
|
|
|
<-ch
|
2014-10-07 20:51:28 +00:00
|
|
|
if resourceVersion != "2" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected resource version, got %#v", resourceVersion)
|
|
|
|
}
|
2014-10-07 20:51:28 +00:00
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"list-services", nil}, {"watch-services", "2"}}) {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected actions, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServicesError(t *testing.T) {
|
|
|
|
fakeClient := &client.Fake{Err: errors.New("test")}
|
|
|
|
services := make(chan ServiceUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll)}}
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := "1"
|
2014-08-29 02:31:41 +00:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-01-11 05:13:32 +00:00
|
|
|
source.s.run(&resourceVersion)
|
2014-08-29 02:31:41 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
2015-01-06 19:36:03 +00:00
|
|
|
// should have listed only
|
|
|
|
<-ch
|
|
|
|
if resourceVersion != "" {
|
|
|
|
t.Errorf("unexpected resource version, got %#v", resourceVersion)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-services", "1"}}) {
|
|
|
|
t.Errorf("unexpected actions, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServicesErrorTimeout(t *testing.T) {
|
|
|
|
fakeClient := &client.Fake{Err: errors.New("use of closed network connection")}
|
|
|
|
services := make(chan ServiceUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll)}}
|
2015-01-06 19:36:03 +00:00
|
|
|
resourceVersion := "1"
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-01-11 05:13:32 +00:00
|
|
|
source.s.run(&resourceVersion)
|
2015-01-06 19:36:03 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
2014-08-29 02:31:41 +00:00
|
|
|
// should have listed only
|
|
|
|
<-ch
|
2014-10-07 20:51:28 +00:00
|
|
|
if resourceVersion != "1" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected resource version, got %#v", resourceVersion)
|
|
|
|
}
|
2014-10-07 20:51:28 +00:00
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-services", "1"}}) {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected actions, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServicesFromZeroError(t *testing.T) {
|
|
|
|
fakeClient := &client.Fake{Err: errors.New("test")}
|
|
|
|
services := make(chan ServiceUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll)}}
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := ""
|
2014-08-29 02:31:41 +00:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-01-11 05:13:32 +00:00
|
|
|
source.s.run(&resourceVersion)
|
2014-08-29 02:31:41 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// should have listed only
|
|
|
|
<-ch
|
2014-10-07 20:51:28 +00:00
|
|
|
if resourceVersion != "" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected resource version, got %#v", resourceVersion)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"list-services", nil}}) {
|
|
|
|
t.Errorf("unexpected actions, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 21:14:22 +00:00
|
|
|
func TestEndpoints(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
endpoint := api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "2"}, Endpoints: []string{"127.0.0.1:9000"}}
|
2014-08-15 21:14:22 +00:00
|
|
|
|
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
fakeClient := &client.Fake{Watch: fakeWatch}
|
|
|
|
endpoints := make(chan EndpointsUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll), endpoints: endpoints}}
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := "1"
|
2014-08-15 21:14:22 +00:00
|
|
|
go func() {
|
|
|
|
// called twice
|
2015-01-11 05:13:32 +00:00
|
|
|
source.e.run(&resourceVersion)
|
|
|
|
source.e.run(&resourceVersion)
|
2014-08-15 21:14:22 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// test adding an endpoint to the watch
|
|
|
|
fakeWatch.Add(&endpoint)
|
2014-10-07 20:51:28 +00:00
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-endpoints", "1"}}) {
|
2014-08-15 21:14:22 +00:00
|
|
|
t.Errorf("expected call to watch-endpoints, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := <-endpoints
|
2014-08-29 02:31:41 +00:00
|
|
|
expected := EndpointsUpdate{Op: ADD, Endpoints: []api.Endpoints{endpoint}}
|
2014-08-15 21:14:22 +00:00
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that a delete results in a config change
|
|
|
|
fakeWatch.Delete(&endpoint)
|
|
|
|
actual = <-endpoints
|
2014-08-29 02:31:41 +00:00
|
|
|
expected = EndpointsUpdate{Op: REMOVE, Endpoints: []api.Endpoints{endpoint}}
|
2014-08-15 21:14:22 +00:00
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that closing the channel results in a new call to WatchEndpoints with a higher resource version
|
|
|
|
newFakeWatch := watch.NewFake()
|
|
|
|
fakeClient.Watch = newFakeWatch
|
|
|
|
fakeWatch.Stop()
|
|
|
|
|
|
|
|
newFakeWatch.Add(&endpoint)
|
2014-10-07 22:31:34 +00:00
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-endpoints", "1"}, {"watch-endpoints", "2"}}) {
|
2014-08-15 21:14:22 +00:00
|
|
|
t.Errorf("expected call to watch-endpoints, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
2014-08-29 02:31:41 +00:00
|
|
|
|
|
|
|
func TestEndpointsFromZero(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
endpoint := api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "2"}, Endpoints: []string{"127.0.0.1:9000"}}
|
2014-08-29 02:31:41 +00:00
|
|
|
|
|
|
|
fakeWatch := watch.NewFake()
|
|
|
|
fakeWatch.Stop()
|
|
|
|
fakeClient := &client.Fake{Watch: fakeWatch}
|
|
|
|
fakeClient.EndpointsList = api.EndpointsList{
|
2014-10-23 20:51:34 +00:00
|
|
|
ListMeta: api.ListMeta{ResourceVersion: "2"},
|
2014-08-29 02:31:41 +00:00
|
|
|
Items: []api.Endpoints{
|
|
|
|
endpoint,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
endpoints := make(chan EndpointsUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll), endpoints: endpoints}}
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := ""
|
2014-08-29 02:31:41 +00:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-01-11 05:13:32 +00:00
|
|
|
source.e.run(&resourceVersion)
|
2014-08-29 02:31:41 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// should get endpoints SET
|
|
|
|
actual := <-endpoints
|
|
|
|
expected := EndpointsUpdate{Op: SET, Endpoints: []api.Endpoints{endpoint}}
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should have listed, then watched
|
|
|
|
<-ch
|
2014-10-07 20:51:28 +00:00
|
|
|
if resourceVersion != "2" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected resource version, got %#v", resourceVersion)
|
|
|
|
}
|
2014-10-07 20:51:28 +00:00
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"list-endpoints", nil}, {"watch-endpoints", "2"}}) {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected actions, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpointsError(t *testing.T) {
|
|
|
|
fakeClient := &client.Fake{Err: errors.New("test")}
|
|
|
|
endpoints := make(chan EndpointsUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll), endpoints: endpoints}}
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := "1"
|
2014-08-29 02:31:41 +00:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-01-11 05:13:32 +00:00
|
|
|
source.e.run(&resourceVersion)
|
2015-01-06 19:36:03 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// should have listed only
|
|
|
|
<-ch
|
|
|
|
if resourceVersion != "" {
|
|
|
|
t.Errorf("unexpected resource version, got %#v", resourceVersion)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-endpoints", "1"}}) {
|
|
|
|
t.Errorf("unexpected actions, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpointsErrorTimeout(t *testing.T) {
|
|
|
|
fakeClient := &client.Fake{Err: errors.New("use of closed network connection")}
|
|
|
|
endpoints := make(chan EndpointsUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll), endpoints: endpoints}}
|
2015-01-06 19:36:03 +00:00
|
|
|
resourceVersion := "1"
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-01-11 05:13:32 +00:00
|
|
|
source.e.run(&resourceVersion)
|
2014-08-29 02:31:41 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// should have listed only
|
|
|
|
<-ch
|
2014-10-07 20:51:28 +00:00
|
|
|
if resourceVersion != "1" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected resource version, got %#v", resourceVersion)
|
|
|
|
}
|
2014-10-07 20:51:28 +00:00
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-endpoints", "1"}}) {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected actions, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpointsFromZeroError(t *testing.T) {
|
|
|
|
fakeClient := &client.Fake{Err: errors.New("test")}
|
|
|
|
endpoints := make(chan EndpointsUpdate)
|
2015-01-11 05:13:32 +00:00
|
|
|
source := SourceAPI{
|
|
|
|
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
|
|
|
|
e: endpointsReflector{watcher: fakeClient.Endpoints(api.NamespaceAll), endpoints: endpoints}}
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := ""
|
2014-08-29 02:31:41 +00:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-01-11 05:13:32 +00:00
|
|
|
source.e.run(&resourceVersion)
|
2014-08-29 02:31:41 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// should have listed only
|
|
|
|
<-ch
|
2014-10-07 20:51:28 +00:00
|
|
|
if resourceVersion != "" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("unexpected resource version, got %#v", resourceVersion)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"list-endpoints", nil}}) {
|
|
|
|
t.Errorf("unexpected actions, got %#v", fakeClient)
|
|
|
|
}
|
|
|
|
}
|