@@ -707,19 +707,34 @@ func TestDropProcMount(t *testing.T) {
707
707
708
708
func TestDropAppArmor (t * testing.T ) {
709
709
tests := []struct {
710
- description string
711
- hasAppArmor bool
712
- pod api.Pod
710
+ description string
711
+ hasAnnotations bool
712
+ hasFields bool
713
+ pod api.Pod
713
714
}{{
714
- description : "with AppArmor Annotations" ,
715
- hasAppArmor : true ,
715
+ description : "with AppArmor Annotations" ,
716
+ hasAnnotations : true ,
716
717
pod : api.Pod {
717
718
ObjectMeta : metav1.ObjectMeta {Annotations : map [string ]string {"a" : "1" , v1 .DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + "foo" : "default" }},
718
719
Spec : api.PodSpec {},
719
720
},
721
+ }, {
722
+ description : "with AppArmor Annotations & fields" ,
723
+ hasAnnotations : true ,
724
+ hasFields : true ,
725
+ pod : api.Pod {
726
+ ObjectMeta : metav1.ObjectMeta {Annotations : map [string ]string {"a" : "1" , v1 .DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + "foo" : "default" }},
727
+ Spec : api.PodSpec {
728
+ SecurityContext : & api.PodSecurityContext {
729
+ AppArmorProfile : & api.AppArmorProfile {
730
+ Type : api .AppArmorProfileTypeRuntimeDefault ,
731
+ },
732
+ },
733
+ },
734
+ },
720
735
}, {
721
736
description : "with pod AppArmor profile" ,
722
- hasAppArmor : true ,
737
+ hasFields : true ,
723
738
pod : api.Pod {
724
739
ObjectMeta : metav1.ObjectMeta {Annotations : map [string ]string {"a" : "1" }},
725
740
Spec : api.PodSpec {
@@ -732,7 +747,7 @@ func TestDropAppArmor(t *testing.T) {
732
747
},
733
748
}, {
734
749
description : "with container AppArmor profile" ,
735
- hasAppArmor : true ,
750
+ hasFields : true ,
736
751
pod : api.Pod {
737
752
ObjectMeta : metav1.ObjectMeta {Annotations : map [string ]string {"a" : "1" }},
738
753
Spec : api.PodSpec {
@@ -747,7 +762,6 @@ func TestDropAppArmor(t *testing.T) {
747
762
},
748
763
}, {
749
764
description : "without AppArmor" ,
750
- hasAppArmor : false ,
751
765
pod : api.Pod {
752
766
ObjectMeta : metav1.ObjectMeta {Annotations : map [string ]string {"a" : "1" }},
753
767
Spec : api.PodSpec {},
@@ -756,34 +770,43 @@ func TestDropAppArmor(t *testing.T) {
756
770
757
771
for _ , test := range tests {
758
772
for _ , enabled := range []bool {true , false } {
759
- t .Run (fmt .Sprintf ("%v/enabled=%v" , test .description , enabled ), func (t * testing.T ) {
760
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .AppArmor , enabled )()
773
+ for _ , fieldsEnabled := range []bool {true , false } {
774
+ t .Run (fmt .Sprintf ("%v/enabled=%v/fields=%v" , test .description , enabled , fieldsEnabled ), func (t * testing.T ) {
775
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .AppArmor , enabled )()
776
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .AppArmorFields , fieldsEnabled )()
761
777
762
- newPod := test .pod .DeepCopy ()
778
+ newPod := test .pod .DeepCopy ()
763
779
764
- if actual := appArmorInUse (newPod .Annotations , & newPod .Spec ); actual != test .hasAppArmor {
765
- t .Errorf ("appArmorInUse does not match expectation: %t != %t" , actual , test .hasAppArmor )
766
- }
780
+ if hasAnnotations := appArmorAnnotationsInUse (newPod .Annotations ); hasAnnotations != test .hasAnnotations {
781
+ t .Errorf ("appArmorAnnotationsInUse does not match expectation: %t != %t" , hasAnnotations , test .hasAnnotations )
782
+ }
783
+ if hasFields := appArmorFieldsInUse (& newPod .Spec ); hasFields != test .hasFields {
784
+ t .Errorf ("appArmorFieldsInUse does not match expectation: %t != %t" , hasFields , test .hasFields )
785
+ }
767
786
768
- DropDisabledPodFields (newPod , newPod )
769
- require .Equal (t , & test .pod , newPod , "unchanged pod should never be mutated" )
787
+ DropDisabledPodFields (newPod , newPod )
788
+ require .Equal (t , & test .pod , newPod , "unchanged pod should never be mutated" )
770
789
771
- DropDisabledPodFields (newPod , nil )
790
+ DropDisabledPodFields (newPod , nil )
772
791
773
- if enabled {
774
- assert .Equal (t , & test .pod , newPod , "pod should not be mutated when AppArmor is enabled" )
775
- } else {
776
- if appArmorInUse (newPod .Annotations , & newPod .Spec ) {
777
- t .Errorf ("newPod should not be using appArmor after dropping disabled fields" )
792
+ if enabled && fieldsEnabled {
793
+ assert .Equal (t , & test .pod , newPod , "pod should not be mutated when both feature gates are enabled" )
794
+ return
778
795
}
779
796
780
- if test .hasAppArmor {
781
- assert .NotEqual (t , & test . pod , newPod , "pod should be mutated to drop AppArmor " )
782
- } else {
783
- assert .Equal (t , & test .pod , newPod , "pod without AppArmor should not be mutated" )
797
+ expectAnnotations := test .hasAnnotations && enabled
798
+ assert .Equal (t , expectAnnotations , appArmorAnnotationsInUse ( newPod . Annotations ) , "AppArmor annotations expectation " )
799
+ if expectAnnotations == test . hasAnnotations {
800
+ assert .Equal (t , test .pod . Annotations , newPod . Annotations , "annotations should not be mutated" )
784
801
}
785
- }
786
- })
802
+
803
+ expectFields := test .hasFields && enabled && fieldsEnabled
804
+ assert .Equal (t , expectFields , appArmorFieldsInUse (& newPod .Spec ), "AppArmor fields expectation" )
805
+ if expectFields == test .hasFields {
806
+ assert .Equal (t , & test .pod .Spec , & newPod .Spec , "PodSpec should not be mutated" )
807
+ }
808
+ })
809
+ }
787
810
}
788
811
}
789
812
}
0 commit comments