|
6 | 6 | "github.com/stretchr/testify/require"
|
7 | 7 | corev1 "k8s.io/api/core/v1"
|
8 | 8 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
| 9 | + "k8s.io/utils/pointer" |
9 | 10 |
|
10 | 11 | "github.com/operator-framework/api/pkg/operators/v1alpha1"
|
11 | 12 | )
|
@@ -80,26 +81,85 @@ func TestPullPolicy(t *testing.T) {
|
80 | 81 | }
|
81 | 82 |
|
82 | 83 | func TestPodContainerSecurityContext(t *testing.T) {
|
83 |
| - expectedReadOnlyRootFilesystem := false |
84 |
| - allowPrivilegeEscalation := false |
85 |
| - expectedContainerSecCtx := &corev1.SecurityContext{ |
86 |
| - ReadOnlyRootFilesystem: &expectedReadOnlyRootFilesystem, |
87 |
| - AllowPrivilegeEscalation: &allowPrivilegeEscalation, |
88 |
| - Capabilities: &corev1.Capabilities{ |
89 |
| - Drop: []corev1.Capability{"ALL"}, |
| 84 | + testcases := []struct { |
| 85 | + title string |
| 86 | + inputCatsrc *v1alpha1.CatalogSource |
| 87 | + expectedSecurityContext *corev1.PodSecurityContext |
| 88 | + expectedContainerSecurityContext *corev1.SecurityContext |
| 89 | + }{ |
| 90 | + { |
| 91 | + title: "NoSpecDefined/PodContainsSecurityConfigForPSARestricted", |
| 92 | + inputCatsrc: &v1alpha1.CatalogSource{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: "test", |
| 95 | + Namespace: "testns", |
| 96 | + }, |
| 97 | + }, |
| 98 | + expectedContainerSecurityContext: &corev1.SecurityContext{ |
| 99 | + ReadOnlyRootFilesystem: pointer.Bool(false), |
| 100 | + AllowPrivilegeEscalation: pointer.Bool(false), |
| 101 | + Capabilities: &corev1.Capabilities{ |
| 102 | + Drop: []corev1.Capability{"ALL"}, |
| 103 | + }, |
| 104 | + }, |
| 105 | + expectedSecurityContext: &corev1.PodSecurityContext{ |
| 106 | + SeccompProfile: &corev1.SeccompProfile{Type: corev1.SeccompProfileTypeRuntimeDefault}, |
| 107 | + RunAsUser: pointer.Int64(workloadUserID), |
| 108 | + RunAsNonRoot: pointer.Bool(true), |
| 109 | + }, |
90 | 110 | },
|
91 |
| - } |
92 |
| - |
93 |
| - catsrc := &v1alpha1.CatalogSource{ |
94 |
| - ObjectMeta: metav1.ObjectMeta{ |
95 |
| - Name: "test", |
96 |
| - Namespace: "testns", |
| 111 | + { |
| 112 | + title: "SpecDefined/SecurityContextConfig:Restricted/PodContainsSecurityConfigForPSARestricted", |
| 113 | + inputCatsrc: &v1alpha1.CatalogSource{ |
| 114 | + ObjectMeta: metav1.ObjectMeta{ |
| 115 | + Name: "test", |
| 116 | + Namespace: "testns", |
| 117 | + }, |
| 118 | + Spec: v1alpha1.CatalogSourceSpec{ |
| 119 | + GrpcPodConfig: &v1alpha1.GrpcPodConfig{ |
| 120 | + SecurityContextConfig: v1alpha1.Restricted, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + expectedContainerSecurityContext: &corev1.SecurityContext{ |
| 125 | + ReadOnlyRootFilesystem: pointer.Bool(false), |
| 126 | + AllowPrivilegeEscalation: pointer.Bool(false), |
| 127 | + Capabilities: &corev1.Capabilities{ |
| 128 | + Drop: []corev1.Capability{"ALL"}, |
| 129 | + }, |
| 130 | + }, |
| 131 | + expectedSecurityContext: &corev1.PodSecurityContext{ |
| 132 | + SeccompProfile: &corev1.SeccompProfile{Type: corev1.SeccompProfileTypeRuntimeDefault}, |
| 133 | + RunAsUser: pointer.Int64(workloadUserID), |
| 134 | + RunAsNonRoot: pointer.Bool(true), |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + title: "SpecDefined/SecurityContextConfig:Legacy/PodDoesNotContainsSecurityConfig", |
| 139 | + inputCatsrc: &v1alpha1.CatalogSource{ |
| 140 | + ObjectMeta: metav1.ObjectMeta{ |
| 141 | + Name: "test", |
| 142 | + Namespace: "testns", |
| 143 | + }, |
| 144 | + Spec: v1alpha1.CatalogSourceSpec{ |
| 145 | + GrpcPodConfig: &v1alpha1.GrpcPodConfig{ |
| 146 | + SecurityContextConfig: v1alpha1.Legacy, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + expectedContainerSecurityContext: nil, |
| 151 | + expectedSecurityContext: nil, |
97 | 152 | },
|
98 | 153 | }
|
99 |
| - |
100 |
| - gotPod := Pod(catsrc, "hello", "busybox", "", map[string]string{}, map[string]string{}, int32(0), int32(0), int64(workloadUserID)) |
101 |
| - gotContainerSecCtx := gotPod.Spec.Containers[0].SecurityContext |
102 |
| - require.Equal(t, expectedContainerSecCtx, gotContainerSecCtx) |
| 154 | + for _, testcase := range testcases { |
| 155 | + outputPod := Pod(testcase.inputCatsrc, "hello", "busybox", "", map[string]string{}, map[string]string{}, int32(0), int32(0), int64(workloadUserID)) |
| 156 | + if testcase.expectedSecurityContext != nil { |
| 157 | + require.Equal(t, testcase.expectedSecurityContext, outputPod.Spec.SecurityContext) |
| 158 | + } |
| 159 | + if testcase.expectedContainerSecurityContext != nil { |
| 160 | + require.Equal(t, testcase.expectedContainerSecurityContext, outputPod.Spec.Containers[0].SecurityContext) |
| 161 | + } |
| 162 | + } |
103 | 163 | }
|
104 | 164 |
|
105 | 165 | // TestPodAvoidsConcurrentWrite is a regression test for
|
|
0 commit comments