-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional port to host mappings and fix IPv6 (#1489)
Add optional port to host mappings. That way people can map a name under different ports. This is very convenient when you run k6 against a fleet of docker containers to test the same service but want to keep the port mapping. This also fixes the IPv6 support in k6. Signed-off-by: David Calavera <[email protected]>
- Loading branch information
Showing
6 changed files
with
362 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* | ||
* k6 - a next-generation load testing tool | ||
* Copyright (C) 2020 Load Impact | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package netext | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/loadimpact/k6/lib" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testResolver struct { | ||
hosts map[string]net.IP | ||
} | ||
|
||
func (r testResolver) FetchOne(host string) (net.IP, error) { return r.hosts[host], nil } | ||
|
||
func TestDialerAddr(t *testing.T) { | ||
dialer := newDialerWithResolver(net.Dialer{}, newResolver()) | ||
dialer.Hosts = map[string]*lib.HostAddress{ | ||
"example.com": {IP: net.ParseIP("3.4.5.6")}, | ||
"example.com:443": {IP: net.ParseIP("3.4.5.6"), Port: 8443}, | ||
"example.com:8080": {IP: net.ParseIP("3.4.5.6"), Port: 9090}, | ||
"example-deny-host.com": {IP: net.ParseIP("8.9.10.11")}, | ||
"example-ipv6.com": {IP: net.ParseIP("2001:db8::68")}, | ||
"example-ipv6.com:443": {IP: net.ParseIP("2001:db8::68"), Port: 8443}, | ||
"example-ipv6-deny-host.com": {IP: net.ParseIP("::1")}, | ||
} | ||
|
||
ipNet, err := lib.ParseCIDR("8.9.10.0/24") | ||
require.NoError(t, err) | ||
|
||
ipV6Net, err := lib.ParseCIDR("::1/24") | ||
require.NoError(t, err) | ||
|
||
dialer.Blacklist = []*lib.IPNet{ipNet, ipV6Net} | ||
|
||
testCases := []struct { | ||
address, expAddress, expErr string | ||
}{ | ||
// IPv4 | ||
{"example-resolver.com:80", "1.2.3.4:80", ""}, | ||
{"example.com:80", "3.4.5.6:80", ""}, | ||
{"example.com:443", "3.4.5.6:8443", ""}, | ||
{"example.com:8080", "3.4.5.6:9090", ""}, | ||
{"1.2.3.4:80", "1.2.3.4:80", ""}, | ||
{"1.2.3.4", "", "address 1.2.3.4: missing port in address"}, | ||
{"example-deny-resolver.com:80", "", "IP (8.9.10.11) is in a blacklisted range (8.9.10.0/24)"}, | ||
{"example-deny-host.com:80", "", "IP (8.9.10.11) is in a blacklisted range (8.9.10.0/24)"}, | ||
{"no-such-host.com:80", "", "lookup no-such-host.com: no such host"}, | ||
|
||
// IPv6 | ||
{"example-ipv6.com:443", "[2001:db8::68]:8443", ""}, | ||
{"[2001:db8:aaaa:1::100]:443", "[2001:db8:aaaa:1::100]:443", ""}, | ||
{"[::1.2.3.4]", "", "address [::1.2.3.4]: missing port in address"}, | ||
{"example-ipv6-deny-resolver.com:80", "", "IP (::1) is in a blacklisted range (::/24)"}, | ||
{"example-ipv6-deny-host.com:80", "", "IP (::1) is in a blacklisted range (::/24)"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.address, func(t *testing.T) { | ||
addr, err := dialer.getDialAddr(tc.address) | ||
|
||
if tc.expErr != "" { | ||
require.EqualError(t, err, tc.expErr) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expAddress, addr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func newResolver() testResolver { | ||
return testResolver{ | ||
hosts: map[string]net.IP{ | ||
"example-resolver.com": net.ParseIP("1.2.3.4"), | ||
"example-deny-resolver.com": net.ParseIP("8.9.10.11"), | ||
"example-ipv6-deny-resolver.com": net.ParseIP("::1"), | ||
}, | ||
} | ||
} |
Oops, something went wrong.