test(container): add sanitization and collision detection tests
Add comprehensive tests for container name sanitization including collision detection between domains like git.example.com and git-example.com. Verify that dot, colon, and hyphen separators produce distinct outputs to prevent name conflicts.
- commit
- 985c8e41cd2fe7301d136cfa285f0b11034fd798
- author
- bnema · · 2026-01-31 22:20 UTC
- parent
- db67927
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +60 -0
M internal/usecase/container/service_test.go +60 -0
| @@ -1153,3 +1153,63 @@ func TestService_StripRegistryPrefix(t *testing.T) { | |||
| 1153 | 1153 | }) | |
| 1154 | 1154 | } | |
| 1155 | 1155 | } | |
| 1156 | + | ||
| 1157 | + | func TestSanitizeName(t *testing.T) { | |
| 1158 | + | tests := []struct { | |
| 1159 | + | domain string | |
| 1160 | + | expected string | |
| 1161 | + | description string | |
| 1162 | + | }{ | |
| 1163 | + | { | |
| 1164 | + | domain: "git.example.com", | |
| 1165 | + | expected: "git__example__com", | |
| 1166 | + | description: "Dots become double underscores", | |
| 1167 | + | }, | |
| 1168 | + | { | |
| 1169 | + | domain: "git-example.com", | |
| 1170 | + | expected: "git-example__com", | |
| 1171 | + | description: "Hyphens preserved, dots become underscores", | |
| 1172 | + | }, | |
| 1173 | + | { | |
| 1174 | + | domain: "app:8080.example.com", | |
| 1175 | + | expected: "app-_8080__example__com", | |
| 1176 | + | description: "Colons become hyphen-underscore", | |
| 1177 | + | }, | |
| 1178 | + | { | |
| 1179 | + | domain: "git.example.com:3000", | |
| 1180 | + | expected: "git__example__com-_3000", | |
| 1181 | + | description: "Multiple separators handled distinctly", | |
| 1182 | + | }, | |
| 1183 | + | { | |
| 1184 | + | domain: "simple.com", | |
| 1185 | + | expected: "simple__com", | |
| 1186 | + | description: "Simple domain", | |
| 1187 | + | }, | |
| 1188 | + | } | |
| 1189 | + | ||
| 1190 | + | for _, tt := range tests { | |
| 1191 | + | t.Run(tt.domain, func(t *testing.T) { | |
| 1192 | + | result := sanitizeName(tt.domain) | |
| 1193 | + | assert.Equal(t, tt.expected, result, "sanitization should match expected") | |
| 1194 | + | }) | |
| 1195 | + | } | |
| 1196 | + | ||
| 1197 | + | // Verify no collisions between potentially conflicting domains | |
| 1198 | + | t.Run("NoCollisions", func(t *testing.T) { | |
| 1199 | + | domains := []string{ | |
| 1200 | + | "git.example.com", | |
| 1201 | + | "git-example.com", | |
| 1202 | + | "app:8080.example.com", | |
| 1203 | + | "app-8080-example.com", | |
| 1204 | + | } | |
| 1205 | + | ||
| 1206 | + | results := make(map[string]string) | |
| 1207 | + | for _, d := range domains { | |
| 1208 | + | result := sanitizeName(d) | |
| 1209 | + | if original, exists := results[result]; exists { | |
| 1210 | + | t.Errorf("COLLISION: %q and %q both sanitize to %q", original, d, result) | |
| 1211 | + | } | |
| 1212 | + | results[result] = d | |
| 1213 | + | } | |
| 1214 | + | }) | |
| 1215 | + | } | |
| @@ -1153,3 +1153,63 @@ func TestService_StripRegistryPrefix(t *testing.T) { | |||||
| 1153 | }) | 1153 | }) | ||
| 1154 | } | 1154 | } | ||
| 1155 | } | 1155 | } | ||
| 1156 | + | ||||
| 1157 | + | func TestSanitizeName(t *testing.T) { | |||
| 1158 | + | tests := []struct { | |||
| 1159 | + | domain string | |||
| 1160 | + | expected string | |||
| 1161 | + | description string | |||
| 1162 | + | }{ | |||
| 1163 | + | { | |||
| 1164 | + | domain: "git.example.com", | |||
| 1165 | + | expected: "git__example__com", | |||
| 1166 | + | description: "Dots become double underscores", | |||
| 1167 | + | }, | |||
| 1168 | + | { | |||
| 1169 | + | domain: "git-example.com", | |||
| 1170 | + | expected: "git-example__com", | |||
| 1171 | + | description: "Hyphens preserved, dots become underscores", | |||
| 1172 | + | }, | |||
| 1173 | + | { | |||
| 1174 | + | domain: "app:8080.example.com", | |||
| 1175 | + | expected: "app-_8080__example__com", | |||
| 1176 | + | description: "Colons become hyphen-underscore", | |||
| 1177 | + | }, | |||
| 1178 | + | { | |||
| 1179 | + | domain: "git.example.com:3000", | |||
| 1180 | + | expected: "git__example__com-_3000", | |||
| 1181 | + | description: "Multiple separators handled distinctly", | |||
| 1182 | + | }, | |||
| 1183 | + | { | |||
| 1184 | + | domain: "simple.com", | |||
| 1185 | + | expected: "simple__com", | |||
| 1186 | + | description: "Simple domain", | |||
| 1187 | + | }, | |||
| 1188 | + | } | |||
| 1189 | + | ||||
| 1190 | + | for _, tt := range tests { | |||
| 1191 | + | t.Run(tt.domain, func(t *testing.T) { | |||
| 1192 | + | result := sanitizeName(tt.domain) | |||
| 1193 | + | assert.Equal(t, tt.expected, result, "sanitization should match expected") | |||
| 1194 | + | }) | |||
| 1195 | + | } | |||
| 1196 | + | ||||
| 1197 | + | // Verify no collisions between potentially conflicting domains | |||
| 1198 | + | t.Run("NoCollisions", func(t *testing.T) { | |||
| 1199 | + | domains := []string{ | |||
| 1200 | + | "git.example.com", | |||
| 1201 | + | "git-example.com", | |||
| 1202 | + | "app:8080.example.com", | |||
| 1203 | + | "app-8080-example.com", | |||
| 1204 | + | } | |||
| 1205 | + | ||||
| 1206 | + | results := make(map[string]string) | |||
| 1207 | + | for _, d := range domains { | |||
| 1208 | + | result := sanitizeName(d) | |||
| 1209 | + | if original, exists := results[result]; exists { | |||
| 1210 | + | t.Errorf("COLLISION: %q and %q both sanitize to %q", original, d, result) | |||
| 1211 | + | } | |||
| 1212 | + | results[result] = d | |||
| 1213 | + | } | |||
| 1214 | + | }) | |||
| 1215 | + | } | |||