diff options
author | 2017-09-21 04:18:13 -0700 | |
---|---|---|
committer | 2017-09-21 04:18:13 -0700 | |
commit | 7109c6715c6b526e583212598a7c98d39a6e8063 (patch) | |
tree | 807d83e15c40f29aa4893d429a854fcfea7ba9bc /plugin/hosts/setup_test.go | |
parent | aecf9163776f1d22bd0732b8bcf74dc2c364afde (diff) | |
download | coredns-7109c6715c6b526e583212598a7c98d39a6e8063.tar.gz coredns-7109c6715c6b526e583212598a7c98d39a6e8063.tar.zst coredns-7109c6715c6b526e583212598a7c98d39a6e8063.zip |
Add inline support for middleware/hosts (#1072)
This fix add inline support for middleware/hosts so that
it is possible to specify hosts file insides the Corefile:
```
hosts inline example.org {
10.0.0.1 example.org
fallthrough
}
```
This fix fixes 999.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Diffstat (limited to 'plugin/hosts/setup_test.go')
-rw-r--r-- | plugin/hosts/setup_test.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/plugin/hosts/setup_test.go b/plugin/hosts/setup_test.go index a4c95b1c6..fefed6c72 100644 --- a/plugin/hosts/setup_test.go +++ b/plugin/hosts/setup_test.go @@ -84,3 +84,77 @@ func TestHostsParse(t *testing.T) { } } } + +func TestHostsInlineParse(t *testing.T) { + tests := []struct { + inputFileRules string + shouldErr bool + expectedbyAddr map[string][]string + expectedFallthrough bool + }{ + { + `hosts highly_unlikely_to_exist_hosts_file example.org { + 10.0.0.1 example.org + fallthrough + }`, + false, + map[string][]string{ + `10.0.0.1`: { + `example.org.`, + }, + }, + true, + }, + { + `hosts highly_unlikely_to_exist_hosts_file example.org { + 10.0.0.1 example.org + }`, + false, + map[string][]string{ + `10.0.0.1`: { + `example.org.`, + }, + }, + false, + }, + { + `hosts highly_unlikely_to_exist_hosts_file example.org { + fallthrough + 10.0.0.1 example.org + }`, + true, + map[string][]string{}, + true, + }, + } + + for i, test := range tests { + c := caddy.NewTestController("dns", test.inputFileRules) + h, err := hostsParse(c) + + if err == nil && test.shouldErr { + t.Fatalf("Test %d expected errors, but got no error", i) + } else if err != nil && !test.shouldErr { + t.Fatalf("Test %d expected no errors, but got '%v'", i, err) + } else if !test.shouldErr { + if h.Fallthrough != test.expectedFallthrough { + t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fallthrough) + } + for k, expectedVal := range test.expectedbyAddr { + if val, ok := h.byAddr[k]; !ok { + t.Fatalf("Test %d expected %v, got no entry", i, k) + } else { + if len(expectedVal) != len(val) { + t.Fatalf("Test %d expected %v records for %v, got %v", i, len(expectedVal), k, len(val)) + } + for j := range expectedVal { + if expectedVal[j] != val[j] { + t.Fatalf("Test %d expected %v for %v, got %v", i, expectedVal[j], j, val[j]) + } + } + } + } + } + } + +} |