...

Source file src/go.etcd.io/etcd/client/v3/internal/endpoint/endpoint_test.go

Documentation: go.etcd.io/etcd/client/v3/internal/endpoint

     1  // Copyright 2021 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 endpoint
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func Test_interpret(t *testing.T) {
    22  	tests := []struct {
    23  		endpoint          string
    24  		wantAddress       string
    25  		wantServerName    string
    26  		wantRequiresCreds CredsRequirement
    27  	}{
    28  		{"127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
    29  		{"localhost", "localhost", "localhost", CREDS_OPTIONAL},
    30  		{"localhost:8080", "localhost:8080", "localhost:8080", CREDS_OPTIONAL},
    31  
    32  		{"unix:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
    33  		{"unix:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_OPTIONAL},
    34  
    35  		{"unix://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
    36  		{"unix://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_OPTIONAL},
    37  
    38  		{"unixs:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
    39  		{"unixs:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
    40  		{"unixs://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
    41  		{"unixs://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
    42  
    43  		{"http://127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_DROP},
    44  		{"http://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CREDS_DROP},
    45  		{"https://127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
    46  		{"https://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
    47  		{"https://localhost:20000", "localhost:20000", "localhost:20000", CREDS_REQUIRE},
    48  
    49  		{"unix:///tmp/abc", "unix:///tmp/abc", "abc", CREDS_OPTIONAL},
    50  		{"unixs:///tmp/abc", "unix:///tmp/abc", "abc", CREDS_REQUIRE},
    51  		{"unix:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CREDS_OPTIONAL},
    52  		{"unixs:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CREDS_REQUIRE},
    53  		{"etcd.io", "etcd.io", "etcd.io", CREDS_OPTIONAL},
    54  		{"http://etcd.io/abc", "etcd.io", "etcd.io", CREDS_DROP},
    55  		{"dns://something-other", "dns://something-other", "something-other", CREDS_OPTIONAL},
    56  
    57  		{"http://[2001:db8:1f70::999:de8:7648:6e8]:100/", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CREDS_DROP},
    58  		{"[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CREDS_OPTIONAL},
    59  		{"unix:unexpected-file_name#123$456", "unix:unexpected-file_name#123$456", "unexpected-file_name#123$456", CREDS_OPTIONAL},
    60  	}
    61  	for _, tt := range tests {
    62  		t.Run("Interpret_"+tt.endpoint, func(t *testing.T) {
    63  			gotAddress, gotServerName := Interpret(tt.endpoint)
    64  			if gotAddress != tt.wantAddress {
    65  				t.Errorf("Interpret() gotAddress = %v, want %v", gotAddress, tt.wantAddress)
    66  			}
    67  			if gotServerName != tt.wantServerName {
    68  				t.Errorf("Interpret() gotServerName = %v, want %v", gotServerName, tt.wantServerName)
    69  			}
    70  		})
    71  		t.Run("RequiresCredentials_"+tt.endpoint, func(t *testing.T) {
    72  			requiresCreds := RequiresCredentials(tt.endpoint)
    73  			if requiresCreds != tt.wantRequiresCreds {
    74  				t.Errorf("RequiresCredentials() got = %v, want %v", requiresCreds, tt.wantRequiresCreds)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func Test_extractHostFromHostPort(t *testing.T) {
    81  	tests := []struct {
    82  		ep   string
    83  		want string
    84  	}{
    85  		{ep: "localhost", want: "localhost"},
    86  		{ep: "localhost:8080", want: "localhost"},
    87  		{ep: "192.158.7.14:8080", want: "192.158.7.14"},
    88  		{ep: "192.158.7.14:8080", want: "192.158.7.14"},
    89  		{ep: "[2001:db8:1f70::999:de8:7648:6e8]", want: "[2001:db8:1f70::999:de8:7648:6e8]"},
    90  		{ep: "[2001:db8:1f70::999:de8:7648:6e8]:100", want: "2001:db8:1f70::999:de8:7648:6e8"},
    91  	}
    92  	for _, tt := range tests {
    93  		t.Run(tt.ep, func(t *testing.T) {
    94  			if got := extractHostFromHostPort(tt.ep); got != tt.want {
    95  				t.Errorf("extractHostFromHostPort() = %v, want %v", got, tt.want)
    96  			}
    97  		})
    98  	}
    99  }
   100  

View as plain text