From eea16e1640ff57fd2ea08ce91af12f0212d64775 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Tue, 23 May 2017 21:05:38 +0200 Subject: [PATCH] test: run mock dns recursors on random ports --- command/agent/dns_test.go | 3 ++- command/agent/testagent.go | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/command/agent/dns_test.go b/command/agent/dns_test.go index 577281f2dd..3ce20d1539 100644 --- a/command/agent/dns_test.go +++ b/command/agent/dns_test.go @@ -32,8 +32,9 @@ const ( // the provided reply. This is useful for mocking a DNS recursor with // an expected result. func makeRecursor(t *testing.T, answer []dns.RR) *dns.Server { + randomPort := TenPorts() cfg := TestConfig() - dnsAddr := fmt.Sprintf("%s:%d", cfg.Addresses.DNS, cfg.Ports.DNS) + dnsAddr := fmt.Sprintf("%s:%d", cfg.Addresses.DNS, randomPort) mux := dns.NewServeMux() mux.HandleFunc(".", func(resp dns.ResponseWriter, msg *dns.Msg) { ans := &dns.Msg{Answer: answer[:]} diff --git a/command/agent/testagent.go b/command/agent/testagent.go index 1bf0746167..65520bfe77 100644 --- a/command/agent/testagent.go +++ b/command/agent/testagent.go @@ -242,6 +242,12 @@ func UniqueID() string { return id } +// TenPorts returns the first port number of a block of +// ten random ports. +func TenPorts() int { + return 1030 + int(rand.Int31n(6440))*10 +} + // pickRandomPorts selects random ports from fixed size random blocks of // ports. This does not eliminate the chance for port conflict but // reduces it significanltly with little overhead. Furthermore, asking @@ -251,7 +257,7 @@ func UniqueID() string { // Instead of relying on one set of ports to be sufficient we retry // starting the agent with different ports on port conflict. func pickRandomPorts(c *Config) { - port := 1030 + int(rand.Int31n(6440))*10 + port := TenPorts() c.Ports.DNS = port + 1 c.Ports.HTTP = port + 2 // when we enable HTTPS then we need to fix finding the @@ -260,7 +266,6 @@ func pickRandomPorts(c *Config) { c.Ports.SerfLan = port + 4 c.Ports.SerfWan = port + 5 c.Ports.Server = port + 6 - //c.ConsulConfig.Memberlist. } // BoolTrue and BoolFalse exist to create a *bool value.