| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Jan Schär | 75ea9f4 | 2024-07-29 17:01:41 +0200 | [diff] [blame] | 4 | package proxy |
| 5 | |
| 6 | // Taken and modified from CoreDNS, under Apache 2.0. |
| 7 | |
| 8 | import ( |
| 9 | "sync/atomic" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/miekg/dns" |
| 14 | |
| 15 | "source.monogon.dev/osbase/net/dns/test" |
| 16 | ) |
| 17 | |
| 18 | func TestHealth(t *testing.T) { |
| 19 | i := uint32(0) |
| 20 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 21 | if r.Question[0].Name == "." && r.RecursionDesired == true { |
| 22 | atomic.AddUint32(&i, 1) |
| 23 | } |
| 24 | ret := new(dns.Msg) |
| 25 | ret.SetReply(r) |
| 26 | w.WriteMsg(ret) |
| 27 | }) |
| 28 | defer s.Close() |
| 29 | |
| 30 | hc := NewHealthChecker(true, ".") |
| 31 | hc.SetReadTimeout(10 * time.Millisecond) |
| 32 | hc.SetWriteTimeout(10 * time.Millisecond) |
| 33 | |
| 34 | p := NewProxy(s.Addr) |
| 35 | p.readTimeout = 10 * time.Millisecond |
| 36 | err := hc.Check(p) |
| 37 | if err != nil { |
| 38 | t.Errorf("check failed: %v", err) |
| 39 | } |
| 40 | |
| 41 | time.Sleep(20 * time.Millisecond) |
| 42 | i1 := atomic.LoadUint32(&i) |
| 43 | if i1 != 1 { |
| 44 | t.Errorf("Expected number of health checks with RecursionDesired==true to be %d, got %d", 1, i1) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestHealthTCP(t *testing.T) { |
| 49 | i := uint32(0) |
| 50 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 51 | if r.Question[0].Name == "." && r.RecursionDesired == true { |
| 52 | atomic.AddUint32(&i, 1) |
| 53 | } |
| 54 | ret := new(dns.Msg) |
| 55 | ret.SetReply(r) |
| 56 | w.WriteMsg(ret) |
| 57 | }) |
| 58 | defer s.Close() |
| 59 | |
| 60 | hc := NewHealthChecker(true, ".") |
| 61 | hc.SetTCPTransport() |
| 62 | hc.SetReadTimeout(10 * time.Millisecond) |
| 63 | hc.SetWriteTimeout(10 * time.Millisecond) |
| 64 | |
| 65 | p := NewProxy(s.Addr) |
| 66 | p.readTimeout = 10 * time.Millisecond |
| 67 | err := hc.Check(p) |
| 68 | if err != nil { |
| 69 | t.Errorf("check failed: %v", err) |
| 70 | } |
| 71 | |
| 72 | time.Sleep(20 * time.Millisecond) |
| 73 | i1 := atomic.LoadUint32(&i) |
| 74 | if i1 != 1 { |
| 75 | t.Errorf("Expected number of health checks with RecursionDesired==true to be %d, got %d", 1, i1) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestHealthNoRecursion(t *testing.T) { |
| 80 | i := uint32(0) |
| 81 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 82 | if r.Question[0].Name == "." && r.RecursionDesired == false { |
| 83 | atomic.AddUint32(&i, 1) |
| 84 | } |
| 85 | ret := new(dns.Msg) |
| 86 | ret.SetReply(r) |
| 87 | w.WriteMsg(ret) |
| 88 | }) |
| 89 | defer s.Close() |
| 90 | |
| 91 | hc := NewHealthChecker(false, ".") |
| 92 | hc.SetReadTimeout(10 * time.Millisecond) |
| 93 | hc.SetWriteTimeout(10 * time.Millisecond) |
| 94 | |
| 95 | p := NewProxy(s.Addr) |
| 96 | p.readTimeout = 10 * time.Millisecond |
| 97 | err := hc.Check(p) |
| 98 | if err != nil { |
| 99 | t.Errorf("check failed: %v", err) |
| 100 | } |
| 101 | |
| 102 | time.Sleep(20 * time.Millisecond) |
| 103 | i1 := atomic.LoadUint32(&i) |
| 104 | if i1 != 1 { |
| 105 | t.Errorf("Expected number of health checks with RecursionDesired==false to be %d, got %d", 1, i1) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestHealthTimeout(t *testing.T) { |
| 110 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 111 | // timeout |
| 112 | }) |
| 113 | defer s.Close() |
| 114 | |
| 115 | hc := NewHealthChecker(false, ".") |
| 116 | hc.SetReadTimeout(10 * time.Millisecond) |
| 117 | hc.SetWriteTimeout(10 * time.Millisecond) |
| 118 | |
| 119 | p := NewProxy(s.Addr) |
| 120 | p.readTimeout = 10 * time.Millisecond |
| 121 | err := hc.Check(p) |
| 122 | if err == nil { |
| 123 | t.Errorf("expected error") |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestHealthDomain(t *testing.T) { |
| 128 | hcDomain := "example.org." |
| 129 | |
| 130 | i := uint32(0) |
| 131 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 132 | if r.Question[0].Name == hcDomain && r.RecursionDesired == true { |
| 133 | atomic.AddUint32(&i, 1) |
| 134 | } |
| 135 | ret := new(dns.Msg) |
| 136 | ret.SetReply(r) |
| 137 | w.WriteMsg(ret) |
| 138 | }) |
| 139 | defer s.Close() |
| 140 | |
| 141 | hc := NewHealthChecker(true, hcDomain) |
| 142 | hc.SetReadTimeout(10 * time.Millisecond) |
| 143 | hc.SetWriteTimeout(10 * time.Millisecond) |
| 144 | |
| 145 | p := NewProxy(s.Addr) |
| 146 | p.readTimeout = 10 * time.Millisecond |
| 147 | err := hc.Check(p) |
| 148 | if err != nil { |
| 149 | t.Errorf("check failed: %v", err) |
| 150 | } |
| 151 | |
| 152 | time.Sleep(12 * time.Millisecond) |
| 153 | i1 := atomic.LoadUint32(&i) |
| 154 | if i1 != 1 { |
| 155 | t.Errorf("Expected number of health checks with Domain==%s to be %d, got %d", hcDomain, 1, i1) |
| 156 | } |
| 157 | } |