mirror of https://github.com/status-im/consul.git
agent: fix DNS recursor tests (#3190)
The makeRecursor function was using an unreliable mechanism to start a server with a random port. This patch changes this so that the server starts on port 0 to let the kernel pick a free port. In addition, to similar functions for starting a test DNS server were folded into one.
This commit is contained in:
parent
f1bded7c5e
commit
4b51d00458
|
@ -32,30 +32,7 @@ const (
|
||||||
// makeRecursor creates a generic DNS server which always returns
|
// makeRecursor creates a generic DNS server which always returns
|
||||||
// the provided reply. This is useful for mocking a DNS recursor with
|
// the provided reply. This is useful for mocking a DNS recursor with
|
||||||
// an expected result.
|
// an expected result.
|
||||||
func makeRecursor(t *testing.T, answer []dns.RR) *dns.Server {
|
func makeRecursor(t *testing.T, answer dns.Msg) *dns.Server {
|
||||||
randomPort := TenPorts()
|
|
||||||
cfg := TestConfig()
|
|
||||||
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[:]}
|
|
||||||
ans.SetReply(msg)
|
|
||||||
if err := resp.WriteMsg(ans); err != nil {
|
|
||||||
t.Fatalf("err: %s", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
server := &dns.Server{
|
|
||||||
Addr: dnsAddr,
|
|
||||||
Net: "udp",
|
|
||||||
Handler: mux,
|
|
||||||
}
|
|
||||||
go server.ListenAndServe()
|
|
||||||
return server
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeRecursorWithMessage(t *testing.T, answer dns.Msg) *dns.Server {
|
|
||||||
cfg := TestConfig()
|
|
||||||
dnsAddr := fmt.Sprintf("%s:%d", cfg.Addresses.DNS, cfg.Ports.DNS)
|
|
||||||
mux := dns.NewServeMux()
|
mux := dns.NewServeMux()
|
||||||
mux.HandleFunc(".", func(resp dns.ResponseWriter, msg *dns.Msg) {
|
mux.HandleFunc(".", func(resp dns.ResponseWriter, msg *dns.Msg) {
|
||||||
answer.SetReply(msg)
|
answer.SetReply(msg)
|
||||||
|
@ -63,12 +40,16 @@ func makeRecursorWithMessage(t *testing.T, answer dns.Msg) *dns.Server {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
up := make(chan struct{})
|
||||||
server := &dns.Server{
|
server := &dns.Server{
|
||||||
Addr: dnsAddr,
|
Addr: "127.0.0.1:0",
|
||||||
Net: "udp",
|
Net: "udp",
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
|
NotifyStartedFunc: func() { close(up) },
|
||||||
}
|
}
|
||||||
go server.ListenAndServe()
|
go server.ListenAndServe()
|
||||||
|
<-up
|
||||||
|
server.Addr = server.PacketConn.LocalAddr().String()
|
||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,9 +298,11 @@ func TestDNS_NodeLookup_AAAA(t *testing.T) {
|
||||||
|
|
||||||
func TestDNS_NodeLookup_CNAME(t *testing.T) {
|
func TestDNS_NodeLookup_CNAME(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
recursor := makeRecursor(t, []dns.RR{
|
recursor := makeRecursor(t, dns.Msg{
|
||||||
dnsCNAME("www.google.com", "google.com"),
|
Answer: []dns.RR{
|
||||||
dnsA("google.com", "1.2.3.4"),
|
dnsCNAME("www.google.com", "google.com"),
|
||||||
|
dnsA("google.com", "1.2.3.4"),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
defer recursor.Shutdown()
|
defer recursor.Shutdown()
|
||||||
|
|
||||||
|
@ -1944,7 +1927,9 @@ func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) {
|
||||||
|
|
||||||
func TestDNS_Recurse(t *testing.T) {
|
func TestDNS_Recurse(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
recursor := makeRecursor(t, []dns.RR{dnsA("apple.com", "1.2.3.4")})
|
recursor := makeRecursor(t, dns.Msg{
|
||||||
|
Answer: []dns.RR{dnsA("apple.com", "1.2.3.4")},
|
||||||
|
})
|
||||||
defer recursor.Shutdown()
|
defer recursor.Shutdown()
|
||||||
|
|
||||||
cfg := TestConfig()
|
cfg := TestConfig()
|
||||||
|
@ -1972,12 +1957,11 @@ func TestDNS_Recurse(t *testing.T) {
|
||||||
|
|
||||||
func TestDNS_Recurse_Truncation(t *testing.T) {
|
func TestDNS_Recurse_Truncation(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
answerMessage := dns.Msg{
|
|
||||||
|
recursor := makeRecursor(t, dns.Msg{
|
||||||
MsgHdr: dns.MsgHdr{Truncated: true},
|
MsgHdr: dns.MsgHdr{Truncated: true},
|
||||||
Answer: []dns.RR{dnsA("apple.com", "1.2.3.4")},
|
Answer: []dns.RR{dnsA("apple.com", "1.2.3.4")},
|
||||||
}
|
})
|
||||||
|
|
||||||
recursor := makeRecursorWithMessage(t, answerMessage)
|
|
||||||
defer recursor.Shutdown()
|
defer recursor.Shutdown()
|
||||||
|
|
||||||
cfg := TestConfig()
|
cfg := TestConfig()
|
||||||
|
@ -2860,9 +2844,11 @@ func TestDNS_ServiceLookup_AnswerLimits(t *testing.T) {
|
||||||
|
|
||||||
func TestDNS_ServiceLookup_CNAME(t *testing.T) {
|
func TestDNS_ServiceLookup_CNAME(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
recursor := makeRecursor(t, []dns.RR{
|
recursor := makeRecursor(t, dns.Msg{
|
||||||
dnsCNAME("www.google.com", "google.com"),
|
Answer: []dns.RR{
|
||||||
dnsA("google.com", "1.2.3.4"),
|
dnsCNAME("www.google.com", "google.com"),
|
||||||
|
dnsA("google.com", "1.2.3.4"),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
defer recursor.Shutdown()
|
defer recursor.Shutdown()
|
||||||
|
|
||||||
|
@ -2955,9 +2941,11 @@ func TestDNS_ServiceLookup_CNAME(t *testing.T) {
|
||||||
|
|
||||||
func TestDNS_NodeLookup_TTL(t *testing.T) {
|
func TestDNS_NodeLookup_TTL(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
recursor := makeRecursor(t, []dns.RR{
|
recursor := makeRecursor(t, dns.Msg{
|
||||||
dnsCNAME("www.google.com", "google.com"),
|
Answer: []dns.RR{
|
||||||
dnsA("google.com", "1.2.3.4"),
|
dnsCNAME("www.google.com", "google.com"),
|
||||||
|
dnsA("google.com", "1.2.3.4"),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
defer recursor.Shutdown()
|
defer recursor.Shutdown()
|
||||||
|
|
||||||
|
@ -4653,7 +4641,9 @@ func TestDNS_Compression_ReverseLookup(t *testing.T) {
|
||||||
|
|
||||||
func TestDNS_Compression_Recurse(t *testing.T) {
|
func TestDNS_Compression_Recurse(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
recursor := makeRecursor(t, []dns.RR{dnsA("apple.com", "1.2.3.4")})
|
recursor := makeRecursor(t, dns.Msg{
|
||||||
|
Answer: []dns.RR{dnsA("apple.com", "1.2.3.4")},
|
||||||
|
})
|
||||||
defer recursor.Shutdown()
|
defer recursor.Shutdown()
|
||||||
|
|
||||||
cfg := TestConfig()
|
cfg := TestConfig()
|
||||||
|
|
Loading…
Reference in New Issue