Skip to content

Commit 8c277a7

Browse files
authored
fix: truncate only when allowed length exceeded (#495)
1 parent 3b8c1a0 commit 8c277a7

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

pkg/clusters/types/gke/builder.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,5 +274,10 @@ func sanitizeCreatedByID(id string) string {
274274
}
275275

276276
// Truncate to the maximum allowed length.
277-
return builder.String()[:63]
277+
const maxAllowedLength = 63
278+
s := builder.String()
279+
if len(s) > maxAllowedLength {
280+
return s[:maxAllowedLength]
281+
}
282+
return s
278283
}

pkg/clusters/types/gke/builder_test.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,28 @@ import (
77
)
88

99
func TestSanitizeCreatedByID(t *testing.T) {
10-
sanitized := sanitizeCreatedByID("764086051850-6qr4p6gpi6hn506pt8ejuq83di345HUR.apps^googleusercontent$com")
11-
require.Equal(t, "764086051850-6qr4p6gpi6hn506pt8ejuq83di345hur-apps-googleuserco", sanitized,
12-
"expected disallowed characters to be replaced with dashes, capitals to be lowered, and output to be truncated")
10+
testCases := []struct {
11+
name string
12+
input string
13+
expected string
14+
}{
15+
{
16+
name: "longer than allowed",
17+
input: "764086051850-6qr4p6gpi6hn506pt8ejuq83di345HUR.apps^googleusercontent$com",
18+
expected: "764086051850-6qr4p6gpi6hn506pt8ejuq83di345hur-apps-googleuserco",
19+
},
20+
{
21+
name: "short",
22+
input: "764086051850",
23+
expected: "764086051850",
24+
},
25+
}
26+
27+
for _, tc := range testCases {
28+
tc := tc
29+
t.Run(tc.name, func(t *testing.T) {
30+
sanitized := sanitizeCreatedByID(tc.input)
31+
require.Equal(t, tc.expected, sanitized)
32+
})
33+
}
1334
}

0 commit comments

Comments
 (0)