github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/command/connect/proxy/flag_upstreams_test.go (about) 1 package proxy 2 3 import ( 4 "flag" 5 "testing" 6 7 "github.com/hashicorp/consul/connect/proxy" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestFlagUpstreams_impl(t *testing.T) { 12 var _ flag.Value = new(FlagUpstreams) 13 } 14 15 func TestFlagUpstreams(t *testing.T) { 16 cases := []struct { 17 Name string 18 Input []string 19 Expected map[string]proxy.UpstreamConfig 20 Error string 21 }{ 22 { 23 "bad format", 24 []string{"foo"}, 25 nil, 26 "should be name:addr", 27 }, 28 29 { 30 "port not int", 31 []string{"db:hello"}, 32 nil, 33 "invalid syntax", 34 }, 35 36 { 37 "4 parts", 38 []string{"db:127.0.0.1:8181:foo"}, 39 nil, 40 "invalid syntax", 41 }, 42 43 { 44 "single value", 45 []string{"db:8181"}, 46 map[string]proxy.UpstreamConfig{ 47 "db": proxy.UpstreamConfig{ 48 LocalBindPort: 8181, 49 DestinationName: "db", 50 DestinationType: "service", 51 }, 52 }, 53 "", 54 }, 55 56 { 57 "single value prepared query", 58 []string{"db.query:8181"}, 59 map[string]proxy.UpstreamConfig{ 60 "db": proxy.UpstreamConfig{ 61 LocalBindPort: 8181, 62 DestinationName: "db", 63 DestinationType: "prepared_query", 64 }, 65 }, 66 "", 67 }, 68 69 { 70 "invalid type", 71 []string{"db.bad:8181"}, 72 nil, 73 "Upstream type", 74 }, 75 76 { 77 "address specified", 78 []string{"db:127.0.0.55:8181"}, 79 map[string]proxy.UpstreamConfig{ 80 "db": proxy.UpstreamConfig{ 81 LocalBindAddress: "127.0.0.55", 82 LocalBindPort: 8181, 83 DestinationName: "db", 84 DestinationType: "service", 85 }, 86 }, 87 "", 88 }, 89 90 { 91 "repeat value, overwrite", 92 []string{"db:8181", "db:8282"}, 93 map[string]proxy.UpstreamConfig{ 94 "db": proxy.UpstreamConfig{ 95 LocalBindPort: 8282, 96 DestinationName: "db", 97 DestinationType: "service", 98 }, 99 }, 100 "", 101 }, 102 } 103 104 for _, tc := range cases { 105 t.Run(tc.Name, func(t *testing.T) { 106 require := require.New(t) 107 108 var actual map[string]proxy.UpstreamConfig 109 f := (*FlagUpstreams)(&actual) 110 111 var err error 112 for _, input := range tc.Input { 113 err = f.Set(input) 114 // Note we only test the last error. This could make some 115 // test failures confusing but it shouldn't be too bad. 116 } 117 if tc.Error != "" { 118 require.Error(err) 119 require.Contains(err.Error(), tc.Error) 120 return 121 } 122 123 require.Equal(tc.Expected, actual) 124 }) 125 } 126 }