2014-06-06 23:40:48 +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.
|
|
|
|
*/
|
2014-06-23 18:32:11 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
)
|
|
|
|
|
2014-08-14 18:09:08 +00:00
|
|
|
func TestValidateWorks(t *testing.T) {
|
|
|
|
if isValidEndpoint("") {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Didn't fail for empty string")
|
|
|
|
}
|
2014-08-14 18:09:08 +00:00
|
|
|
if isValidEndpoint("foobar") {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Didn't fail with no port")
|
|
|
|
}
|
2014-08-14 18:09:08 +00:00
|
|
|
if isValidEndpoint("foobar:-1") {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Didn't fail with a negative port")
|
|
|
|
}
|
2014-08-14 18:09:08 +00:00
|
|
|
if !isValidEndpoint("foobar:8080") {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Failed a valid config.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 18:09:08 +00:00
|
|
|
func TestFilterWorks(t *testing.T) {
|
2014-06-06 23:40:48 +00:00
|
|
|
endpoints := []string{"foobar:1", "foobar:2", "foobar:-1", "foobar:3", "foobar:-2"}
|
2014-08-14 18:09:08 +00:00
|
|
|
filtered := filterValidEndpoints(endpoints)
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
if len(filtered) != 3 {
|
|
|
|
t.Errorf("Failed to filter to the correct size")
|
|
|
|
}
|
|
|
|
if filtered[0] != "foobar:1" {
|
|
|
|
t.Errorf("Index zero is not foobar:1")
|
|
|
|
}
|
|
|
|
if filtered[1] != "foobar:2" {
|
|
|
|
t.Errorf("Index one is not foobar:2")
|
|
|
|
}
|
|
|
|
if filtered[2] != "foobar:3" {
|
|
|
|
t.Errorf("Index two is not foobar:3")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadBalanceFailsWithNoEndpoints(t *testing.T) {
|
|
|
|
loadBalancer := NewLoadBalancerRR()
|
2014-07-11 05:36:06 +00:00
|
|
|
var endpoints []api.Endpoints
|
2014-06-06 23:40:48 +00:00
|
|
|
loadBalancer.OnUpdate(endpoints)
|
2014-08-03 19:23:15 +00:00
|
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
|
|
}
|
|
|
|
if len(endpoint) != 0 {
|
|
|
|
t.Errorf("Got an endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectEndpoint(t *testing.T, loadBalancer *LoadBalancerRR, service string, expected string) {
|
2014-08-03 19:23:15 +00:00
|
|
|
endpoint, err := loadBalancer.NextEndpoint(service, nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Didn't find a service for %s, expected %s, failed with: %v", service, expected, err)
|
|
|
|
}
|
|
|
|
if endpoint != expected {
|
|
|
|
t.Errorf("Didn't get expected endpoint for service %s, expected %s, got: %s", service, expected, endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadBalanceWorksWithSingleEndpoint(t *testing.T) {
|
|
|
|
loadBalancer := NewLoadBalancerRR()
|
2014-08-03 19:23:15 +00:00
|
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err == nil || len(endpoint) != 0 {
|
|
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
|
|
}
|
|
|
|
endpoints := make([]api.Endpoints, 1)
|
2014-07-23 01:53:41 +00:00
|
|
|
endpoints[0] = api.Endpoints{
|
2014-10-22 17:02:02 +00:00
|
|
|
TypeMeta: api.TypeMeta{Name: "foo"},
|
2014-07-23 01:53:41 +00:00
|
|
|
Endpoints: []string{"endpoint1:40"},
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint1:40")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint1:40")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint1:40")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint1:40")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadBalanceWorksWithMultipleEndpoints(t *testing.T) {
|
|
|
|
loadBalancer := NewLoadBalancerRR()
|
2014-08-03 19:23:15 +00:00
|
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err == nil || len(endpoint) != 0 {
|
|
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
|
|
}
|
|
|
|
endpoints := make([]api.Endpoints, 1)
|
2014-07-23 01:53:41 +00:00
|
|
|
endpoints[0] = api.Endpoints{
|
2014-10-22 17:02:02 +00:00
|
|
|
TypeMeta: api.TypeMeta{Name: "foo"},
|
2014-07-23 01:53:41 +00:00
|
|
|
Endpoints: []string{"endpoint:1", "endpoint:2", "endpoint:3"},
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:3")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadBalanceWorksWithMultipleEndpointsAndUpdates(t *testing.T) {
|
|
|
|
loadBalancer := NewLoadBalancerRR()
|
2014-08-03 19:23:15 +00:00
|
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err == nil || len(endpoint) != 0 {
|
|
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
|
|
}
|
|
|
|
endpoints := make([]api.Endpoints, 1)
|
2014-07-23 01:53:41 +00:00
|
|
|
endpoints[0] = api.Endpoints{
|
2014-10-22 17:02:02 +00:00
|
|
|
TypeMeta: api.TypeMeta{Name: "foo"},
|
2014-07-23 01:53:41 +00:00
|
|
|
Endpoints: []string{"endpoint:1", "endpoint:2", "endpoint:3"},
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:3")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
|
|
|
|
// Then update the configuration with one fewer endpoints, make sure
|
|
|
|
// we start in the beginning again
|
2014-10-22 17:02:02 +00:00
|
|
|
endpoints[0] = api.Endpoints{TypeMeta: api.TypeMeta{Name: "foo"},
|
2014-07-23 01:53:41 +00:00
|
|
|
Endpoints: []string{"endpoint:8", "endpoint:9"},
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:8")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:9")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:8")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:9")
|
|
|
|
// Clear endpoints
|
2014-10-22 17:02:02 +00:00
|
|
|
endpoints[0] = api.Endpoints{TypeMeta: api.TypeMeta{Name: "foo"}, Endpoints: []string{}}
|
2014-06-06 23:40:48 +00:00
|
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
|
2014-08-03 19:23:15 +00:00
|
|
|
endpoint, err = loadBalancer.NextEndpoint("foo", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err == nil || len(endpoint) != 0 {
|
|
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadBalanceWorksWithServiceRemoval(t *testing.T) {
|
|
|
|
loadBalancer := NewLoadBalancerRR()
|
2014-08-03 19:23:15 +00:00
|
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err == nil || len(endpoint) != 0 {
|
|
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
|
|
}
|
|
|
|
endpoints := make([]api.Endpoints, 2)
|
2014-07-23 01:53:41 +00:00
|
|
|
endpoints[0] = api.Endpoints{
|
2014-10-22 17:02:02 +00:00
|
|
|
TypeMeta: api.TypeMeta{Name: "foo"},
|
2014-07-23 01:53:41 +00:00
|
|
|
Endpoints: []string{"endpoint:1", "endpoint:2", "endpoint:3"},
|
|
|
|
}
|
|
|
|
endpoints[1] = api.Endpoints{
|
2014-10-22 17:02:02 +00:00
|
|
|
TypeMeta: api.TypeMeta{Name: "bar"},
|
2014-07-23 01:53:41 +00:00
|
|
|
Endpoints: []string{"endpoint:4", "endpoint:5"},
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:3")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
|
|
|
|
expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
|
|
|
|
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:5")
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:5")
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
|
|
|
|
|
|
|
|
// Then update the configuration by removing foo
|
|
|
|
loadBalancer.OnUpdate(endpoints[1:])
|
2014-08-03 19:23:15 +00:00
|
|
|
endpoint, err = loadBalancer.NextEndpoint("foo", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err == nil || len(endpoint) != 0 {
|
|
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// but bar is still there, and we continue RR from where we left off.
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:5")
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:5")
|
|
|
|
expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
|
|
|
|
}
|