diff options
author | 2023-03-24 12:55:51 +0000 | |
---|---|---|
committer | 2023-03-24 08:55:51 -0400 | |
commit | f823825f8a34edb85d5d18cd5d2f6f850adf408e (patch) | |
tree | 79d241ab9b4c7c343d806f4041c8efccbe3f9ca0 /plugin/pkg/proxy/health_test.go | |
parent | 47dceabfc6465ba6c5d41472d6602d4ad5c9fb1b (diff) | |
download | coredns-f823825f8a34edb85d5d18cd5d2f6f850adf408e.tar.gz coredns-f823825f8a34edb85d5d18cd5d2f6f850adf408e.tar.zst coredns-f823825f8a34edb85d5d18cd5d2f6f850adf408e.zip |
plugin/forward: Allow Proxy to be used outside of forward plugin. (#5951)
* plugin/forward: Move Proxy into pkg/plugin/proxy, to allow forward.Proxy to be used outside of forward plugin.
Signed-off-by: Patrick Downey <patrick.downey@dioadconsulting.com>
Diffstat (limited to 'plugin/pkg/proxy/health_test.go')
-rw-r--r-- | plugin/pkg/proxy/health_test.go | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/plugin/pkg/proxy/health_test.go b/plugin/pkg/proxy/health_test.go new file mode 100644 index 000000000..c1b5270ad --- /dev/null +++ b/plugin/pkg/proxy/health_test.go @@ -0,0 +1,153 @@ +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() + + hc := NewHealthChecker(transport.DNS, true, "") + hc.SetReadTimeout(10 * time.Millisecond) + hc.SetWriteTimeout(10 * time.Millisecond) + + p := NewProxy(s.Addr, transport.DNS) + 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() + + hc := NewHealthChecker(transport.DNS, true, "") + hc.SetTCPTransport() + hc.SetReadTimeout(10 * time.Millisecond) + hc.SetWriteTimeout(10 * time.Millisecond) + + p := NewProxy(s.Addr, transport.DNS) + 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() + + hc := NewHealthChecker(transport.DNS, false, "") + hc.SetReadTimeout(10 * time.Millisecond) + hc.SetWriteTimeout(10 * time.Millisecond) + + p := NewProxy(s.Addr, transport.DNS) + 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() + + hc := NewHealthChecker(transport.DNS, false, "") + hc.SetReadTimeout(10 * time.Millisecond) + hc.SetWriteTimeout(10 * time.Millisecond) + + p := NewProxy(s.Addr, transport.DNS) + 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() + + hc := NewHealthChecker(transport.DNS, true, hcDomain) + hc.SetReadTimeout(10 * time.Millisecond) + hc.SetWriteTimeout(10 * time.Millisecond) + + p := NewProxy(s.Addr, transport.DNS) + 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) + } +} |