674 lines
28 KiB
Go
674 lines
28 KiB
Go
/*
|
|
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 proxy
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
)
|
|
|
|
func TestValidateWorks(t *testing.T) {
|
|
if isValidEndpoint(&hostPortPair{}) {
|
|
t.Errorf("Didn't fail for empty string")
|
|
}
|
|
if isValidEndpoint(&hostPortPair{host: "foobar"}) {
|
|
t.Errorf("Didn't fail with no port")
|
|
}
|
|
if isValidEndpoint(&hostPortPair{host: "foobar", port: -1}) {
|
|
t.Errorf("Didn't fail with a negative port")
|
|
}
|
|
if !isValidEndpoint(&hostPortPair{host: "foobar", port: 8080}) {
|
|
t.Errorf("Failed a valid config.")
|
|
}
|
|
}
|
|
|
|
func TestFilterWorks(t *testing.T) {
|
|
endpoints := []hostPortPair{
|
|
{host: "foobar", port: 1},
|
|
{host: "foobar", port: 2},
|
|
{host: "foobar", port: -1},
|
|
{host: "foobar", port: 3},
|
|
{host: "foobar", port: -2},
|
|
}
|
|
filtered := getValidEndpoints(endpoints)
|
|
|
|
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()
|
|
var endpoints []api.Endpoints
|
|
loadBalancer.OnUpdate(endpoints)
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "http", nil)
|
|
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, port string, expected string, netaddr net.Addr) {
|
|
endpoint, err := loadBalancer.NextEndpoint(service, port, netaddr)
|
|
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 client %v, expected %s, got: %s", service, netaddr, expected, endpoint)
|
|
}
|
|
}
|
|
|
|
func TestLoadBalanceWorksWithSingleEndpoint(t *testing.T) {
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 40}}}},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", "endpoint:40", nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", "endpoint:40", nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", "endpoint:40", nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", "endpoint:40", nil)
|
|
}
|
|
|
|
func stringsInSlice(haystack []string, needles ...string) bool {
|
|
for _, needle := range needles {
|
|
found := false
|
|
for i := range haystack {
|
|
if haystack[i] == needle {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if found == false {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func TestLoadBalanceWorksWithMultipleEndpoints(t *testing.T) {
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 2}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 3}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
shuffledEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:1", "endpoint:2", "endpoint:3") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], nil)
|
|
}
|
|
|
|
func TestLoadBalanceWorksWithMultipleEndpointsAndPorts(t *testing.T) {
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 1},
|
|
{Name: "q", Port: 10},
|
|
}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 2},
|
|
{Name: "q", Port: 20},
|
|
}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 3},
|
|
{Name: "q", Port: 30},
|
|
}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
shuffledEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:1", "endpoint:2", "endpoint:3") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], nil)
|
|
|
|
shuffledEndpoints = loadBalancer.services[makeBalancerKey("foo", "q")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:10", "endpoint:20", "endpoint:30") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[2], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[2], nil)
|
|
}
|
|
|
|
func TestLoadBalanceWorksWithMultipleEndpointsAndUpdates(t *testing.T) {
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 1},
|
|
{Name: "q", Port: 10},
|
|
}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 2},
|
|
{Name: "q", Port: 20},
|
|
}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 3},
|
|
{Name: "q", Port: 30},
|
|
}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
shuffledEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:1", "endpoint:2", "endpoint:3") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], nil)
|
|
|
|
shuffledEndpoints = loadBalancer.services[makeBalancerKey("foo", "q")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:10", "endpoint:20", "endpoint:30") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[2], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[1], nil)
|
|
|
|
// Then update the configuration with one fewer endpoints, make sure
|
|
// we start in the beginning again
|
|
endpoints[0] = api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 8},
|
|
{Name: "q", Port: 80},
|
|
}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 9},
|
|
{Name: "q", Port: 90},
|
|
}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
shuffledEndpoints = loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:8", "endpoint:9") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], nil)
|
|
|
|
shuffledEndpoints = loadBalancer.services[makeBalancerKey("foo", "q")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:80", "endpoint:90") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledEndpoints[1], nil)
|
|
|
|
// Clear endpoints
|
|
endpoints[0] = api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}, Endpoints: []api.Endpoint{}}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
endpoint, err = loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
|
|
endpoint, err = loadBalancer.NextEndpoint("foo", "q", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
}
|
|
|
|
func TestLoadBalanceWorksWithServiceRemoval(t *testing.T) {
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
endpoints := make([]api.Endpoints, 2)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 1},
|
|
{Name: "q", Port: 10},
|
|
}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 2},
|
|
{Name: "q", Port: 20},
|
|
}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 3},
|
|
{Name: "q", Port: 30},
|
|
}},
|
|
},
|
|
}
|
|
endpoints[1] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "bar"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 4},
|
|
{Name: "q", Port: 40},
|
|
}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{
|
|
{Name: "p", Port: 5},
|
|
{Name: "q", Port: 50},
|
|
}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
shuffledFooEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledFooEndpoints, "endpoint:1", "endpoint:2", "endpoint:3") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledFooEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[2], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[1], nil)
|
|
|
|
shuffledFooEndpoints = loadBalancer.services[makeBalancerKey("foo", "q")].endpoints
|
|
if !stringsInSlice(shuffledFooEndpoints, "endpoint:10", "endpoint:20", "endpoint:30") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledFooEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledFooEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledFooEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledFooEndpoints[2], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledFooEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "foo", "q", shuffledFooEndpoints[1], nil)
|
|
|
|
shuffledBarEndpoints := loadBalancer.services[makeBalancerKey("bar", "p")].endpoints
|
|
if !stringsInSlice(shuffledBarEndpoints, "endpoint:4", "endpoint:5") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledBarEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[0], nil)
|
|
|
|
shuffledBarEndpoints = loadBalancer.services[makeBalancerKey("bar", "q")].endpoints
|
|
if !stringsInSlice(shuffledBarEndpoints, "endpoint:40", "endpoint:50") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledBarEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "bar", "q", shuffledBarEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "q", shuffledBarEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "q", shuffledBarEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "q", shuffledBarEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "q", shuffledBarEndpoints[0], nil)
|
|
|
|
// Then update the configuration by removing foo
|
|
loadBalancer.OnUpdate(endpoints[1:])
|
|
endpoint, err = loadBalancer.NextEndpoint("foo", "p", nil)
|
|
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", "q", shuffledBarEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "q", shuffledBarEndpoints[0], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "q", shuffledBarEndpoints[1], nil)
|
|
expectEndpoint(t, loadBalancer, "bar", "q", shuffledBarEndpoints[0], nil)
|
|
}
|
|
|
|
func TestStickyLoadBalanceWorksWithSingleEndpoint(t *testing.T) {
|
|
client1 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}
|
|
client2 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 2), Port: 0}
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
loadBalancer.NewService("foo", "p", api.AffinityTypeClientIP, 0)
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", "endpoint:1", client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", "endpoint:1", client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", "endpoint:1", client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", "endpoint:1", client2)
|
|
}
|
|
|
|
func TestStickyLoadBalanceWorksWithMultipleEndpoints(t *testing.T) {
|
|
client1 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}
|
|
client2 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 2), Port: 0}
|
|
client3 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 3), Port: 0}
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
|
|
loadBalancer.NewService("foo", "p", api.AffinityTypeClientIP, 0)
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 2}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 3}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
shuffledEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:1", "endpoint:2", "endpoint:3") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], client3)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], client3)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
}
|
|
|
|
func TestStickyLoadBalanceWorksWithMultipleEndpointsStickyNone(t *testing.T) {
|
|
client1 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}
|
|
client2 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 2), Port: 0}
|
|
client3 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 3), Port: 0}
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
|
|
loadBalancer.NewService("foo", "p", api.AffinityTypeNone, 0)
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 2}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 3}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
shuffledEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:1", "endpoint:2", "endpoint:3") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client3)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], client3)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client1)
|
|
}
|
|
|
|
func TestStickyLoadBalanceWorksWithMultipleEndpointsRemoveOne(t *testing.T) {
|
|
client1 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}
|
|
client2 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 2), Port: 0}
|
|
client3 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 3), Port: 0}
|
|
client4 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 4), Port: 0}
|
|
client5 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 5), Port: 0}
|
|
client6 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 6), Port: 0}
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
|
|
loadBalancer.NewService("foo", "p", api.AffinityTypeClientIP, 0)
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 2}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 3}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
shuffledEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:1", "endpoint:2", "endpoint:3") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
client1Endpoint := shuffledEndpoints[0]
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
client2Endpoint := shuffledEndpoints[1]
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], client3)
|
|
client3Endpoint := shuffledEndpoints[2]
|
|
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 2}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
shuffledEndpoints = loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if client1Endpoint == "endpoint:3" {
|
|
client1Endpoint = shuffledEndpoints[0]
|
|
} else if client2Endpoint == "endpoint:3" {
|
|
client2Endpoint = shuffledEndpoints[0]
|
|
} else if client3Endpoint == "endpoint:3" {
|
|
client3Endpoint = shuffledEndpoints[0]
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", client1Endpoint, client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", client2Endpoint, client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", client3Endpoint, client3)
|
|
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 2}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 4}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
shuffledEndpoints = loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
expectEndpoint(t, loadBalancer, "foo", "p", client1Endpoint, client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", client2Endpoint, client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", client3Endpoint, client3)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client4)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client5)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], client6)
|
|
}
|
|
|
|
func TestStickyLoadBalanceWorksWithMultipleEndpointsAndUpdates(t *testing.T) {
|
|
client1 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}
|
|
client2 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 2), Port: 0}
|
|
client3 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 3), Port: 0}
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
|
|
loadBalancer.NewService("foo", "p", api.AffinityTypeClientIP, 0)
|
|
endpoints := make([]api.Endpoints, 1)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 2}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 3}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
shuffledEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
if !stringsInSlice(shuffledEndpoints, "endpoint:1", "endpoint:2", "endpoint:3") {
|
|
t.Errorf("did not find expected endpoints: %v", shuffledEndpoints)
|
|
}
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[2], client3)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
// Then update the configuration with one fewer endpoints, make sure
|
|
// we start in the beginning again
|
|
endpoints[0] = api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 4}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 5}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
shuffledEndpoints = loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledEndpoints[1], client2)
|
|
|
|
// Clear endpoints
|
|
endpoints[0] = api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}, Endpoints: []api.Endpoint{}}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
|
|
endpoint, err = loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
}
|
|
|
|
func TestStickyLoadBalanceWorksWithServiceRemoval(t *testing.T) {
|
|
client1 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}
|
|
client2 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 2), Port: 0}
|
|
client3 := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 3), Port: 0}
|
|
loadBalancer := NewLoadBalancerRR()
|
|
endpoint, err := loadBalancer.NextEndpoint("foo", "p", nil)
|
|
if err == nil || len(endpoint) != 0 {
|
|
t.Errorf("Didn't fail with non-existent service")
|
|
}
|
|
loadBalancer.NewService("foo", "p", api.AffinityTypeClientIP, 0)
|
|
endpoints := make([]api.Endpoints, 2)
|
|
endpoints[0] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 1}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 2}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 3}}},
|
|
},
|
|
}
|
|
loadBalancer.NewService("bar", "p", api.AffinityTypeClientIP, 0)
|
|
endpoints[1] = api.Endpoints{
|
|
ObjectMeta: api.ObjectMeta{Name: "bar"},
|
|
Endpoints: []api.Endpoint{
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 5}}},
|
|
{IP: "endpoint", Ports: []api.EndpointPort{{Name: "p", Port: 6}}},
|
|
},
|
|
}
|
|
loadBalancer.OnUpdate(endpoints)
|
|
shuffledFooEndpoints := loadBalancer.services[makeBalancerKey("foo", "p")].endpoints
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[2], client3)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[2], client3)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[1], client2)
|
|
|
|
shuffledBarEndpoints := loadBalancer.services[makeBalancerKey("bar", "p")].endpoints
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "foo", "p", shuffledFooEndpoints[0], client1)
|
|
|
|
// Then update the configuration by removing foo
|
|
loadBalancer.OnUpdate(endpoints[1:])
|
|
endpoint, err = loadBalancer.NextEndpoint("foo", "p", nil)
|
|
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.
|
|
shuffledBarEndpoints = loadBalancer.services[makeBalancerKey("bar", "p")].endpoints
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[1], client2)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[0], client1)
|
|
expectEndpoint(t, loadBalancer, "bar", "p", shuffledBarEndpoints[0], client1)
|
|
}
|