mirror of
https://github.com/coredns/coredns.git
synced 2026-06-01 23:00:23 -04:00
fix: use descriptive error for unknown block options in health and log plugins (#8128)
This commit is contained in:
committed by
GitHub
parent
afdf121a5a
commit
0bcb17df06
@@ -58,7 +58,7 @@ func parse(c *caddy.Controller) (string, time.Duration, error) {
|
||||
}
|
||||
dur = l
|
||||
default:
|
||||
return "", 0, c.ArgErr()
|
||||
return "", 0, c.Errf("unknown property '%s'", c.Val())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/caddy"
|
||||
@@ -8,24 +9,25 @@ import (
|
||||
|
||||
func TestSetupHealth(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
shouldErr bool
|
||||
input string
|
||||
shouldErr bool
|
||||
expectedErrContent string
|
||||
}{
|
||||
{`health`, false},
|
||||
{`health localhost:1234`, false},
|
||||
{`health`, false, ""},
|
||||
{`health localhost:1234`, false, ""},
|
||||
{`health localhost:1234 {
|
||||
lameduck 4s
|
||||
}`, false},
|
||||
{`health bla:a`, false},
|
||||
}`, false, ""},
|
||||
{`health bla:a`, false, ""},
|
||||
|
||||
{`health bla`, true},
|
||||
{`health bla bla`, true},
|
||||
{`health bla`, true, ""},
|
||||
{`health bla bla`, true, ""},
|
||||
{`health localhost:1234 {
|
||||
lameduck a
|
||||
}`, true},
|
||||
}`, true, ""},
|
||||
{`health localhost:1234 {
|
||||
lamedudk 4
|
||||
} `, true},
|
||||
} `, true, "unknown property"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
@@ -40,6 +42,9 @@ func TestSetupHealth(t *testing.T) {
|
||||
if !test.shouldErr {
|
||||
t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err)
|
||||
}
|
||||
if test.expectedErrContent != "" && !strings.Contains(err.Error(), test.expectedErrContent) {
|
||||
t.Errorf("Test %d: Expected error to contain %q, got: %v", i, test.expectedErrContent, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
|
||||
classes[cls] = struct{}{}
|
||||
}
|
||||
default:
|
||||
return nil, c.ArgErr()
|
||||
return nil, c.Errf("unknown property '%s'", c.Val())
|
||||
}
|
||||
}
|
||||
if len(classes) == 0 {
|
||||
|
||||
@@ -2,6 +2,7 @@ package log
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/caddy"
|
||||
@@ -182,3 +183,14 @@ func TestLogParse(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogParseUnknownProperty(t *testing.T) {
|
||||
c := caddy.NewTestController("dns", `log { unknown }`)
|
||||
_, err := logParse(c)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown block option, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unknown property") {
|
||||
t.Errorf("expected error to contain 'unknown property', got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user