| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | // +build etcd
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package etcd
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"sort"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/etcd/msg"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/pkg/dnsrecorder"
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/test"
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGroupLookup(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	etc := newEtcdMiddleware()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 	for _, serv := range servicesGroup {
 | 
					
						
							|  |  |  | 		set(t, etc, serv.Key, 0, serv)
 | 
					
						
							|  |  |  | 		defer delete(t, etc, serv.Key)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, tc := range dnsTestCasesGroup {
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		m := tc.Msg()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		rec := dnsrecorder.New(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2016-06-11 08:00:47 +10:00
										 |  |  | 		_, err := etc.ServeDNS(ctxt, rec, m)
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			t.Errorf("expected no error, got %v\n", err)
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 			continue
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		resp := rec.Msg
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		sort.Sort(test.RRSet(resp.Answer))
 | 
					
						
							|  |  |  | 		sort.Sort(test.RRSet(resp.Ns))
 | 
					
						
							|  |  |  | 		sort.Sort(test.RRSet(resp.Extra))
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if !test.Header(t, tc, resp) {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 10:22:27 +01:00
										 |  |  | 		if !test.Section(t, tc, test.Answer, resp.Answer) {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 10:22:27 +01:00
										 |  |  | 		if !test.Section(t, tc, test.Ns, resp.Ns) {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 10:22:27 +01:00
										 |  |  | 		if !test.Section(t, tc, test.Extra, resp.Extra) {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Note the key is encoded as DNS name, while in "reality" it is a etcd path.
 | 
					
						
							|  |  |  | var servicesGroup = []*msg.Service{
 | 
					
						
							|  |  |  | 	{Host: "127.0.0.1", Key: "a.dom.skydns.test.", Group: "g1"},
 | 
					
						
							|  |  |  | 	{Host: "127.0.0.2", Key: "b.sub.dom.skydns.test.", Group: "g1"},
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	{Host: "127.0.0.1", Key: "a.dom2.skydns.test.", Group: "g1"},
 | 
					
						
							|  |  |  | 	{Host: "127.0.0.2", Key: "b.sub.dom2.skydns.test.", Group: ""},
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	{Host: "127.0.0.1", Key: "a.dom1.skydns.test.", Group: "g1"},
 | 
					
						
							|  |  |  | 	{Host: "127.0.0.2", Key: "b.sub.dom1.skydns.test.", Group: "g2"},
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | var dnsTestCasesGroup = []test.Case{
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 	// Groups
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		// hits the group 'g1' and only includes those records
 | 
					
						
							|  |  |  | 		Qname: "dom.skydns.test.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 			test.A("dom.skydns.test. 300 IN A 127.0.0.1"),
 | 
					
						
							|  |  |  | 			test.A("dom.skydns.test. 300 IN A 127.0.0.2"),
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		// One has group, the other has not...  Include the non-group always.
 | 
					
						
							|  |  |  | 		Qname: "dom2.skydns.test.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 			test.A("dom2.skydns.test. 300 IN A 127.0.0.1"),
 | 
					
						
							|  |  |  | 			test.A("dom2.skydns.test. 300 IN A 127.0.0.2"),
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		// The groups differ.
 | 
					
						
							|  |  |  | 		Qname: "dom1.skydns.test.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 			test.A("dom1.skydns.test. 300 IN A 127.0.0.1"),
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | }
 |