| 
									
										
										
										
											2016-03-24 17:31:01 +00:00
										 |  |  | // +build etcd
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | package etcd
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"encoding/json"
 | 
					
						
							|  |  |  | 	"sort"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/etcd/msg"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/pkg/dnsrecorder"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/pkg/singleflight"
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware/proxy"
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/test"
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	etcdc "github.com/coreos/etcd/client"
 | 
					
						
							| 
									
										
										
										
											2016-08-08 19:54:17 -07:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							| 
									
										
										
										
											2016-06-11 08:00:47 +10:00
										 |  |  | 	ctxt, _ = context.WithTimeout(context.Background(), etcdTimeout)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //	etc    *Etcd
 | 
					
						
							|  |  |  | func newEtcdMiddleware() *Etcd {
 | 
					
						
							|  |  |  | 	ctxt, _ = context.WithTimeout(context.Background(), etcdTimeout)
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	etcdCfg := etcdc.Config{
 | 
					
						
							|  |  |  | 		Endpoints: []string{"http://localhost:2379"},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	cli, _ := etcdc.New(etcdCfg)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return &Etcd{
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 		Proxy:      proxy.New([]string{"8.8.8.8:53"}),
 | 
					
						
							|  |  |  | 		PathPrefix: "skydns",
 | 
					
						
							|  |  |  | 		Ctx:        context.Background(),
 | 
					
						
							|  |  |  | 		Inflight:   &singleflight.Group{},
 | 
					
						
							| 
									
										
										
										
											2016-06-08 10:29:46 +01:00
										 |  |  | 		Zones:      []string{"skydns.test.", "skydns_extra.test.", "in-addr.arpa."},
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 		Client:     etcdc.NewKeysAPI(cli),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-08 19:18:55 -07:00
										 |  |  | func set(t *testing.T, e *Etcd, k string, ttl time.Duration, m *msg.Service) {
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 	b, err := json.Marshal(m)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-05-23 09:16:57 +01:00
										 |  |  | 	path, _ := msg.PathWithWildcard(k, e.PathPrefix)
 | 
					
						
							| 
									
										
										
										
											2016-06-11 08:00:47 +10:00
										 |  |  | 	e.Client.Set(ctxt, path, string(b), &etcdc.SetOptions{TTL: ttl})
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-08 19:18:55 -07:00
										 |  |  | func delete(t *testing.T, e *Etcd, k string) {
 | 
					
						
							| 
									
										
										
										
											2016-05-23 09:16:57 +01:00
										 |  |  | 	path, _ := msg.PathWithWildcard(k, e.PathPrefix)
 | 
					
						
							| 
									
										
										
										
											2016-06-11 08:00:47 +10:00
										 |  |  | 	e.Client.Delete(ctxt, path, &etcdc.DeleteOptions{Recursive: false})
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestLookup(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	etc := newEtcdMiddleware()
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 	for _, serv := range services {
 | 
					
						
							|  |  |  | 		set(t, etc, serv.Key, 0, serv)
 | 
					
						
							|  |  |  | 		defer delete(t, etc, serv.Key)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 	for _, tc := range dnsTestCases {
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		m := tc.Msg()
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		rec := dnsrecorder.New(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2016-06-11 08:00:47 +10:00
										 |  |  | 		_, err := etc.ServeDNS(ctxt, rec, m)
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-13 08:03:56 +01:00
										 |  |  | 			t.Errorf("expected no error, got: %v for %s %s\n", err, m.Question[0].Name, dns.Type(m.Question[0].Qtype))
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		resp := rec.Msg
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		sort.Sort(test.RRSet(resp.Answer))
 | 
					
						
							|  |  |  | 		sort.Sort(test.RRSet(resp.Ns))
 | 
					
						
							|  |  |  | 		sort.Sort(test.RRSet(resp.Extra))
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if !test.Header(t, tc, resp) {
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 10:22:27 +01:00
										 |  |  | 		if !test.Section(t, tc, test.Answer, resp.Answer) {
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 10:22:27 +01:00
										 |  |  | 		if !test.Section(t, tc, test.Ns, resp.Ns) {
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 10:22:27 +01:00
										 |  |  | 		if !test.Section(t, tc, test.Extra, resp.Extra) {
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | var ctxt context.Context
 |