forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract host resolution from Dialer, add test
Resolves grafana#1562 (comment)
- Loading branch information
Ivan Mirić
committed
Aug 5, 2020
1 parent
048b16a
commit 4c44b48
Showing
2 changed files
with
63 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package netext | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/viki-org/dnscache" | ||
) | ||
|
||
func TestDialerResolveHost(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
host string | ||
hosts map[string]net.IP | ||
ipVer int | ||
expErr string | ||
}{ | ||
{"1.2.3.4", nil, 4, ""}, | ||
{"256.1.1.1", nil, 4, "lookup 256.1.1.1: no such host"}, | ||
{"example.com", nil, 4, ""}, | ||
{"::1", nil, 6, ""}, | ||
{"::1.2.3.4", nil, 6, ""}, | ||
{"abcd:ef01:2345:6789", nil, 6, "lookup abcd:ef01:2345:6789: no such host"}, | ||
{"2001:db8:aaaa:1::100", nil, 6, ""}, | ||
{"ipv6.google.com", nil, 6, ""}, | ||
{"mycustomv4.host", map[string]net.IP{"mycustomv4.host": net.ParseIP("1.2.3.4")}, 4, ""}, | ||
{"mycustomv6.host", map[string]net.IP{"mycustomv6.host": net.ParseIP("::1")}, 6, ""}, | ||
} | ||
|
||
resolver := dnscache.New(0) | ||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.host, func(t *testing.T) { | ||
ip, err := resolveHost(tc.host, tc.hosts, resolver) | ||
if tc.expErr != "" { | ||
assert.EqualError(t, err, tc.expErr) | ||
return | ||
} | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.ipVer == 6, ip.To4() == nil) | ||
}) | ||
} | ||
} |