| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | package autopath
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var autopathTestCases = []test.Case{
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		// search path expansion.
 | 
					
						
							|  |  |  | 		Qname: "b.example.org.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.CNAME("b.example.org. 3600 IN CNAME b.com."),
 | 
					
						
							|  |  |  | 			test.A("b.com." + defaultA),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		// No search path expansion
 | 
					
						
							|  |  |  | 		Qname: "a.example.com.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("a.example.com." + defaultA),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newTestAutoPath() *AutoPath {
 | 
					
						
							|  |  |  | 	ap := new(AutoPath)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  | 	ap.Zones = []string{"."}
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	ap.Next = nextHandler(map[string]int{
 | 
					
						
							|  |  |  | 		"b.example.org.": dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		"b.com.":         dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		"a.example.com.": dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ap.search = []string{"example.org.", "example.com.", "com.", ""}
 | 
					
						
							|  |  |  | 	return ap
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAutoPath(t *testing.T) {
 | 
					
						
							|  |  |  | 	ap := newTestAutoPath()
 | 
					
						
							|  |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range autopathTestCases {
 | 
					
						
							|  |  |  | 		m := tc.Msg()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		_, err := ap.ServeDNS(ctx, rec, m)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-01-19 11:23:13 +00:00
										 |  |  | 			t.Errorf("Expected no error, got %v", err)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 15:30:58 +01:00
										 |  |  | 		// No sorting here as we want to check if the CNAME sits *before* the
 | 
					
						
							|  |  |  | 		// test of the answer.
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		resp := rec.Msg
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-19 11:23:13 +00:00
										 |  |  | 		if err := test.Header(tc, resp); err != nil {
 | 
					
						
							|  |  |  | 			t.Error(err)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-01-19 11:23:13 +00:00
										 |  |  | 		if err := test.Section(tc, test.Answer, resp.Answer); err != nil {
 | 
					
						
							|  |  |  | 			t.Error(err)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-01-19 11:23:13 +00:00
										 |  |  | 		if err := test.Section(tc, test.Ns, resp.Ns); err != nil {
 | 
					
						
							|  |  |  | 			t.Error(err)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-01-19 11:23:13 +00:00
										 |  |  | 		if err := test.Section(tc, test.Extra, resp.Extra); err != nil {
 | 
					
						
							|  |  |  | 			t.Error(err)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var autopathNoAnswerTestCases = []test.Case{
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		// search path expansion, no answer
 | 
					
						
							|  |  |  | 		Qname: "c.example.org.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.CNAME("b.example.org. 3600 IN CNAME b.com."),
 | 
					
						
							|  |  |  | 			test.A("b.com." + defaultA),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAutoPathNoAnswer(t *testing.T) {
 | 
					
						
							|  |  |  | 	ap := newTestAutoPath()
 | 
					
						
							|  |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range autopathNoAnswerTestCases {
 | 
					
						
							|  |  |  | 		m := tc.Msg()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		rcode, err := ap.ServeDNS(ctx, rec, m)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-01-19 11:23:13 +00:00
										 |  |  | 			t.Errorf("Expected no error, got %v", err)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		if plugin.ClientWrite(rcode) {
 | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 			t.Fatalf("Expected no client write, got one for rcode %d", rcode)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // nextHandler returns a Handler that returns an answer for the question in the
 | 
					
						
							|  |  |  | // request per the domain->answer map. On success an RR will be returned: "qname 3600 IN A 127.0.0.53"
 | 
					
						
							|  |  |  | func nextHandler(mm map[string]int) test.Handler {
 | 
					
						
							|  |  |  | 	return test.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 		rcode, ok := mm[r.Question[0].Name]
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			return dns.RcodeServerFailure, nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		m := new(dns.Msg)
 | 
					
						
							|  |  |  | 		m.SetReply(r)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch rcode {
 | 
					
						
							|  |  |  | 		case dns.RcodeNameError:
 | 
					
						
							|  |  |  | 			m.Rcode = rcode
 | 
					
						
							|  |  |  | 			m.Ns = []dns.RR{soa}
 | 
					
						
							|  |  |  | 			w.WriteMsg(m)
 | 
					
						
							|  |  |  | 			return m.Rcode, nil
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case dns.RcodeSuccess:
 | 
					
						
							|  |  |  | 			m.Rcode = rcode
 | 
					
						
							|  |  |  | 			a, _ := dns.NewRR(r.Question[0].Name + defaultA)
 | 
					
						
							|  |  |  | 			m.Answer = []dns.RR{a}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			w.WriteMsg(m)
 | 
					
						
							|  |  |  | 			return m.Rcode, nil
 | 
					
						
							|  |  |  | 		default:
 | 
					
						
							|  |  |  | 			panic("nextHandler: unhandled rcode")
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const defaultA = " 3600 IN A 127.0.0.53"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var soa = func() dns.RR {
 | 
					
						
							|  |  |  | 	s, _ := dns.NewRR("example.org.		1800	IN	SOA	example.org. example.org. 1502165581 14400 3600 604800 14400")
 | 
					
						
							|  |  |  | 	return s
 | 
					
						
							|  |  |  | }()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestInSearchPath(t *testing.T) {
 | 
					
						
							|  |  |  | 	a := AutoPath{search: []string{"default.svc.cluster.local.", "svc.cluster.local.", "cluster.local."}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		qname string
 | 
					
						
							|  |  |  | 		b     bool
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{"google.com", false},
 | 
					
						
							|  |  |  | 		{"default.svc.cluster.local.", true},
 | 
					
						
							|  |  |  | 		{"a.default.svc.cluster.local.", true},
 | 
					
						
							|  |  |  | 		{"a.b.svc.cluster.local.", false},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for i, tc := range tests {
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 		got := firstInSearchPath(tc.qname, a.search)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		if got != tc.b {
 | 
					
						
							| 
									
										
										
										
											2017-08-09 11:00:03 -07:00
										 |  |  | 			t.Errorf("Test %d, got %v, expected %v", i, got, tc.b)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |