Skip to content

Commit

Permalink
Move DNS config types to lib/types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Oct 15, 2020
1 parent e048040 commit caf82ab
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 288 deletions.
3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/executor"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
"github.com/loadimpact/k6/stats/cloud"
"github.com/loadimpact/k6/stats/csv"
Expand Down Expand Up @@ -259,7 +260,7 @@ func applyDefault(conf Config) Config {
if conf.Options.SummaryTrendStats == nil {
conf.Options.SummaryTrendStats = lib.DefaultSummaryTrendStats
}
defDNS := lib.DefaultDNSConfig()
defDNS := types.DefaultDNSConfig()
if !conf.DNS.TTL.Valid {
conf.DNS.TTL = defDNS.TTL
}
Expand Down
48 changes: 24 additions & 24 deletions cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,51 +383,51 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {
},
},
{opts{cli: []string{}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
assert.Equal(t, types.DNSConfig{
TTL: null.NewString("5m", false),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSRandom, Valid: false},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSpreferIPv4, Valid: false},
Select: types.NullDNSSelect{DNSSelect: types.DNSRandom, Valid: false},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},
}, c.Options.DNS)
}},
{opts{env: []string{"K6_DNS=ttl=5,select=round-robin"}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
assert.Equal(t, types.DNSConfig{
TTL: null.StringFrom("5"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSRoundRobin, Valid: true},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSpreferIPv4, Valid: false},
Select: types.NullDNSSelect{DNSSelect: types.DNSRoundRobin, Valid: true},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},
}, c.Options.DNS)
}},
{opts{env: []string{"K6_DNS=ttl=inf,select=random,policy=preferIPv6"}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
assert.Equal(t, types.DNSConfig{
TTL: null.StringFrom("inf"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSRandom, Valid: true},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSpreferIPv6, Valid: true},
Select: types.NullDNSSelect{DNSSelect: types.DNSRandom, Valid: true},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv6, Valid: true},
}, c.Options.DNS)
}},
// This is functionally invalid, but will error out in validation done in js.parseTTL().
{opts{cli: []string{"--dns", "ttl=-1"}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
assert.Equal(t, types.DNSConfig{
TTL: null.StringFrom("-1"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSRandom, Valid: false},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSpreferIPv4, Valid: false},
Select: types.NullDNSSelect{DNSSelect: types.DNSRandom, Valid: false},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},
}, c.Options.DNS)
}},
{opts{cli: []string{"--dns", "ttl=0,blah=nope"}}, exp{cliReadError: true}, nil},
{opts{cli: []string{"--dns", "ttl=0"}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
assert.Equal(t, types.DNSConfig{
TTL: null.StringFrom("0"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSRandom, Valid: false},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSpreferIPv4, Valid: false},
Select: types.NullDNSSelect{DNSSelect: types.DNSRandom, Valid: false},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},
}, c.Options.DNS)
}},
{opts{cli: []string{"--dns", "ttl=5s,select="}}, exp{cliReadError: true}, nil},
{
opts{fs: defaultConfig(`{"dns": {"ttl": "0", "select": "round-robin", "policy": "onlyIPv4"}}`)},
exp{},
func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
assert.Equal(t, types.DNSConfig{
TTL: null.StringFrom("0"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSRoundRobin, Valid: true},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSonlyIPv4, Valid: true},
Select: types.NullDNSSelect{DNSSelect: types.DNSRoundRobin, Valid: true},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSonlyIPv4, Valid: true},
}, c.Options.DNS)
},
},
Expand All @@ -438,10 +438,10 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {
},
exp{},
func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
assert.Equal(t, types.DNSConfig{
TTL: null.StringFrom("30"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSRandom, Valid: false},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSany, Valid: true},
Select: types.NullDNSSelect{DNSSelect: types.DNSRandom, Valid: false},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSany, Valid: true},
}, c.Options.DNS)
},
},
Expand All @@ -454,10 +454,10 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {
},
exp{},
func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
assert.Equal(t, types.DNSConfig{
TTL: null.StringFrom("5"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSRandom, Valid: true},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSany, Valid: true},
Select: types.NullDNSSelect{DNSSelect: types.DNSRandom, Valid: true},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSany, Valid: true},
}, c.Options.DNS)
},
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func optionFlagSet() *pflag.FlagSet {
flags.StringSlice("tag", nil, "add a `tag` to be applied to all samples, as `[name]=[value]`")
flags.String("console-output", "", "redirects the console logging to the provided output file")
flags.Bool("discard-response-bodies", false, "Read but don't process or save HTTP response bodies")
flags.String("dns", lib.DefaultDNSConfig().String(), "DNS configuration. Possible ttl values are: 'inf' "+
flags.String("dns", types.DefaultDNSConfig().String(), "DNS resolver configuration. Possible ttl values are: 'inf' "+
"for a persistent cache, '0' to disable the cache,\nor a positive duration, e.g. '1s', '1m', etc. "+
"Milliseconds are assumed if no unit is provided.\n"+
"Possible select values to return a single IP are: 'first', 'random' or 'round-robin'.\n"+
Expand Down
22 changes: 11 additions & 11 deletions core/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,27 +1002,27 @@ func TestDNSResolver(t *testing.T) {
expLogEntries int
}{
"default": { // IPs are cached for 5m
lib.Options{DNS: lib.DefaultDNSConfig()}, 0,
lib.Options{DNS: types.DefaultDNSConfig()}, 0,
},
"0": { // cache is disabled, every request does a DNS lookup
lib.Options{DNS: lib.DNSConfig{
lib.Options{DNS: types.DNSConfig{
TTL: null.StringFrom("0"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSFirst, Valid: true},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSpreferIPv4, Valid: false},
Select: types.NullDNSSelect{DNSSelect: types.DNSFirst, Valid: true},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},
}}, 5,
},
"1000": { // cache IPs for 1s, check that unitless values are interpreted as ms
lib.Options{DNS: lib.DNSConfig{
lib.Options{DNS: types.DNSConfig{
TTL: null.StringFrom("1000"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSFirst, Valid: true},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSpreferIPv4, Valid: false},
Select: types.NullDNSSelect{DNSSelect: types.DNSFirst, Valid: true},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},
}}, 4,
},
"3s": {
lib.Options{DNS: lib.DNSConfig{
lib.Options{DNS: types.DNSConfig{
TTL: null.StringFrom("3s"),
Select: lib.NullDNSSelect{DNSSelect: lib.DNSFirst, Valid: true},
Policy: lib.NullDNSPolicy{DNSPolicy: lib.DNSpreferIPv4, Valid: false},
Select: types.NullDNSSelect{DNSSelect: types.DNSFirst, Valid: true},
Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},
}}, 3,
},
}
Expand Down Expand Up @@ -1204,7 +1204,7 @@ func TestRealTimeAndSetupTeardownMetrics(t *testing.T) {
expTags = append(expTags, addExpTags...)
return netext.NewDialer(
net.Dialer{},
netext.NewResolver(net.LookupIP, 0, lib.DNSFirst, lib.DNSpreferIPv4),
netext.NewResolver(net.LookupIP, 0, types.DNSFirst, types.DNSpreferIPv4),
).GetTrail(time.Now(), time.Now(),
true, emitIterations, getTags(expTags...))
}
Expand Down
3 changes: 2 additions & 1 deletion js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/loadimpact/k6/lib/consts"
"github.com/loadimpact/k6/lib/netext"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
)

Expand Down Expand Up @@ -394,7 +395,7 @@ func TestRequestWithBinaryFile(t *testing.T) {
KeepAlive: 60 * time.Second,
DualStack: true,
},
netext.NewResolver(net.LookupIP, 0, lib.DNSFirst, lib.DNSpreferIPv4),
netext.NewResolver(net.LookupIP, 0, types.DNSFirst, types.DNSpreferIPv4),
)).DialContext,
},
BPool: bpool.NewBufferPool(1),
Expand Down
10 changes: 5 additions & 5 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func newFromBundle(logger *logrus.Logger, b *Bundle) (*Runner, error) {
return nil, err
}

defDNS := lib.DefaultDNSConfig()
defDNS := types.DefaultDNSConfig()
r := &Runner{
Bundle: b,
Logger: logger,
Expand Down Expand Up @@ -340,19 +340,19 @@ func (r *Runner) SetOptions(opts lib.Options) error {
return nil
}

func (r *Runner) setResolver(dns lib.DNSConfig) error {
func (r *Runner) setResolver(dns types.DNSConfig) error {
ttl, err := parseTTL(dns.TTL.String)
if err != nil {
return err
}

dnsSel := dns.Select
if !dnsSel.Valid {
dnsSel = lib.DefaultDNSConfig().Select
dnsSel = types.DefaultDNSConfig().Select
}
dnsPol := dns.Policy
if !dnsPol.Valid {
dnsPol = lib.DefaultDNSConfig().Policy
dnsPol = types.DefaultDNSConfig().Policy
}

r.Resolver = netext.NewResolver(
Expand All @@ -370,7 +370,7 @@ func parseTTL(ttlS string) (time.Duration, error) {
case "0":
// disable cache
case "":
ttlS = lib.DefaultDNSConfig().TTL.String
ttlS = types.DefaultDNSConfig().TTL.String
fallthrough
default:
var err error
Expand Down
4 changes: 2 additions & 2 deletions lib/netext/httpext/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/lib/netext"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
)

Expand All @@ -58,7 +58,7 @@ func TestTracer(t *testing.T) {
assert.True(t, ok)
transport.DialContext = netext.NewDialer(
net.Dialer{},
netext.NewResolver(net.LookupIP, 0, lib.DNSFirst, lib.DNSpreferIPv4),
netext.NewResolver(net.LookupIP, 0, types.DNSFirst, types.DNSpreferIPv4),
).DialContext

var prev int64
Expand Down
24 changes: 12 additions & 12 deletions lib/netext/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"sync"
"time"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/types"
)

// MultiResolver returns all IP addresses for the given host.
Expand All @@ -39,8 +39,8 @@ type Resolver interface {

type resolver struct {
resolve MultiResolver
selectIndex lib.DNSSelect
policy lib.DNSPolicy
selectIndex types.DNSSelect
policy types.DNSPolicy
rrm *sync.Mutex
rand *rand.Rand
roundRobin map[string]uint8
Expand All @@ -62,7 +62,7 @@ type cacheResolver struct {
// will be cached per host for the specified period. The IP returned from
// LookupIP() will be selected based on the given sel and pol values.
func NewResolver(
actRes MultiResolver, ttl time.Duration, sel lib.DNSSelect, pol lib.DNSPolicy,
actRes MultiResolver, ttl time.Duration, sel types.DNSSelect, pol types.DNSPolicy,
) Resolver {
r := rand.New(rand.NewSource(time.Now().UnixNano())) // nolint: gosec
res := resolver{
Expand Down Expand Up @@ -131,16 +131,16 @@ func (r *resolver) selectOne(host string, ips []net.IP) net.IP {

var ip net.IP
switch r.selectIndex {
case lib.DNSFirst:
case types.DNSFirst:
return ips[0]
case lib.DNSRoundRobin:
case types.DNSRoundRobin:
r.rrm.Lock()
// NOTE: This index approach is not stable and might result in returning
// repeated or skipped IPs if the records change during a test run.
ip = ips[int(r.roundRobin[host])%len(ips)]
r.roundRobin[host]++
r.rrm.Unlock()
case lib.DNSRandom:
case types.DNSRandom:
r.rrm.Lock()
ip = ips[r.rand.Intn(len(ips))]
r.rrm.Unlock()
Expand All @@ -150,24 +150,24 @@ func (r *resolver) selectOne(host string, ips []net.IP) net.IP {
}

func (r *resolver) applyPolicy(ips []net.IP) (retIPs []net.IP) {
if r.policy == lib.DNSany {
if r.policy == types.DNSany {
return ips
}
ip4, ip6 := groupByVersion(ips)
switch r.policy { //nolint: exhaustive
case lib.DNSpreferIPv4:
case types.DNSpreferIPv4:
retIPs = ip6
if len(ip4) > 0 {
retIPs = ip4
}
case lib.DNSpreferIPv6:
case types.DNSpreferIPv6:
retIPs = ip4
if len(ip6) > 0 {
retIPs = ip6
}
case lib.DNSonlyIPv4:
case types.DNSonlyIPv4:
retIPs = ip4
case lib.DNSonlyIPv6:
case types.DNSonlyIPv6:
retIPs = ip6
}

Expand Down
18 changes: 9 additions & 9 deletions lib/netext/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/testutils/mockresolver"
"github.com/loadimpact/k6/lib/types"
)

func TestResolver(t *testing.T) {
Expand All @@ -51,29 +51,29 @@ func TestResolver(t *testing.T) {
t.Run("LookupIP", func(t *testing.T) {
testCases := []struct {
ttl time.Duration
sel lib.DNSSelect
pol lib.DNSPolicy
sel types.DNSSelect
pol types.DNSPolicy
expIP []net.IP
}{
{
0, lib.DNSFirst, lib.DNSpreferIPv4,
0, types.DNSFirst, types.DNSpreferIPv4,
[]net.IP{net.ParseIP("127.0.0.10")},
},
{
time.Second, lib.DNSFirst, lib.DNSpreferIPv4,
time.Second, types.DNSFirst, types.DNSpreferIPv4,
[]net.IP{net.ParseIP("127.0.0.10")},
},
{0, lib.DNSRoundRobin, lib.DNSonlyIPv6, []net.IP{
{0, types.DNSRoundRobin, types.DNSonlyIPv6, []net.IP{
net.ParseIP("2001:db8::10"),
net.ParseIP("2001:db8::11"),
net.ParseIP("2001:db8::12"),
net.ParseIP("2001:db8::10"),
}},
{
0, lib.DNSFirst, lib.DNSpreferIPv6,
0, types.DNSFirst, types.DNSpreferIPv6,
[]net.IP{net.ParseIP("2001:db8::10")},
},
{0, lib.DNSRoundRobin, lib.DNSpreferIPv4, []net.IP{
{0, types.DNSRoundRobin, types.DNSpreferIPv4, []net.IP{
net.ParseIP("127.0.0.10"),
net.ParseIP("127.0.0.11"),
net.ParseIP("127.0.0.12"),
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestResolver(t *testing.T) {
assert.True(t, cr.cache[host].lastLookup.After(firstLookup))
}

if tc.sel == lib.DNSRoundRobin {
if tc.sel == types.DNSRoundRobin {
ips := []net.IP{ip}
for i := 0; i < 3; i++ {
ip, err = r.LookupIP(host)
Expand Down
Loading

0 comments on commit caf82ab

Please sign in to comment.