| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | package forward
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	"sync/atomic"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/transport"
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealth(t *testing.T) {
 | 
					
						
							|  |  |  | 	const expected = 0
 | 
					
						
							|  |  |  | 	i := uint32(0)
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		if r.Question[0].Name == "." {
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&i, 1)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	p := NewProxy(s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	f := New()
 | 
					
						
							|  |  |  | 	f.SetProxy(p)
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 	defer f.OnShutdown()
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	req := new(dns.Msg)
 | 
					
						
							|  |  |  | 	req.SetQuestion("example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(1 * time.Second)
 | 
					
						
							|  |  |  | 	i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 	if i1 != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected number of health checks to be %d, got %d", expected, i1)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealthTimeout(t *testing.T) {
 | 
					
						
							|  |  |  | 	const expected = 1
 | 
					
						
							|  |  |  | 	i := uint32(0)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	q := uint32(0)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		if r.Question[0].Name == "." {
 | 
					
						
							|  |  |  | 			// health check, answer
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&i, 1)
 | 
					
						
							|  |  |  | 			ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 			ret.SetReply(r)
 | 
					
						
							|  |  |  | 			w.WriteMsg(ret)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 			return
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 		if atomic.LoadUint32(&q) == 0 { //drop only first query
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&q, 1)
 | 
					
						
							|  |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	p := NewProxy(s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	f := New()
 | 
					
						
							|  |  |  | 	f.SetProxy(p)
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 	defer f.OnShutdown()
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	req := new(dns.Msg)
 | 
					
						
							|  |  |  | 	req.SetQuestion("example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(1 * time.Second)
 | 
					
						
							|  |  |  | 	i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 	if i1 != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected number of health checks to be %d, got %d", expected, i1)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealthFailTwice(t *testing.T) {
 | 
					
						
							|  |  |  | 	const expected = 2
 | 
					
						
							|  |  |  | 	i := uint32(0)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	q := uint32(0)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		if r.Question[0].Name == "." {
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&i, 1)
 | 
					
						
							|  |  |  | 			i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 			// Timeout health until we get the second one
 | 
					
						
							|  |  |  | 			if i1 < 2 {
 | 
					
						
							|  |  |  | 				return
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 			ret.SetReply(r)
 | 
					
						
							|  |  |  | 			w.WriteMsg(ret)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if atomic.LoadUint32(&q) == 0 { //drop only first query
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&q, 1)
 | 
					
						
							|  |  |  | 			return
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	p := NewProxy(s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	f := New()
 | 
					
						
							|  |  |  | 	f.SetProxy(p)
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 	defer f.OnShutdown()
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	req := new(dns.Msg)
 | 
					
						
							|  |  |  | 	req.SetQuestion("example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(3 * time.Second)
 | 
					
						
							|  |  |  | 	i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 	if i1 != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected number of health checks to be %d, got %d", expected, i1)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealthMaxFails(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		// timeout
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	p := NewProxy(s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	f := New()
 | 
					
						
							|  |  |  | 	f.maxfails = 2
 | 
					
						
							|  |  |  | 	f.SetProxy(p)
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 	defer f.OnShutdown()
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	req := new(dns.Msg)
 | 
					
						
							|  |  |  | 	req.SetQuestion("example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 08:48:56 +01:00
										 |  |  | 	time.Sleep(readTimeout + 1*time.Second)
 | 
					
						
							|  |  |  | 	fails := atomic.LoadUint32(&p.fails)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	if !p.Down(f.maxfails) {
 | 
					
						
							| 
									
										
										
										
											2018-11-20 08:48:56 +01:00
										 |  |  | 		t.Errorf("Expected Proxy fails to be greater than %d, got %d", f.maxfails, fails)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealthNoMaxFails(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	const expected = 0
 | 
					
						
							|  |  |  | 	i := uint32(0)
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 		if r.Question[0].Name == "." {
 | 
					
						
							|  |  |  | 			// health check, answer
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&i, 1)
 | 
					
						
							|  |  |  | 			ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 			ret.SetReply(r)
 | 
					
						
							|  |  |  | 			w.WriteMsg(ret)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	p := NewProxy(s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	f := New()
 | 
					
						
							|  |  |  | 	f.maxfails = 0
 | 
					
						
							|  |  |  | 	f.SetProxy(p)
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 	defer f.OnShutdown()
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	req := new(dns.Msg)
 | 
					
						
							|  |  |  | 	req.SetQuestion("example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(1 * time.Second)
 | 
					
						
							|  |  |  | 	i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 	if i1 != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected number of health checks to be %d, got %d", expected, i1)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |