Skip to content

Commit

Permalink
Requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
eugercek committed Dec 4, 2022
1 parent 120436b commit 1b1c328
Show file tree
Hide file tree
Showing 20 changed files with 590 additions and 406 deletions.
4 changes: 2 additions & 2 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ func TestSentReceivedMetrics(t *testing.T) {
test := newTestEngine(t, nil, r, []output.Output{mockOutput}, lib.Options{
Iterations: null.IntFrom(tc.Iterations),
VUs: null.IntFrom(tc.VUs),
Hosts: types.NullAddressTrie{Trie: tb.Dialer.Hosts, Valid: true},
Hosts: types.NullHosts{Trie: tb.Dialer.Hosts, Valid: true},
InsecureSkipTLSVerify: null.BoolFrom(true),
NoVUConnectionReuse: null.BoolFrom(noConnReuse),
Batch: null.IntFrom(20),
Expand Down Expand Up @@ -795,7 +795,7 @@ func TestRunTags(t *testing.T) {
test := newTestEngine(t, nil, r, []output.Output{mockOutput}, lib.Options{
Iterations: null.IntFrom(3),
VUs: null.IntFrom(2),
Hosts: types.NullAddressTrie{Trie: tb.Dialer.Hosts, Valid: true},
Hosts: types.NullHosts{Trie: tb.Dialer.Hosts, Valid: true},
RunTags: runTags,
SystemTags: &metrics.DefaultSystemTagSet,
InsecureSkipTLSVerify: null.BoolFrom(true),
Expand Down
4 changes: 2 additions & 2 deletions js/http_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func BenchmarkHTTPRequests(b *testing.B) {
err = r.SetOptions(lib.Options{
Throw: null.BoolFrom(true),
MaxRedirects: null.IntFrom(10),
Hosts: types.NullAddressTrie{Trie: tb.Dialer.Hosts},
Hosts: types.NullHosts{Trie: tb.Dialer.Hosts},
NoCookiesReset: null.BoolFrom(true),
SystemTags: &metrics.DefaultSystemTagSet,
RunTags: map[string]string{"myapp": "myhttpbench"},
Expand Down Expand Up @@ -72,7 +72,7 @@ func BenchmarkHTTPRequestsBase(b *testing.B) {
err = r.SetOptions(lib.Options{
Throw: null.BoolFrom(true),
MaxRedirects: null.IntFrom(10),
Hosts: types.NullAddressTrie{Trie: tb.Dialer.Hosts},
Hosts: types.NullHosts{Trie: tb.Dialer.Hosts},
NoCookiesReset: null.BoolFrom(true),
})
require.NoError(b, err)
Expand Down
2 changes: 1 addition & 1 deletion js/module_loading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestLoadDoesntBreakHTTPGet(t *testing.T) {
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

require.NoError(t, r1.SetOptions(lib.Options{Hosts: types.NullAddressTrie{Trie: tb.Dialer.Hosts}}))
require.NoError(t, r1.SetOptions(lib.Options{Hosts: types.NullHosts{Trie: tb.Dialer.Hosts}}))
arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down
16 changes: 10 additions & 6 deletions js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,16 @@ func TestOptionsTestFull(t *testing.T) {
require.NoError(t, err)
return bh
}(),
Hosts: types.NewNullAddressTrie(map[string]types.HostAddress{
"test.k6.io": {
IP: []byte{0x01, 0x02, 0x03, 0x04},
Port: 8443,
},
}),
Hosts: func() types.NullHosts {
hs, err := types.NewNullHosts(map[string]types.Host{
"test.k6.io": {
IP: []byte{0x01, 0x02, 0x03, 0x04},
Port: 8443,
},
})
require.NoError(t, err)
return hs
}(),
External: map[string]json.RawMessage{
"ext-one": json.RawMessage(`{"rawkey":"rawvalue"}`),
},
Expand Down
26 changes: 18 additions & 8 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2360,11 +2360,17 @@ func TestRequestAndBatchTLS(t *testing.T) {
host, port, err := net.SplitHostPort(s.Listener.Addr().String())
require.NoError(t, err)
ip := net.ParseIP(host)
mybadsslHostname, err := types.NewHostAddress(ip, port)
mybadsslHostname, err := types.NewHost(ip, port)
require.NoError(t, err)
state.Transport = client.Transport
state.TLSConfig = s.TLS
state.Dialer = &netext.Dialer{Hosts: types.NewAddressTrie(map[string]types.HostAddress{"expired.localhost": *mybadsslHostname})}
state.Dialer = &netext.Dialer{
Hosts: func() *types.Hosts {
hosts, err := types.NewHosts(map[string]types.Host{"expired.localhost": *mybadsslHostname})
require.NoError(t, err)
return hosts
}(),
}
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext //nolint:forcetypeassert
_, err = rt.RunString(`throw JSON.stringify(http.get("https://expired.localhost/"));`)
require.Error(t, err)
Expand Down Expand Up @@ -2404,11 +2410,13 @@ func TestRequestAndBatchTLS(t *testing.T) {
host, port, err := net.SplitHostPort(s.Listener.Addr().String())
require.NoError(t, err)
ip := net.ParseIP(host)
mybadsslHostname, err := types.NewHostAddress(ip, port)
mybadsslHostname, err := types.NewHost(ip, port)
require.NoError(t, err)
state.Dialer = &netext.Dialer{Hosts: types.NewAddressTrie(map[string]types.HostAddress{
hosts, err := types.NewHosts(map[string]types.Host{
versionTest.URL: *mybadsslHostname,
})}
})
require.NoError(t, err)
state.Dialer = &netext.Dialer{Hosts: hosts}
state.Transport = client.Transport
state.TLSConfig = s.TLS
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext //nolint:forcetypeassert
Expand Down Expand Up @@ -2444,11 +2452,13 @@ func TestRequestAndBatchTLS(t *testing.T) {
host, port, err := net.SplitHostPort(s.Listener.Addr().String())
require.NoError(t, err)
ip := net.ParseIP(host)
mybadsslHostname, err := types.NewHostAddress(ip, port)
mybadsslHostname, err := types.NewHost(ip, port)
require.NoError(t, err)
state.Dialer = &netext.Dialer{Hosts: types.NewAddressTrie(map[string]types.HostAddress{
hosts, err := types.NewHosts(map[string]types.Host{
cipherSuiteTest.URL: *mybadsslHostname,
})}
})
require.NoError(t, err)
state.Dialer = &netext.Dialer{Hosts: hosts}
state.Transport = client.Transport
state.TLSConfig = s.TLS
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext //nolint:forcetypeassert
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/marshalling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestSetupDataMarshalling(t *testing.T) {

err = runner.SetOptions(lib.Options{
SetupTimeout: types.NullDurationFrom(5 * time.Second),
Hosts: types.NullAddressTrie{Trie: tb.Dialer.Hosts},
Hosts: types.NullHosts{Trie: tb.Dialer.Hosts},
})
require.NoError(t, err)

Expand Down
27 changes: 17 additions & 10 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ func TestVUIntegrationInsecureRequests(t *testing.T) {
host, port, err := net.SplitHostPort(s.Listener.Addr().String())
require.NoError(t, err)
ip := net.ParseIP(host)
mybadsslHostname, err := types.NewHostAddress(ip, port)
mybadsslHostname, err := types.NewHost(ip, port)
require.NoError(t, err)
cert, err := x509.ParseCertificate(s.TLS.Certificates[0].Certificate[0])
require.NoError(t, err)
Expand Down Expand Up @@ -1033,9 +1033,10 @@ func TestVUIntegrationInsecureRequests(t *testing.T) {
require.NoError(t, err)
require.NoError(t, r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts)))

r1.Bundle.Options.Hosts = types.NewNullAddressTrie(map[string]types.HostAddress{
r1.Bundle.Options.Hosts, err = types.NewNullHosts(map[string]types.Host{
"mybadssl.localhost": *mybadsslHostname,
})
require.NoError(t, err)
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
Expand Down Expand Up @@ -1261,9 +1262,14 @@ func TestVUIntegrationHosts(t *testing.T) {

r1.SetOptions(lib.Options{
Throw: null.BoolFrom(true),
Hosts: types.NewNullAddressTrie(map[string]types.HostAddress{
"test.loadimpact.com": {IP: net.ParseIP("127.0.0.1")},
}),
Hosts: func() types.NullHosts {
hosts, err := types.NewNullHosts(map[string]types.Host{
"test.loadimpact.com": {IP: net.ParseIP("127.0.0.1")},
})
require.NoError(t, err)

return hosts
}(),
})

registry := metrics.NewRegistry()
Expand Down Expand Up @@ -1306,7 +1312,7 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
host, port, err := net.SplitHostPort(s.Listener.Addr().String())
require.NoError(t, err)
ip := net.ParseIP(host)
mybadsslHostname, err := types.NewHostAddress(ip, port)
mybadsslHostname, err := types.NewHost(ip, port)
require.NoError(t, err)
unsupportedVersionErrorMsg := "remote error: tls: handshake failure"
for _, tag := range build.Default.ReleaseTags {
Expand Down Expand Up @@ -1364,9 +1370,10 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
opts := lib.Options{Throw: null.BoolFrom(true)}
require.NoError(t, r1.SetOptions(opts.Apply(data.opts)))

r1.Bundle.Options.Hosts = types.NewNullAddressTrie(map[string]types.HostAddress{
r1.Bundle.Options.Hosts, err = types.NewNullHosts(map[string]types.Host{
"sha256-badssl.localhost": *mybadsslHostname,
})
require.NoError(t, err)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
Expand Down Expand Up @@ -1534,7 +1541,7 @@ func TestVUIntegrationCookiesReset(t *testing.T) {
require.NoError(t, err)
r1.Bundle.Options.Throw = null.BoolFrom(true)
r1.Bundle.Options.MaxRedirects = null.IntFrom(10)
r1.Bundle.Options.Hosts = types.NullAddressTrie{Trie: tb.Dialer.Hosts}
r1.Bundle.Options.Hosts = types.NullHosts{Trie: tb.Dialer.Hosts}

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down Expand Up @@ -1592,7 +1599,7 @@ func TestVUIntegrationCookiesNoReset(t *testing.T) {
r1.SetOptions(lib.Options{
Throw: null.BoolFrom(true),
MaxRedirects: null.IntFrom(10),
Hosts: types.NullAddressTrie{Trie: tb.Dialer.Hosts},
Hosts: types.NullHosts{Trie: tb.Dialer.Hosts},
NoCookiesReset: null.BoolFrom(true),
})

Expand Down Expand Up @@ -2531,7 +2538,7 @@ func TestForceHTTP1Feature(t *testing.T) {
require.NoError(t, err)

err = r1.SetOptions(lib.Options{
Hosts: types.NullAddressTrie{Trie: tb.Dialer.Hosts},
Hosts: types.NullHosts{Trie: tb.Dialer.Hosts},
// We disable TLS verify so that we don't get a TLS handshake error since
// the certificates on the endpoint are not certified by a certificate authority
InsecureSkipTLSVerify: null.BoolFrom(true),
Expand Down
10 changes: 5 additions & 5 deletions lib/netext/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Dialer struct {
Resolver Resolver
Blacklist []*lib.IPNet
BlockedHostnames *types.HostnameTrie
Hosts *types.AddressTrie
Hosts *types.Hosts

BytesRead int64
BytesWritten int64
Expand Down Expand Up @@ -148,7 +148,7 @@ func (d *Dialer) getDialAddr(addr string) (string, error) {
return remote.String(), nil
}

func (d *Dialer) findRemote(addr string) (*types.HostAddress, error) {
func (d *Dialer) findRemote(addr string) (*types.Host, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
Expand All @@ -169,7 +169,7 @@ func (d *Dialer) findRemote(addr string) (*types.HostAddress, error) {
}

if ip != nil {
return types.NewHostAddress(ip, port)
return types.NewHost(ip, port)
}

ip, err = d.Resolver.LookupIP(host)
Expand All @@ -181,10 +181,10 @@ func (d *Dialer) findRemote(addr string) (*types.HostAddress, error) {
return nil, fmt.Errorf("lookup %s: no such host", host)
}

return types.NewHostAddress(ip, port)
return types.NewHost(ip, port)
}

func (d *Dialer) getConfiguredHost(addr, host, port string) (*types.HostAddress, error) {
func (d *Dialer) getConfiguredHost(addr, host, port string) (*types.Host, error) {
if remote := d.Hosts.Match(addr); remote != nil {
return remote, nil
}
Expand Down
14 changes: 9 additions & 5 deletions lib/netext/dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
func TestDialerAddr(t *testing.T) {
t.Parallel()
dialer := NewDialer(net.Dialer{}, newResolver())
dialer.Hosts = types.NewAddressTrie(
map[string]types.HostAddress{
hosts, err := types.NewHosts(
map[string]types.Host{
"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},
Expand All @@ -24,7 +24,8 @@ func TestDialerAddr(t *testing.T) {
"example-ipv6.com:443": {IP: net.ParseIP("2001:db8::68"), Port: 8443},
"example-ipv6-deny-host.com": {IP: net.ParseIP("::1")},
})

require.NoError(t, err)
dialer.Hosts = hosts
ipNet, err := lib.ParseCIDR("8.9.10.0/24")
require.NoError(t, err)

Expand Down Expand Up @@ -76,9 +77,11 @@ func TestDialerAddr(t *testing.T) {
func TestDialerAddrBlockHostnamesStar(t *testing.T) {
t.Parallel()
dialer := NewDialer(net.Dialer{}, newResolver())
dialer.Hosts = types.NewAddressTrie(map[string]types.HostAddress{
hosts, err := types.NewHosts(map[string]types.Host{
"example.com": {IP: net.ParseIP("3.4.5.6")},
})
require.NoError(t, err)
dialer.Hosts = hosts

blocked, err := types.NewHostnameTrie([]string{"*"})
require.NoError(t, err)
Expand Down Expand Up @@ -112,12 +115,13 @@ func TestDialerAddrBlockHostnamesStar(t *testing.T) {

// Benchmarks /etc/hosts like hostname mapping
func BenchmarkDialerHosts(b *testing.B) {
hosts := types.NewAddressTrie(map[string]types.HostAddress{
hosts, err := types.NewHosts(map[string]types.Host{
"k6.io": {IP: []byte("192.168.1.1"), Port: 80},
"specific.k6.io": {IP: []byte("192.168.1.2"), Port: 80},
"grafana.com": {IP: []byte("aa::ff"), Port: 80},
"specific.grafana.com": {IP: []byte("aa:bb:::ff"), Port: 80},
})
require.NoError(b, err)

dialer := Dialer{
Dialer: net.Dialer{},
Expand Down
11 changes: 6 additions & 5 deletions lib/netext/httpext/error_codes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ func TestX509HostnameError(t *testing.T) {
}
var err error
badHostname := "somewhere.else"
badHostAddress, err := types.NewHostAddress(net.ParseIP(tb.Replacer.Replace("HTTPSBIN_IP")), "")
badHost, err := types.NewHost(net.ParseIP(tb.Replacer.Replace("HTTPSBIN_IP")), "")

tb.Dialer.Hosts = types.NewAddressTrie(map[string]types.HostAddress{
badHostname: *badHostAddress,
tb.Dialer.Hosts, err = types.NewHosts(map[string]types.Host{
badHostname: *badHost,
})
require.NoError(t, err)
req, err := http.NewRequestWithContext(context.Background(), "GET", tb.Replacer.Replace("https://"+badHostname+":HTTPSBIN_PORT/get"), nil)
Expand Down Expand Up @@ -350,7 +350,7 @@ func getHTTP2ServerWithCustomConnContext(t *testing.T) *httpmultibin.HTTPMultiBi
require.NoError(t, err)
http2IP := net.ParseIP(http2URL.Hostname())
require.NotNil(t, http2IP)
http2DomainValue, err := types.NewHostAddress(http2IP, "")
http2DomainValue, err := types.NewHost(http2IP, "")
require.NoError(t, err)

// Set up the dialer with shorter timeouts and the custom domains
Expand All @@ -359,9 +359,10 @@ func getHTTP2ServerWithCustomConnContext(t *testing.T) *httpmultibin.HTTPMultiBi
KeepAlive: 10 * time.Second,
DualStack: true,
}, netext.NewResolver(net.LookupIP, 0, types.DNSfirst, types.DNSpreferIPv4))
dialer.Hosts = types.NewAddressTrie(map[string]types.HostAddress{
dialer.Hosts, err = types.NewHosts(map[string]types.Host{
http2Domain: *http2DomainValue,
})
require.NoError(t, err)

transport := &http.Transport{
DialContext: dialer.DialContext,
Expand Down
2 changes: 1 addition & 1 deletion lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ type Options struct {
BlockedHostnames types.NullHostnameTrie `json:"blockHostnames" envconfig:"K6_BLOCK_HOSTNAMES"`

// Hosts overrides dns entries for given hosts
Hosts types.NullAddressTrie `json:"hosts" envconfig:"K6_HOSTS"`
Hosts types.NullHosts `json:"hosts" envconfig:"K6_HOSTS"`

// Disable keep-alive connections
NoConnectionReuse null.Bool `json:"noConnectionReuse" envconfig:"K6_NO_CONNECTION_REUSE"`
Expand Down
Loading

0 comments on commit 1b1c328

Please sign in to comment.