| 
									
										
										
										
											2016-03-24 17:31:01 +00:00
										 |  |  | // +build etcd
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | package etcd
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"encoding/json"
 | 
					
						
							|  |  |  | 	"sort"
 | 
					
						
							| 
									
										
										
										
											2016-09-26 14:43:38 +01:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 	"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"
 | 
					
						
							| 
									
										
										
										
											2017-01-12 03:14:24 -05:00
										 |  |  | 	"github.com/miekg/coredns/middleware/pkg/tls"
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	etcdc "github.com/coreos/etcd/client"
 | 
					
						
							| 
									
										
										
										
											2016-11-09 21:26:49 +00:00
										 |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newEtcdMiddleware() *Etcd {
 | 
					
						
							|  |  |  | 	ctxt, _ = context.WithTimeout(context.Background(), etcdTimeout)
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | 	endpoints := []string{"http://localhost:2379"}
 | 
					
						
							| 
									
										
										
										
											2017-01-12 03:14:24 -05:00
										 |  |  | 	tlsc, _ := tls.NewTLSConfigFromArgs()
 | 
					
						
							|  |  |  | 	client, _ := newEtcdClient(endpoints, tlsc)
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-10-30 15:54:16 +00:00
										 |  |  | 		Client:     client,
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-12-20 18:58:05 +00:00
										 |  |  | 		etc.ServeDNS(ctxt, rec, m)
 | 
					
						
							| 
									
										
										
										
											2016-03-24 08:22:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 14:43:38 +01:00
										 |  |  | func TestSetupEtcd(t *testing.T) {
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		input              string
 | 
					
						
							|  |  |  | 		shouldErr          bool
 | 
					
						
							|  |  |  | 		expectedPath       string
 | 
					
						
							|  |  |  | 		expectedEndpoint   string
 | 
					
						
							|  |  |  | 		expectedErrContent string // substring from the expected error. Empty for positive cases.
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		// positive
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`etcd`, false, "skydns", "http://localhost:2379", "",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`etcd skydns.local {
 | 
					
						
							|  |  |  | 	endpoint localhost:300
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | `, false, "skydns", "localhost:300", "",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// negative
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`etcd {
 | 
					
						
							|  |  |  | 	endpoints localhost:300
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | `, true, "", "", "unknown property 'endpoints'",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests {
 | 
					
						
							|  |  |  | 		c := caddy.NewTestController("dns", test.input)
 | 
					
						
							|  |  |  | 		etcd, _ /*stubzones*/, err := etcdParse(c)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if test.shouldErr && err == nil {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected error but found %s for input %s", i, err, test.input)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			if !test.shouldErr {
 | 
					
						
							|  |  |  | 				t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err)
 | 
					
						
							|  |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if !strings.Contains(err.Error(), test.expectedErrContent) {
 | 
					
						
							|  |  |  | 				t.Errorf("Test %d: Expected error to contain: %v, found error: %v, input: %s", i, test.expectedErrContent, err, test.input)
 | 
					
						
							|  |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !test.shouldErr && etcd.PathPrefix != test.expectedPath {
 | 
					
						
							|  |  |  | 			t.Errorf("Etcd not correctly set for input %s. Expected: %s, actual: %s", test.input, test.expectedPath, etcd.PathPrefix)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if !test.shouldErr && etcd.endpoints[0] != test.expectedEndpoint { // only checks the first
 | 
					
						
							|  |  |  | 			t.Errorf("Etcd not correctly set for input %s. Expected: '%s', actual: '%s'", test.input, test.expectedEndpoint, etcd.endpoints[0])
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | var ctxt context.Context
 |