...

Source file src/go.etcd.io/etcd/server/v3/proxy/httpproxy/director_test.go

Documentation: go.etcd.io/etcd/server/v3/proxy/httpproxy

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package httpproxy
    16  
    17  import (
    18  	"net/url"
    19  	"reflect"
    20  	"sort"
    21  	"testing"
    22  	"time"
    23  
    24  	"go.uber.org/zap"
    25  )
    26  
    27  func TestNewDirectorScheme(t *testing.T) {
    28  	tests := []struct {
    29  		urls []string
    30  		want []string
    31  	}{
    32  		{
    33  			urls: []string{"http://192.0.2.8:4002", "http://example.com:8080"},
    34  			want: []string{"http://192.0.2.8:4002", "http://example.com:8080"},
    35  		},
    36  		{
    37  			urls: []string{"https://192.0.2.8:4002", "https://example.com:8080"},
    38  			want: []string{"https://192.0.2.8:4002", "https://example.com:8080"},
    39  		},
    40  
    41  		// accept urls without a port
    42  		{
    43  			urls: []string{"http://192.0.2.8"},
    44  			want: []string{"http://192.0.2.8"},
    45  		},
    46  
    47  		// accept urls even if they are garbage
    48  		{
    49  			urls: []string{"http://."},
    50  			want: []string{"http://."},
    51  		},
    52  	}
    53  
    54  	for i, tt := range tests {
    55  		uf := func() []string {
    56  			return tt.urls
    57  		}
    58  		got := newDirector(zap.NewExample(), uf, time.Minute, time.Minute)
    59  
    60  		var gep []string
    61  		for _, ep := range got.ep {
    62  			gep = append(gep, ep.URL.String())
    63  		}
    64  		sort.Strings(tt.want)
    65  		sort.Strings(gep)
    66  		if !reflect.DeepEqual(tt.want, gep) {
    67  			t.Errorf("#%d: want endpoints = %#v, got = %#v", i, tt.want, gep)
    68  		}
    69  	}
    70  }
    71  
    72  func TestDirectorEndpointsFiltering(t *testing.T) {
    73  	d := director{
    74  		ep: []*endpoint{
    75  			{
    76  				URL:       url.URL{Scheme: "http", Host: "192.0.2.5:5050"},
    77  				Available: false,
    78  			},
    79  			{
    80  				URL:       url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
    81  				Available: true,
    82  			},
    83  		},
    84  	}
    85  
    86  	got := d.endpoints()
    87  	want := []*endpoint{
    88  		{
    89  			URL:       url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
    90  			Available: true,
    91  		},
    92  	}
    93  
    94  	if !reflect.DeepEqual(want, got) {
    95  		t.Fatalf("directed to incorrect endpoint: want = %#v, got = %#v", want, got)
    96  	}
    97  }
    98  

View as plain text