Add some logging
This commit is contained in:
parent
6a3894e670
commit
d7869d92ca
4
go.mod
4
go.mod
|
@ -7,6 +7,6 @@ require (
|
|||
github.com/coredns/coredns v1.7.1
|
||||
github.com/gomodule/redigo v1.8.2
|
||||
github.com/miekg/dns v1.1.31
|
||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381
|
||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
|
||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
|
||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
|
||||
)
|
||||
|
|
|
@ -2,6 +2,7 @@ package plugin
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
||||
"github.com/coredns/coredns/plugin/test"
|
||||
"github.com/miekg/dns"
|
||||
|
@ -9,8 +10,11 @@ import (
|
|||
"github.com/rverst/coredns-redis/record"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
//todo: mock redis for testing
|
||||
|
||||
const (
|
||||
prefix, suffix = "lookup-test_", "_lookup-test"
|
||||
defaultTtl = 500
|
||||
|
@ -215,4 +219,79 @@ func TestPlugin_Lookup(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestPlugin_Lookup2(t *testing.T) {
|
||||
|
||||
plug, err := newRedisPlugin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i:=0;i<5;i++ {
|
||||
fmt.Println("shutdown redis backend for log testing")
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
|
||||
for _, z := range zones {
|
||||
zone := record.NewZone(z, record.SOA{
|
||||
Ttl: testTtl,
|
||||
MName: "ns1." + z + ".",
|
||||
RName: "hostmaster",
|
||||
Serial: 2006010201,
|
||||
Refresh: 3600,
|
||||
Retry: 1800,
|
||||
Expire: 10000,
|
||||
MinTtl: 300,
|
||||
})
|
||||
|
||||
for _, tr := range testRecords {
|
||||
zone.Add(tr.l, tr.r)
|
||||
}
|
||||
|
||||
err := plug.Redis.SaveZone(*zone)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
tc test.Case
|
||||
}{
|
||||
{name: "example.net. IN SOA 1", tc: test.Case{Qname: "example.net.", Qtype: dns.TypeSOA,
|
||||
Answer: []dns.RR{test.SOA("example.net. 4242 IN SOA ns1.example.net. hostmaster.example.net 2006010201 3600 1800 10000 300")},
|
||||
}},
|
||||
{name: "example.net. IN SOA 2", tc: test.Case{Qname: "example.net.", Qtype: dns.TypeSOA,
|
||||
Answer: []dns.RR{test.SOA("example.net. 4242 IN SOA ns1.example.net. hostmaster.example.net 2006010201 3600 1800 10000 300")},
|
||||
}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
m := tt.tc.Msg()
|
||||
recorder := dnstest.NewRecorder(&test.ResponseWriter{})
|
||||
_, _ = plug.ServeDNS(ctxt, recorder, m)
|
||||
|
||||
res := recorder.Msg
|
||||
// todo: FIX, should not happen
|
||||
if res == nil {
|
||||
res = new(dns.Msg)
|
||||
}
|
||||
|
||||
err := test.SortAndCheck(res, tt.tc)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
for i:=0;i<5;i++ {
|
||||
fmt.Println("shutdown redis backend for log testing")
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
var ctxt context.Context
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/request"
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
"github.com/miekg/dns"
|
||||
redis "github.com/rverst/coredns-redis"
|
||||
"github.com/rverst/coredns-redis/record"
|
||||
|
@ -12,6 +13,8 @@ import (
|
|||
|
||||
const name = "redis"
|
||||
|
||||
var log = clog.NewWithPlugin("redis")
|
||||
|
||||
type Plugin struct {
|
||||
Redis *redis.Redis
|
||||
Next plugin.Handler
|
||||
|
@ -30,8 +33,12 @@ func (p *Plugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
|
|||
return plugin.NextOrFailure(qName, p.Next, ctx, w, r)
|
||||
}
|
||||
|
||||
zones, err := p.Redis.LoadZones(qName)
|
||||
zones, err, connOk := p.Redis.LoadZones(qName)
|
||||
if err != nil {
|
||||
if !connOk {
|
||||
log.Error(err)
|
||||
return dns.RcodeServerFailure, err
|
||||
}
|
||||
return plugin.NextOrFailure(qName, p.Next, ctx, w, r)
|
||||
}
|
||||
zoneName := plugin.Zones(zones).Matches(qName)
|
||||
|
@ -85,7 +92,6 @@ func (p *Plugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
|
|||
m.Authoritative, m.RecursionAvailable, m.Compress = true, false, true
|
||||
m.Answer = append(m.Answer, answers...)
|
||||
m.Extra = append(m.Extra, extras...)
|
||||
|
||||
state.SizeAndDo(m)
|
||||
m = state.Scrub(m)
|
||||
_ = w.WriteMsg(m)
|
||||
|
|
12
redis.go
12
redis.go
|
@ -298,7 +298,7 @@ func (redis *Redis) getExtras(name string, z *record.Zone, zones []string) []dns
|
|||
if location == "" {
|
||||
zoneName := plugin.Zones(zones).Matches(name)
|
||||
if zoneName == "" {
|
||||
zones, err := redis.LoadZones(name)
|
||||
zones, err, _ := redis.LoadZones(name)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -545,7 +545,7 @@ func (redis *Redis) LoadZoneRecords(key string, z *record.Zone) *record.Records
|
|||
return r
|
||||
}
|
||||
|
||||
func (redis *Redis) LoadZones(name string) ([]string, error) {
|
||||
func (redis *Redis) LoadZones(name string) ([]string, error, bool) {
|
||||
var (
|
||||
reply interface{}
|
||||
err error
|
||||
|
@ -561,16 +561,20 @@ func (redis *Redis) LoadZones(name string) ([]string, error) {
|
|||
defer conn.Close()
|
||||
|
||||
reply, err = conn.Do("KEYS", redis.keyPrefix+"*"+query+redis.keySuffix)
|
||||
if err != nil {
|
||||
return nil, err, false
|
||||
}
|
||||
|
||||
zones, err = redisCon.Strings(reply, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, err, true
|
||||
}
|
||||
|
||||
for i, _ := range zones {
|
||||
zones[i] = strings.TrimPrefix(zones[i], redis.keyPrefix)
|
||||
zones[i] = strings.TrimSuffix(zones[i], redis.keySuffix)
|
||||
}
|
||||
return zones, nil
|
||||
return zones, nil, true
|
||||
}
|
||||
|
||||
// Key returns the given key with prefix and suffix
|
||||
|
|
|
@ -2,7 +2,6 @@ package redis
|
|||
|
||||
import (
|
||||
"github.com/rverst/coredns-redis/record"
|
||||
"log"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
@ -47,8 +46,7 @@ var testRecords = []testRecord{
|
|||
{"*", record.TXT{Ttl: testTtl, Text: wcTxt}},
|
||||
}
|
||||
|
||||
|
||||
func newRedis() *Redis {
|
||||
func newRedis() (*Redis, error) {
|
||||
|
||||
r := New()
|
||||
r.SetKeyPrefix(prefix)
|
||||
|
@ -56,14 +54,17 @@ func newRedis() *Redis {
|
|||
r.SetDefaultTtl(minTtl)
|
||||
r.SetAddress("192.168.0.100:6379")
|
||||
if err := r.Connect(); err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
return r
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func TestRedis_SaveZone(t *testing.T) {
|
||||
|
||||
redis := newRedis()
|
||||
redis, err := newRedis()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
for _, z := range zones {
|
||||
zone := record.NewZone(z, record.SOA{
|
||||
|
@ -89,3 +90,5 @@ func TestRedis_SaveZone(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue