@@ -2,8 +2,10 @@ package opts
22
33import (
44 "fmt"
5- "strings"
65 "testing"
6+
7+ "gotest.tools/v3/assert"
8+ is "gotest.tools/v3/assert/cmp"
79)
810
911func TestParseHost (t * testing.T ) {
@@ -146,32 +148,152 @@ func TestParseInvalidUnixAddrInvalid(t *testing.T) {
146148}
147149
148150func TestValidateExtraHosts (t * testing.T ) {
149- valid := []string {
150- `myhost:192.168.0.1` ,
151- `thathost:10.0.2.1` ,
152- `anipv6host:2003:ab34:e::1` ,
153- `ipv6local:::1` ,
154- `host.docker.internal:host-gateway` ,
155- }
156-
157- invalid := map [string ]string {
158- `myhost:192.notanipaddress.1` : `invalid IP` ,
159- `thathost-nosemicolon10.0.0.1` : `bad format` ,
160- `anipv6host:::::1` : `invalid IP` ,
161- `ipv6local:::0::` : `invalid IP` ,
162- }
163-
164- for _ , extrahost := range valid {
165- if _ , err := ValidateExtraHost (extrahost ); err != nil {
166- t .Fatalf ("ValidateExtraHost(`" + extrahost + "`) should succeed: error %v" , err )
167- }
151+ tests := []struct {
152+ doc string
153+ input string
154+ expectedOut string // Expect output==input if not set.
155+ expectedErr string // Expect success if not set.
156+ }{
157+ {
158+ doc : "IPv4, colon sep" ,
159+ input : `myhost:192.168.0.1` ,
160+ },
161+ {
162+ doc : "IPv4, eq sep" ,
163+ input : `myhost=192.168.0.1` ,
164+ expectedOut : `myhost:192.168.0.1` ,
165+ },
166+ {
167+ doc : "Weird but permitted, IPv4 with brackets" ,
168+ input : `myhost=[192.168.0.1]` ,
169+ expectedOut : `myhost:192.168.0.1` ,
170+ },
171+ {
172+ doc : "Host and domain" ,
173+ input : `host.and.domain.invalid:10.0.2.1` ,
174+ },
175+ {
176+ doc : "IPv6, colon sep" ,
177+ input : `anipv6host:2003:ab34:e::1` ,
178+ },
179+ {
180+ doc : "IPv6, colon sep, brackets" ,
181+ input : `anipv6host:[2003:ab34:e::1]` ,
182+ expectedOut : `anipv6host:2003:ab34:e::1` ,
183+ },
184+ {
185+ doc : "IPv6, eq sep, brackets" ,
186+ input : `anipv6host=[2003:ab34:e::1]` ,
187+ expectedOut : `anipv6host:2003:ab34:e::1` ,
188+ },
189+ {
190+ doc : "IPv6 localhost, colon sep" ,
191+ input : `ipv6local:::1` ,
192+ },
193+ {
194+ doc : "IPv6 localhost, eq sep" ,
195+ input : `ipv6local=::1` ,
196+ expectedOut : `ipv6local:::1` ,
197+ },
198+ {
199+ doc : "IPv6 localhost, eq sep, brackets" ,
200+ input : `ipv6local=[::1]` ,
201+ expectedOut : `ipv6local:::1` ,
202+ },
203+ {
204+ doc : "IPv6 localhost, non-canonical, colon sep" ,
205+ input : `ipv6local:0:0:0:0:0:0:0:1` ,
206+ },
207+ {
208+ doc : "IPv6 localhost, non-canonical, eq sep" ,
209+ input : `ipv6local=0:0:0:0:0:0:0:1` ,
210+ expectedOut : `ipv6local:0:0:0:0:0:0:0:1` ,
211+ },
212+ {
213+ doc : "IPv6 localhost, non-canonical, eq sep, brackets" ,
214+ input : `ipv6local=[0:0:0:0:0:0:0:1]` ,
215+ expectedOut : `ipv6local:0:0:0:0:0:0:0:1` ,
216+ },
217+ {
218+ doc : "host-gateway special case, colon sep" ,
219+ input : `host.docker.internal:host-gateway` ,
220+ },
221+ {
222+ doc : "host-gateway special case, eq sep" ,
223+ input : `host.docker.internal=host-gateway` ,
224+ expectedOut : `host.docker.internal:host-gateway` ,
225+ },
226+ {
227+ doc : "Bad address, colon sep" ,
228+ input : `myhost:192.notanipaddress.1` ,
229+ expectedErr : `invalid IP address in add-host: "192.notanipaddress.1"` ,
230+ },
231+ {
232+ doc : "Bad address, eq sep" ,
233+ input : `myhost=192.notanipaddress.1` ,
234+ expectedErr : `invalid IP address in add-host: "192.notanipaddress.1"` ,
235+ },
236+ {
237+ doc : "No sep" ,
238+ input : `thathost-nosemicolon10.0.0.1` ,
239+ expectedErr : `bad format for add-host: "thathost-nosemicolon10.0.0.1"` ,
240+ },
241+ {
242+ doc : "Bad IPv6" ,
243+ input : `anipv6host:::::1` ,
244+ expectedErr : `invalid IP address in add-host: "::::1"` ,
245+ },
246+ {
247+ doc : "Bad IPv6, trailing colons" ,
248+ input : `ipv6local:::0::` ,
249+ expectedErr : `invalid IP address in add-host: "::0::"` ,
250+ },
251+ {
252+ doc : "Bad IPv6, missing close bracket" ,
253+ input : `ipv6addr=[::1` ,
254+ expectedErr : `invalid IP address in add-host: "[::1"` ,
255+ },
256+ {
257+ doc : "Bad IPv6, missing open bracket" ,
258+ input : `ipv6addr=::1]` ,
259+ expectedErr : `invalid IP address in add-host: "::1]"` ,
260+ },
261+ {
262+ doc : "Missing address, colon sep" ,
263+ input : `myhost.invalid:` ,
264+ expectedErr : `invalid IP address in add-host: ""` ,
265+ },
266+ {
267+ doc : "Missing address, eq sep" ,
268+ input : `myhost.invalid=` ,
269+ expectedErr : `invalid IP address in add-host: ""` ,
270+ },
271+ {
272+ doc : "IPv6 localhost, bad name" ,
273+ input : `:=::1` ,
274+ expectedErr : `bad format for add-host: ":=::1"` ,
275+ },
276+ {
277+ doc : "No input" ,
278+ input : `` ,
279+ expectedErr : `bad format for add-host: ""` ,
280+ },
168281 }
169282
170- for extraHost , expectedError := range invalid {
171- if _ , err := ValidateExtraHost (extraHost ); err == nil {
172- t .Fatalf ("ValidateExtraHost(`%q`) should have failed validation" , extraHost )
173- } else if ! strings .Contains (err .Error (), expectedError ) {
174- t .Fatalf ("ValidateExtraHost(`%q`) error should contain %q" , extraHost , expectedError )
283+ for _ , tc := range tests {
284+ tc := tc
285+ if tc .expectedOut == "" {
286+ tc .expectedOut = tc .input
175287 }
288+ t .Run (tc .input , func (t * testing.T ) {
289+ actualOut , actualErr := ValidateExtraHost (tc .input )
290+ if tc .expectedErr == "" {
291+ assert .Check (t , is .Equal (tc .expectedOut , actualOut ))
292+ assert .NilError (t , actualErr )
293+ } else {
294+ assert .Check (t , actualOut == "" )
295+ assert .Check (t , is .Error (actualErr , tc .expectedErr ))
296+ }
297+ })
176298 }
177299}
0 commit comments