mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 18:53:13 -05:00 
			
		
		
		
	The host plugin kept on adding entries instead of overwriting. Split the inline cache off from the /etc/hosts file cache and clear /etc/hosts file cache and re-parsing. A bunch of other cleanup as well. Use functions defined in the plugin package, don't re-parse strings if you don't have to and use To4() to check the family for IP addresses. Fix all test cases a carried entries are always fqdn-ed. Various smaller cleanup in unnessacry constants. Fixes: #3014 Signed-off-by: Miek Gieben <miek@miek.nl>
		
			
				
	
	
		
			101 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package hosts
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"strings"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
						|
	"github.com/coredns/coredns/plugin/test"
 | 
						|
 | 
						|
	"github.com/miekg/dns"
 | 
						|
)
 | 
						|
 | 
						|
func TestLookupA(t *testing.T) {
 | 
						|
	h := Hosts{
 | 
						|
		Next: test.ErrorHandler(),
 | 
						|
		Hostsfile: &Hostsfile{
 | 
						|
			Origins: []string{"."},
 | 
						|
			hmap:    newMap(),
 | 
						|
			inline:  newMap(),
 | 
						|
			options: newOptions(),
 | 
						|
		},
 | 
						|
	}
 | 
						|
	h.hmap = h.parse(strings.NewReader(hostsExample))
 | 
						|
 | 
						|
	ctx := context.TODO()
 | 
						|
 | 
						|
	for _, tc := range hostsTestCases {
 | 
						|
		m := tc.Msg()
 | 
						|
 | 
						|
		rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
						|
		_, err := h.ServeDNS(ctx, rec, m)
 | 
						|
		if err != nil {
 | 
						|
			t.Errorf("Expected no error, got %v", err)
 | 
						|
			return
 | 
						|
		}
 | 
						|
 | 
						|
		resp := rec.Msg
 | 
						|
		if err := test.SortAndCheck(resp, tc); err != nil {
 | 
						|
			t.Error(err)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
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: "example.com.", Qtype: dns.TypeA,
 | 
						|
		Answer: []dns.RR{
 | 
						|
			test.A("example.com. 3600	IN	A 10.0.0.2"),
 | 
						|
		},
 | 
						|
	},
 | 
						|
	{
 | 
						|
		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: "2.0.0.10.in-addr.arpa.", Qtype: dns.TypePTR,
 | 
						|
		Answer: []dns.RR{
 | 
						|
			test.PTR("2.0.0.10.in-addr.arpa. 3600 PTR example.com."),
 | 
						|
		},
 | 
						|
	},
 | 
						|
	{
 | 
						|
		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."),
 | 
						|
		},
 | 
						|
	},
 | 
						|
	{
 | 
						|
		Qname: "example.org.", Qtype: dns.TypeAAAA,
 | 
						|
		Answer: []dns.RR{},
 | 
						|
	},
 | 
						|
	{
 | 
						|
		Qname: "example.org.", Qtype: dns.TypeMX,
 | 
						|
		Answer: []dns.RR{},
 | 
						|
	},
 | 
						|
}
 | 
						|
 | 
						|
const hostsExample = `
 | 
						|
127.0.0.1 localhost localhost.domain
 | 
						|
::1 localhost localhost.domain
 | 
						|
10.0.0.1 example.org
 | 
						|
::FFFF:10.0.0.2 example.com
 | 
						|
reload 5s
 | 
						|
timeout 3600
 | 
						|
`
 |