From 1c759627ec5e35aabb73e18ae794c8d8fd5cb880 Mon Sep 17 00:00:00 2001 From: Jordan Whited Date: Wed, 27 May 2020 13:03:24 -0700 Subject: [PATCH] add setup() tests --- setup.go | 2 +- setup_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 setup_test.go diff --git a/setup.go b/setup.go index 2c64708..350ca0d 100644 --- a/setup.go +++ b/setup.go @@ -37,7 +37,7 @@ func setup(c *caddy.Controller) error { client, err := wgctrl.New() if err != nil { return plugin.Error("wgsd", - fmt.Errorf("wgsd: error constructing wgctrl client: %v", + fmt.Errorf("error constructing wgctrl client: %v", err)) } diff --git a/setup_test.go b/setup_test.go new file mode 100644 index 0000000..87b5a7e --- /dev/null +++ b/setup_test.go @@ -0,0 +1,41 @@ +package wgsd + +import ( + "testing" + + "github.com/caddyserver/caddy" +) + +func TestSetup(t *testing.T) { + testCases := []struct { + name string + input string + expectErr bool + }{ + { + "valid input", + "wgsd example.com. wg0", + false, + }, + { + "missing token", + "wgsd example.com.", + true, + }, + { + "too many tokens", + "wgsd example.com. wg0 extra", + true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + c := caddy.NewTestController("dns", tc.input) + err := setup(c) + if (err != nil) != tc.expectErr { + t.Fatalf("expectErr: %v, got err=%v", tc.expectErr, err) + } + }) + } +}