github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/proxy/roundrobbin_test.go (about)

     1  /*
     2  Copyright 2014 Google Inc. All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package proxy
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
    22  )
    23  
    24  func TestLoadBalanceValidateWorks(t *testing.T) {
    25  	loadBalancer := NewLoadBalancerRR()
    26  	if loadBalancer.IsValid("") {
    27  		t.Errorf("Didn't fail for empty string")
    28  	}
    29  	if loadBalancer.IsValid("foobar") {
    30  		t.Errorf("Didn't fail with no port")
    31  	}
    32  	if loadBalancer.IsValid("foobar:-1") {
    33  		t.Errorf("Didn't fail with a negative port")
    34  	}
    35  	if !loadBalancer.IsValid("foobar:8080") {
    36  		t.Errorf("Failed a valid config.")
    37  	}
    38  }
    39  
    40  func TestLoadBalanceFilterWorks(t *testing.T) {
    41  	loadBalancer := NewLoadBalancerRR()
    42  	endpoints := []string{"foobar:1", "foobar:2", "foobar:-1", "foobar:3", "foobar:-2"}
    43  	filtered := loadBalancer.FilterValidEndpoints(endpoints)
    44  
    45  	if len(filtered) != 3 {
    46  		t.Errorf("Failed to filter to the correct size")
    47  	}
    48  	if filtered[0] != "foobar:1" {
    49  		t.Errorf("Index zero is not foobar:1")
    50  	}
    51  	if filtered[1] != "foobar:2" {
    52  		t.Errorf("Index one is not foobar:2")
    53  	}
    54  	if filtered[2] != "foobar:3" {
    55  		t.Errorf("Index two is not foobar:3")
    56  	}
    57  }
    58  
    59  func TestLoadBalanceFailsWithNoEndpoints(t *testing.T) {
    60  	loadBalancer := NewLoadBalancerRR()
    61  	endpoints := make([]api.Endpoints, 0)
    62  	loadBalancer.OnUpdate(endpoints)
    63  	endpoint, err := loadBalancer.LoadBalance("foo", nil)
    64  	if err == nil {
    65  		t.Errorf("Didn't fail with non-existent service")
    66  	}
    67  	if len(endpoint) != 0 {
    68  		t.Errorf("Got an endpoint")
    69  	}
    70  }
    71  
    72  func expectEndpoint(t *testing.T, loadBalancer *LoadBalancerRR, service string, expected string) {
    73  	endpoint, err := loadBalancer.LoadBalance(service, nil)
    74  	if err != nil {
    75  		t.Errorf("Didn't find a service for %s, expected %s, failed with: %v", service, expected, err)
    76  	}
    77  	if endpoint != expected {
    78  		t.Errorf("Didn't get expected endpoint for service %s, expected %s, got: %s", service, expected, endpoint)
    79  	}
    80  }
    81  
    82  func TestLoadBalanceWorksWithSingleEndpoint(t *testing.T) {
    83  	loadBalancer := NewLoadBalancerRR()
    84  	endpoint, err := loadBalancer.LoadBalance("foo", nil)
    85  	if err == nil || len(endpoint) != 0 {
    86  		t.Errorf("Didn't fail with non-existent service")
    87  	}
    88  	endpoints := make([]api.Endpoints, 1)
    89  	endpoints[0] = api.Endpoints{Name: "foo", Endpoints: []string{"endpoint1:40"}}
    90  	loadBalancer.OnUpdate(endpoints)
    91  	expectEndpoint(t, loadBalancer, "foo", "endpoint1:40")
    92  	expectEndpoint(t, loadBalancer, "foo", "endpoint1:40")
    93  	expectEndpoint(t, loadBalancer, "foo", "endpoint1:40")
    94  	expectEndpoint(t, loadBalancer, "foo", "endpoint1:40")
    95  }
    96  
    97  func TestLoadBalanceWorksWithMultipleEndpoints(t *testing.T) {
    98  	loadBalancer := NewLoadBalancerRR()
    99  	endpoint, err := loadBalancer.LoadBalance("foo", nil)
   100  	if err == nil || len(endpoint) != 0 {
   101  		t.Errorf("Didn't fail with non-existent service")
   102  	}
   103  	endpoints := make([]api.Endpoints, 1)
   104  	endpoints[0] = api.Endpoints{Name: "foo", Endpoints: []string{"endpoint:1", "endpoint:2", "endpoint:3"}}
   105  	loadBalancer.OnUpdate(endpoints)
   106  	expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
   107  	expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
   108  	expectEndpoint(t, loadBalancer, "foo", "endpoint:3")
   109  	expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
   110  }
   111  
   112  func TestLoadBalanceWorksWithMultipleEndpointsAndUpdates(t *testing.T) {
   113  	loadBalancer := NewLoadBalancerRR()
   114  	endpoint, err := loadBalancer.LoadBalance("foo", nil)
   115  	if err == nil || len(endpoint) != 0 {
   116  		t.Errorf("Didn't fail with non-existent service")
   117  	}
   118  	endpoints := make([]api.Endpoints, 1)
   119  	endpoints[0] = api.Endpoints{Name: "foo", Endpoints: []string{"endpoint:1", "endpoint:2", "endpoint:3"}}
   120  	loadBalancer.OnUpdate(endpoints)
   121  	expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
   122  	expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
   123  	expectEndpoint(t, loadBalancer, "foo", "endpoint:3")
   124  	expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
   125  	expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
   126  	// Then update the configuration with one fewer endpoints, make sure
   127  	// we start in the beginning again
   128  	endpoints[0] = api.Endpoints{Name: "foo", Endpoints: []string{"endpoint:8", "endpoint:9"}}
   129  	loadBalancer.OnUpdate(endpoints)
   130  	expectEndpoint(t, loadBalancer, "foo", "endpoint:8")
   131  	expectEndpoint(t, loadBalancer, "foo", "endpoint:9")
   132  	expectEndpoint(t, loadBalancer, "foo", "endpoint:8")
   133  	expectEndpoint(t, loadBalancer, "foo", "endpoint:9")
   134  	// Clear endpoints
   135  	endpoints[0] = api.Endpoints{Name: "foo", Endpoints: []string{}}
   136  	loadBalancer.OnUpdate(endpoints)
   137  
   138  	endpoint, err = loadBalancer.LoadBalance("foo", nil)
   139  	if err == nil || len(endpoint) != 0 {
   140  		t.Errorf("Didn't fail with non-existent service")
   141  	}
   142  }
   143  
   144  func TestLoadBalanceWorksWithServiceRemoval(t *testing.T) {
   145  	loadBalancer := NewLoadBalancerRR()
   146  	endpoint, err := loadBalancer.LoadBalance("foo", nil)
   147  	if err == nil || len(endpoint) != 0 {
   148  		t.Errorf("Didn't fail with non-existent service")
   149  	}
   150  	endpoints := make([]api.Endpoints, 2)
   151  	endpoints[0] = api.Endpoints{Name: "foo", Endpoints: []string{"endpoint:1", "endpoint:2", "endpoint:3"}}
   152  	endpoints[1] = api.Endpoints{Name: "bar", Endpoints: []string{"endpoint:4", "endpoint:5"}}
   153  	loadBalancer.OnUpdate(endpoints)
   154  	expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
   155  	expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
   156  	expectEndpoint(t, loadBalancer, "foo", "endpoint:3")
   157  	expectEndpoint(t, loadBalancer, "foo", "endpoint:1")
   158  	expectEndpoint(t, loadBalancer, "foo", "endpoint:2")
   159  
   160  	expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
   161  	expectEndpoint(t, loadBalancer, "bar", "endpoint:5")
   162  	expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
   163  	expectEndpoint(t, loadBalancer, "bar", "endpoint:5")
   164  	expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
   165  
   166  	// Then update the configuration by removing foo
   167  	loadBalancer.OnUpdate(endpoints[1:])
   168  	endpoint, err = loadBalancer.LoadBalance("foo", nil)
   169  	if err == nil || len(endpoint) != 0 {
   170  		t.Errorf("Didn't fail with non-existent service")
   171  	}
   172  
   173  	// but bar is still there, and we continue RR from where we left off.
   174  	expectEndpoint(t, loadBalancer, "bar", "endpoint:5")
   175  	expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
   176  	expectEndpoint(t, loadBalancer, "bar", "endpoint:5")
   177  	expectEndpoint(t, loadBalancer, "bar", "endpoint:4")
   178  }