1package starter 2 3import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9) 10 11func TestIsSecure(t *testing.T) { 12 for _, test := range []struct { 13 name string 14 secure bool 15 }{ 16 {"tcp", false}, 17 {"unix", false}, 18 {"tls", true}, 19 } { 20 t.Run(test.name, func(t *testing.T) { 21 conf := Config{Name: test.name} 22 require.Equal(t, test.secure, conf.IsSecure()) 23 }) 24 } 25} 26 27func TestFamily(t *testing.T) { 28 for _, test := range []struct { 29 name, family string 30 }{ 31 {"tcp", "tcp"}, 32 {"unix", "unix"}, 33 {"tls", "tcp"}, 34 } { 35 t.Run(test.name, func(t *testing.T) { 36 conf := Config{Name: test.name} 37 require.Equal(t, test.family, conf.family()) 38 }) 39 } 40} 41 42func TestComposeEndpoint(t *testing.T) { 43 for _, tc := range []struct { 44 desc string 45 schema string 46 addr string 47 exp string 48 expErr error 49 }{ 50 { 51 desc: "no addresses", 52 schema: TCP, 53 addr: "", 54 expErr: errors.New("empty address can't be used"), 55 }, 56 { 57 desc: "incorrect schema", 58 schema: "bad", 59 addr: "127.0.0.1:2306", 60 expErr: errors.New(`unsupported schema: "bad"`), 61 }, 62 { 63 desc: "no schema", 64 addr: "127.0.0.1:2306", 65 schema: "", 66 expErr: errors.New("empty schema can't be used"), 67 }, 68 { 69 desc: "tcp schema addresses", 70 schema: TCP, 71 addr: "127.0.0.1:2306", 72 exp: "tcp://127.0.0.1:2306", 73 }, 74 { 75 desc: "tls schema addresses", 76 schema: TLS, 77 addr: "127.0.0.1:2306", 78 exp: "tls://127.0.0.1:2306", 79 }, 80 { 81 desc: "unix schema addresses", 82 schema: Unix, 83 addr: "/path/to/socket", 84 exp: "unix:///path/to/socket", 85 }, 86 } { 87 t.Run(tc.desc, func(t *testing.T) { 88 actual, err := ComposeEndpoint(tc.schema, tc.addr) 89 require.Equal(t, tc.expErr, err) 90 require.Equal(t, tc.exp, actual) 91 }) 92 } 93} 94 95func TestParseEndpoint(t *testing.T) { 96 for _, tc := range []struct { 97 desc string 98 addr string 99 exp Config 100 expErr error 101 }{ 102 { 103 desc: "no addresses", 104 expErr: errEmptyAddress, 105 }, 106 { 107 desc: "incorrect schema", 108 addr: "bad://127.0.0.1:2306", 109 expErr: errors.New(`unsupported schema: "bad"`), 110 }, 111 { 112 desc: "no schema", 113 addr: "://127.0.0.1:2306", 114 expErr: ErrEmptySchema, 115 }, 116 { 117 desc: "bad format", 118 addr: "127.0.0.1:2306", 119 expErr: fmt.Errorf(`unsupported format: "127.0.0.1:2306": %w`, ErrEmptySchema), 120 }, 121 { 122 desc: "tcp schema addresses", 123 addr: "tcp://127.0.0.1:2306", 124 exp: Config{Name: TCP, Addr: "127.0.0.1:2306"}, 125 }, 126 { 127 desc: "tls schema addresses", 128 addr: "tls://127.0.0.1:2306", 129 exp: Config{Name: TLS, Addr: "127.0.0.1:2306"}, 130 }, 131 { 132 desc: "unix schema addresses", 133 addr: "unix:///path/to/socket", 134 exp: Config{Name: Unix, Addr: "/path/to/socket"}, 135 }, 136 } { 137 t.Run(tc.desc, func(t *testing.T) { 138 actual, err := ParseEndpoint(tc.addr) 139 require.Equal(t, tc.expErr, err) 140 require.Equal(t, tc.exp, actual) 141 }) 142 } 143} 144 145func TestConfig_Endpoint(t *testing.T) { 146 for _, tc := range []struct { 147 desc string 148 conf Config 149 exp string 150 expErr error 151 }{ 152 { 153 desc: "no address", 154 conf: Config{Name: TCP}, 155 expErr: errors.New("empty address can't be used"), 156 }, 157 { 158 desc: "no schema", 159 conf: Config{Addr: "localhost"}, 160 expErr: errors.New("empty schema can't be used"), 161 }, 162 { 163 desc: "invalid schema", 164 conf: Config{Name: "invalid", Addr: "localhost"}, 165 expErr: errors.New(`unsupported schema: "invalid"`), 166 }, 167 { 168 desc: "unix", 169 conf: Config{Name: Unix, Addr: "/var/opt/some"}, 170 exp: "unix:///var/opt/some", 171 }, 172 { 173 desc: "tcp", 174 conf: Config{Name: TCP, Addr: "localhost:1234"}, 175 exp: "tcp://localhost:1234", 176 }, 177 { 178 desc: "tls", 179 conf: Config{Name: TLS, Addr: "localhost:4321"}, 180 exp: "tls://localhost:4321", 181 }, 182 } { 183 t.Run(tc.desc, func(t *testing.T) { 184 actual, err := tc.conf.Endpoint() 185 require.Equal(t, tc.expErr, err) 186 require.Equal(t, tc.exp, actual) 187 }) 188 } 189} 190