Update IsValidLabelKey to include ':' (#11384)

This commit is contained in:
Alex McGrath
2022-03-28 12:25:17 +00:00
committed by GitHub
parent 9e1b887d94
commit cf0d569067
4 changed files with 34 additions and 9 deletions
+1 -1
View File
@@ -348,7 +348,7 @@ func MatchLabels(resource ResourceWithLabels, labels map[string]string) bool {
}
// LabelPattern is a regexp that describes a valid label key
const LabelPattern = `^[a-zA-Z/.0-9_*-]+$`
const LabelPattern = `^[a-zA-Z/.0-9_:*-]+$`
var validLabelKey = regexp.MustCompile(LabelPattern)
+23
View File
@@ -372,3 +372,26 @@ func TestResourcesWithLabels_ToMap(t *testing.T) {
})
}
}
func TestValidLabelKey(t *testing.T) {
for _, tc := range []struct {
label string
valid bool
}{
{
label: "1x/Y*_-",
valid: true,
},
{
label: "x:y",
valid: true,
},
{
label: "x\\y",
valid: false,
},
} {
isValid := IsValidLabelKey(tc.label)
require.Equal(t, tc.valid, isValid)
}
}
+7 -5
View File
@@ -423,7 +423,8 @@ func TestRDSTagsToLabels(t *testing.T) {
},
}
labels := rdsTagsToLabels(rdsTags)
require.Equal(t, map[string]string{"Name": "test", "Env": "dev"}, labels)
require.Equal(t, map[string]string{"Name": "test", "Env": "dev",
"aws:cloudformation:stack-id": "some-id"}, labels)
}
// TestDatabaseFromRedshiftCluster tests converting an Redshift cluster to a database resource.
@@ -451,10 +452,11 @@ func TestDatabaseFromRedshiftCluster(t *testing.T) {
Name: "mycluster",
Description: "Redshift cluster in us-east-1",
Labels: map[string]string{
types.OriginLabel: types.OriginCloud,
labelAccountID: "1234567890",
labelRegion: "us-east-1",
"key": "val",
types.OriginLabel: types.OriginCloud,
labelAccountID: "1234567890",
labelRegion: "us-east-1",
"key": "val",
"elasticbeanstalk:environment-id": "id",
},
}, types.DatabaseSpecV3{
Protocol: defaults.ProtocolPostgres,
+3 -3
View File
@@ -86,7 +86,7 @@ func (s *ServicesSuite) TestCommandLabels(c *check.C) {
c.Assert(label.Command[0], check.Not(check.Equals), out["a"].GetCommand())
}
func (s *ServicesSuite) TestLabelKeyValidation(c *check.C) {
func TestLabelKeyValidation(t *testing.T) {
tts := []struct {
label string
ok bool
@@ -96,12 +96,12 @@ func (s *ServicesSuite) TestLabelKeyValidation(c *check.C) {
{label: "this-that", ok: true},
{label: "8675309", ok: true},
{label: "", ok: false},
{label: "spam:eggs", ok: false},
{label: "spam:eggs", ok: true},
{label: "cats dogs", ok: false},
{label: "wut?", ok: false},
}
for _, tt := range tts {
c.Assert(types.IsValidLabelKey(tt.label), check.Equals, tt.ok, check.Commentf("tt=%+v", tt))
require.Equal(t, types.IsValidLabelKey(tt.label), tt.ok)
}
}