-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: label values not validated (#355)
- Loading branch information
Showing
3 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package oplabels | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
/** | ||
A valid label must be an empty string or consist of alphanumeric characters, | ||
'-', '_' or '.', and must start and end with an alphanumeric. | ||
*/ | ||
func TestLabelNameClean(t *testing.T) { | ||
|
||
var cleaned = CleanLabelName("TestCluster") | ||
require.EqualValues(t, "TestCluster", cleaned, | ||
"expect label name to not be cleaned as no need") | ||
|
||
cleaned = CleanLabelName("Test Cluster") | ||
require.EqualValues(t, "TestCluster", cleaned, | ||
"expect space to be cleaned") | ||
|
||
cleaned = CleanLabelName("+!*(-_)cor @#$%^&rect_ LABEL.name-1=<>_?,.") | ||
require.EqualValues(t, "correct_LABEL.name-1", cleaned, | ||
"expect label name w/ outlier chars to be cleaned") | ||
|
||
cleaned = CleanLabelName("cor!@#$%^&*()rect__ LABEL.name") | ||
require.EqualValues(t, "correct__LABEL.name", cleaned, | ||
"expect label name w/ inside chars to be cleaned") | ||
|
||
cleaned = CleanLabelName("correct") | ||
require.EqualValues(t, "correct", cleaned, | ||
"expect label name without chars to be correct") | ||
|
||
cleaned = CleanLabelName("") | ||
require.EqualValues(t, "", cleaned, | ||
"expect label name as empty without chars to be correct") | ||
|
||
cleaned = CleanLabelName("-_!@#$%-^&*.()<>?._-&*.") | ||
require.EqualValues(t, "", cleaned, | ||
"expect label name as empty as contains all bad chars") | ||
|
||
cleaned = CleanLabelName("-_!@#$%-^&*.()<>?._-&*.X") | ||
require.EqualValues(t, "X", cleaned, | ||
"expect label name as last char only") | ||
|
||
cleaned = CleanLabelName("Y-_!@#$%-^&*.()<>?._-&*.") | ||
require.EqualValues(t, "Y", cleaned, | ||
"expect label name as first char only") | ||
} | ||
|
||
func TestWhitelistRegex(t *testing.T) { | ||
|
||
var whitelistRegex = regexp.MustCompile(`(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?`) | ||
var unclean = "+!*(-_)cor @#$%^&rect _ LABEL.name-1=<>_?,." | ||
var regexpResult = whitelistRegex.FindAllString(strings.Replace(unclean, " ", "", -1), -1) | ||
require.EqualValues(t, "correct_LABEL.name-1", strings.Join(regexpResult, "")) | ||
|
||
unclean = "+!*(-_)cor @#$ %^&re _c-t-.LABEL.name-1=<>_?,." | ||
regexpResult = whitelistRegex.FindAllString(strings.Replace(unclean, " ", "", -1), -1) | ||
require.EqualValues(t, "corre_c-t-.LABEL.name-1", strings.Join(regexpResult, "")) | ||
} |