Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 62e46c2

Browse files
committed
Add "host-gateway" to tests for extra_hosts / --add-host
67ebcd6dcf82c00f133b886a9a343b67124e58ec added an exception for the "host-gateway" magic value to the validation rules, but didn't add thise value to any of the tests. This patch adds the magic value to tests, to verify the validation is skipped for this magic value. Note that validation on the client side is "optional" and mostly done to provide a more user-friendly error message for regular values (IP-addresses). Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commit f88ae74135fc46227d3f62e4308a79a92a76bd76) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: fd9eedce3c1e06f3cbdd0f10dece3ffd73aefc4a Component: cli
1 parent 3199cd1 commit 62e46c2

5 files changed

Lines changed: 17 additions & 3 deletions

File tree

components/cli/cli/command/service/update_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,19 @@ func TestUpdateHosts(t *testing.T) {
358358
flags := newUpdateCommand(nil).Flags()
359359
flags.Set("host-add", "example.net:2.2.2.2")
360360
flags.Set("host-add", "ipv6.net:2001:db8:abc8::1")
361+
// adding the special "host-gateway" target should work
362+
flags.Set("host-add", "host.docker.internal:host-gateway")
361363
// remove with ipv6 should work
362364
flags.Set("host-rm", "example.net:2001:db8:abc8::1")
363365
// just hostname should work as well
364366
flags.Set("host-rm", "example.net")
367+
// removing the special "host-gateway" target should work
368+
flags.Set("host-rm", "gateway.docker.internal:host-gateway")
365369
// bad format error
366370
assert.ErrorContains(t, flags.Set("host-add", "$example.com$"), `bad format for add-host: "$example.com$"`)
367371

368-
hosts := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2001:db8:abc8::1 example.net"}
369-
expected := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2.2.2.2 example.net", "2001:db8:abc8::1 ipv6.net"}
372+
hosts := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2001:db8:abc8::1 example.net", "gateway.docker.internal:host-gateway"}
373+
expected := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2.2.2.2 example.net", "2001:db8:abc8::1 ipv6.net", "host-gateway host.docker.internal"}
370374

371375
err := updateHosts(flags, &hosts)
372376
assert.NilError(t, err)

components/cli/cli/compose/loader/full-example.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ services:
140140
# extra_hosts:
141141
# somehost: "162.242.195.82"
142142
# otherhost: "50.31.209.229"
143+
# host.docker.internal: "host-gateway"
143144
extra_hosts:
144145
- "somehost:162.242.195.82"
145146
- "otherhost:50.31.209.229"
147+
- "host.docker.internal:host-gateway"
146148

147149
hostname: foo
148150

components/cli/cli/compose/loader/full-struct_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
144144
ExtraHosts: []string{
145145
"somehost:162.242.195.82",
146146
"otherhost:50.31.209.229",
147+
"host.docker.internal:host-gateway",
147148
},
148149
Extras: map[string]interface{}{
149150
"x-bar": "baz",
@@ -626,6 +627,7 @@ services:
626627
extra_hosts:
627628
- somehost:162.242.195.82
628629
- otherhost:50.31.209.229
630+
- host.docker.internal:host-gateway
629631
hostname: foo
630632
healthcheck:
631633
test:
@@ -1135,7 +1137,8 @@ func fullExampleJSON(workingDir string) string {
11351137
],
11361138
"extra_hosts": [
11371139
"somehost:162.242.195.82",
1138-
"otherhost:50.31.209.229"
1140+
"otherhost:50.31.209.229",
1141+
"host.docker.internal:host-gateway"
11391142
],
11401143
"hostname": "foo",
11411144
"healthcheck": {

components/cli/cli/compose/loader/loader_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,11 +1276,13 @@ services:
12761276
extra_hosts:
12771277
"zulu": "162.242.195.82"
12781278
"alpha": "50.31.209.229"
1279+
"host.docker.internal": "host-gateway"
12791280
`)
12801281
assert.NilError(t, err)
12811282

12821283
expected := types.HostsList{
12831284
"alpha:50.31.209.229",
1285+
"host.docker.internal:host-gateway",
12841286
"zulu:162.242.195.82",
12851287
}
12861288

@@ -1298,13 +1300,15 @@ services:
12981300
- "zulu:162.242.195.82"
12991301
- "alpha:50.31.209.229"
13001302
- "zulu:ff02::1"
1303+
- "host.docker.internal:host-gateway"
13011304
`)
13021305
assert.NilError(t, err)
13031306

13041307
expected := types.HostsList{
13051308
"zulu:162.242.195.82",
13061309
"alpha:50.31.209.229",
13071310
"zulu:ff02::1",
1311+
"host.docker.internal:host-gateway",
13081312
}
13091313

13101314
assert.Assert(t, is.Len(config.Services, 1))

components/cli/opts/hosts_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func TestValidateExtraHosts(t *testing.T) {
154154
`thathost:10.0.2.1`,
155155
`anipv6host:2003:ab34:e::1`,
156156
`ipv6local:::1`,
157+
`host.docker.internal:host-gateway`,
157158
}
158159

159160
invalid := map[string]string{

0 commit comments

Comments
 (0)