@@ -239,6 +239,108 @@ func TestLabelKey(t *testing.T) {
239239 }
240240}
241241
242+ func TestPrefixedLabelKey (t * testing.T ) {
243+ ctx := context .Background ()
244+ fldPath := field .NewPath ("test" )
245+
246+ testCases := []struct {
247+ name string
248+ input string
249+ wantErrs field.ErrorList
250+ }{{
251+ name : "valid key" ,
252+ input : "app" ,
253+ wantErrs : field.ErrorList {
254+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
255+ },
256+ }, {
257+ name : "valid key with dash" ,
258+ input : "app-name" ,
259+ wantErrs : field.ErrorList {
260+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
261+ },
262+ }, {
263+ name : "valid key with dot" ,
264+ input : "app.name" ,
265+ wantErrs : field.ErrorList {
266+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
267+ },
268+ }, {
269+ name : "valid key with underscore" ,
270+ input : "app_name" ,
271+ wantErrs : field.ErrorList {
272+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
273+ },
274+ }, {
275+ name : "valid key with prefix" ,
276+ input : "example.com/app" ,
277+ wantErrs : nil ,
278+ }, {
279+ name : "valid key with long prefix" ,
280+ input : strings .Repeat ("a" , 63 ) + "." + strings .Repeat ("b" , 63 ) + "." + strings .Repeat ("c" , 63 ) + "." + strings .Repeat ("d" , 55 ) + "/app" ,
281+ wantErrs : nil ,
282+ }, {
283+ name : "invalid: empty string" ,
284+ input : "" ,
285+ wantErrs : field.ErrorList {
286+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
287+ },
288+ }, {
289+ name : "invalid: starts with dash" ,
290+ input : "-app" ,
291+ wantErrs : field.ErrorList {
292+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
293+ },
294+ }, {
295+ name : "invalid: ends with dash" ,
296+ input : "app-" ,
297+ wantErrs : field.ErrorList {
298+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
299+ },
300+ }, {
301+ name : "invalid: contains invalid characters" ,
302+ input : "app^" ,
303+ wantErrs : field.ErrorList {
304+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
305+ },
306+ }, {
307+ name : "invalid: name too long" ,
308+ input : strings .Repeat ("a" , 64 ),
309+ wantErrs : field.ErrorList {
310+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
311+ },
312+ }, {
313+ name : "invalid: prefix too long" ,
314+ input : strings .Repeat ("a" , 254 ) + "/app" ,
315+ wantErrs : field.ErrorList {
316+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
317+ },
318+ }, {
319+ name : "invalid: prefix is not a DNS subdomain" ,
320+ input : "example-.com/app" ,
321+ wantErrs : field.ErrorList {
322+ field .Invalid (fldPath , nil , "" ).WithOrigin ("format=k8s-prefixed-label-key" ),
323+ },
324+ }, {
325+ name : "nil value" ,
326+ input : "" , // This will be handled by setting value to nil in the test runner
327+ wantErrs : nil ,
328+ }}
329+
330+ matcher := field.ErrorMatcher {}.ByType ().ByField ().ByOrigin ()
331+ for _ , tc := range testCases {
332+ t .Run (tc .name , func (t * testing.T ) {
333+ var value * string
334+ if tc .name != "nil value" {
335+ v := tc .input
336+ value = & v
337+ }
338+ gotErrs := PrefixedLabelKey (ctx , operation.Operation {}, fldPath , value , nil )
339+ matcher .Test (t , tc .wantErrs , gotErrs )
340+ })
341+ }
342+ }
343+
242344func TestK8sUUID (t * testing.T ) {
243345 ctx := context .Background ()
244346 fldPath := field .NewPath ("test" )
0 commit comments