-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostport_test.go
532 lines (502 loc) · 13.5 KB
/
hostport_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package network_test
import (
"fmt"
"net"
"strings"
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/network"
"github.com/juju/juju/testing"
)
type HostPortSuite struct {
testing.BaseSuite
}
var _ = gc.Suite(&HostPortSuite{})
type hostPortTest struct {
about string
hostPorts []network.HostPort
expectedIndex int
preferIPv6 bool
}
// hostPortTest returns the HostPort equivalent test to the
// receiving selectTest.
func (t selectTest) hostPortTest() hostPortTest {
hps := network.AddressesWithPort(t.addresses, 9999)
for i := range hps {
hps[i].Port = i + 1
}
return hostPortTest{
about: t.about,
hostPorts: hps,
expectedIndex: t.expectedIndex,
preferIPv6: t.preferIPv6,
}
}
// expected returns the expected host:port result
// of the test.
func (t hostPortTest) expected() string {
if t.expectedIndex == -1 {
return ""
}
return t.hostPorts[t.expectedIndex].NetAddr()
}
func (*HostPortSuite) TestSelectPublicHostPort(c *gc.C) {
oldValue := network.GetPreferIPv6()
defer func() {
network.SetPreferIPv6(oldValue)
}()
for i, t0 := range selectPublicTests {
t := t0.hostPortTest()
c.Logf("test %d: %s", i, t.about)
network.SetPreferIPv6(t.preferIPv6)
c.Check(network.SelectPublicHostPort(t.hostPorts), jc.DeepEquals, t.expected())
}
}
func (*HostPortSuite) TestSelectInternalHostPort(c *gc.C) {
oldValue := network.GetPreferIPv6()
defer func() {
network.SetPreferIPv6(oldValue)
}()
for i, t0 := range selectInternalTests {
t := t0.hostPortTest()
c.Logf("test %d: %s", i, t.about)
network.SetPreferIPv6(t.preferIPv6)
c.Check(network.SelectInternalHostPort(t.hostPorts, false), jc.DeepEquals, t.expected())
}
}
func (*HostPortSuite) TestSelectInternalMachineHostPort(c *gc.C) {
oldValue := network.GetPreferIPv6()
defer func() {
network.SetPreferIPv6(oldValue)
}()
for i, t0 := range selectInternalMachineTests {
t := t0.hostPortTest()
c.Logf("test %d: %s", i, t.about)
network.SetPreferIPv6(t.preferIPv6)
c.Check(network.SelectInternalHostPort(t.hostPorts, true), gc.DeepEquals, t.expected())
}
}
func (s *HostPortSuite) TestResolveOrDropHostnames(c *gc.C) {
seq := 0
s.PatchValue(network.NetLookupIP, func(host string) ([]net.IP, error) {
if host == "invalid host" {
return nil, errors.New("lookup invalid host: no such host")
}
if host == "localhost" {
return []net.IP{net.ParseIP("127.0.0.1")}, nil
}
// Return 2 IPs for .net hosts, 1 IP otherwise.
var ips []net.IP
ips = append(ips, net.ParseIP(fmt.Sprintf("0.1.2.%d", seq)))
seq++
if strings.Contains(host, ".net") {
ips = append(ips, net.ParseIP(fmt.Sprintf("0.1.2.%d", seq)))
seq++
}
c.Logf("lookup host %q -> %v", host, ips)
return ips, nil
})
resolved := network.ResolveOrDropHostnames(s.makeHostPorts())
c.Assert(
c.GetTestLog(),
jc.Contains,
`DEBUG juju.network removing unresolvable address "invalid host"`,
)
// Order should be preserved, duplicates dropped and hostnames,
// except localhost resolved or dropped.
c.Assert(resolved, jc.DeepEquals, network.NewHostPorts(1234,
"127.0.0.1",
"localhost", // localhost is not resolved intentionally.
"0.1.2.0", // from example.com
"127.0.1.1",
"0.1.2.1", // from example.org
"2001:db8::2",
"169.254.1.1",
"0.1.2.2", // from example.net
"0.1.2.3", // from example.net
"fd00::22",
"2001:db8::1",
"169.254.1.2",
"ff01::22",
"10.0.0.1",
"::1",
"fc00::1",
"fe80::2",
"172.16.0.1",
"8.8.8.8",
"7.8.8.8",
))
}
func (s *HostPortSuite) TestFilterUnusableHostPorts(c *gc.C) {
// The order is preserved, but machine- and link-local addresses
// are dropped.
expected := network.NewHostPorts(1234,
"localhost",
"example.com",
"example.org",
"2001:db8::2",
"example.net",
"invalid host",
"fd00::22",
"2001:db8::1",
"0.1.2.0",
"2001:db8::1",
"localhost",
"10.0.0.1",
"fc00::1",
"172.16.0.1",
"8.8.8.8",
"7.8.8.8",
)
result := network.FilterUnusableHostPorts(s.makeHostPorts())
c.Assert(result, gc.HasLen, len(expected))
c.Assert(result, jc.DeepEquals, expected)
}
func (*HostPortSuite) TestCollapseHostPorts(c *gc.C) {
servers := [][]network.HostPort{
network.NewHostPorts(1234,
"0.1.2.3", "10.0.1.2", "fc00::1", "2001:db8::1", "::1",
"127.0.0.1", "localhost", "fe80::123", "example.com",
),
network.NewHostPorts(4321,
"8.8.8.8", "1.2.3.4", "fc00::2", "127.0.0.1", "foo",
),
network.NewHostPorts(9999,
"localhost", "127.0.0.1",
),
}
expected := append(servers[0], append(servers[1], servers[2]...)...)
result := network.CollapseHostPorts(servers)
c.Assert(result, gc.HasLen, len(servers[0])+len(servers[1])+len(servers[2]))
c.Assert(result, jc.DeepEquals, expected)
}
func (s *HostPortSuite) TestEnsureFirstHostPort(c *gc.C) {
first := network.NewHostPorts(1234, "1.2.3.4")[0]
// Without any HostPorts, it still works.
hps := network.EnsureFirstHostPort(first, []network.HostPort{})
c.Assert(hps, jc.DeepEquals, []network.HostPort{first})
// If already there, no changes happen.
hps = s.makeHostPorts()
result := network.EnsureFirstHostPort(hps[0], hps)
c.Assert(result, jc.DeepEquals, hps)
// If not at the top, pop it up and put it on top.
firstLast := append(hps, first)
result = network.EnsureFirstHostPort(first, firstLast)
c.Assert(result, jc.DeepEquals, append([]network.HostPort{first}, hps...))
}
func (*HostPortSuite) TestNewHostPorts(c *gc.C) {
addrs := []string{"0.1.2.3", "fc00::1", "::1", "example.com"}
expected := network.AddressesWithPort(
network.NewAddresses(addrs...), 42,
)
result := network.NewHostPorts(42, addrs...)
c.Assert(result, gc.HasLen, len(addrs))
c.Assert(result, jc.DeepEquals, expected)
}
func (*HostPortSuite) TestParseHostPortsErrors(c *gc.C) {
for i, test := range []struct {
input string
err string
}{{
input: "",
err: `cannot parse "" as address:port: missing port in address`,
}, {
input: " ",
err: `cannot parse " " as address:port: missing port in address `,
}, {
input: ":",
err: `cannot parse ":" port: strconv.ParseInt: parsing "": invalid syntax`,
}, {
input: "host",
err: `cannot parse "host" as address:port: missing port in address host`,
}, {
input: "host:port",
err: `cannot parse "host:port" port: strconv.ParseInt: parsing "port": invalid syntax`,
}, {
input: "::1",
err: `cannot parse "::1" as address:port: too many colons in address ::1`,
}, {
input: "1.2.3.4",
err: `cannot parse "1.2.3.4" as address:port: missing port in address 1.2.3.4`,
}, {
input: "1.2.3.4:foo",
err: `cannot parse "1.2.3.4:foo" port: strconv.ParseInt: parsing "foo": invalid syntax`,
}} {
c.Logf("test %d: input %q", i, test.input)
// First test all error cases with a single argument.
hps, err := network.ParseHostPorts(test.input)
c.Check(err, gc.ErrorMatches, test.err)
c.Check(hps, gc.IsNil)
}
// Finally, test with mixed valid and invalid args.
hps, err := network.ParseHostPorts("1.2.3.4:42", "[fc00::1]:12", "foo")
c.Assert(err, gc.ErrorMatches, `cannot parse "foo" as address:port: missing port in address foo`)
c.Assert(hps, gc.IsNil)
}
func (*HostPortSuite) TestParseHostPortsSuccess(c *gc.C) {
for i, test := range []struct {
args []string
expect []network.HostPort
}{{
args: nil,
expect: []network.HostPort{},
}, {
args: []string{"1.2.3.4:42"},
expect: network.NewHostPorts(42, "1.2.3.4"),
}, {
args: []string{"[fc00::1]:1234"},
expect: network.NewHostPorts(1234, "fc00::1"),
}, {
args: []string{"[fc00::1]:1234", "127.0.0.1:4321", "example.com:42"},
expect: []network.HostPort{
{network.NewAddress("fc00::1"), 1234},
{network.NewAddress("127.0.0.1"), 4321},
{network.NewAddress("example.com"), 42},
},
}} {
c.Logf("test %d: args %v", i, test.args)
hps, err := network.ParseHostPorts(test.args...)
c.Check(err, jc.ErrorIsNil)
c.Check(hps, jc.DeepEquals, test.expect)
}
}
func (*HostPortSuite) TestAddressesWithPortAndHostsWithoutPort(c *gc.C) {
addrs := network.NewAddresses("0.1.2.3", "0.2.4.6")
hps := network.AddressesWithPort(addrs, 999)
c.Assert(hps, jc.DeepEquals, []network.HostPort{{
Address: network.NewAddress("0.1.2.3"),
Port: 999,
}, {
Address: network.NewAddress("0.2.4.6"),
Port: 999,
}})
c.Assert(network.HostsWithoutPort(hps), jc.DeepEquals, addrs)
}
func (s *HostPortSuite) TestSortHostPorts(c *gc.C) {
hps := s.makeHostPorts()
// Simulate prefer-ipv6: false first.
network.SortHostPorts(hps, false)
c.Assert(hps, jc.DeepEquals, network.NewHostPorts(1234,
// Public IPv4 addresses on top.
"0.1.2.0",
"7.8.8.8",
"8.8.8.8",
// After that public IPv6 addresses.
"2001:db8::1",
"2001:db8::1",
"2001:db8::2",
// Then hostnames.
"example.com",
"example.net",
"example.org",
"invalid host",
"localhost",
"localhost",
// Then IPv4 cloud-local addresses.
"10.0.0.1",
"172.16.0.1",
// Then IPv6 cloud-local addresses.
"fc00::1",
"fd00::22",
// Then machine-local IPv4 addresses.
"127.0.0.1",
"127.0.0.1",
"127.0.1.1",
// Then machine-local IPv6 addresses.
"::1",
"::1",
// Then link-local IPv4 addresses.
"169.254.1.1",
"169.254.1.2",
// Finally, link-local IPv6 addresses.
"fe80::2",
"ff01::22",
))
// Now, simulate prefer-ipv6: true.
network.SortHostPorts(hps, true)
c.Assert(hps, jc.DeepEquals, network.NewHostPorts(1234,
// Public IPv6 addresses on top.
"2001:db8::1",
"2001:db8::1",
"2001:db8::2",
// After that public IPv4 addresses.
"0.1.2.0",
"7.8.8.8",
"8.8.8.8",
// Then hostnames.
"example.com",
"example.net",
"example.org",
"invalid host",
"localhost",
"localhost",
// Then IPv6 cloud-local addresses.
"fc00::1",
"fd00::22",
// Then IPv4 cloud-local addresses.
"10.0.0.1",
"172.16.0.1",
// Then machine-local IPv6 addresses.
"::1",
"::1",
// Then machine-local IPv4 addresses.
"127.0.0.1",
"127.0.0.1",
"127.0.1.1",
// Then link-local IPv6 addresses.
"fe80::2",
"ff01::22",
// Finally, link-local IPv4 addresses.
"169.254.1.1",
"169.254.1.2",
))
}
var netAddrTests = []struct {
addr network.Address
port int
expect string
}{{
addr: network.NewAddress("0.1.2.3"),
port: 99,
expect: "0.1.2.3:99",
}, {
addr: network.NewAddress("2001:DB8::1"),
port: 100,
expect: "[2001:DB8::1]:100",
}, {
addr: network.NewAddress("172.16.0.1"),
port: 52,
expect: "172.16.0.1:52",
}, {
addr: network.NewAddress("fc00::2"),
port: 1111,
expect: "[fc00::2]:1111",
}, {
addr: network.NewAddress("example.com"),
port: 9999,
expect: "example.com:9999",
}, {
addr: network.NewScopedAddress("example.com", network.ScopePublic),
port: 1234,
expect: "example.com:1234",
}, {
addr: network.NewAddress("169.254.1.2"),
port: 123,
expect: "169.254.1.2:123",
}, {
addr: network.NewAddress("fe80::222"),
port: 321,
expect: "[fe80::222]:321",
}, {
addr: network.NewAddress("127.0.0.2"),
port: 121,
expect: "127.0.0.2:121",
}, {
addr: network.NewAddress("::1"),
port: 111,
expect: "[::1]:111",
}}
func (*HostPortSuite) TestNetAddrAndString(c *gc.C) {
for i, test := range netAddrTests {
c.Logf("test %d: %q", i, test.addr)
hp := network.HostPort{
Address: test.addr,
Port: test.port,
}
c.Check(hp.NetAddr(), gc.Equals, test.expect)
c.Check(hp.String(), gc.Equals, test.expect)
c.Check(hp.GoString(), gc.Equals, test.expect)
}
}
func (s *HostPortSuite) TestDropDuplicatedHostPorts(c *gc.C) {
hps := s.makeHostPorts()
noDups := network.DropDuplicatedHostPorts(hps)
c.Assert(noDups, gc.Not(gc.HasLen), len(hps))
c.Assert(noDups, jc.DeepEquals, network.NewHostPorts(1234,
"127.0.0.1",
"localhost",
"example.com",
"127.0.1.1",
"example.org",
"2001:db8::2",
"169.254.1.1",
"example.net",
"invalid host",
"fd00::22",
"2001:db8::1",
"169.254.1.2",
"ff01::22",
"0.1.2.0",
"10.0.0.1",
"::1",
"fc00::1",
"fe80::2",
"172.16.0.1",
"8.8.8.8",
"7.8.8.8",
))
}
func (s *HostPortSuite) TestHostPortsToStrings(c *gc.C) {
hps := s.makeHostPorts()
strHPs := network.HostPortsToStrings(hps)
c.Assert(strHPs, gc.HasLen, len(hps))
c.Assert(strHPs, jc.DeepEquals, []string{
"127.0.0.1:1234",
"localhost:1234",
"example.com:1234",
"127.0.1.1:1234",
"example.org:1234",
"[2001:db8::2]:1234",
"169.254.1.1:1234",
"example.net:1234",
"invalid host:1234",
"[fd00::22]:1234",
"127.0.0.1:1234",
"[2001:db8::1]:1234",
"169.254.1.2:1234",
"[ff01::22]:1234",
"0.1.2.0:1234",
"[2001:db8::1]:1234",
"localhost:1234",
"10.0.0.1:1234",
"[::1]:1234",
"[fc00::1]:1234",
"[fe80::2]:1234",
"172.16.0.1:1234",
"[::1]:1234",
"8.8.8.8:1234",
"7.8.8.8:1234",
})
}
func (*HostPortSuite) makeHostPorts() []network.HostPort {
return network.NewHostPorts(1234,
"127.0.0.1", // machine-local
"localhost", // hostname
"example.com", // hostname
"127.0.1.1", // machine-local
"example.org", // hostname
"2001:db8::2", // public
"169.254.1.1", // link-local
"example.net", // hostname
"invalid host", // hostname
"fd00::22", // cloud-local
"127.0.0.1", // machine-local
"2001:db8::1", // public
"169.254.1.2", // link-local
"ff01::22", // link-local
"0.1.2.0", // public
"2001:db8::1", // public
"localhost", // hostname
"10.0.0.1", // cloud-local
"::1", // machine-local
"fc00::1", // cloud-local
"fe80::2", // link-local
"172.16.0.1", // cloud-local
"::1", // machine-local
"8.8.8.8", // public
"7.8.8.8", // public
)
}