aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/parse/host_test.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2018-09-19 08:16:04 +0100
committerGravatar GitHub <noreply@github.com> 2018-09-19 08:16:04 +0100
commitcb932ca23103485d67e447eeddd855007015d30e (patch)
tree4d2c6bc89f4d92fb7a3c75f71a5b31b5fc397b7d /plugin/pkg/parse/host_test.go
parentc349446a23440b336f4ca21900cce4d6a031cdf5 (diff)
downloadcoredns-cb932ca23103485d67e447eeddd855007015d30e.tar.gz
coredns-cb932ca23103485d67e447eeddd855007015d30e.tar.zst
coredns-cb932ca23103485d67e447eeddd855007015d30e.zip
Better naming (#2104)
* Move functions from pkg/transport to pkg/parse Although "parse" is a fairly generic name I believe this is somewhat better named. pkg/transport keeps a few constants that are uses throughout for the rest is is renaming a bunch (and the fallout from there to make things compile again). Signed-off-by: Miek Gieben <miek@miek.nl> * Fix tests Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'plugin/pkg/parse/host_test.go')
-rw-r--r--plugin/pkg/parse/host_test.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/plugin/pkg/parse/host_test.go b/plugin/pkg/parse/host_test.go
new file mode 100644
index 000000000..f6e771f29
--- /dev/null
+++ b/plugin/pkg/parse/host_test.go
@@ -0,0 +1,87 @@
+package parse
+
+import (
+ "io/ioutil"
+ "os"
+ "testing"
+
+ "github.com/coredns/coredns/plugin/pkg/transport"
+)
+
+func TestHostPortOrFile(t *testing.T) {
+ tests := []struct {
+ in string
+ expected string
+ shouldErr bool
+ }{
+ {
+ "8.8.8.8",
+ "8.8.8.8:53",
+ false,
+ },
+ {
+ "8.8.8.8:153",
+ "8.8.8.8:153",
+ false,
+ },
+ {
+ "/etc/resolv.conf:53",
+ "",
+ true,
+ },
+ {
+ "resolv.conf",
+ "127.0.0.1:53",
+ false,
+ },
+ }
+
+ err := ioutil.WriteFile("resolv.conf", []byte("nameserver 127.0.0.1\n"), 0600)
+ if err != nil {
+ t.Fatalf("Failed to write test resolv.conf")
+ }
+ defer os.Remove("resolv.conf")
+
+ for i, tc := range tests {
+ got, err := HostPortOrFile(tc.in)
+ if err == nil && tc.shouldErr {
+ t.Errorf("Test %d, expected error, got nil", i)
+ continue
+ }
+ if err != nil && tc.shouldErr {
+ continue
+ }
+ if got[0] != tc.expected {
+ t.Errorf("Test %d, expected %q, got %q", i, tc.expected, got[0])
+ }
+ }
+}
+
+func TestParseHostPort(t *testing.T) {
+ tests := []struct {
+ in string
+ expected string
+ shouldErr bool
+ }{
+ {"8.8.8.8:53", "8.8.8.8:53", false},
+ {"a.a.a.a:153", "", true},
+ {"8.8.8.8", "8.8.8.8:53", false},
+ {"8.8.8.8:", "8.8.8.8:53", false},
+ {"8.8.8.8::53", "", true},
+ {"resolv.conf", "", true},
+ }
+
+ for i, tc := range tests {
+ got, err := HostPort(tc.in, transport.Port)
+ if err == nil && tc.shouldErr {
+ t.Errorf("Test %d, expected error, got nil", i)
+ continue
+ }
+ if err != nil && !tc.shouldErr {
+ t.Errorf("Test %d, expected no error, got %q", i, err)
+ }
+ if got != tc.expected {
+ t.Errorf("Test %d, expected %q, got %q", i, tc.expected, got)
+ }
+ }
+}