mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	Add Stub resolving
SkyDNS can forward requests from one instance to another. Add this base infrastructure for this feature to CoreDNS. Add more tests as well.
This commit is contained in:
		| @@ -7,128 +7,10 @@ package etcd | ||||
| // names. | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"sort" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/miekg/coredns/middleware" | ||||
| 	"github.com/miekg/coredns/middleware/etcd/msg" | ||||
| 	"github.com/miekg/coredns/middleware/etcd/singleflight" | ||||
| 	"github.com/miekg/coredns/middleware/proxy" | ||||
| 	"github.com/miekg/dns" | ||||
|  | ||||
| 	etcdc "github.com/coreos/etcd/client" | ||||
| 	"golang.org/x/net/context" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	etc    Etcd | ||||
| 	client etcdc.KeysAPI | ||||
| 	ctx    context.Context | ||||
| ) | ||||
|  | ||||
| type Section int | ||||
|  | ||||
| const ( | ||||
| 	Answer Section = iota | ||||
| 	Ns | ||||
| 	Extra | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| 	ctx = context.TODO() | ||||
|  | ||||
| 	etcdCfg := etcdc.Config{ | ||||
| 		Endpoints: []string{"http://localhost:2379"}, | ||||
| 	} | ||||
| 	cli, _ := etcdc.New(etcdCfg) | ||||
| 	etc = Etcd{ | ||||
| 		Proxy:      proxy.New([]string{"8.8.8.8:53"}), | ||||
| 		PathPrefix: "skydns", | ||||
| 		Ctx:        context.Background(), | ||||
| 		Inflight:   &singleflight.Group{}, | ||||
| 		Zones:      []string{"skydns.test."}, | ||||
| 		Client:     etcdc.NewKeysAPI(cli), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func set(t *testing.T, e Etcd, k string, ttl time.Duration, m *msg.Service) { | ||||
| 	b, err := json.Marshal(m) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	path, _ := e.PathWithWildcard(k) | ||||
| 	e.Client.Set(ctx, path, string(b), &etcdc.SetOptions{TTL: ttl}) | ||||
| } | ||||
|  | ||||
| func delete(t *testing.T, e Etcd, k string) { | ||||
| 	path, _ := e.PathWithWildcard(k) | ||||
| 	e.Client.Delete(ctx, path, &etcdc.DeleteOptions{Recursive: false}) | ||||
| } | ||||
|  | ||||
| type rrSet []dns.RR | ||||
|  | ||||
| func (p rrSet) Len() int           { return len(p) } | ||||
| func (p rrSet) Swap(i, j int)      { p[i], p[j] = p[j], p[i] } | ||||
| func (p rrSet) Less(i, j int) bool { return p[i].String() < p[j].String() } | ||||
|  | ||||
| func TestLookup(t *testing.T) { | ||||
| 	for _, serv := range services { | ||||
| 		set(t, etc, serv.Key, 0, serv) | ||||
| 		defer delete(t, etc, serv.Key) | ||||
| 	} | ||||
| 	for _, tc := range dnsTestCases { | ||||
| 		m := new(dns.Msg) | ||||
| 		m.SetQuestion(dns.Fqdn(tc.Qname), tc.Qtype) | ||||
|  | ||||
| 		rec := middleware.NewResponseRecorder(&middleware.TestResponseWriter{}) | ||||
| 		_, err := etc.ServeDNS(ctx, rec, m) | ||||
| 		if err != nil { | ||||
| 			t.Errorf("expected no error, got %v\n", err) | ||||
| 			return | ||||
| 		} | ||||
| 		resp := rec.Reply() | ||||
|  | ||||
| 		sort.Sort(rrSet(resp.Answer)) | ||||
| 		sort.Sort(rrSet(resp.Ns)) | ||||
| 		sort.Sort(rrSet(resp.Extra)) | ||||
|  | ||||
| 		t.Logf("%v\n", resp) | ||||
|  | ||||
| 		if resp.Rcode != tc.Rcode { | ||||
| 			t.Errorf("rcode is %q, expected %q", dns.RcodeToString[resp.Rcode], dns.RcodeToString[tc.Rcode]) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		if len(resp.Answer) != len(tc.Answer) { | ||||
| 			t.Errorf("answer for %q contained %d results, %d expected", tc.Qname, len(resp.Answer), len(tc.Answer)) | ||||
| 			continue | ||||
| 		} | ||||
| 		if len(resp.Ns) != len(tc.Ns) { | ||||
| 			t.Errorf("authority for %q contained %d results, %d expected", tc.Qname, len(resp.Ns), len(tc.Ns)) | ||||
| 			continue | ||||
| 		} | ||||
| 		if len(resp.Extra) != len(tc.Extra) { | ||||
| 			t.Errorf("additional for %q contained %d results, %d expected", tc.Qname, len(resp.Extra), len(tc.Extra)) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		checkSection(t, tc, Answer, resp.Answer) | ||||
| 		checkSection(t, tc, Ns, resp.Ns) | ||||
| 		checkSection(t, tc, Extra, resp.Extra) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type dnsTestCase struct { | ||||
| 	Qname  string | ||||
| 	Qtype  uint16 | ||||
| 	Rcode  int | ||||
| 	Answer []dns.RR | ||||
| 	Ns     []dns.RR | ||||
| 	Extra  []dns.RR | ||||
| } | ||||
|  | ||||
| // Note the key is encoded as DNS name, while in "reality" it is a etcd path. | ||||
| var services = []*msg.Service{ | ||||
| 	{Host: "server1", Port: 8080, Key: "a.server1.dev.region1.skydns.test."}, | ||||
| @@ -136,12 +18,18 @@ var services = []*msg.Service{ | ||||
| 	{Host: "10.0.0.2", Port: 8080, Key: "b.server1.prod.region1.skydns.test."}, | ||||
| 	{Host: "::1", Port: 8080, Key: "b.server6.prod.region1.skydns.test."}, | ||||
|  | ||||
| 	// CNAME dedup Test | ||||
| 	// CNAME dedup | ||||
| 	{Host: "www.miek.nl", Key: "a.miek.nl.skydns.test."}, | ||||
| 	{Host: "www.miek.nl", Key: "b.miek.nl.skydns.test."}, | ||||
|  | ||||
| 	// Unresolvable internal name | ||||
| 	{Host: "unresolvable.skydns.test", Key: "cname.prod.region1.skydns.test."}, | ||||
| 	// priority | ||||
| 	{Host: "server1", Priority: 333, Port: 8080, Key: "priority.skydns.test."}, | ||||
| 	// Subdomain | ||||
| 	{Host: "server1", Port: 0, Key: "a.sub.region1.skydns.test."}, | ||||
| 	{Host: "server2", Port: 80, Key: "b.sub.region1.skydns.test."}, | ||||
| 	{Host: "10.0.0.1", Port: 8080, Key: "c.sub.region1.skydns.test."}, | ||||
| } | ||||
|  | ||||
| var dnsTestCases = []dnsTestCase{ | ||||
| @@ -182,6 +70,21 @@ var dnsTestCases = []dnsTestCase{ | ||||
| 			newA("server1.prod.region1.skydns.test. 300 A 10.0.0.2"), | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Priority Test | ||||
| 	{ | ||||
| 		Qname: "priority.skydns.test.", Qtype: dns.TypeSRV, | ||||
| 		Answer: []dns.RR{newSRV("priority.skydns.test. 300 SRV 333 100 8080 server1.")}, | ||||
| 	}, | ||||
| 	// Subdomain Test | ||||
| 	{ | ||||
| 		Qname: "sub.region1.skydns.test.", Qtype: dns.TypeSRV, | ||||
| 		Answer: []dns.RR{ | ||||
| 			newSRV("sub.region1.skydns.test. 300 IN SRV 10 33 0 server1."), | ||||
| 			newSRV("sub.region1.skydns.test. 300 IN SRV 10 33 80 server2."), | ||||
| 			newSRV("sub.region1.skydns.test. 300 IN SRV 10 33 8080 c.sub.region1.skydns.test."), | ||||
| 		}, | ||||
| 		Extra: []dns.RR{newA("c.sub.region1.skydns.test. 300 IN A 10.0.0.1")}, | ||||
| 	}, | ||||
| 	// Multi SRV with the same target, should be dedupped. | ||||
| 	{ | ||||
| 		Qname: "*.miek.nl.skydns.test.", Qtype: dns.TypeSRV, | ||||
| @@ -223,29 +126,6 @@ var dnsTestCases = []dnsTestCase{ | ||||
| 			Answer: []dns.RR{}, | ||||
| 			Ns:     []dns.RR{newSOA("skydns.test. 60 SOA ns.dns.skydns.test. hostmaster.skydns.test. 1407441600 28800 7200 604800 60")}, | ||||
| 		}, | ||||
| 		// Priority Test | ||||
| 		{ | ||||
| 			Qname: "region6.skydns.test.", Qtype: dns.TypeSRV, | ||||
| 			Answer: []dns.RR{newSRV("region6.skydns.test. 300 SRV 333 100 80 server4.")}, | ||||
| 		}, | ||||
| 		// Subdomain Test | ||||
| 		{ | ||||
| 			Qname: "region1.skydns.test.", Qtype: dns.TypeSRV, | ||||
| 			Answer: []dns.RR{ | ||||
| 				newSRV("region1.skydns.test. 300 SRV 10 33 0 104.server1.dev.region1.skydns.test."), | ||||
| 				newSRV("region1.skydns.test. 300 SRV 10 33 80 server2"), | ||||
| 				newSRV("region1.skydns.test. 300 SRV 10 33 8080 server1.")}, | ||||
| 			Extra: []dns.RR{newA("104.server1.dev.region1.skydns.test. 300 A 10.0.0.1")}, | ||||
| 		}, | ||||
| 		// Subdomain Weight Test | ||||
| 		{ | ||||
| 			Qname: "region5.skydns.test.", Qtype: dns.TypeSRV, | ||||
| 			Answer: []dns.RR{ | ||||
| 				newSRV("region5.skydns.test. 300 SRV 10 22 0 server2."), | ||||
| 				newSRV("region5.skydns.test. 300 SRV 10 36 0 server1."), | ||||
| 				newSRV("region5.skydns.test. 300 SRV 10 41 0 server3."), | ||||
| 				newSRV("region5.skydns.test. 300 SRV 30 100 0 server4.")}, | ||||
| 		}, | ||||
| 		// Wildcard Test | ||||
| 		{ | ||||
| 			Qname: "*.region1.skydns.test.", Qtype: dns.TypeSRV, | ||||
| @@ -299,99 +179,3 @@ var dnsTestCases = []dnsTestCase{ | ||||
| 		}, | ||||
| 	*/ | ||||
| } | ||||
|  | ||||
| func newA(rr string) *dns.A         { r, _ := dns.NewRR(rr); return r.(*dns.A) } | ||||
| func newAAAA(rr string) *dns.AAAA   { r, _ := dns.NewRR(rr); return r.(*dns.AAAA) } | ||||
| func newCNAME(rr string) *dns.CNAME { r, _ := dns.NewRR(rr); return r.(*dns.CNAME) } | ||||
| func newSRV(rr string) *dns.SRV     { r, _ := dns.NewRR(rr); return r.(*dns.SRV) } | ||||
| func newSOA(rr string) *dns.SOA     { r, _ := dns.NewRR(rr); return r.(*dns.SOA) } | ||||
| func newNS(rr string) *dns.NS       { r, _ := dns.NewRR(rr); return r.(*dns.NS) } | ||||
| func newPTR(rr string) *dns.PTR     { r, _ := dns.NewRR(rr); return r.(*dns.PTR) } | ||||
| func newTXT(rr string) *dns.TXT     { r, _ := dns.NewRR(rr); return r.(*dns.TXT) } | ||||
| func newMX(rr string) *dns.MX       { r, _ := dns.NewRR(rr); return r.(*dns.MX) } | ||||
|  | ||||
| func checkSection(t *testing.T, tc dnsTestCase, sect Section, rr []dns.RR) { | ||||
| 	section := []dns.RR{} | ||||
| 	switch sect { | ||||
| 	case 0: | ||||
| 		section = tc.Answer | ||||
| 	case 1: | ||||
| 		section = tc.Ns | ||||
| 	case 2: | ||||
| 		section = tc.Extra | ||||
| 	} | ||||
|  | ||||
| 	for i, a := range rr { | ||||
| 		if a.Header().Name != section[i].Header().Name { | ||||
| 			t.Errorf("answer %d should have a Header Name of %q, but has %q", i, section[i].Header().Name, a.Header().Name) | ||||
| 			continue | ||||
| 		} | ||||
| 		// 303 signals: don't care what the ttl is. | ||||
| 		if section[i].Header().Ttl != 303 && a.Header().Ttl != section[i].Header().Ttl { | ||||
| 			t.Errorf("Answer %d should have a Header TTL of %d, but has %d", i, section[i].Header().Ttl, a.Header().Ttl) | ||||
| 			continue | ||||
| 		} | ||||
| 		if a.Header().Rrtype != section[i].Header().Rrtype { | ||||
| 			t.Errorf("answer %d should have a header rr type of %d, but has %d", i, section[i].Header().Rrtype, a.Header().Rrtype) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		switch x := a.(type) { | ||||
| 		case *dns.SRV: | ||||
| 			if x.Priority != section[i].(*dns.SRV).Priority { | ||||
| 				t.Errorf("answer %d should have a Priority of %d, but has %d", i, section[i].(*dns.SRV).Priority, x.Priority) | ||||
| 			} | ||||
| 			if x.Weight != section[i].(*dns.SRV).Weight { | ||||
| 				t.Errorf("answer %d should have a Weight of %d, but has %d", i, section[i].(*dns.SRV).Weight, x.Weight) | ||||
| 			} | ||||
| 			if x.Port != section[i].(*dns.SRV).Port { | ||||
| 				t.Errorf("answer %d should have a Port of %d, but has %d", i, section[i].(*dns.SRV).Port, x.Port) | ||||
| 			} | ||||
| 			if x.Target != section[i].(*dns.SRV).Target { | ||||
| 				t.Errorf("answer %d should have a Target of %q, but has %q", i, section[i].(*dns.SRV).Target, x.Target) | ||||
| 			} | ||||
| 		case *dns.A: | ||||
| 			if x.A.String() != section[i].(*dns.A).A.String() { | ||||
| 				t.Errorf("answer %d should have a Address of %q, but has %q", i, section[i].(*dns.A).A.String(), x.A.String()) | ||||
| 			} | ||||
| 		case *dns.AAAA: | ||||
| 			if x.AAAA.String() != section[i].(*dns.AAAA).AAAA.String() { | ||||
| 				t.Errorf("answer %d should have a Address of %q, but has %q", i, section[i].(*dns.AAAA).AAAA.String(), x.AAAA.String()) | ||||
| 			} | ||||
| 		case *dns.TXT: | ||||
| 			for j, txt := range x.Txt { | ||||
| 				if txt != section[i].(*dns.TXT).Txt[j] { | ||||
| 					t.Errorf("answer %d should have a Txt of %q, but has %q", i, section[i].(*dns.TXT).Txt[j], txt) | ||||
| 				} | ||||
| 			} | ||||
| 		case *dns.SOA: | ||||
| 			tt := section[i].(*dns.SOA) | ||||
| 			if x.Ns != tt.Ns { | ||||
| 				t.Errorf("SOA nameserver should be %q, but is %q", x.Ns, tt.Ns) | ||||
| 			} | ||||
| 		case *dns.PTR: | ||||
| 			tt := section[i].(*dns.PTR) | ||||
| 			if x.Ptr != tt.Ptr { | ||||
| 				t.Errorf("PTR ptr should be %q, but is %q", x.Ptr, tt.Ptr) | ||||
| 			} | ||||
| 		case *dns.CNAME: | ||||
| 			tt := section[i].(*dns.CNAME) | ||||
| 			if x.Target != tt.Target { | ||||
| 				t.Errorf("CNAME target should be %q, but is %q", x.Target, tt.Target) | ||||
| 			} | ||||
| 		case *dns.MX: | ||||
| 			tt := section[i].(*dns.MX) | ||||
| 			if x.Mx != tt.Mx { | ||||
| 				t.Errorf("MX Mx should be %q, but is %q", x.Mx, tt.Mx) | ||||
| 			} | ||||
| 			if x.Preference != tt.Preference { | ||||
| 				t.Errorf("MX Preference should be %q, but is %q", x.Preference, tt.Preference) | ||||
| 			} | ||||
| 		case *dns.NS: | ||||
| 			tt := section[i].(*dns.NS) | ||||
| 			if x.Ns != tt.Ns { | ||||
| 				t.Errorf("NS nameserver should be %q, but is %q", x.Ns, tt.Ns) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user