go.etcd.io/etcd@v3.3.27+incompatible/pkg/types/urlsmap_test.go (about) 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 types 16 17 import ( 18 "github.com/coreos/etcd/pkg/testutil" 19 "reflect" 20 "testing" 21 ) 22 23 func TestParseInitialCluster(t *testing.T) { 24 c, err := NewURLsMap("mem1=http://10.0.0.1:2379,mem1=http://128.193.4.20:2379,mem2=http://10.0.0.2:2379,default=http://127.0.0.1:2379") 25 if err != nil { 26 t.Fatalf("unexpected parse error: %v", err) 27 } 28 wc := URLsMap(map[string]URLs{ 29 "mem1": testutil.MustNewURLs(t, []string{"http://10.0.0.1:2379", "http://128.193.4.20:2379"}), 30 "mem2": testutil.MustNewURLs(t, []string{"http://10.0.0.2:2379"}), 31 "default": testutil.MustNewURLs(t, []string{"http://127.0.0.1:2379"}), 32 }) 33 if !reflect.DeepEqual(c, wc) { 34 t.Errorf("cluster = %+v, want %+v", c, wc) 35 } 36 } 37 38 func TestParseInitialClusterBad(t *testing.T) { 39 tests := []string{ 40 // invalid URL 41 "%^", 42 // no URL defined for member 43 "mem1=,mem2=http://128.193.4.20:2379,mem3=http://10.0.0.2:2379", 44 "mem1,mem2=http://128.193.4.20:2379,mem3=http://10.0.0.2:2379", 45 // bad URL for member 46 "default=http://localhost/", 47 } 48 for i, tt := range tests { 49 if _, err := NewURLsMap(tt); err == nil { 50 t.Errorf("#%d: unexpected successful parse, want err", i) 51 } 52 } 53 } 54 55 func TestNameURLPairsString(t *testing.T) { 56 cls := URLsMap(map[string]URLs{ 57 "abc": testutil.MustNewURLs(t, []string{"http://1.1.1.1:1111", "http://0.0.0.0:0000"}), 58 "def": testutil.MustNewURLs(t, []string{"http://2.2.2.2:2222"}), 59 "ghi": testutil.MustNewURLs(t, []string{"http://3.3.3.3:1234", "http://127.0.0.1:2380"}), 60 // no PeerURLs = not included 61 "four": testutil.MustNewURLs(t, []string{}), 62 "five": testutil.MustNewURLs(t, nil), 63 }) 64 w := "abc=http://0.0.0.0:0000,abc=http://1.1.1.1:1111,def=http://2.2.2.2:2222,ghi=http://127.0.0.1:2380,ghi=http://3.3.3.3:1234" 65 if g := cls.String(); g != w { 66 t.Fatalf("NameURLPairs.String():\ngot %#v\nwant %#v", g, w) 67 } 68 } 69 70 func TestParse(t *testing.T) { 71 tests := []struct { 72 s string 73 wm map[string][]string 74 }{ 75 { 76 "", 77 map[string][]string{}, 78 }, 79 { 80 "a=b", 81 map[string][]string{"a": {"b"}}, 82 }, 83 { 84 "a=b,a=c", 85 map[string][]string{"a": {"b", "c"}}, 86 }, 87 { 88 "a=b,a1=c", 89 map[string][]string{"a": {"b"}, "a1": {"c"}}, 90 }, 91 } 92 for i, tt := range tests { 93 m := parse(tt.s) 94 if !reflect.DeepEqual(m, tt.wm) { 95 t.Errorf("#%d: m = %+v, want %+v", i, m, tt.wm) 96 } 97 } 98 } 99 100 // TestNewURLsMapIPV6 is only tested in Go1.5+ because Go1.4 doesn't support literal IPv6 address with zone in 101 // URI (https://github.com/golang/go/issues/6530). 102 func TestNewURLsMapIPV6(t *testing.T) { 103 c, err := NewURLsMap("mem1=http://[2001:db8::1]:2380,mem1=http://[fe80::6e40:8ff:feb1:58e4%25en0]:2380,mem2=http://[fe80::92e2:baff:fe7c:3224%25ext0]:2380") 104 if err != nil { 105 t.Fatalf("unexpected parse error: %v", err) 106 } 107 wc := URLsMap(map[string]URLs{ 108 "mem1": testutil.MustNewURLs(t, []string{"http://[2001:db8::1]:2380", "http://[fe80::6e40:8ff:feb1:58e4%25en0]:2380"}), 109 "mem2": testutil.MustNewURLs(t, []string{"http://[fe80::92e2:baff:fe7c:3224%25ext0]:2380"}), 110 }) 111 if !reflect.DeepEqual(c, wc) { 112 t.Errorf("cluster = %#v, want %#v", c, wc) 113 } 114 } 115 116 func TestNewURLsMapFromStringMapEmpty(t *testing.T) { 117 mss := make(map[string]string) 118 urlsMap, err := NewURLsMapFromStringMap(mss, ",") 119 if err != nil { 120 t.Errorf("Unexpected error: %v", err) 121 } 122 s := "" 123 um, err := NewURLsMap(s) 124 if err != nil { 125 t.Errorf("Unexpected error: %v", err) 126 } 127 128 if um.String() != urlsMap.String() { 129 t.Errorf("Expected:\n%+v\ngot:\n%+v", um, urlsMap) 130 } 131 } 132 133 func TestNewURLsMapFromStringMapNormal(t *testing.T) { 134 mss := make(map[string]string) 135 mss["host0"] = "http://127.0.0.1:2379,http://127.0.0.1:2380" 136 mss["host1"] = "http://127.0.0.1:2381,http://127.0.0.1:2382" 137 mss["host2"] = "http://127.0.0.1:2383,http://127.0.0.1:2384" 138 mss["host3"] = "http://127.0.0.1:2385,http://127.0.0.1:2386" 139 urlsMap, err := NewURLsMapFromStringMap(mss, ",") 140 if err != nil { 141 t.Errorf("Unexpected error: %v", err) 142 } 143 s := "host0=http://127.0.0.1:2379,host0=http://127.0.0.1:2380," + 144 "host1=http://127.0.0.1:2381,host1=http://127.0.0.1:2382," + 145 "host2=http://127.0.0.1:2383,host2=http://127.0.0.1:2384," + 146 "host3=http://127.0.0.1:2385,host3=http://127.0.0.1:2386" 147 um, err := NewURLsMap(s) 148 if err != nil { 149 t.Errorf("Unexpected error: %v", err) 150 } 151 152 if um.String() != urlsMap.String() { 153 t.Errorf("Expected:\n%+v\ngot:\n%+v", um, urlsMap) 154 } 155 }