@@ -299,3 +299,215 @@ func TestEnsureStringSlicesAreEquivalent(t *testing.T) {
299299 })
300300 }
301301}
302+
303+ func TestValidateZeroTransitionPtr (t * testing.T ) {
304+ testPath := field .NewPath ("Spec" , "Foo" )
305+
306+ tests := []struct {
307+ name string
308+ input1 * bool
309+ input2 * bool
310+ expectedOutput * field.Error
311+ }{
312+ {
313+ name : "nil" ,
314+ input1 : nil ,
315+ input2 : nil ,
316+ },
317+ {
318+ name : "no change" ,
319+ input1 : pointer .Bool (true ),
320+ input2 : pointer .Bool (true ),
321+ },
322+ {
323+ name : "can unset" ,
324+ input1 : pointer .Bool (true ),
325+ input2 : nil ,
326+ },
327+ {
328+ name : "can't set from empty" ,
329+ input1 : nil ,
330+ input2 : pointer .Bool (true ),
331+ expectedOutput : field .Invalid (testPath , nil , setMessage ),
332+ },
333+ {
334+ name : "can't change" ,
335+ input1 : pointer .Bool (true ),
336+ input2 : pointer .Bool (false ),
337+ expectedOutput : field .Invalid (testPath , nil , immutableMessage ),
338+ },
339+ }
340+ for _ , tc := range tests {
341+ t .Run (tc .name , func (t * testing.T ) {
342+ g := NewWithT (t )
343+ err := ValidateZeroTransition (testPath , tc .input1 , tc .input2 )
344+ if tc .expectedOutput != nil {
345+ g .Expect (err ).To (HaveOccurred ())
346+ g .Expect (err .Detail ).To (Equal (tc .expectedOutput .Detail ))
347+ g .Expect (err .Type ).To (Equal (tc .expectedOutput .Type ))
348+ g .Expect (err .Field ).To (Equal (tc .expectedOutput .Field ))
349+ } else {
350+ g .Expect (err ).NotTo (HaveOccurred ())
351+ }
352+ })
353+ }
354+ }
355+
356+ func TestValidateZeroTransitionString (t * testing.T ) {
357+ testPath := field .NewPath ("Spec" , "Foo" )
358+
359+ tests := []struct {
360+ name string
361+ input1 string
362+ input2 string
363+ expectedOutput * field.Error
364+ }{
365+ {
366+ name : "empty string" ,
367+ input1 : "" ,
368+ input2 : "" ,
369+ },
370+ {
371+ name : "no change" ,
372+ input1 : "foo" ,
373+ input2 : "foo" ,
374+ },
375+ {
376+ name : "can unset" ,
377+ input1 : "foo" ,
378+ input2 : "" ,
379+ },
380+ {
381+ name : "can't set from empty" ,
382+ input1 : "" ,
383+ input2 : "foo" ,
384+ expectedOutput : field .Invalid (testPath , nil , setMessage ),
385+ },
386+ {
387+ name : "can't change" ,
388+ input1 : "foo" ,
389+ input2 : "bar" ,
390+ expectedOutput : field .Invalid (testPath , nil , immutableMessage ),
391+ },
392+ }
393+ for _ , tc := range tests {
394+ t .Run (tc .name , func (t * testing.T ) {
395+ g := NewWithT (t )
396+ err := ValidateZeroTransition (testPath , tc .input1 , tc .input2 )
397+ if tc .expectedOutput != nil {
398+ g .Expect (err ).To (HaveOccurred ())
399+ g .Expect (err .Detail ).To (Equal (tc .expectedOutput .Detail ))
400+ g .Expect (err .Type ).To (Equal (tc .expectedOutput .Type ))
401+ g .Expect (err .Field ).To (Equal (tc .expectedOutput .Field ))
402+ } else {
403+ g .Expect (err ).NotTo (HaveOccurred ())
404+ }
405+ })
406+ }
407+ }
408+
409+ func TestValidateZeroTransitionStringPtr (t * testing.T ) {
410+ testPath := field .NewPath ("Spec" , "Foo" )
411+
412+ tests := []struct {
413+ name string
414+ input1 * string
415+ input2 * string
416+ expectedOutput * field.Error
417+ }{
418+ {
419+ name : "nil" ,
420+ input1 : nil ,
421+ input2 : nil ,
422+ },
423+ {
424+ name : "no change" ,
425+ input1 : pointer .String ("foo" ),
426+ input2 : pointer .String ("foo" ),
427+ },
428+ {
429+ name : "can unset" ,
430+ input1 : pointer .String ("foo" ),
431+ input2 : nil ,
432+ },
433+ {
434+ name : "can't set from empty" ,
435+ input1 : nil ,
436+ input2 : pointer .String ("foo" ),
437+ expectedOutput : field .Invalid (testPath , nil , setMessage ),
438+ },
439+ {
440+ name : "can't change" ,
441+ input1 : pointer .String ("foo" ),
442+ input2 : pointer .String ("bar" ),
443+ expectedOutput : field .Invalid (testPath , nil , immutableMessage ),
444+ },
445+ }
446+ for _ , tc := range tests {
447+ t .Run (tc .name , func (t * testing.T ) {
448+ g := NewWithT (t )
449+ err := ValidateZeroTransition (testPath , tc .input1 , tc .input2 )
450+ if tc .expectedOutput != nil {
451+ g .Expect (err ).To (HaveOccurred ())
452+ g .Expect (err .Detail ).To (Equal (tc .expectedOutput .Detail ))
453+ g .Expect (err .Type ).To (Equal (tc .expectedOutput .Type ))
454+ g .Expect (err .Field ).To (Equal (tc .expectedOutput .Field ))
455+ } else {
456+ g .Expect (err ).NotTo (HaveOccurred ())
457+ }
458+ })
459+ }
460+ }
461+
462+ func TestValidateZeroTransitionInt32 (t * testing.T ) {
463+ testPath := field .NewPath ("Spec" , "Foo" )
464+
465+ tests := []struct {
466+ name string
467+ input1 int32
468+ input2 int32
469+ expectedOutput * field.Error
470+ }{
471+ {
472+ name : "unset" ,
473+ input1 : 0 ,
474+ input2 : 0 ,
475+ },
476+ {
477+ name : "no change" ,
478+ input1 : 5 ,
479+ input2 : 5 ,
480+ },
481+ {
482+ name : "can unset" ,
483+ input1 : 5 ,
484+ input2 : 0 ,
485+ },
486+ {
487+ name : "can't set from empty" ,
488+ input1 : 0 ,
489+ input2 : 5 ,
490+ expectedOutput : field .Invalid (testPath , nil , setMessage ),
491+ },
492+ {
493+ name : "can't change" ,
494+ input1 : 5 ,
495+ input2 : 6 ,
496+ expectedOutput : field .Invalid (testPath , nil , immutableMessage ),
497+ },
498+ }
499+ for _ , tc := range tests {
500+ t .Run (tc .name , func (t * testing.T ) {
501+ g := NewWithT (t )
502+ err := ValidateZeroTransition (testPath , tc .input1 , tc .input2 )
503+ if tc .expectedOutput != nil {
504+ g .Expect (err ).To (HaveOccurred ())
505+ g .Expect (err .Detail ).To (Equal (tc .expectedOutput .Detail ))
506+ g .Expect (err .Type ).To (Equal (tc .expectedOutput .Type ))
507+ g .Expect (err .Field ).To (Equal (tc .expectedOutput .Field ))
508+ } else {
509+ g .Expect (err ).NotTo (HaveOccurred ())
510+ }
511+ })
512+ }
513+ }
0 commit comments