mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 17:53:21 -04:00 
			
		
		
		
	* add hosts middleware * forgot pointer receiver * add appropriately modified hostsfile tests from golang repo * remove test artifacts, separate hostsfile parsing from caching and opening, remove unused metrics references, move middleware up the chain * refactored the logic for creating records and filtering ip address versions. also got PTR lookups working * Add README.md. Modify config to be more concise. Add zones list to config. Filter PTR responses based on zones list. * add Fallthrough and return correct dns response code otherwise * Simplified Hostsfile to only store hosts in the zones we care about, and by ip version. Added handler tests and improved other tests. * oops, goimports loaded a package from a different repo
		
			
				
	
	
		
			87 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package hosts
 | |
| 
 | |
| import (
 | |
| 	"sort"
 | |
| 	"strings"
 | |
| 	"testing"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/coredns/coredns/middleware/pkg/dnsrecorder"
 | |
| 	"github.com/coredns/coredns/middleware/test"
 | |
| 
 | |
| 	"github.com/miekg/dns"
 | |
| 	"golang.org/x/net/context"
 | |
| )
 | |
| 
 | |
| func TestLookupA(t *testing.T) {
 | |
| 	h := Hosts{Next: test.ErrorHandler(), Hostsfile: &Hostsfile{expire: time.Now().Add(1 * time.Hour), Origins: []string{"."}}}
 | |
| 	h.Parse(strings.NewReader(hostsExample))
 | |
| 
 | |
| 	ctx := context.TODO()
 | |
| 
 | |
| 	for _, tc := range hostsTestCases {
 | |
| 		m := tc.Msg()
 | |
| 
 | |
| 		rec := dnsrecorder.New(&test.ResponseWriter{})
 | |
| 		_, err := h.ServeDNS(ctx, rec, m)
 | |
| 		if err != nil {
 | |
| 			t.Errorf("Expected no error, got %v\n", err)
 | |
| 			return
 | |
| 		}
 | |
| 
 | |
| 		resp := rec.Msg
 | |
| 		sort.Sort(test.RRSet(resp.Answer))
 | |
| 		sort.Sort(test.RRSet(resp.Ns))
 | |
| 		sort.Sort(test.RRSet(resp.Extra))
 | |
| 
 | |
| 		if !test.Header(t, tc, resp) {
 | |
| 			t.Logf("%v\n", resp)
 | |
| 			continue
 | |
| 		}
 | |
| 
 | |
| 		if !test.Section(t, tc, test.Answer, resp.Answer) {
 | |
| 			t.Logf("%v\n", resp)
 | |
| 		}
 | |
| 		if !test.Section(t, tc, test.Ns, resp.Ns) {
 | |
| 			t.Logf("%v\n", resp)
 | |
| 
 | |
| 		}
 | |
| 		if !test.Section(t, tc, test.Extra, resp.Extra) {
 | |
| 			t.Logf("%v\n", resp)
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| var hostsTestCases = []test.Case{
 | |
| 	{
 | |
| 		Qname: "example.org.", Qtype: dns.TypeA,
 | |
| 		Answer: []dns.RR{
 | |
| 			test.A("example.org. 3600	IN	A 10.0.0.1"),
 | |
| 		},
 | |
| 	},
 | |
| 	{
 | |
| 		Qname: "localhost.", Qtype: dns.TypeAAAA,
 | |
| 		Answer: []dns.RR{
 | |
| 			test.AAAA("localhost. 3600	IN	AAAA ::1"),
 | |
| 		},
 | |
| 	},
 | |
| 	{
 | |
| 		Qname: "1.0.0.10.in-addr.arpa.", Qtype: dns.TypePTR,
 | |
| 		Answer: []dns.RR{
 | |
| 			test.PTR("1.0.0.10.in-addr.arpa. 3600 PTR example.org."),
 | |
| 		},
 | |
| 	},
 | |
| 	{
 | |
| 		Qname: "1.0.0.127.in-addr.arpa.", Qtype: dns.TypePTR,
 | |
| 		Answer: []dns.RR{
 | |
| 			test.PTR("1.0.0.127.in-addr.arpa. 3600 PTR localhost."),
 | |
| 			test.PTR("1.0.0.127.in-addr.arpa. 3600 PTR localhost.domain."),
 | |
| 		},
 | |
| 	},
 | |
| }
 | |
| 
 | |
| const hostsExample = `
 | |
| 127.0.0.1 localhost localhost.domain
 | |
| ::1 localhost localhost.domain
 | |
| 10.0.0.1 example.org`
 |