aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugin/auto/setup.go14
-rw-r--r--plugin/auto/setup_test.go49
-rw-r--r--plugin/file/setup.go7
3 files changed, 67 insertions, 3 deletions
diff --git a/plugin/auto/setup.go b/plugin/auto/setup.go
index 1162274a0..38e8d26fe 100644
--- a/plugin/auto/setup.go
+++ b/plugin/auto/setup.go
@@ -1,6 +1,7 @@
package auto
import (
+ "errors"
"os"
"path/filepath"
"regexp"
@@ -44,7 +45,9 @@ func setup(c *caddy.Controller) error {
if err != nil {
return err
}
-
+ if a.loader.ReloadInterval == 0 {
+ return nil
+ }
go func() {
ticker := time.NewTicker(a.loader.ReloadInterval)
for {
@@ -131,7 +134,14 @@ func autoParse(c *caddy.Controller) (Auto, error) {
}
case "reload":
- d, err := time.ParseDuration(c.RemainingArgs()[0])
+ t := c.RemainingArgs()
+ if len(t) < 1 {
+ return a, errors.New("reload duration value is expected")
+ }
+ d, err := time.ParseDuration(t[0])
+ if d < 0 {
+ err = errors.New("invalid duration")
+ }
if err != nil {
return a, plugin.Error("file", err)
}
diff --git a/plugin/auto/setup_test.go b/plugin/auto/setup_test.go
index 987411c07..4fada6f6b 100644
--- a/plugin/auto/setup_test.go
+++ b/plugin/auto/setup_test.go
@@ -126,3 +126,52 @@ func TestAutoParse(t *testing.T) {
}
}
}
+
+func TestSetupReload(t *testing.T) {
+ tests := []struct {
+ name string
+ config string
+ wantErr bool
+ }{
+ {
+ name: "reload valid",
+ config: `auto {
+ directory .
+ reload 5s
+ }`,
+ wantErr: false,
+ },
+ {
+ name: "reload disable",
+ config: `auto {
+ directory .
+ reload 0
+ }`,
+ wantErr: false,
+ },
+ {
+ name: "reload invalid",
+ config: `auto {
+ directory .
+ reload -1s
+ }`,
+ wantErr: true,
+ },
+ {
+ name: "reload invalid",
+ config: `auto {
+ directory .
+ reload
+ }`,
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ ctr := caddy.NewTestController("dns", tt.config)
+ if err := setup(ctr); (err != nil) != tt.wantErr {
+ t.Errorf("Error: setup() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
diff --git a/plugin/file/setup.go b/plugin/file/setup.go
index 0444836b5..f25fba735 100644
--- a/plugin/file/setup.go
+++ b/plugin/file/setup.go
@@ -1,6 +1,7 @@
package file
import (
+ "errors"
"os"
"path/filepath"
"time"
@@ -108,7 +109,11 @@ func fileParse(c *caddy.Controller) (Zones, error) {
for c.NextBlock() {
switch c.Val() {
case "reload":
- d, err := time.ParseDuration(c.RemainingArgs()[0])
+ t := c.RemainingArgs()
+ if len(t) < 1 {
+ return Zones{}, errors.New("reload duration value is expected")
+ }
+ d, err := time.ParseDuration(t[0])
if err != nil {
return Zones{}, plugin.Error("file", err)
}