test: run mock dns recursors on random ports

This commit is contained in:
Frank Schroeder 2017-05-23 21:05:38 +02:00
parent d07f2a2542
commit 5735b31ebc
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
2 changed files with 9 additions and 3 deletions

View File

@ -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[:]}

View File

@ -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.