@@ -642,41 +642,83 @@ func TestReconcile_PSSLabels(t *testing.T) {
642642 addKnownTypesToScheme (s )
643643
644644 testCases := []struct {
645- name string
646- namespace string
647- labels map [string ]string
645+ name string
646+ namespace string
647+ initial_labels map [string ]string
648+ expected_labels map [string ]string
648649 }{
649650 {
650- name : "modified valid PSS labels for openshift-gitops ns " ,
651+ name : "openshift-gitops: podSecurityLabelSync absent, valid PSS labels only " ,
651652 namespace : "openshift-gitops" ,
652- labels : map [string ]string {
653+ initial_labels : map [string ]string {
653654 "pod-security.kubernetes.io/enforce" : "privileged" ,
654655 "pod-security.kubernetes.io/enforce-version" : "v1.30" ,
655656 "pod-security.kubernetes.io/audit" : "privileged" ,
656657 "pod-security.kubernetes.io/audit-version" : "v1.29" ,
657658 "pod-security.kubernetes.io/warn" : "privileged" ,
658659 "pod-security.kubernetes.io/warn-version" : "v1.29" ,
659660 },
661+ expected_labels : map [string ]string {
662+ "pod-security.kubernetes.io/enforce" : "privileged" ,
663+ "pod-security.kubernetes.io/enforce-version" : "v1.30" ,
664+ "pod-security.kubernetes.io/audit" : "privileged" ,
665+ "pod-security.kubernetes.io/audit-version" : "v1.29" ,
666+ "pod-security.kubernetes.io/warn" : "privileged" ,
667+ "pod-security.kubernetes.io/warn-version" : "v1.29" ,
668+ PodSecurityLabelSyncLabel : PodSecurityLabelSyncLabelValue ,
669+ },
660670 },
661671 {
662- name : "modified invalid and empty PSS labels for openshift-gitops ns " ,
672+ name : "openshift-gitops: podSecurityLabelSync absent, invalid PSS labels only " ,
663673 namespace : "openshift-gitops" ,
664- labels : map [string ]string {
674+ initial_labels : map [string ]string {
665675 "pod-security.kubernetes.io/enforce" : "invalid" ,
666676 "pod-security.kubernetes.io/enforce-version" : "invalid" ,
667677 "pod-security.kubernetes.io/warn" : "invalid" ,
668678 "pod-security.kubernetes.io/warn-version" : "invalid" ,
669679 },
680+ expected_labels : map [string ]string {
681+ "pod-security.kubernetes.io/enforce" : "invalid" ,
682+ "pod-security.kubernetes.io/enforce-version" : "invalid" ,
683+ "pod-security.kubernetes.io/warn" : "invalid" ,
684+ "pod-security.kubernetes.io/warn-version" : "invalid" ,
685+ PodSecurityLabelSyncLabel : PodSecurityLabelSyncLabelValue ,
686+ },
687+ },
688+ {
689+ name : "openshift-gitops: podSecurityLabelSync wrong value" ,
690+ namespace : "openshift-gitops" ,
691+ initial_labels : map [string ]string {
692+ "openshift.io/cluster-monitoring" : "true" ,
693+ PodSecurityLabelSyncLabel : "false" ,
694+ "pod-security.kubernetes.io/enforce" : "restricted" ,
695+ "pod-security.kubernetes.io/enforce-version" : "v1.29" ,
696+ "pod-security.kubernetes.io/audit" : "restricted" ,
697+ "pod-security.kubernetes.io/audit-version" : "latest" ,
698+ "pod-security.kubernetes.io/warn" : "restricted" ,
699+ "pod-security.kubernetes.io/warn-version" : "latest" ,
700+ },
701+ expected_labels : map [string ]string {
702+ "openshift.io/cluster-monitoring" : "true" ,
703+ PodSecurityLabelSyncLabel : PodSecurityLabelSyncLabelValue ,
704+ "pod-security.kubernetes.io/enforce" : "restricted" ,
705+ "pod-security.kubernetes.io/enforce-version" : "v1.29" ,
706+ "pod-security.kubernetes.io/audit" : "restricted" ,
707+ "pod-security.kubernetes.io/audit-version" : "latest" ,
708+ "pod-security.kubernetes.io/warn" : "restricted" ,
709+ "pod-security.kubernetes.io/warn-version" : "latest" ,
710+ },
711+ },
712+ {
713+ name : "test: user namespace labels unchanged by reconcile (no PSS / no sync)" ,
714+ namespace : "test" ,
715+ initial_labels : map [string ]string {
716+ "openshift.io/cluster-monitoring" : "true" ,
717+ },
718+ expected_labels : map [string ]string {
719+ "openshift.io/cluster-monitoring" : "true" ,
720+ },
670721 },
671- }
672-
673- expected_labels := map [string ]string {
674- "pod-security.kubernetes.io/enforce" : "restricted" ,
675- "pod-security.kubernetes.io/enforce-version" : "v1.29" ,
676- "pod-security.kubernetes.io/audit" : "restricted" ,
677- "pod-security.kubernetes.io/audit-version" : "latest" ,
678- "pod-security.kubernetes.io/warn" : "restricted" ,
679- "pod-security.kubernetes.io/warn-version" : "latest" ,
680722 }
681723
682724 fakeClient := fake .NewFakeClient (util .NewClusterVersion ("4.7.1" ), newGitopsService ())
@@ -704,40 +746,24 @@ func TestReconcile_PSSLabels(t *testing.T) {
704746 _ , err = reconciler .Reconcile (context .TODO (), newRequest ("test" , "test" ))
705747 assertNoError (t , err )
706748
707- // Check if PSS labels are addded to the user defined ns
708- reconciled_ns := & corev1.Namespace {}
709- err = fakeClient .Get (context .TODO (), types.NamespacedName {Name : "test" },
710- reconciled_ns )
711- assertNoError (t , err )
712-
713- for label := range reconciled_ns .Labels {
714- _ , found := expected_labels [label ]
715- // Fail if label is found
716- assert .Check (t , found != true )
717- }
718-
719749 for _ , tc := range testCases {
720- existing_ns := & corev1.Namespace {}
721- assert .NilError (t , fakeClient .Get (context .TODO (), types.NamespacedName {Name : tc .namespace }, existing_ns ), err )
722-
723- // Assign new values, confirm the assignment and update the PSS labels
724- existing_ns .Labels = tc .labels
725- err := fakeClient .Update (context .TODO (), existing_ns )
726- assert .NilError (t , err )
727- assert .NilError (t , fakeClient .Get (context .TODO (), types.NamespacedName {Name : tc .namespace }, existing_ns ), err )
728- assert .DeepEqual (t , existing_ns .Labels , tc .labels )
729-
730- _ , err = reconciler .Reconcile (context .TODO (), newRequest ("test" , "test" ))
731- assertNoError (t , err )
732-
733- assert .NilError (t , fakeClient .Get (context .TODO (), types.NamespacedName {Name : tc .namespace }, reconciled_ns ), err )
734-
735- for key , value := range expected_labels {
736- label , found := reconciled_ns .Labels [key ]
737- // Fail if label is not found, comapre the values with the expected values if found
738- assert .Check (t , found )
739- assert .Equal (t , label , value )
740- }
750+ t .Run (tc .name , func (t * testing.T ) {
751+ ns := & corev1.Namespace {}
752+ assert .NilError (t , fakeClient .Get (context .TODO (), types.NamespacedName {Name : tc .namespace }, ns ))
753+ ns .Labels = tc .initial_labels
754+ assert .NilError (t , fakeClient .Update (context .TODO (), ns ))
755+
756+ _ , err := reconciler .Reconcile (context .TODO (), newRequest ("test" , "test" ))
757+ assertNoError (t , err )
758+
759+ reconciled_ns := & corev1.Namespace {}
760+ assert .NilError (t , fakeClient .Get (context .TODO (), types.NamespacedName {Name : tc .namespace }, reconciled_ns ))
761+ for key , value := range tc .expected_labels {
762+ label , found := reconciled_ns .Labels [key ]
763+ assert .Check (t , found )
764+ assert .Equal (t , label , value )
765+ }
766+ })
741767 }
742768}
743769
0 commit comments