2014-01-02 21:12:05 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-02-14 20:26:51 +00:00
|
|
|
"fmt"
|
2016-03-30 06:08:40 +00:00
|
|
|
"math/rand"
|
2015-04-13 20:22:45 +00:00
|
|
|
"net"
|
2014-01-02 21:12:05 +00:00
|
|
|
"os"
|
2016-08-12 04:46:14 +00:00
|
|
|
"reflect"
|
2014-02-14 20:26:51 +00:00
|
|
|
"strings"
|
2014-01-02 21:12:05 +00:00
|
|
|
"testing"
|
2014-06-08 23:14:21 +00:00
|
|
|
"time"
|
2014-09-02 19:47:40 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2016-03-30 18:48:32 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2014-09-02 19:47:40 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
|
|
|
"github.com/miekg/dns"
|
2014-01-02 21:12:05 +00:00
|
|
|
)
|
|
|
|
|
2016-02-12 07:58:48 +00:00
|
|
|
const (
|
2016-03-30 06:08:40 +00:00
|
|
|
configUDPAnswerLimit = 4
|
|
|
|
defaultNumUDPResponses = 3
|
2016-02-12 07:58:48 +00:00
|
|
|
testUDPTruncateLimit = 8
|
2016-03-30 06:08:40 +00:00
|
|
|
|
|
|
|
pctNodesWithIPv6 = 0.5
|
|
|
|
|
|
|
|
// generateNumNodes is the upper bounds for the number of hosts used
|
|
|
|
// in testing below. Generate an arbitrarily large number of hosts.
|
|
|
|
generateNumNodes = testUDPTruncateLimit * defaultNumUDPResponses * configUDPAnswerLimit
|
2016-02-12 07:58:48 +00:00
|
|
|
)
|
|
|
|
|
2015-06-12 22:59:18 +00:00
|
|
|
func makeDNSServer(t *testing.T) (string, *DNSServer) {
|
|
|
|
return makeDNSServerConfig(t, nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeDNSServerConfig(
|
|
|
|
t *testing.T,
|
|
|
|
agentFn func(c *Config),
|
|
|
|
dnsFn func(*DNSConfig)) (string, *DNSServer) {
|
|
|
|
// Create the configs and apply the functions
|
|
|
|
agentConf := nextConfig()
|
|
|
|
if agentFn != nil {
|
|
|
|
agentFn(agentConf)
|
|
|
|
}
|
2016-02-12 07:58:48 +00:00
|
|
|
dnsConf := &DefaultConfig().DNSConfig
|
2015-06-12 22:59:18 +00:00
|
|
|
if dnsFn != nil {
|
|
|
|
dnsFn(dnsConf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add in the recursor if any
|
|
|
|
if r := agentConf.DNSRecursor; r != "" {
|
|
|
|
agentConf.DNSRecursors = append(agentConf.DNSRecursors, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the server
|
|
|
|
addr, _ := agentConf.ClientListener(agentConf.Addresses.DNS, agentConf.Ports.DNS)
|
|
|
|
dir, agent := makeAgent(t, agentConf)
|
|
|
|
server, err := NewDNSServer(agent, dnsConf, agent.logOutput,
|
|
|
|
agentConf.Domain, addr.String(), agentConf.DNSRecursors)
|
2014-01-02 21:12:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-06-12 22:59:18 +00:00
|
|
|
|
2014-01-02 21:12:05 +00:00
|
|
|
return dir, server
|
|
|
|
}
|
|
|
|
|
2015-04-14 02:19:22 +00:00
|
|
|
// makeRecursor creates a generic DNS server which always returns
|
|
|
|
// 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 {
|
|
|
|
dnsConf := nextConfig()
|
|
|
|
dnsAddr := fmt.Sprintf("%s:%d", dnsConf.Addresses.DNS, dnsConf.Ports.DNS)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// dnsCNAME returns a DNS CNAME record struct
|
|
|
|
func dnsCNAME(src, dest string) *dns.CNAME {
|
|
|
|
return &dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: dns.Fqdn(src),
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: dns.Fqdn(dest),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dnsA returns a DNS A record struct
|
|
|
|
func dnsA(src, dest string) *dns.A {
|
|
|
|
return &dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: dns.Fqdn(src),
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP(dest),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 01:31:11 +00:00
|
|
|
func TestRecursorAddr(t *testing.T) {
|
|
|
|
addr, err := recursorAddr("8.8.8.8")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if addr != "8.8.8.8:53" {
|
|
|
|
t.Fatalf("bad: %v", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 01:58:58 +00:00
|
|
|
func TestDNS_NodeLookup(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-01-03 01:58:58 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2014-01-03 01:58:58 +00:00
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
2016-02-07 18:37:34 +00:00
|
|
|
TaggedAddresses: map[string]string{
|
2016-01-26 18:30:28 +00:00
|
|
|
"wan": "127.0.0.2",
|
|
|
|
},
|
2014-01-03 01:58:58 +00:00
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-01-03 01:58:58 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("foo.node.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-04-11 22:46:55 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
2014-01-03 01:58:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Answer[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-06-08 23:14:21 +00:00
|
|
|
if aRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-01-03 01:58:58 +00:00
|
|
|
|
|
|
|
// Re-do the query, but specify the DC
|
|
|
|
m = new(dns.Msg)
|
|
|
|
m.SetQuestion("foo.node.dc1.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
c = new(dns.Client)
|
2014-04-11 22:46:55 +00:00
|
|
|
in, _, err = c.Exchange(m, addr.String())
|
2014-01-03 01:58:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok = in.Answer[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-06-08 23:14:21 +00:00
|
|
|
if aRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2015-06-02 21:47:18 +00:00
|
|
|
|
|
|
|
// lookup a non-existing node, we should receive a SOA
|
|
|
|
m = new(dns.Msg)
|
|
|
|
m.SetQuestion("nofoo.node.dc1.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
c = new(dns.Client)
|
|
|
|
in, _, err = c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Ns) != 1 {
|
2015-07-14 18:42:51 +00:00
|
|
|
t.Fatalf("Bad: %#v %#v", in, len(in.Answer))
|
2015-06-02 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
soaRec, ok := in.Ns[0].(*dns.SOA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
if soaRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
2014-01-03 01:58:58 +00:00
|
|
|
}
|
2014-01-03 21:00:03 +00:00
|
|
|
|
2014-07-17 06:38:24 +00:00
|
|
|
func TestDNS_CaseInsensitiveNodeLookup(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-07-17 06:38:24 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "Foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
2014-07-23 08:29:28 +00:00
|
|
|
m.SetQuestion("fOO.node.dc1.consul.", dns.TypeANY)
|
2014-07-17 06:38:24 +00:00
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-07-17 06:38:24 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("empty lookup: %#v", in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 22:33:01 +00:00
|
|
|
func TestDNS_NodeLookup_PeriodName(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-04-21 22:33:01 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2014-04-21 22:33:01 +00:00
|
|
|
// Register node with period in name
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo.bar",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-04-21 22:33:01 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("foo.bar.node.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-04-21 22:33:01 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Answer[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 20:07:20 +00:00
|
|
|
func TestDNS_NodeLookup_AAAA(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-02-25 20:07:20 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2014-02-25 20:07:20 +00:00
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "::4242:4242",
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-02-25 20:07:20 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
2015-09-08 13:54:13 +00:00
|
|
|
m.SetQuestion("bar.node.consul.", dns.TypeAAAA)
|
2014-02-25 20:07:20 +00:00
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-04-11 22:46:55 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
2014-02-25 20:07:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Answer[0].(*dns.AAAA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.AAAA.String() != "::4242:4242" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-06-08 23:14:21 +00:00
|
|
|
if aRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_NodeLookup_CNAME(t *testing.T) {
|
2015-04-14 02:19:22 +00:00
|
|
|
recursor := makeRecursor(t, []dns.RR{
|
|
|
|
dnsCNAME("www.google.com", "google.com"),
|
|
|
|
dnsA("google.com", "1.2.3.4"),
|
|
|
|
})
|
|
|
|
defer recursor.Shutdown()
|
|
|
|
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServerConfig(t, func(c *Config) {
|
|
|
|
c.DNSRecursor = recursor.Addr
|
|
|
|
}, nil)
|
2014-02-25 20:07:20 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2014-02-25 20:07:20 +00:00
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "google",
|
|
|
|
Address: "www.google.com",
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-02-25 20:07:20 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("google.node.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-04-11 22:46:55 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
2014-02-25 20:07:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-14 02:19:22 +00:00
|
|
|
// Should have the service record, CNAME record + A record
|
|
|
|
if len(in.Answer) != 3 {
|
2014-02-25 20:07:20 +00:00
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
cnRec, ok := in.Answer[0].(*dns.CNAME)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if cnRec.Target != "www.google.com." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-06-08 23:14:21 +00:00
|
|
|
if cnRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 08:16:37 +00:00
|
|
|
func TestDNS_ReverseLookup(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-11-23 08:16:37 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo2",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("2.0.0.127.in-addr.arpa.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
ptrRec, ok := in.Answer[0].(*dns.PTR)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if ptrRec.Ptr != "foo2.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", ptrRec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 18:24:49 +00:00
|
|
|
func TestDNS_ReverseLookup_CustomDomain(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2015-01-08 18:24:49 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
srv.domain = dns.Fqdn("custom")
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo2",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("2.0.0.127.in-addr.arpa.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
ptrRec, ok := in.Answer[0].(*dns.PTR)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if ptrRec.Ptr != "foo2.node.dc1.custom." {
|
|
|
|
t.Fatalf("Bad: %#v", ptrRec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-23 08:16:37 +00:00
|
|
|
func TestDNS_ReverseLookup_IPV6(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-11-23 08:16:37 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "::4242:4242",
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("2.4.2.4.2.4.2.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
ptrRec, ok := in.Answer[0].(*dns.PTR)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if ptrRec.Ptr != "bar.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", ptrRec)
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 19:58:32 +00:00
|
|
|
|
2014-01-03 21:00:03 +00:00
|
|
|
func TestDNS_ServiceLookup(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-01-03 21:00:03 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register a node with a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-03 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-03 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2014-01-03 21:00:03 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
2014-01-03 21:00:03 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Port != 12345 {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Target != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2014-04-21 22:33:01 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
|
|
|
|
// Lookup a non-existing service/query, we should receive an SOA.
|
|
|
|
questions = []string{
|
|
|
|
"nodb.service.consul.",
|
|
|
|
"nope.query.consul.",
|
2014-04-21 22:33:01 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Ns) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
soaRec, ok := in.Ns[0].(*dns.SOA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
if soaRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
2014-06-08 23:14:21 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
2014-04-21 22:33:01 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
func TestDNS_ServiceLookup_ServiceAddress(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a node with a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 22:33:01 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
|
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 22:33:01 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
|
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2014-04-21 22:33:01 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Port != 12345 {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Target != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.2" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2014-06-08 23:14:21 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
2015-06-02 21:47:18 +00:00
|
|
|
|
2015-12-24 00:07:27 +00:00
|
|
|
func TestDNS_ServiceLookup_WanAddress(t *testing.T) {
|
|
|
|
dir1, srv1 := makeDNSServerConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc1"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
}, nil)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer srv1.Shutdown()
|
|
|
|
|
|
|
|
dir2, srv2 := makeDNSServerConfig(t, func(c *Config) {
|
|
|
|
c.Datacenter = "dc2"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
}, nil)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer srv2.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv1.agent.RPC, "dc1")
|
|
|
|
testutil.WaitForLeader(t, srv2.agent.RPC, "dc2")
|
|
|
|
|
|
|
|
// Join WAN cluster
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
srv1.agent.config.Ports.SerfWan)
|
|
|
|
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(
|
|
|
|
func() (bool, error) {
|
|
|
|
return len(srv1.agent.WANMembers()) > 1, nil
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
t.Fatalf("Failed waiting for WAN join: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Register a remote node with a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
2016-02-07 18:37:34 +00:00
|
|
|
TaggedAddresses: map[string]string{
|
2016-01-26 18:30:28 +00:00
|
|
|
"wan": "127.0.0.2",
|
|
|
|
},
|
2015-12-24 00:07:27 +00:00
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv2.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv2.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the SRV record via service and prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.dc2.consul.",
|
|
|
|
id + ".query.dc2.consul.",
|
|
|
|
}
|
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv1.agent.config.ClientListener("", srv1.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc2.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.2" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also check the A record directly
|
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeA)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv1.agent.config.ClientListener("", srv1.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Answer[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != question {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.2" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 22:16:15 +00:00
|
|
|
|
|
|
|
// Now query from the same DC and make sure we get the local address
|
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv2.agent.config.ClientListener("", srv2.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc2.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also check the A record directly from DC2
|
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeA)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv2.agent.config.ClientListener("", srv2.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Answer[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != question {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
}
|
2015-12-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
func TestDNS_CaseInsensitiveServiceLookup(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
2015-06-02 21:47:18 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a node with a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "Db",
|
|
|
|
Tags: []string{"Master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-06-02 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query, as well as a name.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Name: "somequery",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-06-02 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Try some variations to make sure case doesn't matter.
|
|
|
|
questions := []string{
|
|
|
|
"master.db.service.consul.",
|
|
|
|
"mASTER.dB.service.consul.",
|
|
|
|
"MASTER.dB.service.consul.",
|
|
|
|
"db.service.consul.",
|
|
|
|
"DB.service.consul.",
|
|
|
|
"Db.service.consul.",
|
|
|
|
"somequery.query.consul.",
|
|
|
|
"SomeQuery.query.consul.",
|
|
|
|
"SOMEQUERY.query.consul.",
|
2015-06-02 21:47:18 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("empty lookup: %#v", in)
|
|
|
|
}
|
2015-06-02 21:47:18 +00:00
|
|
|
}
|
2014-04-21 22:33:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
func TestDNS_ServiceLookup_TagPeriod(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2015-01-08 19:58:32 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2015-11-13 11:39:07 +00:00
|
|
|
Tags: []string{"v1.master"},
|
2015-01-08 19:58:32 +00:00
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
2015-11-13 11:39:07 +00:00
|
|
|
m.SetQuestion("v1.master.db.service.consul.", dns.TypeSRV)
|
2015-01-08 19:58:32 +00:00
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Port != 12345 {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Target != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
2015-01-08 19:58:32 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
func TestDNS_ServiceLookup_PreparedQueryNamePeriod(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-07-17 06:38:24 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register a node with a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
2014-07-17 06:38:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-07-17 06:38:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register a prepared query with a period in the name.
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Name: "some.query.we.like",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var id string
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 22:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
2015-11-13 11:39:07 +00:00
|
|
|
m.SetQuestion("some.query.we.like.query.consul.", dns.TypeSRV)
|
2014-04-21 22:33:01 +00:00
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-04-21 22:33:01 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
2014-01-03 21:00:03 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Port != 12345 {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Target != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
2014-01-03 21:00:03 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
2014-01-03 23:43:35 +00:00
|
|
|
|
2014-01-06 22:56:41 +00:00
|
|
|
func TestDNS_ServiceLookup_Dedup(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-01-06 22:56:41 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register a single node with multiple instances of a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db2",
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"slave"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db3",
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"slave"},
|
|
|
|
Port: 12346,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query, make sure only
|
|
|
|
// one IP is returned.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeANY)
|
2014-01-06 22:56:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Answer[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
2014-02-26 01:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-02-26 01:41:48 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register a single node with multiple instances of a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-26 01:41:48 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db2",
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"slave"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-26 01:41:48 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db3",
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"slave"},
|
|
|
|
Port: 12346,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-26 01:41:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-26 01:41:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query, make sure only
|
|
|
|
// one IP is returned and two unique ports are returned.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2014-02-26 01:41:48 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
2014-01-06 22:56:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if len(in.Answer) != 2 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Port != 12345 && srvRec.Port != 12346 {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Target != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok = in.Answer[1].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[1])
|
|
|
|
}
|
|
|
|
if srvRec.Port != 12346 && srvRec.Port != 12345 {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Port == in.Answer[0].(*dns.SRV).Port {
|
|
|
|
t.Fatalf("should be a different port")
|
|
|
|
}
|
|
|
|
if srvRec.Target != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 23:43:35 +00:00
|
|
|
func TestDNS_Recurse(t *testing.T) {
|
2015-04-14 02:19:22 +00:00
|
|
|
recursor := makeRecursor(t, []dns.RR{dnsA("apple.com", "1.2.3.4")})
|
|
|
|
defer recursor.Shutdown()
|
|
|
|
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServerConfig(t, func(c *Config) {
|
|
|
|
c.DNSRecursor = recursor.Addr
|
|
|
|
}, nil)
|
2014-01-03 23:43:35 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("apple.com.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-04-11 22:46:55 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
2014-01-03 23:43:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) == 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
if in.Rcode != dns.RcodeSuccess {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 21:30:04 +00:00
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_FilterCritical(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-01-15 21:30:04 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register nodes with health checks in various states.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "serf",
|
|
|
|
Name: "serf",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-15 21:30:04 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args2 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "serf",
|
|
|
|
Name: "serf",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args2, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-24 02:04:12 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args3 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db",
|
|
|
|
ServiceID: "db",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args3, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-07-16 22:11:45 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args4 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "baz",
|
|
|
|
Address: "127.0.0.3",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args4, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-07-16 22:11:45 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args5 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "quux",
|
|
|
|
Address: "127.0.0.4",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db",
|
|
|
|
ServiceID: "db",
|
|
|
|
Status: structs.HealthWarning,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args5, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-15 21:30:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2015-01-13 13:59:24 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeANY)
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Only 4 and 5 are not failing, so we should get 2 answers
|
|
|
|
if len(in.Answer) != 2 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
ips := make(map[string]bool)
|
|
|
|
for _, resp := range in.Answer {
|
|
|
|
aRec := resp.(*dns.A)
|
|
|
|
ips[aRec.A.String()] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ips["127.0.0.3"] {
|
|
|
|
t.Fatalf("Bad: %#v should contain 127.0.0.3 (state healthy)", in)
|
|
|
|
}
|
|
|
|
if !ips["127.0.0.4"] {
|
|
|
|
t.Fatalf("Bad: %#v should contain 127.0.0.4 (state warning)", in)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 22:21:16 +00:00
|
|
|
func TestDNS_ServiceLookup_OnlyFailing(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register nodes with all health checks in a critical state.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "serf",
|
|
|
|
Name: "serf",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-07-29 22:21:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args2 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "serf",
|
|
|
|
Name: "serf",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args2, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-07-29 22:21:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args3 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db",
|
|
|
|
ServiceID: "db",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args3, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-07-29 22:21:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-07-29 22:21:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2015-07-29 22:21:16 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeANY)
|
2015-07-29 22:21:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-07-29 22:21:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// All 3 are failing, so we should get 0 answers and an NXDOMAIN response
|
|
|
|
if len(in.Answer) != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2015-07-29 22:21:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if in.Rcode != dns.RcodeNameError {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2015-07-29 22:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 13:59:24 +00:00
|
|
|
func TestDNS_ServiceLookup_OnlyPassing(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServerConfig(t, nil, func(c *DNSConfig) {
|
|
|
|
c.OnlyPassing = true
|
|
|
|
})
|
2015-01-13 13:59:24 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register nodes with health checks in various states.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db",
|
|
|
|
ServiceID: "db",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
},
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args2 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db",
|
|
|
|
ServiceID: "db",
|
|
|
|
Status: structs.HealthWarning,
|
|
|
|
},
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if err := srv.agent.RPC("Catalog.Register", args2, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args3 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "baz",
|
|
|
|
Address: "127.0.0.3",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db",
|
|
|
|
ServiceID: "db",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if err := srv.agent.RPC("Catalog.Register", args3, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args4 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "quux",
|
|
|
|
Address: "127.0.0.4",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db",
|
|
|
|
ServiceID: "db",
|
|
|
|
Status: structs.HealthUnknown,
|
|
|
|
},
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if err := srv.agent.RPC("Catalog.Register", args4, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
OnlyPassing: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2014-01-15 21:30:04 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-07-16 22:11:45 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Only 1 is passing, so we should only get 1 answer
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2015-01-13 13:59:24 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
resp := in.Answer[0]
|
|
|
|
aRec := resp.(*dns.A)
|
|
|
|
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-07-16 22:11:45 +00:00
|
|
|
}
|
2014-01-15 21:30:04 +00:00
|
|
|
}
|
2014-02-14 20:26:51 +00:00
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_Randomize(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-02-14 20:26:51 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2016-03-30 06:08:40 +00:00
|
|
|
// Register a large number of nodes.
|
|
|
|
for i := 0; i < generateNumNodes; i++ {
|
2014-02-14 20:26:51 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: fmt.Sprintf("foo%d", i),
|
|
|
|
Address: fmt.Sprintf("127.0.0.%d", i+1),
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "web",
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-02-14 20:26:51 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "web",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
2014-02-14 20:26:51 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
2014-02-14 20:26:51 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query. Ensure the
|
|
|
|
// response is randomized each time.
|
|
|
|
questions := []string{
|
|
|
|
"web.service.consul.",
|
|
|
|
id + ".query.consul.",
|
|
|
|
}
|
|
|
|
for _, question := range questions {
|
|
|
|
uniques := map[string]struct{}{}
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeANY)
|
2014-02-14 20:26:51 +00:00
|
|
|
|
2016-02-12 07:58:48 +00:00
|
|
|
c := &dns.Client{Net: "udp"}
|
2015-11-13 11:39:07 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2014-02-14 20:26:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Response length should be truncated and we should get
|
|
|
|
// an A record for each response.
|
2016-03-30 06:08:40 +00:00
|
|
|
if len(in.Answer) != defaultNumUDPResponses {
|
2015-11-13 11:39:07 +00:00
|
|
|
t.Fatalf("Bad: %#v", len(in.Answer))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect all the names.
|
|
|
|
var names []string
|
|
|
|
for _, rec := range in.Answer {
|
|
|
|
switch v := rec.(type) {
|
|
|
|
case *dns.SRV:
|
|
|
|
names = append(names, v.Target)
|
|
|
|
case *dns.A:
|
|
|
|
names = append(names, v.A.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nameS := strings.Join(names, "|")
|
|
|
|
|
|
|
|
// Tally the results.
|
|
|
|
uniques[nameS] = struct{}{}
|
|
|
|
}
|
2015-01-13 00:05:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Give some wiggle room. Since the responses are randomized and
|
|
|
|
// there is a finite number of combinations, requiring 0
|
|
|
|
// duplicates every test run eventually gives us failures.
|
|
|
|
if len(uniques) < 2 {
|
|
|
|
t.Fatalf("unique response ratio too low: %d/10\n%v", len(uniques), uniques)
|
|
|
|
}
|
2015-01-13 00:05:41 +00:00
|
|
|
}
|
2014-02-14 20:26:51 +00:00
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
|
2015-05-05 21:14:41 +00:00
|
|
|
func TestDNS_ServiceLookup_Truncate(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServerConfig(t, nil, func(c *DNSConfig) {
|
|
|
|
c.EnableTruncate = true
|
|
|
|
})
|
2015-05-05 21:14:41 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
2016-03-30 06:08:40 +00:00
|
|
|
// Register a large number of nodes.
|
|
|
|
for i := 0; i < generateNumNodes; i++ {
|
2015-05-05 21:14:41 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: fmt.Sprintf("foo%d", i),
|
|
|
|
Address: fmt.Sprintf("127.0.0.%d", i+1),
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "web",
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "web",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 21:14:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query. Ensure the
|
|
|
|
// response is truncated each time.
|
|
|
|
questions := []string{
|
|
|
|
"web.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2015-05-05 21:14:41 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeANY)
|
2015-05-05 21:14:41 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
c := new(dns.Client)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil && err != dns.ErrTruncated {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the truncate bit
|
|
|
|
if !in.Truncated {
|
|
|
|
t.Fatalf("should have truncate bit")
|
|
|
|
}
|
2015-05-05 21:14:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 00:54:28 +00:00
|
|
|
func TestDNS_ServiceLookup_LargeResponses(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServerConfig(t, nil, func(c *DNSConfig) {
|
|
|
|
c.EnableTruncate = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
longServiceName := "this-is-a-very-very-very-very-very-long-name-for-a-service"
|
|
|
|
|
2016-08-12 04:46:14 +00:00
|
|
|
// Register a lot of nodes.
|
|
|
|
for i := 0; i < 4; i++ {
|
2016-02-18 00:54:28 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: fmt.Sprintf("foo%d", i),
|
|
|
|
Address: fmt.Sprintf("127.0.0.%d", i+1),
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: longServiceName,
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 18:37:54 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Name: longServiceName,
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: longServiceName,
|
|
|
|
Tags: []string{"master"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var id string
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-02-18 00:54:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 18:37:54 +00:00
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"_" + longServiceName + "._master.service.consul.",
|
|
|
|
longServiceName + ".query.consul.",
|
2016-02-18 00:54:28 +00:00
|
|
|
}
|
2016-03-07 18:37:54 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
2016-02-18 00:54:28 +00:00
|
|
|
|
2016-03-07 18:37:54 +00:00
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil && err != dns.ErrTruncated {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the response size is RFC 1035-compliant for UDP messages
|
|
|
|
if in.Len() > 512 {
|
2016-08-12 04:46:14 +00:00
|
|
|
t.Fatalf("Bad: %d", in.Len())
|
2016-03-07 18:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We should only have two answers now
|
|
|
|
if len(in.Answer) != 2 {
|
2016-08-12 04:46:14 +00:00
|
|
|
t.Fatalf("Bad: %d", len(in.Answer))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the ADDITIONAL section matches the ANSWER section.
|
|
|
|
if len(in.Answer) != len(in.Extra) {
|
|
|
|
t.Fatalf("Bad: %d vs. %d", len(in.Answer), len(in.Extra))
|
|
|
|
}
|
|
|
|
for i := 0; i < len(in.Answer); i++ {
|
|
|
|
srv, ok := in.Answer[i].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
a, ok := in.Extra[i].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if srv.Target != a.Hdr.Name {
|
|
|
|
t.Fatalf("Bad: %#v %#v", srv, a)
|
|
|
|
}
|
2016-03-07 18:37:54 +00:00
|
|
|
}
|
2016-02-18 00:54:28 +00:00
|
|
|
|
2016-03-07 18:37:54 +00:00
|
|
|
// Check for the truncate bit
|
|
|
|
if !in.Truncated {
|
|
|
|
t.Fatalf("should have truncate bit")
|
|
|
|
}
|
2016-02-18 00:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 08:05:49 +00:00
|
|
|
func testDNS_ServiceLookup_responseLimits(t *testing.T, answerLimit int, qType uint16,
|
|
|
|
expectedService, expectedQuery, expectedQueryID int) (bool, error) {
|
2016-02-12 07:58:48 +00:00
|
|
|
dir, srv := makeDNSServerConfig(t, nil, func(c *DNSConfig) {
|
2016-03-30 08:05:49 +00:00
|
|
|
c.UDPAnswerLimit = answerLimit
|
2016-02-12 07:58:48 +00:00
|
|
|
})
|
2015-08-25 19:54:11 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
2016-03-30 06:08:58 +00:00
|
|
|
for i := 0; i < generateNumNodes; i++ {
|
|
|
|
nodeAddress := fmt.Sprintf("127.0.0.%d", i+1)
|
|
|
|
if rand.Float64() < pctNodesWithIPv6 {
|
|
|
|
nodeAddress = fmt.Sprintf("fe80::%d", i+1)
|
|
|
|
}
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: fmt.Sprintf("foo%d", i),
|
|
|
|
Address: nodeAddress,
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api-tier",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
2016-03-30 08:05:49 +00:00
|
|
|
return false, fmt.Errorf("err: %v", err)
|
2016-03-30 06:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-30 08:05:49 +00:00
|
|
|
var id string
|
2016-03-30 06:08:58 +00:00
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Name: "api-tier",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "api-tier",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
2016-03-30 08:05:49 +00:00
|
|
|
return false, fmt.Errorf("err: %v", err)
|
2016-03-30 06:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 07:58:48 +00:00
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
2016-03-30 06:08:58 +00:00
|
|
|
"api-tier.service.consul.",
|
|
|
|
"api-tier.query.consul.",
|
2016-03-30 08:05:49 +00:00
|
|
|
id + ".query.consul.",
|
2016-02-12 07:58:48 +00:00
|
|
|
}
|
2016-03-30 08:05:49 +00:00
|
|
|
for idx, question := range questions {
|
2016-02-12 07:58:48 +00:00
|
|
|
m := new(dns.Msg)
|
2016-03-30 08:05:49 +00:00
|
|
|
m.SetQuestion(question, qType)
|
2016-02-12 07:58:48 +00:00
|
|
|
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
c := &dns.Client{Net: "udp"}
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
2016-03-30 08:05:49 +00:00
|
|
|
return false, fmt.Errorf("err: %v", err)
|
2016-02-12 07:58:48 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:05:49 +00:00
|
|
|
switch idx {
|
|
|
|
case 0:
|
2016-03-30 18:48:32 +00:00
|
|
|
if (expectedService > 0 && len(in.Answer) != expectedService) ||
|
|
|
|
(expectedService < -1 && len(in.Answer) < lib.AbsInt(expectedService)) {
|
2016-03-30 17:16:05 +00:00
|
|
|
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, qType, question)
|
2016-03-30 08:05:49 +00:00
|
|
|
}
|
|
|
|
case 1:
|
2016-03-30 18:48:32 +00:00
|
|
|
if (expectedQuery > 0 && len(in.Answer) != expectedQuery) ||
|
|
|
|
(expectedQuery < -1 && len(in.Answer) < lib.AbsInt(expectedQuery)) {
|
2016-03-30 17:16:05 +00:00
|
|
|
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, qType, question)
|
2016-03-30 08:05:49 +00:00
|
|
|
}
|
|
|
|
case 2:
|
2016-03-30 18:48:32 +00:00
|
|
|
if (expectedQueryID > 0 && len(in.Answer) != expectedQueryID) ||
|
|
|
|
(expectedQueryID < -1 && len(in.Answer) < lib.AbsInt(expectedQueryID)) {
|
2016-03-30 17:16:05 +00:00
|
|
|
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, qType, question)
|
2016-03-30 08:05:49 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("abort")
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
2016-03-30 08:05:49 +00:00
|
|
|
}
|
2015-08-25 19:54:11 +00:00
|
|
|
|
2016-03-30 08:05:49 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
|
2016-03-30 08:05:49 +00:00
|
|
|
func TestDNS_ServiceLookup_AnswerLimits(t *testing.T) {
|
|
|
|
// Build a matrix of config parameters (udpAnswerLimit), and the
|
2016-03-30 18:48:32 +00:00
|
|
|
// length of the response per query type and question. Negative
|
|
|
|
// values imply the test must return at least the abs(value) number
|
|
|
|
// of records in the answer section. This is required because, for
|
|
|
|
// example, on OS-X and Linux, the number of answers returned in a
|
|
|
|
// 512B response is different even though both platforms are x86_64
|
|
|
|
// and using the same version of Go.
|
|
|
|
//
|
|
|
|
// TODO(sean@): Why is it not identical everywhere when using the
|
|
|
|
// same compiler?
|
2016-03-30 08:05:49 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
udpAnswerLimit int
|
|
|
|
expectedAService int
|
|
|
|
expectedAQuery int
|
|
|
|
expectedAQueryID int
|
|
|
|
expectedAAAAService int
|
|
|
|
expectedAAAAQuery int
|
|
|
|
expectedAAAAQueryID int
|
|
|
|
expectedANYService int
|
|
|
|
expectedANYQuery int
|
|
|
|
expectedANYQueryID int
|
|
|
|
}{
|
|
|
|
{"0", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
{"1", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
|
|
|
|
{"2", 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
|
|
|
|
{"3", 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
|
|
|
|
{"4", 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
|
|
|
|
{"5", 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
|
2016-03-30 18:48:32 +00:00
|
|
|
{"6", 6, 6, 6, 6, 6, 6, 5, 6, 6, -5},
|
|
|
|
{"7", 7, 7, 7, 6, 7, 7, 5, 7, 7, -5},
|
|
|
|
{"8", 8, 8, 8, 6, 8, 8, 5, 8, 8, -5},
|
|
|
|
{"9", 9, 8, 8, 6, 8, 8, 5, 8, 8, -5},
|
|
|
|
{"20", 20, 8, 8, 6, 8, 8, 5, 8, -5, -5},
|
|
|
|
{"30", 30, 8, 8, 6, 8, 8, 5, 8, -5, -5},
|
2016-03-30 08:05:49 +00:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
ok, err := testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeA, test.expectedAService, test.expectedAQuery, test.expectedAQueryID)
|
|
|
|
if !ok {
|
2016-03-30 17:16:05 +00:00
|
|
|
t.Errorf("Expected service A lookup %s to pass: %v", test.name, err)
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
2015-08-25 19:54:11 +00:00
|
|
|
|
2016-03-30 08:05:49 +00:00
|
|
|
ok, err = testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeAAAA, test.expectedAAAAService, test.expectedAAAAQuery, test.expectedAAAAQueryID)
|
|
|
|
if !ok {
|
2016-03-30 17:16:05 +00:00
|
|
|
t.Errorf("Expected service AAAA lookup %s to pass: %v", test.name, err)
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
2015-08-25 19:54:11 +00:00
|
|
|
|
2016-03-30 08:05:49 +00:00
|
|
|
ok, err = testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeANY, test.expectedANYService, test.expectedANYQuery, test.expectedANYQueryID)
|
|
|
|
if !ok {
|
2016-03-30 17:16:05 +00:00
|
|
|
t.Errorf("Expected service ANY lookup %s to pass: %v", test.name, err)
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-25 19:54:11 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 20:07:20 +00:00
|
|
|
func TestDNS_ServiceLookup_CNAME(t *testing.T) {
|
2015-04-14 02:19:22 +00:00
|
|
|
recursor := makeRecursor(t, []dns.RR{
|
|
|
|
dnsCNAME("www.google.com", "google.com"),
|
|
|
|
dnsA("google.com", "1.2.3.4"),
|
|
|
|
})
|
|
|
|
defer recursor.Shutdown()
|
|
|
|
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServerConfig(t, func(c *Config) {
|
|
|
|
c.DNSRecursor = recursor.Addr
|
|
|
|
}, nil)
|
2014-02-25 20:07:20 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register a node with a name for an address.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "google",
|
|
|
|
Address: "www.google.com",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "search",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "search",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"search.service.consul.",
|
|
|
|
id + ".query.consul.",
|
2014-02-25 20:07:20 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeANY)
|
2014-02-25 20:07:20 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Service CNAME, google CNAME, google A record
|
|
|
|
if len(in.Answer) != 3 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should have service CNAME
|
|
|
|
cnRec, ok := in.Answer[0].(*dns.CNAME)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if cnRec.Target != "www.google.com." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should have google CNAME
|
|
|
|
cnRec, ok = in.Answer[1].(*dns.CNAME)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[1])
|
|
|
|
}
|
|
|
|
if cnRec.Target != "google.com." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[1])
|
|
|
|
}
|
2015-04-14 02:19:22 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Check we recursively resolve
|
|
|
|
if _, ok := in.Answer[2].(*dns.A); !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[2])
|
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 23:14:21 +00:00
|
|
|
|
|
|
|
func TestDNS_NodeLookup_TTL(t *testing.T) {
|
2015-04-14 02:19:22 +00:00
|
|
|
recursor := makeRecursor(t, []dns.RR{
|
|
|
|
dnsCNAME("www.google.com", "google.com"),
|
|
|
|
dnsA("google.com", "1.2.3.4"),
|
|
|
|
})
|
|
|
|
defer recursor.Shutdown()
|
|
|
|
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServerConfig(t, func(c *Config) {
|
|
|
|
c.DNSRecursor = recursor.Addr
|
|
|
|
}, func(c *DNSConfig) {
|
|
|
|
c.NodeTTL = 10 * time.Second
|
|
|
|
c.AllowStale = true
|
|
|
|
c.MaxStale = time.Second
|
|
|
|
})
|
2014-06-08 23:14:21 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("foo.node.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-06-08 23:14:21 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Answer[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Ttl != 10 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register node with IPv6
|
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "::4242:4242",
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check an IPv6 record
|
|
|
|
m = new(dns.Msg)
|
|
|
|
m.SetQuestion("bar.node.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
in, _, err = c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
aaaaRec, ok := in.Answer[0].(*dns.AAAA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aaaaRec.AAAA.String() != "::4242:4242" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if aaaaRec.Hdr.Ttl != 10 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register node with CNAME
|
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "google",
|
|
|
|
Address: "www.google.com",
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m = new(dns.Msg)
|
|
|
|
m.SetQuestion("google.node.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
in, _, err = c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should have the CNAME record + a few A records
|
|
|
|
if len(in.Answer) < 2 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
cnRec, ok := in.Answer[0].(*dns.CNAME)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if cnRec.Target != "www.google.com." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if cnRec.Hdr.Ttl != 10 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_TTL(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
confFn := func(c *DNSConfig) {
|
|
|
|
c.ServiceTTL = map[string]time.Duration{
|
2014-06-08 23:14:21 +00:00
|
|
|
"db": 10 * time.Second,
|
|
|
|
"*": 5 * time.Second,
|
2015-06-12 22:59:18 +00:00
|
|
|
}
|
|
|
|
c.AllowStale = true
|
|
|
|
c.MaxStale = time.Second
|
|
|
|
}
|
|
|
|
dir, srv := makeDNSServerConfig(t, nil, confFn)
|
2014-06-08 23:14:21 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node with 2 services
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
Port: 2222,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("db.service.consul.", dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-06-08 23:14:21 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Hdr.Ttl != 10 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Ttl != 10 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
m = new(dns.Msg)
|
|
|
|
m.SetQuestion("api.service.consul.", dns.TypeSRV)
|
|
|
|
in, _, err = c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok = in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Hdr.Ttl != 5 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok = in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Ttl != 5 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 19:45:56 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
func TestDNS_PreparedQuery_TTL(t *testing.T) {
|
2015-11-13 18:38:44 +00:00
|
|
|
confFn := func(c *DNSConfig) {
|
|
|
|
c.ServiceTTL = map[string]time.Duration{
|
|
|
|
"db": 10 * time.Second,
|
|
|
|
"*": 5 * time.Second,
|
|
|
|
}
|
|
|
|
c.AllowStale = true
|
|
|
|
c.MaxStale = time.Second
|
|
|
|
}
|
|
|
|
dir, srv := makeDNSServerConfig(t, nil, confFn)
|
2015-11-13 11:39:07 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a node and a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-13 18:38:44 +00:00
|
|
|
|
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
Port: 2222,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 18:38:44 +00:00
|
|
|
// Register prepared queries with and without a TTL set for "db", as
|
|
|
|
// well as one for "api".
|
2015-11-13 11:39:07 +00:00
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
2015-11-13 18:38:44 +00:00
|
|
|
Name: "db-ttl",
|
2015-11-13 11:39:07 +00:00
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
DNS: structs.QueryDNSOptions{
|
2015-11-13 18:38:44 +00:00
|
|
|
TTL: "18s",
|
2015-11-13 11:39:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var id string
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
2015-11-13 18:38:44 +00:00
|
|
|
Name: "db-nottl",
|
2015-11-13 11:39:07 +00:00
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-13 18:38:44 +00:00
|
|
|
|
|
|
|
args = &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Name: "api-nottl",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "api",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 18:38:44 +00:00
|
|
|
// Make sure the TTL is set when requested, and overrides the agent-
|
|
|
|
// specific config since the query takes precedence.
|
2015-11-13 11:39:07 +00:00
|
|
|
m := new(dns.Msg)
|
2015-11-13 18:38:44 +00:00
|
|
|
m.SetQuestion("db-ttl.query.consul.", dns.TypeSRV)
|
2015-11-13 11:39:07 +00:00
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2015-11-13 18:38:44 +00:00
|
|
|
if srvRec.Hdr.Ttl != 18 {
|
2015-11-13 11:39:07 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2015-11-13 18:38:44 +00:00
|
|
|
if aRec.Hdr.Ttl != 18 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// And the TTL should take the service-specific value from the agent's
|
|
|
|
// config otherwise.
|
|
|
|
m = new(dns.Msg)
|
|
|
|
m.SetQuestion("db-nottl.query.consul.", dns.TypeSRV)
|
|
|
|
in, _, err = c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok = in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Hdr.Ttl != 10 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok = in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
if aRec.Hdr.Ttl != 10 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
|
2015-11-13 18:38:44 +00:00
|
|
|
// If there's no query TTL and no service-specific value then the wild
|
|
|
|
// card value should be used.
|
2015-11-13 11:39:07 +00:00
|
|
|
m = new(dns.Msg)
|
2015-11-13 18:38:44 +00:00
|
|
|
m.SetQuestion("api-nottl.query.consul.", dns.TypeSRV)
|
2015-11-13 11:39:07 +00:00
|
|
|
in, _, err = c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok = in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2015-11-13 18:38:44 +00:00
|
|
|
if srvRec.Hdr.Ttl != 5 {
|
2015-11-13 11:39:07 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok = in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2015-11-13 18:38:44 +00:00
|
|
|
if aRec.Hdr.Ttl != 5 {
|
2015-11-13 11:39:07 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 22:11:42 +00:00
|
|
|
func TestDNS_PreparedQuery_Failover(t *testing.T) {
|
|
|
|
dir1, srv1 := makeDNSServerConfig(t, func(c *Config) {
|
2016-08-13 00:16:08 +00:00
|
|
|
c.Datacenter = "dc1"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
}, nil)
|
2016-07-27 22:11:42 +00:00
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer srv1.Shutdown()
|
|
|
|
|
|
|
|
dir2, srv2 := makeDNSServerConfig(t, func(c *Config) {
|
|
|
|
c.Datacenter = "dc2"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
}, nil)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer srv2.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv1.agent.RPC, "dc1")
|
|
|
|
testutil.WaitForLeader(t, srv2.agent.RPC, "dc2")
|
|
|
|
|
2016-08-13 00:16:08 +00:00
|
|
|
// Join WAN cluster.
|
2016-07-27 22:11:42 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
srv1.agent.config.Ports.SerfWan)
|
|
|
|
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
testutil.WaitForResult(
|
|
|
|
func() (bool, error) {
|
|
|
|
return len(srv1.agent.WANMembers()) > 1, nil
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
t.Fatalf("Failed waiting for WAN join: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Register a remote node with a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": "127.0.0.2",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv2.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 00:16:08 +00:00
|
|
|
// Register a local prepared query.
|
2016-07-27 22:11:42 +00:00
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Name: "my-query",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
Failover: structs.QueryDatacenterOptions{
|
|
|
|
Datacenters: []string{"dc2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-08-13 00:16:08 +00:00
|
|
|
var id string
|
|
|
|
if err := srv1.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
2016-07-27 22:11:42 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 00:16:08 +00:00
|
|
|
// Look up the SRV record via the query.
|
2016-07-27 22:11:42 +00:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("my-query.query.consul.", dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
cl_addr, _ := srv1.agent.config.ClientListener("", srv1.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, cl_addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-13 00:16:08 +00:00
|
|
|
// Make sure we see the remote DC and that the address gets
|
|
|
|
// translated.
|
2016-07-27 22:11:42 +00:00
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2016-08-13 00:16:08 +00:00
|
|
|
if in.Answer[0].Header().Name != "my-query.query.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
srv, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srv.Target != "foo.node.dc2.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2016-07-27 22:11:42 +00:00
|
|
|
|
2016-08-13 00:16:08 +00:00
|
|
|
a, ok := in.Extra[0].(*dns.A)
|
2016-07-27 22:11:42 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2016-08-13 00:16:08 +00:00
|
|
|
if a.Hdr.Name != "foo.node.dc2.consul." {
|
2016-07-27 22:11:42 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2016-08-13 00:16:08 +00:00
|
|
|
if a.A.String() != "127.0.0.2" {
|
2016-07-27 22:11:42 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 19:45:56 +00:00
|
|
|
func TestDNS_ServiceLookup_SRV_RFC(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-08-18 19:45:56 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("_db._master.service.consul.", dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-08-18 19:45:56 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Port != 12345 {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Target != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_SRV_RFC_TCP_Default(t *testing.T) {
|
2015-06-12 22:59:18 +00:00
|
|
|
dir, srv := makeDNSServer(t)
|
2014-08-18 19:45:56 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("_db._tcp.service.consul.", dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2014-08-18 19:45:56 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if srvRec.Port != 12345 {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Target != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
|
|
|
if srvRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec, ok := in.Extra[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Name != "foo.node.dc1.consul." {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
if aRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
|
|
|
}
|
2015-06-12 22:59:18 +00:00
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_FilterACL(t *testing.T) {
|
|
|
|
confFn := func(c *Config) {
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLDownPolicy = "deny"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
}
|
|
|
|
dir, srv := makeDNSServerConfig(t, confFn, nil)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a service
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "foo",
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the DNS query
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("foo.service.consul.", dns.TypeA)
|
|
|
|
|
|
|
|
// Query with the root token. Should get results.
|
|
|
|
srv.agent.config.ACLToken = "root"
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query with a non-root token without access. Should get nothing.
|
|
|
|
srv.agent.config.ACLToken = "anonymous"
|
|
|
|
in, _, err = c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(in.Answer) != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 21:47:18 +00:00
|
|
|
|
|
|
|
func TestDNS_NonExistingLookup(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
|
|
|
|
// lookup a non-existing node, we should receive a SOA
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("nonexisting.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Ns) != 1 {
|
2015-07-14 18:42:51 +00:00
|
|
|
t.Fatalf("Bad: %#v %#v", in, len(in.Answer))
|
2015-06-02 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
soaRec, ok := in.Ns[0].(*dns.SOA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
if soaRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_NonExistingLookupEmptyAorAAAA(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register a v6-only service and a v4-only service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foov6",
|
|
|
|
Address: "fe80::1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "webv6",
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foov4",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "webv4",
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Register equivalent prepared queries.
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Name: "webv4",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "webv4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
var id string
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
args = &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Name: "webv6",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "webv6",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
}
|
2015-06-02 21:47:18 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Check for ipv6 records on ipv4-only service directly and via the
|
|
|
|
// prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"webv4.service.consul.",
|
|
|
|
"webv4.query.consul.",
|
2015-09-02 14:12:22 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeAAAA)
|
|
|
|
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
c := new(dns.Client)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-09-02 14:12:22 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if len(in.Ns) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
soaRec, ok := in.Ns[0].(*dns.SOA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
if soaRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if in.Rcode != dns.RcodeSuccess {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2015-08-25 20:37:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
// Check for ipv4 records on ipv6-only service directly and via the
|
|
|
|
// prepared query.
|
|
|
|
questions = []string{
|
|
|
|
"webv6.service.consul.",
|
|
|
|
"webv6.query.consul.",
|
2015-08-25 20:37:33 +00:00
|
|
|
}
|
2015-11-13 11:39:07 +00:00
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeA)
|
2015-09-02 14:12:22 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
c := new(dns.Client)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Ns) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
soaRec, ok := in.Ns[0].(*dns.SOA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
if soaRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
2015-09-02 14:12:22 +00:00
|
|
|
|
2015-11-13 11:39:07 +00:00
|
|
|
if in.Rcode != dns.RcodeSuccess {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 21:47:18 +00:00
|
|
|
}
|
2015-11-14 01:18:15 +00:00
|
|
|
|
|
|
|
func TestDNS_PreparedQuery_AllowStale(t *testing.T) {
|
|
|
|
confFn := func(c *DNSConfig) {
|
|
|
|
c.AllowStale = true
|
|
|
|
c.MaxStale = time.Second
|
|
|
|
}
|
|
|
|
dir, srv := makeDNSServerConfig(t, nil, confFn)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
m := MockPreparedQuery{}
|
|
|
|
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.executeFn = func(args *structs.PreparedQueryExecuteRequest, reply *structs.PreparedQueryExecuteResponse) error {
|
|
|
|
// Return a response that's perpetually too stale.
|
|
|
|
reply.LastContact = 2 * time.Second
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the lookup terminates and results in an SOA since
|
|
|
|
// the query doesn't exist.
|
|
|
|
{
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("nope.query.consul.", dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Ns) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
soaRec, ok := in.Ns[0].(*dns.SOA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
if soaRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-17 16:40:47 +00:00
|
|
|
|
|
|
|
func TestDNS_InvalidQueries(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Try invalid forms of queries that should hit the special invalid case
|
|
|
|
// of our query parser.
|
|
|
|
questions := []string{
|
|
|
|
"consul.",
|
|
|
|
"node.consul.",
|
|
|
|
"service.consul.",
|
|
|
|
"query.consul.",
|
|
|
|
}
|
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Ns) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
soaRec, ok := in.Ns[0].(*dns.SOA)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
if soaRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Ns[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-01 16:46:26 +00:00
|
|
|
|
|
|
|
func TestDNS_PreparedQuery_AgentSource(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
m := MockPreparedQuery{}
|
|
|
|
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.executeFn = func(args *structs.PreparedQueryExecuteRequest, reply *structs.PreparedQueryExecuteResponse) error {
|
|
|
|
// Check that the agent inserted its self-name and datacenter to
|
|
|
|
// the RPC request body.
|
|
|
|
if args.Agent.Datacenter != srv.agent.config.Datacenter ||
|
|
|
|
args.Agent.Node != srv.agent.config.NodeName {
|
|
|
|
t.Fatalf("bad: %#v", args.Agent)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("foo.query.consul.", dns.TypeSRV)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
if _, _, err := c.Exchange(m, addr.String()); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 23:24:44 +00:00
|
|
|
|
2016-08-12 04:46:14 +00:00
|
|
|
func TestDNS_trimUDPResponse_NoTrim(t *testing.T) {
|
|
|
|
resp := &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Extra: []dns.RR{
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("10.0.1.185"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &DefaultConfig().DNSConfig
|
|
|
|
if trimmed := trimUDPResponse(config, resp); trimmed {
|
|
|
|
t.Fatalf("Bad %#v", *resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Extra: []dns.RR{
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("10.0.1.185"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp, expected) {
|
|
|
|
t.Fatalf("Bad %#v vs. %#v", *resp, *expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_trimUDPResponse_TrimLimit(t *testing.T) {
|
|
|
|
config := &DefaultConfig().DNSConfig
|
|
|
|
|
|
|
|
resp, expected := &dns.Msg{}, &dns.Msg{}
|
|
|
|
for i := 0; i < config.UDPAnswerLimit+1; i++ {
|
|
|
|
target := fmt.Sprintf("ip-10-0-1-%d.node.dc1.consul.", 185+i)
|
|
|
|
srv := &dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: target,
|
|
|
|
}
|
|
|
|
a := &dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: target,
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP(fmt.Sprintf("10.0.1.%d", 185+i)),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, srv)
|
|
|
|
resp.Extra = append(resp.Extra, a)
|
|
|
|
if i < config.UDPAnswerLimit {
|
|
|
|
expected.Answer = append(expected.Answer, srv)
|
|
|
|
expected.Extra = append(expected.Extra, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if trimmed := trimUDPResponse(config, resp); !trimmed {
|
|
|
|
t.Fatalf("Bad %#v", *resp)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp, expected) {
|
|
|
|
t.Fatalf("Bad %#v vs. %#v", *resp, *expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_trimUDPResponse_TrimSize(t *testing.T) {
|
|
|
|
config := &DefaultConfig().DNSConfig
|
|
|
|
|
|
|
|
resp := &dns.Msg{}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
target := fmt.Sprintf("ip-10-0-1-%d.node.dc1.consul.", 185+i)
|
|
|
|
srv := &dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: target,
|
|
|
|
}
|
|
|
|
a := &dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: target,
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP(fmt.Sprintf("10.0.1.%d", 185+i)),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, srv)
|
|
|
|
resp.Extra = append(resp.Extra, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't know the exact trim, but we know the resulting answer
|
|
|
|
// data should match its extra data.
|
|
|
|
if trimmed := trimUDPResponse(config, resp); !trimmed {
|
|
|
|
t.Fatalf("Bad %#v", *resp)
|
|
|
|
}
|
|
|
|
if len(resp.Answer) == 0 || len(resp.Answer) != len(resp.Extra) {
|
|
|
|
t.Fatalf("Bad %#v", *resp)
|
|
|
|
}
|
|
|
|
for i, _ := range resp.Answer {
|
|
|
|
srv, ok := resp.Answer[i].(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should be SRV")
|
|
|
|
}
|
|
|
|
|
|
|
|
a, ok := resp.Extra[i].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should be A")
|
|
|
|
}
|
|
|
|
|
|
|
|
if srv.Target != a.Header().Name {
|
|
|
|
t.Fatalf("Bad %#v vs. %#v", *srv, *a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_syncExtra(t *testing.T) {
|
|
|
|
resp := &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
// These two are on the same host so the redundant extra
|
|
|
|
// records should get deduplicated.
|
|
|
|
&dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Port: 1001,
|
|
|
|
Target: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
},
|
|
|
|
&dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Port: 1002,
|
|
|
|
Target: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
},
|
|
|
|
// This one isn't in the Consul domain so it will get a
|
|
|
|
// CNAME and then an A record from the recursor.
|
|
|
|
&dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Port: 1003,
|
|
|
|
Target: "demo.consul.io.",
|
|
|
|
},
|
2016-08-12 19:16:21 +00:00
|
|
|
// This one isn't in the Consul domain and it will get
|
|
|
|
// a CNAME and A record from a recursor that alters the
|
|
|
|
// case of the name. This proves we look up in the index
|
|
|
|
// in a case-insensitive way.
|
|
|
|
&dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Port: 1001,
|
|
|
|
Target: "insensitive.consul.io.",
|
|
|
|
},
|
2016-08-12 04:46:14 +00:00
|
|
|
// This is also a CNAME, but it'll be set up to loop to
|
|
|
|
// make sure we don't crash.
|
|
|
|
&dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Port: 1001,
|
|
|
|
Target: "deadly.consul.io.",
|
|
|
|
},
|
|
|
|
// This is also a CNAME, but it won't have another record.
|
|
|
|
&dns.SRV{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "redis-cache-redis.service.consul.",
|
|
|
|
Rrtype: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Port: 1001,
|
|
|
|
Target: "nope.consul.io.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Extra: []dns.RR{
|
|
|
|
// These should get deduplicated.
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("10.0.1.185"),
|
|
|
|
},
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("10.0.1.185"),
|
|
|
|
},
|
|
|
|
// This is a normal CNAME followed by an A record but we
|
|
|
|
// have flipped the order. The algorithm should emit them
|
|
|
|
// in the opposite order.
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "fakeserver.consul.io.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("127.0.0.1"),
|
|
|
|
},
|
|
|
|
&dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "demo.consul.io.",
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "fakeserver.consul.io.",
|
|
|
|
},
|
2016-08-12 19:16:21 +00:00
|
|
|
// These differ in case to test case insensitivity.
|
|
|
|
&dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "INSENSITIVE.CONSUL.IO.",
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "Another.Server.Com.",
|
|
|
|
},
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "another.server.com.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("127.0.0.1"),
|
|
|
|
},
|
2016-08-12 04:46:14 +00:00
|
|
|
// This doesn't appear in the answer, so should get
|
|
|
|
// dropped.
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "ip-10-0-1-186.node.dc1.consul.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("10.0.1.186"),
|
|
|
|
},
|
|
|
|
// These two test edge cases with CNAME handling.
|
|
|
|
&dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "deadly.consul.io.",
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "deadly.consul.io.",
|
|
|
|
},
|
|
|
|
&dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "nope.consul.io.",
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "notthere.consul.io.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-12 21:51:50 +00:00
|
|
|
index := make(map[string]dns.RR)
|
|
|
|
indexRRs(resp.Extra, index)
|
2016-08-12 04:46:14 +00:00
|
|
|
syncExtra(index, resp)
|
|
|
|
|
|
|
|
expected := &dns.Msg{
|
|
|
|
Answer: resp.Answer,
|
|
|
|
Extra: []dns.RR{
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "ip-10-0-1-185.node.dc1.consul.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("10.0.1.185"),
|
|
|
|
},
|
|
|
|
&dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "demo.consul.io.",
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "fakeserver.consul.io.",
|
|
|
|
},
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "fakeserver.consul.io.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("127.0.0.1"),
|
|
|
|
},
|
2016-08-12 19:16:21 +00:00
|
|
|
&dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "INSENSITIVE.CONSUL.IO.",
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "Another.Server.Com.",
|
|
|
|
},
|
|
|
|
&dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "another.server.com.",
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: net.ParseIP("127.0.0.1"),
|
|
|
|
},
|
2016-08-12 04:46:14 +00:00
|
|
|
&dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "deadly.consul.io.",
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "deadly.consul.io.",
|
|
|
|
},
|
|
|
|
&dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "nope.consul.io.",
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Target: "notthere.consul.io.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp, expected) {
|
|
|
|
t.Fatalf("Bad %#v vs. %#v", *resp, *expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_Compression_trimUDPResponse(t *testing.T) {
|
2016-08-11 23:24:44 +00:00
|
|
|
config := &DefaultConfig().DNSConfig
|
|
|
|
|
|
|
|
m := dns.Msg{}
|
2016-08-12 04:46:14 +00:00
|
|
|
trimUDPResponse(config, &m)
|
2016-08-11 23:24:44 +00:00
|
|
|
if m.Compress {
|
|
|
|
t.Fatalf("compression should be off")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The trim function temporarily turns off compression, so we need to
|
|
|
|
// make sure the setting gets restored properly.
|
|
|
|
m.Compress = true
|
2016-08-12 04:46:14 +00:00
|
|
|
trimUDPResponse(config, &m)
|
2016-08-11 23:24:44 +00:00
|
|
|
if !m.Compress {
|
|
|
|
t.Fatalf("compression should be on")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_Compression_Query(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a node with a service.
|
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an equivalent prepared query.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
args := &structs.PreparedQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the service directly and via prepared query.
|
|
|
|
questions := []string{
|
|
|
|
"db.service.consul.",
|
|
|
|
id + ".query.consul.",
|
|
|
|
}
|
|
|
|
for _, question := range questions {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(question, dns.TypeSRV)
|
|
|
|
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
conn, err := dns.Dial("udp", addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a manual exchange with compression on (the default).
|
|
|
|
srv.config.DisableCompression = false
|
|
|
|
if err := conn.WriteMsg(m); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
p := make([]byte, dns.MaxMsgSize)
|
|
|
|
compressed, err := conn.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable compression and try again.
|
|
|
|
srv.config.DisableCompression = true
|
|
|
|
if err := conn.WriteMsg(m); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
unc, err := conn.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't see the compressed status given the DNS API, so we
|
|
|
|
// just make sure the message is smaller to see if it's
|
|
|
|
// respecting the flag.
|
|
|
|
if compressed == 0 || unc == 0 || compressed >= unc {
|
|
|
|
t.Fatalf("'%s' doesn't look compressed: %d vs. %d", question, compressed, unc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_Compression_ReverseLookup(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node.
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo2",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("2.0.0.127.in-addr.arpa.", dns.TypeANY)
|
|
|
|
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
conn, err := dns.Dial("udp", addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a manual exchange with compression on (the default).
|
|
|
|
if err := conn.WriteMsg(m); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
p := make([]byte, dns.MaxMsgSize)
|
|
|
|
compressed, err := conn.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable compression and try again.
|
|
|
|
srv.config.DisableCompression = true
|
|
|
|
if err := conn.WriteMsg(m); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
unc, err := conn.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't see the compressed status given the DNS API, so we just make
|
|
|
|
// sure the message is smaller to see if it's respecting the flag.
|
|
|
|
if compressed == 0 || unc == 0 || compressed >= unc {
|
|
|
|
t.Fatalf("doesn't look compressed: %d vs. %d", compressed, unc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_Compression_Recurse(t *testing.T) {
|
|
|
|
recursor := makeRecursor(t, []dns.RR{dnsA("apple.com", "1.2.3.4")})
|
|
|
|
defer recursor.Shutdown()
|
|
|
|
|
|
|
|
dir, srv := makeDNSServerConfig(t, func(c *Config) {
|
|
|
|
c.DNSRecursor = recursor.Addr
|
|
|
|
}, nil)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("apple.com.", dns.TypeANY)
|
|
|
|
|
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
|
|
|
conn, err := dns.Dial("udp", addr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a manual exchange with compression on (the default).
|
|
|
|
if err := conn.WriteMsg(m); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
p := make([]byte, dns.MaxMsgSize)
|
|
|
|
compressed, err := conn.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable compression and try again.
|
|
|
|
srv.config.DisableCompression = true
|
|
|
|
if err := conn.WriteMsg(m); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
unc, err := conn.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't see the compressed status given the DNS API, so we just make
|
|
|
|
// sure the message is smaller to see if it's respecting the flag.
|
|
|
|
if compressed == 0 || unc == 0 || compressed >= unc {
|
|
|
|
t.Fatalf("doesn't look compressed: %d vs. %d", compressed, unc)
|
|
|
|
}
|
|
|
|
}
|