diff options
Diffstat (limited to 'plugin/metadata')
-rw-r--r-- | plugin/metadata/metadata_test.go | 30 | ||||
-rw-r--r-- | plugin/metadata/provider.go | 16 |
2 files changed, 44 insertions, 2 deletions
diff --git a/plugin/metadata/metadata_test.go b/plugin/metadata/metadata_test.go index 7ded05c03..08d9b6734 100644 --- a/plugin/metadata/metadata_test.go +++ b/plugin/metadata/metadata_test.go @@ -30,8 +30,8 @@ func (m *testHandler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns func TestMetadataServeDNS(t *testing.T) { expectedMetadata := []testProvider{ - testProvider{"test/key1": func() string { return "testvalue1" }}, - testProvider{"test/key2": func() string { return "two" }, "test/key3": func() string { return "testvalue3" }}, + {"test/key1": func() string { return "testvalue1" }}, + {"test/key2": func() string { return "two" }, "test/key3": func() string { return "testvalue3" }}, } // Create fake Providers based on expectedMetadata providers := []Provider{} @@ -52,6 +52,9 @@ func TestMetadataServeDNS(t *testing.T) { for _, expected := range expectedMetadata { for label, expVal := range expected { + if !IsLabel(label) { + t.Errorf("Expected label %s is not considered a valid label", label) + } val := ValueFunc(nctx, label) if val() != expVal() { t.Errorf("Expected value %s for %s, but got %s", expVal(), label, val()) @@ -59,3 +62,26 @@ func TestMetadataServeDNS(t *testing.T) { } } } + +func TestLabelFormat(t *testing.T) { + labels := []struct { + label string + isValid bool + }{ + {"plugin/LABEL", true}, + {"p/LABEL", true}, + {"plugin/L", true}, + {"LABEL", false}, + {"plugin.LABEL", false}, + {"/NO-PLUGIN-NOT-ACCEPTED", false}, + {"ONLY-PLUGIN-NOT-ACCEPTED/", false}, + {"PLUGIN/LABEL/SUB-LABEL", false}, + {"/", false}, + } + + for _, test := range labels { + if IsLabel(test.label) != test.isValid { + t.Errorf("Label %v is expected to have this validaty : %v - and has the opposite", test.label, test.isValid) + } + } +} diff --git a/plugin/metadata/provider.go b/plugin/metadata/provider.go index eb7bb9755..276a44127 100644 --- a/plugin/metadata/provider.go +++ b/plugin/metadata/provider.go @@ -32,6 +32,7 @@ package metadata import ( "context" + "strings" "github.com/coredns/coredns/request" ) @@ -48,6 +49,21 @@ type Provider interface { // Func is the type of function in the metadata, when called they return the value of the label. type Func func() string +// IsLabel check that the provided name looks like a valid label name +func IsLabel(label string) bool { + p := strings.Index(label, "/") + if p <= 0 || p >= len(label)-1 { + // cannot accept namespace empty nor label empty + return false + } + if strings.LastIndex(label, "/") != p { + // several slash in the Label + return false + } + return true + +} + // Labels returns all metadata keys stored in the context. These label names should be named // as: plugin/NAME, where NAME is something descriptive. func Labels(ctx context.Context) []string { |