Skip to content

Commit

Permalink
test: run mock dns recursors on random ports
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed May 23, 2017
1 parent def8ba3 commit 2191f6a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion command/agent/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]}
Expand Down
9 changes: 7 additions & 2 deletions command/agent/testagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 2191f6a

Please sign in to comment.