From 6c9a5997610c66de7795276dd285926e6a8252af Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 18 Jul 2019 14:56:59 +0000 Subject: [PATCH] plugin/file: fix setting ReloadInterval (#3017) * plugin/file: fix setting ReloadInterval The reload interval was only correctly set if there was an extra block for the file. Move this down to set up. Add test case that fails before, but now works. Signed-off-by: Miek Gieben * layout and use Errorf Signed-off-by: Miek Gieben --- plugin/file/setup.go | 9 ++++++--- plugin/file/setup_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/plugin/file/setup.go b/plugin/file/setup.go index 9be09fe8d..162db6d65 100644 --- a/plugin/file/setup.go +++ b/plugin/file/setup.go @@ -97,7 +97,6 @@ func fileParse(c *caddy.Controller) (Zones, error) { names = append(names, origins[i]) } - upstr := upstream.New() t := []string{} var e error @@ -128,11 +127,15 @@ func fileParse(c *caddy.Controller) (Zones, error) { if t != nil { z[origin].TransferTo = append(z[origin].TransferTo, t...) } - z[origin].ReloadInterval = reload - z[origin].Upstream = upstr } } } + + for origin := range z { + z[origin].ReloadInterval = reload + z[origin].Upstream = upstream.New() + } + if openErr != nil { if reload == 0 { // reload hasn't been set make this a fatal error diff --git a/plugin/file/setup_test.go b/plugin/file/setup_test.go index f6252759b..6a1d5e790 100644 --- a/plugin/file/setup_test.go +++ b/plugin/file/setup_test.go @@ -2,6 +2,7 @@ package file import ( "testing" + "time" "github.com/coredns/coredns/plugin/test" @@ -90,3 +91,35 @@ func TestFileParse(t *testing.T) { } } } + +func TestParseReload(t *testing.T) { + name, rm, err := test.TempFile(".", dbMiekNL) + if err != nil { + t.Fatal(err) + } + defer rm() + + tests := []struct { + input string + reload time.Duration + }{ + { + `file ` + name + ` example.org.`, + 1 * time.Minute, + }, + { + `file ` + name + ` example.org. { + reload 5s + }`, + 5 * time.Second, + }, + } + + for i, test := range tests { + c := caddy.NewTestController("dns", test.input) + z, _ := fileParse(c) + if x := z.Z["example.org."].ReloadInterval; x != test.reload { + t.Errorf("Test %d expected reload to be %s, but got %s", i, test.reload, x) + } + } +}