mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 10:43:20 -05:00 
			
		
		
		
	Default to using pkg/log for all logging and use a fixed time prefix which is RFC3339Millli (doesn't exist in time, so we just extended RFC3339), i.e. Nano might be pushing it. Logs go from: 2018/10/30 19:14:55 [INFO] CoreDNS-1.2.5 2018/10/30 19:14:55 [INFO] linux/amd64, go1.11, to: 2018-10-30T19:10:07.547Z [INFO] CoreDNS-1.2.5 2018-10-30T19:10:07.547Z [INFO] linux/amd64, go1.11, Which includes the timezone - which oddly the std log package doesn't natively do. Signed-off-by: Miek Gieben <miek@miek.nl>
		
			
				
	
	
		
			109 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			109 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package log
 | 
						|
 | 
						|
import (
 | 
						|
	"bytes"
 | 
						|
	"context"
 | 
						|
	"log"
 | 
						|
	"strings"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
						|
	clog "github.com/coredns/coredns/plugin/pkg/log"
 | 
						|
	"github.com/coredns/coredns/plugin/pkg/response"
 | 
						|
	"github.com/coredns/coredns/plugin/test"
 | 
						|
 | 
						|
	"github.com/miekg/dns"
 | 
						|
)
 | 
						|
 | 
						|
func init() { clog.Discard() }
 | 
						|
 | 
						|
func TestLoggedStatus(t *testing.T) {
 | 
						|
	rule := Rule{
 | 
						|
		NameScope: ".",
 | 
						|
		Format:    DefaultLogFormat,
 | 
						|
		Class:     map[response.Class]bool{response.All: true},
 | 
						|
	}
 | 
						|
 | 
						|
	var f bytes.Buffer
 | 
						|
	log.SetOutput(&f)
 | 
						|
 | 
						|
	logger := Logger{
 | 
						|
		Rules: []Rule{rule},
 | 
						|
		Next:  test.ErrorHandler(),
 | 
						|
	}
 | 
						|
 | 
						|
	ctx := context.TODO()
 | 
						|
	r := new(dns.Msg)
 | 
						|
	r.SetQuestion("example.org.", dns.TypeA)
 | 
						|
 | 
						|
	rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
						|
 | 
						|
	rcode, _ := logger.ServeDNS(ctx, rec, r)
 | 
						|
	if rcode != 0 {
 | 
						|
		t.Errorf("Expected rcode to be 0 - was: %d", rcode)
 | 
						|
	}
 | 
						|
 | 
						|
	logged := f.String()
 | 
						|
	if !strings.Contains(logged, "A IN example.org. udp 29 false 512") {
 | 
						|
		t.Errorf("Expected it to be logged. Logged string: %s", logged)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestLoggedClassDenial(t *testing.T) {
 | 
						|
	rule := Rule{
 | 
						|
		NameScope: ".",
 | 
						|
		Format:    DefaultLogFormat,
 | 
						|
		Class:     map[response.Class]bool{response.Denial: true},
 | 
						|
	}
 | 
						|
 | 
						|
	var f bytes.Buffer
 | 
						|
	log.SetOutput(&f)
 | 
						|
 | 
						|
	logger := Logger{
 | 
						|
		Rules: []Rule{rule},
 | 
						|
		Next:  test.ErrorHandler(),
 | 
						|
	}
 | 
						|
 | 
						|
	ctx := context.TODO()
 | 
						|
	r := new(dns.Msg)
 | 
						|
	r.SetQuestion("example.org.", dns.TypeA)
 | 
						|
 | 
						|
	rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
						|
 | 
						|
	logger.ServeDNS(ctx, rec, r)
 | 
						|
 | 
						|
	logged := f.String()
 | 
						|
	if len(logged) != 0 {
 | 
						|
		t.Errorf("Expected it not to be logged, but got string: %s", logged)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestLoggedClassError(t *testing.T) {
 | 
						|
	rule := Rule{
 | 
						|
		NameScope: ".",
 | 
						|
		Format:    DefaultLogFormat,
 | 
						|
		Class:     map[response.Class]bool{response.Error: true},
 | 
						|
	}
 | 
						|
 | 
						|
	var f bytes.Buffer
 | 
						|
	log.SetOutput(&f)
 | 
						|
 | 
						|
	logger := Logger{
 | 
						|
		Rules: []Rule{rule},
 | 
						|
		Next:  test.ErrorHandler(),
 | 
						|
	}
 | 
						|
 | 
						|
	ctx := context.TODO()
 | 
						|
	r := new(dns.Msg)
 | 
						|
	r.SetQuestion("example.org.", dns.TypeA)
 | 
						|
 | 
						|
	rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
						|
 | 
						|
	logger.ServeDNS(ctx, rec, r)
 | 
						|
 | 
						|
	logged := f.String()
 | 
						|
	if !strings.Contains(logged, "SERVFAIL") {
 | 
						|
		t.Errorf("Expected it to be logged. Logged string: %s", logged)
 | 
						|
	}
 | 
						|
}
 |