|
| 1 | +package egress_dns_proxy_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestHAProxyFrontendBackendConf(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + dest string |
| 14 | + frontends []string |
| 15 | + backends []string |
| 16 | + }{ |
| 17 | + // Single destination IP |
| 18 | + { |
| 19 | + dest: "80 11.12.13.14", |
| 20 | + frontends: []string{` |
| 21 | +frontend fe1 |
| 22 | + bind :80 |
| 23 | + default_backend be1`}, |
| 24 | + backends: []string{` |
| 25 | +backend be1 |
| 26 | + server dest1 11.12.13.14:80 check`}, |
| 27 | + }, |
| 28 | + // Multiple destination IPs |
| 29 | + { |
| 30 | + dest: "80 11.12.13.14\n8080 21.22.23.24 100", |
| 31 | + frontends: []string{` |
| 32 | +frontend fe1 |
| 33 | + bind :80 |
| 34 | + default_backend be1`, ` |
| 35 | +frontend fe2 |
| 36 | + bind :8080 |
| 37 | + default_backend be2`}, |
| 38 | + backends: []string{` |
| 39 | +backend be1 |
| 40 | + server dest1 11.12.13.14:80 check`, ` |
| 41 | +backend be2 |
| 42 | + server dest2 21.22.23.24:100 check`}, |
| 43 | + }, |
| 44 | + // Single destination domain name |
| 45 | + { |
| 46 | + dest: "80 example.com", |
| 47 | + frontends: []string{` |
| 48 | +frontend fe1 |
| 49 | + bind :80 |
| 50 | + default_backend be1`}, |
| 51 | + backends: []string{` |
| 52 | +backend be1 |
| 53 | + server dest1 example.com:80 check resolvers dns-resolver`}, |
| 54 | + }, |
| 55 | + // Multiple destination domain names |
| 56 | + { |
| 57 | + dest: "80 example.com\n8080 foo.com 100", |
| 58 | + frontends: []string{` |
| 59 | +frontend fe1 |
| 60 | + bind :80 |
| 61 | + default_backend be1`, ` |
| 62 | +frontend fe2 |
| 63 | + bind :8080 |
| 64 | + default_backend be2`}, |
| 65 | + backends: []string{` |
| 66 | +backend be1 |
| 67 | + server dest1 example.com:80 check resolvers dns-resolver`, ` |
| 68 | +backend be2 |
| 69 | + server dest2 foo.com:100 check resolvers dns-resolver`}, |
| 70 | + }, |
| 71 | + // Destination IP and destination domain name |
| 72 | + { |
| 73 | + dest: "80 11.12.13.14\n8080 example.com 100", |
| 74 | + frontends: []string{` |
| 75 | +frontend fe1 |
| 76 | + bind :80 |
| 77 | + default_backend be1`, ` |
| 78 | +frontend fe2 |
| 79 | + bind :8080 |
| 80 | + default_backend be2`}, |
| 81 | + backends: []string{` |
| 82 | +backend be1 |
| 83 | + server dest1 11.12.13.14:80 check`, ` |
| 84 | +backend be2 |
| 85 | + server dest2 example.com:100 check resolvers dns-resolver`}, |
| 86 | + }, |
| 87 | + // Destination with comments and blank lines |
| 88 | + { |
| 89 | + dest: ` |
| 90 | +# My DNS proxy egress router rules |
| 91 | +
|
| 92 | +# Port 80 forwards to 11.12.13.14 |
| 93 | +80 11.12.13.14 |
| 94 | +
|
| 95 | +# Port 8080 forwards to port 100 on example.com |
| 96 | +8080 example.com 100 |
| 97 | +
|
| 98 | +# Skip this rule for now |
| 99 | +# 9000 foo.com 200 |
| 100 | +
|
| 101 | +# End |
| 102 | +`, |
| 103 | + frontends: []string{` |
| 104 | +frontend fe1 |
| 105 | + bind :80 |
| 106 | + default_backend be1`, ` |
| 107 | +frontend fe2 |
| 108 | + bind :8080 |
| 109 | + default_backend be2`}, |
| 110 | + backends: []string{` |
| 111 | +backend be1 |
| 112 | + server dest1 11.12.13.14:80 check`, ` |
| 113 | +backend be2 |
| 114 | + server dest2 example.com:100 check resolvers dns-resolver`}, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + frontendRegex := regexp.MustCompile("\nfrontend ") |
| 119 | + backendRegex := regexp.MustCompile("\nbackend ") |
| 120 | + |
| 121 | + for n, test := range tests { |
| 122 | + cmd := exec.Command("./egress-dns-proxy.sh") |
| 123 | + cmd.Env = []string{ |
| 124 | + fmt.Sprintf("EGRESS_DNS_PROXY_DESTINATION=%s", test.dest), |
| 125 | + fmt.Sprintf("EGRESS_DNS_PROXY_MODE=unit-test"), |
| 126 | + } |
| 127 | + outBytes, err := cmd.CombinedOutput() |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("test %d unexpected error %v, output: %q", n+1, err, string(outBytes)) |
| 130 | + } |
| 131 | + out := string(outBytes) |
| 132 | + for _, frontend := range test.frontends { |
| 133 | + if !strings.Contains(out, frontend) { |
| 134 | + t.Fatalf("test %d expected frontend in output %q but got %q", n+1, frontend, out) |
| 135 | + } |
| 136 | + } |
| 137 | + matches := frontendRegex.FindAllStringIndex(out, -1) |
| 138 | + if len(matches) != len(test.frontends) { |
| 139 | + t.Fatalf("test %d number of frontends mismatch, expected %q but got %q", n+1, test.frontends, out) |
| 140 | + } |
| 141 | + |
| 142 | + for _, backend := range test.backends { |
| 143 | + if !strings.Contains(out, backend) { |
| 144 | + t.Fatalf("test %d expected backend in output %q but got %q", n+1, backend, out) |
| 145 | + } |
| 146 | + } |
| 147 | + matches = backendRegex.FindAllStringIndex(out, -1) |
| 148 | + if len(matches) != len(test.backends) { |
| 149 | + t.Fatalf("test %d number of backends mismatch, expected %q but got %q", n+1, test.backends, out) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func TestHAProxyFrontendBackendConfBad(t *testing.T) { |
| 155 | + tests := []struct { |
| 156 | + dest string |
| 157 | + err string |
| 158 | + }{ |
| 159 | + { |
| 160 | + dest: "", |
| 161 | + err: "Must specify EGRESS_DNS_PROXY_DESTINATION", |
| 162 | + }, |
| 163 | + { |
| 164 | + dest: "80 11.12.13.14\ninvalid", |
| 165 | + err: "Bad destination 'invalid'", |
| 166 | + }, |
| 167 | + { |
| 168 | + dest: "80 11.12.13.14\n8080 invalid", |
| 169 | + err: "Bad destination '8080 invalid'", |
| 170 | + }, |
| 171 | + { |
| 172 | + dest: "99999 11.12.13.14", |
| 173 | + err: "Invalid port: 99999, must be in the range 1 to 65535", |
| 174 | + }, |
| 175 | + { |
| 176 | + dest: "80 11.12.13.14 88888", |
| 177 | + err: "Invalid port: 88888, must be in the range 1 to 65535", |
| 178 | + }, |
| 179 | + { |
| 180 | + dest: "80 11.12.13.14\n80 21.22.23.24 100", |
| 181 | + err: "Proxy port 80 already used, must be unique for each destination", |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + for n, test := range tests { |
| 186 | + cmd := exec.Command("./egress-dns-proxy.sh") |
| 187 | + cmd.Env = []string{ |
| 188 | + "EGRESS_DNS_PROXY_MODE=unit-test", |
| 189 | + fmt.Sprintf("EGRESS_DNS_PROXY_DESTINATION=%s", test.dest), |
| 190 | + } |
| 191 | + out, err := cmd.CombinedOutput() |
| 192 | + out_lines := strings.Split(string(out), "\n") |
| 193 | + got := out_lines[len(out_lines)-2] |
| 194 | + if err == nil { |
| 195 | + t.Fatalf("test %d expected error %q but got output %q", n+1, test.err, got) |
| 196 | + } |
| 197 | + if got != test.err { |
| 198 | + t.Fatalf("test %d expected output %q but got %q", n+1, test.err, got) |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +func TestHAProxyDefaults(t *testing.T) { |
| 204 | + defaults := ` |
| 205 | +global |
| 206 | + log 127.0.0.1 local2 |
| 207 | +
|
| 208 | + chroot /var/lib/haproxy |
| 209 | + pidfile /var/run/haproxy.pid |
| 210 | + maxconn 4000 |
| 211 | + user haproxy |
| 212 | + group haproxy |
| 213 | +
|
| 214 | +defaults |
| 215 | + log global |
| 216 | + mode tcp |
| 217 | + option dontlognull |
| 218 | + option tcplog |
| 219 | + option redispatch |
| 220 | + retries 3 |
| 221 | + timeout http-request 100s |
| 222 | + timeout queue 1m |
| 223 | + timeout connect 10s |
| 224 | + timeout client 1m |
| 225 | + timeout server 1m |
| 226 | + timeout http-keep-alive 100s |
| 227 | + timeout check 10s |
| 228 | +` |
| 229 | + cmd := exec.Command("./egress-dns-proxy.sh") |
| 230 | + cmd.Env = []string{ |
| 231 | + "EGRESS_DNS_PROXY_MODE=unit-test", |
| 232 | + "EGRESS_DNS_PROXY_DESTINATION=80 11.12.13.14", |
| 233 | + } |
| 234 | + out, err := cmd.CombinedOutput() |
| 235 | + if err != nil { |
| 236 | + t.Fatalf("unexpected error %v", err) |
| 237 | + } |
| 238 | + if !strings.Contains(string(out), defaults) { |
| 239 | + t.Fatalf("expected defaults in output %q but got %q", defaults, string(out)) |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +func TestHAProxyResolver(t *testing.T) { |
| 244 | + resolverRegex := "resolvers dns-resolver\n *(nameserver ns.*)+\n +" |
| 245 | + |
| 246 | + cmd := exec.Command("./egress-dns-proxy.sh") |
| 247 | + cmd.Env = []string{ |
| 248 | + "EGRESS_DNS_PROXY_MODE=unit-test", |
| 249 | + "EGRESS_DNS_PROXY_DESTINATION=80 11.12.13.14", |
| 250 | + } |
| 251 | + outBytes, err := cmd.CombinedOutput() |
| 252 | + if err != nil { |
| 253 | + t.Fatalf("unexpected error %v", err) |
| 254 | + } |
| 255 | + out := string(outBytes) |
| 256 | + match, er := regexp.MatchString(resolverRegex, out) |
| 257 | + if !match || er != nil { |
| 258 | + t.Fatalf("dns resolver section not found in output %q", out) |
| 259 | + } |
| 260 | +} |
0 commit comments