github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/proxy/config/config_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  
    17  package config
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
    24  )
    25  
    26  const TomcatPort int = 8080
    27  const TomcatName = "tomcat"
    28  
    29  var TomcatEndpoints = map[string]string{"c0": "1.1.1.1:18080", "c1": "2.2.2.2:18081"}
    30  
    31  const MysqlPort int = 3306
    32  const MysqlName = "mysql"
    33  
    34  var MysqlEndpoints = map[string]string{"c0": "1.1.1.1:13306", "c3": "2.2.2.2:13306"}
    35  
    36  type ServiceHandlerMock struct {
    37  	services []api.Service
    38  }
    39  
    40  func NewServiceHandlerMock() ServiceHandlerMock {
    41  	return ServiceHandlerMock{services: make([]api.Service, 0)}
    42  }
    43  
    44  func (impl ServiceHandlerMock) OnUpdate(services []api.Service) {
    45  	impl.services = services
    46  }
    47  
    48  func (impl ServiceHandlerMock) ValidateServices(t *testing.T, expectedServices []api.Service) {
    49  	if reflect.DeepEqual(impl.services, expectedServices) {
    50  		t.Errorf("Services don't match %+v expected: %+v", impl.services, expectedServices)
    51  	}
    52  }
    53  
    54  type EndpointsHandlerMock struct {
    55  	endpoints []api.Endpoints
    56  }
    57  
    58  func NewEndpointsHandlerMock() EndpointsHandlerMock {
    59  	return EndpointsHandlerMock{endpoints: make([]api.Endpoints, 0)}
    60  }
    61  
    62  func (impl EndpointsHandlerMock) OnUpdate(endpoints []api.Endpoints) {
    63  	impl.endpoints = endpoints
    64  }
    65  
    66  func (impl EndpointsHandlerMock) ValidateEndpoints(t *testing.T, expectedEndpoints []api.Endpoints) {
    67  	if reflect.DeepEqual(impl.endpoints, expectedEndpoints) {
    68  		t.Errorf("Endpoints don't match %+v", impl.endpoints, expectedEndpoints)
    69  	}
    70  }
    71  
    72  func CreateServiceUpdate(op Operation, services ...api.Service) ServiceUpdate {
    73  	ret := ServiceUpdate{Op: op}
    74  	ret.Services = make([]api.Service, len(services))
    75  	for i, value := range services {
    76  		ret.Services[i] = value
    77  	}
    78  	return ret
    79  }
    80  
    81  func CreateEndpointsUpdate(op Operation, endpoints ...api.Endpoints) EndpointsUpdate {
    82  	ret := EndpointsUpdate{Op: op}
    83  	ret.Endpoints = make([]api.Endpoints, len(endpoints))
    84  	for i, value := range endpoints {
    85  		ret.Endpoints[i] = value
    86  	}
    87  	return ret
    88  }
    89  
    90  func TestServiceConfigurationChannels(t *testing.T) {
    91  	config := NewServiceConfig()
    92  	channelOne := config.GetServiceConfigurationChannel("one")
    93  	if channelOne != config.GetServiceConfigurationChannel("one") {
    94  		t.Error("Didn't get the same service configuration channel back with the same name")
    95  	}
    96  	channelTwo := config.GetServiceConfigurationChannel("two")
    97  	if channelOne == channelTwo {
    98  		t.Error("Got back the same service configuration channel for different names")
    99  	}
   100  }
   101  
   102  func TestEndpointConfigurationChannels(t *testing.T) {
   103  	config := NewServiceConfig()
   104  	channelOne := config.GetEndpointsConfigurationChannel("one")
   105  	if channelOne != config.GetEndpointsConfigurationChannel("one") {
   106  		t.Error("Didn't get the same endpoint configuration channel back with the same name")
   107  	}
   108  	channelTwo := config.GetEndpointsConfigurationChannel("two")
   109  	if channelOne == channelTwo {
   110  		t.Error("Got back the same endpoint configuration channel for different names")
   111  	}
   112  }
   113  
   114  func TestNewServiceAddedAndNotified(t *testing.T) {
   115  	config := NewServiceConfig()
   116  	channel := config.GetServiceConfigurationChannel("one")
   117  	handler := NewServiceHandlerMock()
   118  	config.RegisterServiceHandler(&handler)
   119  	serviceUpdate := CreateServiceUpdate(ADD, api.Service{JSONBase: api.JSONBase{ID: "foo"}, Port: 10})
   120  	channel <- serviceUpdate
   121  	handler.ValidateServices(t, serviceUpdate.Services)
   122  
   123  }
   124  
   125  func TestServiceAddedRemovedSetAndNotified(t *testing.T) {
   126  	config := NewServiceConfig()
   127  	channel := config.GetServiceConfigurationChannel("one")
   128  	handler := NewServiceHandlerMock()
   129  	config.RegisterServiceHandler(&handler)
   130  	serviceUpdate := CreateServiceUpdate(ADD, api.Service{JSONBase: api.JSONBase{ID: "foo"}, Port: 10})
   131  	channel <- serviceUpdate
   132  	handler.ValidateServices(t, serviceUpdate.Services)
   133  
   134  	serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{JSONBase: api.JSONBase{ID: "bar"}, Port: 20})
   135  	channel <- serviceUpdate2
   136  	services := []api.Service{serviceUpdate.Services[0], serviceUpdate2.Services[0]}
   137  	handler.ValidateServices(t, services)
   138  
   139  	serviceUpdate3 := CreateServiceUpdate(REMOVE, api.Service{JSONBase: api.JSONBase{ID: "foo"}})
   140  	channel <- serviceUpdate3
   141  	services = []api.Service{serviceUpdate2.Services[0]}
   142  	handler.ValidateServices(t, services)
   143  
   144  	serviceUpdate4 := CreateServiceUpdate(SET, api.Service{JSONBase: api.JSONBase{ID: "foobar"}, Port: 99})
   145  	channel <- serviceUpdate4
   146  	services = []api.Service{serviceUpdate4.Services[0]}
   147  	handler.ValidateServices(t, services)
   148  }
   149  
   150  func TestNewMultipleSourcesServicesAddedAndNotified(t *testing.T) {
   151  	config := NewServiceConfig()
   152  	channelOne := config.GetServiceConfigurationChannel("one")
   153  	channelTwo := config.GetServiceConfigurationChannel("two")
   154  	if channelOne == channelTwo {
   155  		t.Error("Same channel handed back for one and two")
   156  	}
   157  	handler := NewServiceHandlerMock()
   158  	config.RegisterServiceHandler(handler)
   159  	serviceUpdate1 := CreateServiceUpdate(ADD, api.Service{JSONBase: api.JSONBase{ID: "foo"}, Port: 10})
   160  	serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{JSONBase: api.JSONBase{ID: "bar"}, Port: 20})
   161  	channelOne <- serviceUpdate1
   162  	channelTwo <- serviceUpdate2
   163  	services := []api.Service{serviceUpdate1.Services[0], serviceUpdate2.Services[0]}
   164  	handler.ValidateServices(t, services)
   165  }
   166  
   167  func TestNewMultipleSourcesServicesMultipleHandlersAddedAndNotified(t *testing.T) {
   168  	config := NewServiceConfig()
   169  	channelOne := config.GetServiceConfigurationChannel("one")
   170  	channelTwo := config.GetServiceConfigurationChannel("two")
   171  	handler := NewServiceHandlerMock()
   172  	handler2 := NewServiceHandlerMock()
   173  	config.RegisterServiceHandler(handler)
   174  	config.RegisterServiceHandler(handler2)
   175  	serviceUpdate1 := CreateServiceUpdate(ADD, api.Service{JSONBase: api.JSONBase{ID: "foo"}, Port: 10})
   176  	serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{JSONBase: api.JSONBase{ID: "bar"}, Port: 20})
   177  	channelOne <- serviceUpdate1
   178  	channelTwo <- serviceUpdate2
   179  	services := []api.Service{serviceUpdate1.Services[0], serviceUpdate2.Services[0]}
   180  	handler.ValidateServices(t, services)
   181  	handler2.ValidateServices(t, services)
   182  }
   183  
   184  func TestNewMultipleSourcesEndpointsMultipleHandlersAddedAndNotified(t *testing.T) {
   185  	config := NewServiceConfig()
   186  	channelOne := config.GetEndpointsConfigurationChannel("one")
   187  	channelTwo := config.GetEndpointsConfigurationChannel("two")
   188  	handler := NewEndpointsHandlerMock()
   189  	handler2 := NewEndpointsHandlerMock()
   190  	config.RegisterEndpointsHandler(handler)
   191  	config.RegisterEndpointsHandler(handler2)
   192  	endpointsUpdate1 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "foo", Endpoints: []string{"endpoint1", "endpoint2"}})
   193  	endpointsUpdate2 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "bar", Endpoints: []string{"endpoint3", "endpoint4"}})
   194  	channelOne <- endpointsUpdate1
   195  	channelTwo <- endpointsUpdate2
   196  
   197  	endpoints := []api.Endpoints{endpointsUpdate1.Endpoints[0], endpointsUpdate2.Endpoints[0]}
   198  	handler.ValidateEndpoints(t, endpoints)
   199  	handler2.ValidateEndpoints(t, endpoints)
   200  }
   201  
   202  func TestNewMultipleSourcesEndpointsMultipleHandlersAddRemoveSetAndNotified(t *testing.T) {
   203  	config := NewServiceConfig()
   204  	channelOne := config.GetEndpointsConfigurationChannel("one")
   205  	channelTwo := config.GetEndpointsConfigurationChannel("two")
   206  	handler := NewEndpointsHandlerMock()
   207  	handler2 := NewEndpointsHandlerMock()
   208  	config.RegisterEndpointsHandler(handler)
   209  	config.RegisterEndpointsHandler(handler2)
   210  	endpointsUpdate1 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "foo", Endpoints: []string{"endpoint1", "endpoint2"}})
   211  	endpointsUpdate2 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "bar", Endpoints: []string{"endpoint3", "endpoint4"}})
   212  	channelOne <- endpointsUpdate1
   213  	channelTwo <- endpointsUpdate2
   214  
   215  	endpoints := []api.Endpoints{endpointsUpdate1.Endpoints[0], endpointsUpdate2.Endpoints[0]}
   216  	handler.ValidateEndpoints(t, endpoints)
   217  	handler2.ValidateEndpoints(t, endpoints)
   218  
   219  	// Add one more
   220  	endpointsUpdate3 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "foobar", Endpoints: []string{"endpoint5", "endpoint6"}})
   221  	channelTwo <- endpointsUpdate3
   222  	endpoints = []api.Endpoints{endpointsUpdate1.Endpoints[0], endpointsUpdate2.Endpoints[0], endpointsUpdate3.Endpoints[0]}
   223  	handler.ValidateEndpoints(t, endpoints)
   224  	handler2.ValidateEndpoints(t, endpoints)
   225  
   226  	// Update the "foo" service with new endpoints
   227  	endpointsUpdate1 = CreateEndpointsUpdate(ADD, api.Endpoints{Name: "foo", Endpoints: []string{"endpoint77"}})
   228  	channelOne <- endpointsUpdate1
   229  	endpoints = []api.Endpoints{endpointsUpdate1.Endpoints[0], endpointsUpdate2.Endpoints[0], endpointsUpdate3.Endpoints[0]}
   230  	handler.ValidateEndpoints(t, endpoints)
   231  	handler2.ValidateEndpoints(t, endpoints)
   232  
   233  	// Remove "bar" service
   234  	endpointsUpdate2 = CreateEndpointsUpdate(REMOVE, api.Endpoints{Name: "bar"})
   235  	channelTwo <- endpointsUpdate2
   236  
   237  	endpoints = []api.Endpoints{endpointsUpdate1.Endpoints[0], endpointsUpdate3.Endpoints[0]}
   238  	handler.ValidateEndpoints(t, endpoints)
   239  	handler2.ValidateEndpoints(t, endpoints)
   240  }