...

Source file src/go.etcd.io/etcd/pkg/v3/flags/urls_test.go

Documentation: go.etcd.io/etcd/pkg/v3/flags

     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 flags
    16  
    17  import (
    18  	"net/url"
    19  	"reflect"
    20  	"testing"
    21  )
    22  
    23  func TestValidateURLsValueBad(t *testing.T) {
    24  	tests := []string{
    25  		// bad IP specification
    26  		":2379",
    27  		"127.0:8080",
    28  		"123:456",
    29  		// bad port specification
    30  		"127.0.0.1:foo",
    31  		"127.0.0.1:",
    32  		// bad strings
    33  		"somewhere",
    34  		"234#$",
    35  		"file://foo/bar",
    36  		"http://hello/asdf",
    37  		"http://10.1.1.1",
    38  	}
    39  	for i, in := range tests {
    40  		u := URLsValue{}
    41  		if err := u.Set(in); err == nil {
    42  			t.Errorf(`#%d: unexpected nil error for in=%q`, i, in)
    43  		}
    44  	}
    45  }
    46  
    47  func TestNewURLsValue(t *testing.T) {
    48  	tests := []struct {
    49  		s   string
    50  		exp []url.URL
    51  	}{
    52  		{s: "https://1.2.3.4:8080", exp: []url.URL{{Scheme: "https", Host: "1.2.3.4:8080"}}},
    53  		{s: "http://10.1.1.1:80", exp: []url.URL{{Scheme: "http", Host: "10.1.1.1:80"}}},
    54  		{s: "http://localhost:80", exp: []url.URL{{Scheme: "http", Host: "localhost:80"}}},
    55  		{s: "http://:80", exp: []url.URL{{Scheme: "http", Host: ":80"}}},
    56  		{s: "unix://tmp/etcd.sock", exp: []url.URL{{Scheme: "unix", Host: "tmp", Path: "/etcd.sock"}}},
    57  		{s: "unix:///tmp/127.27.84.4:23432", exp: []url.URL{{Scheme: "unix", Path: "/tmp/127.27.84.4:23432"}}},
    58  		{s: "unix://127.0.0.5:1456", exp: []url.URL{{Scheme: "unix", Host: "127.0.0.5:1456"}}},
    59  		{
    60  			s: "http://localhost:1,https://localhost:2",
    61  			exp: []url.URL{
    62  				{Scheme: "http", Host: "localhost:1"},
    63  				{Scheme: "https", Host: "localhost:2"},
    64  			},
    65  		},
    66  	}
    67  	for i := range tests {
    68  		uu := []url.URL(*NewURLsValue(tests[i].s))
    69  		if !reflect.DeepEqual(tests[i].exp, uu) {
    70  			t.Fatalf("#%d: expected %+v, got %+v", i, tests[i].exp, uu)
    71  		}
    72  	}
    73  }
    74  

View as plain text