From ec6524ce747f2424daae04a7f46ed98461d24bb2 Mon Sep 17 00:00:00 2001 From: Matti Savolainen Date: Wed, 19 Oct 2016 13:32:42 +0300 Subject: [PATCH] test the labelTarget regex to make sure it properly validates pre-interpolated label names. --- config/config_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/config/config_test.go b/config/config_test.go index a8cff5ddf..4bbb3e9b4 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -512,6 +512,34 @@ func TestEmptyGlobalBlock(t *testing.T) { } } +func TestTargetLabelValidity(t *testing.T) { + tests := []struct { + str string + valid bool + }{ + {"-label", false}, + {"label", true}, + {"label${1}", true}, + {"${1}label", true}, + {"${1}", true}, + {"${1}label", true}, + {"${", false}, + {"$", false}, + {"${}", false}, + {"foo${", false}, + {"$1", true}, + {"asd$2asd", true}, + {"-foo${1}bar-", false}, + {"_${1}_", true}, + {"foo${bar}foo", true}, + } + for _, test := range tests { + if relabelTarget.Match([]byte(test.str)) != test.valid { + t.Fatalf("Expected %q to be %v", test.str, test.valid) + } + } +} + func kubernetesSDHostURL() URL { tURL, _ := url.Parse("https://localhost:1234") return URL{URL: tURL}