| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | package proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"sync/atomic"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/transport"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealth(t *testing.T) {
 | 
					
						
							|  |  |  | 	i := uint32(0)
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		if r.Question[0].Name == "." && r.RecursionDesired == true {
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&i, 1)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 06:30:32 +10:30
										 |  |  | 	hc := NewHealthChecker("TestHealth", transport.DNS, true, ".")
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	hc.SetReadTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 	hc.SetWriteTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 	p := NewProxy("TestHealth", s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	p.readTimeout = 10 * time.Millisecond
 | 
					
						
							|  |  |  | 	err := hc.Check(p)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Errorf("check failed: %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(20 * time.Millisecond)
 | 
					
						
							|  |  |  | 	i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 	if i1 != 1 {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected number of health checks with RecursionDesired==true to be %d, got %d", 1, i1)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealthTCP(t *testing.T) {
 | 
					
						
							|  |  |  | 	i := uint32(0)
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		if r.Question[0].Name == "." && r.RecursionDesired == true {
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&i, 1)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 06:30:32 +10:30
										 |  |  | 	hc := NewHealthChecker("TestHealthTCP", transport.DNS, true, ".")
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	hc.SetTCPTransport()
 | 
					
						
							|  |  |  | 	hc.SetReadTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 	hc.SetWriteTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 	p := NewProxy("TestHealthTCP", s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	p.readTimeout = 10 * time.Millisecond
 | 
					
						
							|  |  |  | 	err := hc.Check(p)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Errorf("check failed: %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(20 * time.Millisecond)
 | 
					
						
							|  |  |  | 	i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 	if i1 != 1 {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected number of health checks with RecursionDesired==true to be %d, got %d", 1, i1)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealthNoRecursion(t *testing.T) {
 | 
					
						
							|  |  |  | 	i := uint32(0)
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		if r.Question[0].Name == "." && r.RecursionDesired == false {
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&i, 1)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 06:30:32 +10:30
										 |  |  | 	hc := NewHealthChecker("TestHealthNoRecursion", transport.DNS, false, ".")
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	hc.SetReadTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 	hc.SetWriteTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 	p := NewProxy("TestHealthNoRecursion", s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	p.readTimeout = 10 * time.Millisecond
 | 
					
						
							|  |  |  | 	err := hc.Check(p)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Errorf("check failed: %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(20 * time.Millisecond)
 | 
					
						
							|  |  |  | 	i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 	if i1 != 1 {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected number of health checks with RecursionDesired==false to be %d, got %d", 1, i1)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealthTimeout(t *testing.T) {
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		// timeout
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 06:30:32 +10:30
										 |  |  | 	hc := NewHealthChecker("TestHealthTimeout", transport.DNS, false, ".")
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	hc.SetReadTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 	hc.SetWriteTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 	p := NewProxy("TestHealthTimeout", s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	p.readTimeout = 10 * time.Millisecond
 | 
					
						
							|  |  |  | 	err := hc.Check(p)
 | 
					
						
							|  |  |  | 	if err == nil {
 | 
					
						
							|  |  |  | 		t.Errorf("expected error")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHealthDomain(t *testing.T) {
 | 
					
						
							|  |  |  | 	hcDomain := "example.org."
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	i := uint32(0)
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		if r.Question[0].Name == hcDomain && r.RecursionDesired == true {
 | 
					
						
							|  |  |  | 			atomic.AddUint32(&i, 1)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 	hc := NewHealthChecker("TestHealthDomain", transport.DNS, true, hcDomain)
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	hc.SetReadTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 	hc.SetWriteTimeout(10 * time.Millisecond)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 	p := NewProxy("TestHealthDomain", s.Addr, transport.DNS)
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	p.readTimeout = 10 * time.Millisecond
 | 
					
						
							|  |  |  | 	err := hc.Check(p)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Errorf("check failed: %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(12 * time.Millisecond)
 | 
					
						
							|  |  |  | 	i1 := atomic.LoadUint32(&i)
 | 
					
						
							|  |  |  | 	if i1 != 1 {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected number of health checks with Domain==%s to be %d, got %d", hcDomain, 1, i1)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |