Skip to content

Commit

Permalink
fix: truncate only when allowed length exceeded (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed Jan 11, 2023
1 parent 3b8c1a0 commit 8c277a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pkg/clusters/types/gke/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,10 @@ func sanitizeCreatedByID(id string) string {
}

// Truncate to the maximum allowed length.
return builder.String()[:63]
const maxAllowedLength = 63
s := builder.String()
if len(s) > maxAllowedLength {
return s[:maxAllowedLength]
}
return s
}
27 changes: 24 additions & 3 deletions pkg/clusters/types/gke/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,28 @@ import (
)

func TestSanitizeCreatedByID(t *testing.T) {
sanitized := sanitizeCreatedByID("764086051850-6qr4p6gpi6hn506pt8ejuq83di345HUR.apps^googleusercontent$com")
require.Equal(t, "764086051850-6qr4p6gpi6hn506pt8ejuq83di345hur-apps-googleuserco", sanitized,
"expected disallowed characters to be replaced with dashes, capitals to be lowered, and output to be truncated")
testCases := []struct {
name string
input string
expected string
}{
{
name: "longer than allowed",
input: "764086051850-6qr4p6gpi6hn506pt8ejuq83di345HUR.apps^googleusercontent$com",
expected: "764086051850-6qr4p6gpi6hn506pt8ejuq83di345hur-apps-googleuserco",
},
{
name: "short",
input: "764086051850",
expected: "764086051850",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
sanitized := sanitizeCreatedByID(tc.input)
require.Equal(t, tc.expected, sanitized)
})
}
}

0 comments on commit 8c277a7

Please sign in to comment.