2014-01-02 21:12:05 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-02-14 20:26:51 +00:00
|
|
|
"fmt"
|
2014-01-02 21:12:05 +00:00
|
|
|
"os"
|
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"
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
|
|
|
"github.com/miekg/dns"
|
2014-01-02 21:12:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeDNSServer(t *testing.T) (string, *DNSServer) {
|
2014-06-08 23:14:21 +00:00
|
|
|
config := &DNSConfig{}
|
|
|
|
return makeDNSServerConfig(t, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeDNSServerConfig(t *testing.T, config *DNSConfig) (string, *DNSServer) {
|
2014-01-02 21:12:05 +00:00
|
|
|
conf := nextConfig()
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := conf.ClientListener(conf.Addresses.DNS, conf.Ports.DNS)
|
2014-01-02 21:12:05 +00:00
|
|
|
dir, agent := makeAgent(t, conf)
|
2014-06-08 22:49:24 +00:00
|
|
|
server, err := NewDNSServer(agent, config, agent.logOutput,
|
2014-10-31 19:19:41 +00:00
|
|
|
conf.Domain, addr.String(), []string{"8.8.8.8:53"})
|
2014-01-02 21:12:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return dir, server
|
|
|
|
}
|
|
|
|
|
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-02 21:12:05 +00:00
|
|
|
func TestDNS_IsAlive(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("_test.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-02 21:12:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
txt, ok := in.Answer[0].(*dns.TXT)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
if txt.Txt[0] != "ok" {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
|
|
|
}
|
2014-01-03 01:58:58 +00:00
|
|
|
|
|
|
|
func TestDNS_NodeLookup(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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",
|
|
|
|
}
|
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])
|
|
|
|
}
|
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) {
|
|
|
|
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: "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) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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)
|
|
|
|
m.SetQuestion("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-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) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
// Should have the CNAME record + a few A records
|
|
|
|
if len(in.Answer) < 2 {
|
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) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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) {
|
|
|
|
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: "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) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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 21:00:03 +00:00
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
2014-01-08 23:13:27 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 23:13:27 +00:00
|
|
|
Port: 12345,
|
|
|
|
},
|
2014-01-03 21:00:03 +00:00
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-01-03 21:00:03 +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)
|
2014-02-26 01:41:48 +00:00
|
|
|
m.SetQuestion("db.service.consul.", dns.TypeSRV)
|
2014-01-03 21:00:03 +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-01-03 21:00:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
if len(in.Answer) != 1 {
|
2014-01-03 21:00:03 +00:00
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
2014-01-03 21:00:03 +00:00
|
|
|
if !ok {
|
2014-04-21 22:33:01 +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-06-08 23:14:21 +00:00
|
|
|
if srvRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-04-21 22:33:01 +00:00
|
|
|
|
|
|
|
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-06-08 23:14:21 +00:00
|
|
|
if aRec.Hdr.Ttl != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", in.Extra[0])
|
|
|
|
}
|
2014-04-21 22:33:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 19:58:32 +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 node
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("db.service.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.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-07-17 06:38:24 +00:00
|
|
|
func TestDNS_CaseInsensitiveServiceLookup(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: "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("mASTER.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-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_ServiceLookup_TagPeriod(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"v1.master"},
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
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("v1.master.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-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) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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-06 22:56:41 +00:00
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
2014-01-08 23:13:27 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 23:13:27 +00:00
|
|
|
Port: 12345,
|
|
|
|
},
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-01-06 22:56:41 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = &structs.RegisterRequest{
|
2014-01-08 23:13:27 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db2",
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"slave"},
|
2014-01-08 23:13:27 +00:00
|
|
|
Port: 12345,
|
|
|
|
},
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = &structs.RegisterRequest{
|
2014-01-08 23:13:27 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db3",
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"slave"},
|
2014-01-08 23:13:27 +00:00
|
|
|
Port: 12346,
|
|
|
|
},
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
|
|
|
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.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-06 22:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
if len(in.Answer) != 1 {
|
2014-01-06 22:56:41 +00:00
|
|
|
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-26 01:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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-26 01:41:48 +00:00
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"master"},
|
2014-02-26 01:41:48 +00:00
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
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{
|
|
|
|
ID: "db2",
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"slave"},
|
2014-02-26 01:41:48 +00:00
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
ID: "db3",
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"slave"},
|
2014-02-26 01:41:48 +00:00
|
|
|
Port: 12346,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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-04-11 22:46:55 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
2014-02-26 01:41:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) != 2 {
|
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
srvRec, ok := in.Answer[0].(*dns.SRV)
|
2014-01-06 22:56:41 +00:00
|
|
|
if !ok {
|
2014-02-26 01:41:48 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
2014-01-06 22:56:41 +00:00
|
|
|
}
|
2014-02-14 20:26:51 +00:00
|
|
|
if srvRec.Port != 12345 && srvRec.Port != 12346 {
|
2014-01-06 22:56:41 +00:00
|
|
|
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
|
|
|
srvRec, ok = in.Answer[1].(*dns.SRV)
|
2014-01-06 22:56:41 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[1])
|
|
|
|
}
|
2014-02-14 20:26:51 +00:00
|
|
|
if srvRec.Port != 12346 && srvRec.Port != 12345 {
|
2014-01-06 22:56:41 +00:00
|
|
|
t.Fatalf("Bad: %#v", srvRec)
|
|
|
|
}
|
2014-02-26 01:41:48 +00:00
|
|
|
if srvRec.Port == in.Answer[0].(*dns.SRV).Port {
|
2014-02-14 20:26:51 +00:00
|
|
|
t.Fatalf("should be a different port")
|
|
|
|
}
|
2014-01-06 22:56:41 +00:00
|
|
|
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-06 22:56:41 +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
|
|
|
func TestDNS_Recurse(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("apple.com.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
c.Net = "tcp"
|
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) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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-24 02:04:12 +00:00
|
|
|
// Register nodes
|
2014-01-15 21:30:04 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-15 21:30:04 +00:00
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "serf",
|
|
|
|
Name: "serf",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-01-15 21:30:04 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-24 02:04:12 +00:00
|
|
|
args2 := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"master"},
|
2014-02-24 02:04:12 +00:00
|
|
|
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-07-16 22:11:45 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-01-13 13:59:24 +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)
|
|
|
|
}
|
|
|
|
|
2014-01-15 21:30:04 +00:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("db.service.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-15 21:30:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-13 13:59:24 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_OnlyPassing(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServerConfig(t, &DNSConfig{OnlyPassing: true})
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register nodes
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args2, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args3, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args4, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("db.service.consul.", 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only 1 is passing, so we should only get 1 answer
|
2014-07-16 22:11:45 +00:00
|
|
|
if len(in.Answer) != 1 {
|
2014-01-15 21:30:04 +00:00
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
2014-07-16 22:11:45 +00:00
|
|
|
|
|
|
|
resp := in.Answer[0]
|
|
|
|
aRec := resp.(*dns.A)
|
2015-01-13 13:59:24 +00:00
|
|
|
|
|
|
|
if aRec.A.String() != "127.0.0.1" {
|
2014-07-16 22:11:45 +00:00
|
|
|
t.Fatalf("Bad: %#v", in.Answer[0])
|
|
|
|
}
|
2014-01-15 21:30:04 +00:00
|
|
|
}
|
2014-02-14 20:26:51 +00:00
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_Randomize(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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-14 20:26:51 +00:00
|
|
|
// Register nodes
|
|
|
|
for i := 0; i < 3*maxServiceResponses; i++ {
|
|
|
|
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-01-13 00:05:41 +00:00
|
|
|
// Ensure the response is randomized each time.
|
|
|
|
loops := 5
|
2014-02-14 20:26:51 +00:00
|
|
|
uniques := map[string]struct{}{}
|
2014-09-02 19:47:40 +00:00
|
|
|
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
|
2015-01-13 00:05:41 +00:00
|
|
|
for i := 0; i < loops; i++ {
|
2014-02-14 20:26:51 +00:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("web.service.consul.", dns.TypeANY)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2014-04-11 22:46:55 +00:00
|
|
|
in, _, err := c.Exchange(m, addr.String())
|
2014-02-14 20:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response length should be truncated
|
2014-02-26 01:41:48 +00:00
|
|
|
// We should get an A record for each response
|
|
|
|
if len(in.Answer) != maxServiceResponses {
|
2014-02-14 20:26:51 +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, "|")
|
|
|
|
|
2015-01-13 00:05:41 +00:00
|
|
|
// Tally the results
|
2014-02-14 20:26:51 +00:00
|
|
|
uniques[nameS] = struct{}{}
|
|
|
|
}
|
2015-01-13 00:05:41 +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) < (loops - 1) {
|
|
|
|
t.Fatalf("unique response ratio too low: %d/%d\n%v",
|
|
|
|
len(uniques), loops, uniques)
|
|
|
|
}
|
2014-02-14 20:26:51 +00:00
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_CNAME(t *testing.T) {
|
|
|
|
dir, srv := makeDNSServer(t)
|
|
|
|
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",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "search",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
}
|
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("search.service.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)
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
if len(in.Answer) != 4 {
|
2014-02-25 20:07:20 +00:00
|
|
|
t.Fatalf("Bad: %#v", in)
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:41:48 +00:00
|
|
|
// Should have google CNAME
|
2014-02-25 20:07:20 +00:00
|
|
|
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-02-26 01:41:48 +00:00
|
|
|
// Check we recursively resolve
|
|
|
|
for i := 1; i < 4; i++ {
|
|
|
|
if _, ok := in.Answer[i].(*dns.A); !ok {
|
|
|
|
t.Fatalf("Bad: %#v", in.Answer[i])
|
|
|
|
}
|
2014-02-25 20:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 23:14:21 +00:00
|
|
|
|
|
|
|
func TestDNS_NodeLookup_TTL(t *testing.T) {
|
|
|
|
config := &DNSConfig{
|
|
|
|
NodeTTL: 10 * time.Second,
|
|
|
|
AllowStale: true,
|
|
|
|
MaxStale: time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, srv := makeDNSServerConfig(t, config)
|
|
|
|
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) {
|
|
|
|
config := &DNSConfig{
|
|
|
|
ServiceTTL: map[string]time.Duration{
|
|
|
|
"db": 10 * time.Second,
|
|
|
|
"*": 5 * time.Second,
|
|
|
|
},
|
|
|
|
AllowStale: true,
|
|
|
|
MaxStale: time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, srv := makeDNSServerConfig(t, config)
|
|
|
|
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
|
|
|
|
|
|
|
func TestDNS_ServiceLookup_SRV_RFC(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: "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) {
|
|
|
|
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: "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])
|
|
|
|
}
|
|
|
|
}
|