...

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

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

     1  // Copyright 2018 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  	"flag"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestNewUniqueURLsWithExceptions(t *testing.T) {
    26  	tests := []struct {
    27  		s         string
    28  		exp       map[string]struct{}
    29  		rs        string
    30  		exception string
    31  	}{
    32  		{ // non-URL but allowed by exception
    33  			s:         "*",
    34  			exp:       map[string]struct{}{"*": {}},
    35  			rs:        "*",
    36  			exception: "*",
    37  		},
    38  		{
    39  			s:         "",
    40  			exp:       map[string]struct{}{},
    41  			rs:        "",
    42  			exception: "*",
    43  		},
    44  		{
    45  			s:         "https://1.2.3.4:8080",
    46  			exp:       map[string]struct{}{"https://1.2.3.4:8080": {}},
    47  			rs:        "https://1.2.3.4:8080",
    48  			exception: "*",
    49  		},
    50  		{
    51  			s:         "https://1.2.3.4:8080,https://1.2.3.4:8080",
    52  			exp:       map[string]struct{}{"https://1.2.3.4:8080": {}},
    53  			rs:        "https://1.2.3.4:8080",
    54  			exception: "*",
    55  		},
    56  		{
    57  			s:         "http://10.1.1.1:80",
    58  			exp:       map[string]struct{}{"http://10.1.1.1:80": {}},
    59  			rs:        "http://10.1.1.1:80",
    60  			exception: "*",
    61  		},
    62  		{
    63  			s:         "http://localhost:80",
    64  			exp:       map[string]struct{}{"http://localhost:80": {}},
    65  			rs:        "http://localhost:80",
    66  			exception: "*",
    67  		},
    68  		{
    69  			s:         "http://:80",
    70  			exp:       map[string]struct{}{"http://:80": {}},
    71  			rs:        "http://:80",
    72  			exception: "*",
    73  		},
    74  		{
    75  			s:         "https://localhost:5,https://localhost:3",
    76  			exp:       map[string]struct{}{"https://localhost:3": {}, "https://localhost:5": {}},
    77  			rs:        "https://localhost:3,https://localhost:5",
    78  			exception: "*",
    79  		},
    80  		{
    81  			s:         "http://localhost:5,https://localhost:3",
    82  			exp:       map[string]struct{}{"https://localhost:3": {}, "http://localhost:5": {}},
    83  			rs:        "http://localhost:5,https://localhost:3",
    84  			exception: "*",
    85  		},
    86  	}
    87  	for i := range tests {
    88  		uv := NewUniqueURLsWithExceptions(tests[i].s, tests[i].exception)
    89  		require.Equal(t, tests[i].exp, uv.Values)
    90  		require.Equal(t, uv.String(), tests[i].rs)
    91  	}
    92  }
    93  
    94  func TestUniqueURLsFromFlag(t *testing.T) {
    95  	const name = "test"
    96  	urls := []string{
    97  		"https://1.2.3.4:1",
    98  		"https://1.2.3.4:2",
    99  		"https://1.2.3.4:3",
   100  		"https://1.2.3.4:1",
   101  	}
   102  	fs := flag.NewFlagSet(name, flag.ExitOnError)
   103  	u := NewUniqueURLsWithExceptions(strings.Join(urls, ","))
   104  	fs.Var(u, name, "usage")
   105  	uss := UniqueURLsFromFlag(fs, name)
   106  
   107  	require.Equal(t, len(u.Values), len(uss))
   108  
   109  	um := make(map[string]struct{})
   110  	for _, x := range uss {
   111  		um[x.String()] = struct{}{}
   112  	}
   113  	require.Equal(t, u.Values, um)
   114  }
   115  

View as plain text