|
4 | 4 | "testing" |
5 | 5 |
|
6 | 6 | operatorv1 "github.com/openshift/api/operator/v1" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | func TestCondition(t *testing.T) { |
@@ -62,8 +64,184 @@ func TestCondition(t *testing.T) { |
62 | 64 | } |
63 | 65 | }) |
64 | 66 |
|
65 | | - t.Run("without anything", func(t *testing.T) { |
66 | | - cond := podSecurityOperatorConditions{} |
67 | | - cond.addViolation("hello world") |
68 | | - }) |
| 67 | +} |
| 68 | + |
| 69 | +func TestOperatorStatus(t *testing.T) { |
| 70 | + for _, tt := range []struct { |
| 71 | + name string |
| 72 | + namespace []*corev1.Namespace |
| 73 | + expected map[string]operatorv1.ConditionStatus |
| 74 | + }{ |
| 75 | + { |
| 76 | + name: "with default namespace", |
| 77 | + namespace: []*corev1.Namespace{ |
| 78 | + { |
| 79 | + ObjectMeta: metav1.ObjectMeta{ |
| 80 | + Name: "syncer-by-default", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + expected: map[string]operatorv1.ConditionStatus{ |
| 85 | + "PodSecurityCustomerEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 86 | + "PodSecurityOpenshiftEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 87 | + "PodSecurityRunLevelZeroEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 88 | + "PodSecurityDisabledSyncerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "with customer disabled syncer", |
| 93 | + namespace: []*corev1.Namespace{ |
| 94 | + { |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Name: "syncer-no-thx", |
| 97 | + Labels: map[string]string{ |
| 98 | + "security.openshift.io/scc.podSecurityLabelSync": "false", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + expected: map[string]operatorv1.ConditionStatus{ |
| 104 | + "PodSecurityCustomerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 105 | + "PodSecurityOpenshiftEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 106 | + "PodSecurityRunLevelZeroEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 107 | + "PodSecurityDisabledSyncerEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "with customer re-enabled syncer", |
| 112 | + namespace: []*corev1.Namespace{ |
| 113 | + { |
| 114 | + ObjectMeta: metav1.ObjectMeta{ |
| 115 | + Name: "syncer-yes-plz", |
| 116 | + Labels: map[string]string{ |
| 117 | + "security.openshift.io/scc.podSecurityLabelSync": "true", |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + expected: map[string]operatorv1.ConditionStatus{ |
| 123 | + "PodSecurityCustomerEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 124 | + "PodSecurityOpenshiftEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 125 | + "PodSecurityRunLevelZeroEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 126 | + "PodSecurityDisabledSyncerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "with openshift namespace", |
| 131 | + namespace: []*corev1.Namespace{ |
| 132 | + { |
| 133 | + ObjectMeta: metav1.ObjectMeta{ |
| 134 | + Name: "openshift-fail", |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + expected: map[string]operatorv1.ConditionStatus{ |
| 139 | + "PodSecurityCustomerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 140 | + "PodSecurityOpenshiftEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 141 | + "PodSecurityRunLevelZeroEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 142 | + "PodSecurityDisabledSyncerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "with run-level 0 namespace", |
| 147 | + namespace: []*corev1.Namespace{ |
| 148 | + { |
| 149 | + ObjectMeta: metav1.ObjectMeta{ |
| 150 | + Name: "kube-system", |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + expected: map[string]operatorv1.ConditionStatus{ |
| 155 | + "PodSecurityCustomerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 156 | + "PodSecurityOpenshiftEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 157 | + "PodSecurityRunLevelZeroEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 158 | + "PodSecurityDisabledSyncerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 159 | + }, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "with other customer types in combination", |
| 163 | + namespace: []*corev1.Namespace{ |
| 164 | + { |
| 165 | + ObjectMeta: metav1.ObjectMeta{ |
| 166 | + Name: "foobar", |
| 167 | + }, |
| 168 | + }, |
| 169 | + { |
| 170 | + ObjectMeta: metav1.ObjectMeta{ |
| 171 | + Name: "foobar", |
| 172 | + Labels: map[string]string{ |
| 173 | + "security.openshift.io/scc.podSecurityLabelSync": "false", |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + expected: map[string]operatorv1.ConditionStatus{ |
| 179 | + "PodSecurityCustomerEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 180 | + "PodSecurityOpenshiftEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 181 | + "PodSecurityRunLevelZeroEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 182 | + "PodSecurityDisabledSyncerEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 183 | + }, |
| 184 | + }, |
| 185 | + { |
| 186 | + name: "with other system types in combination", |
| 187 | + namespace: []*corev1.Namespace{ |
| 188 | + { |
| 189 | + ObjectMeta: metav1.ObjectMeta{ |
| 190 | + Name: "openshift-namespace", |
| 191 | + Labels: map[string]string{ |
| 192 | + "pod-security.kubernetes.io/audit": "restricted", |
| 193 | + "pod-security.kubernetes.io/audit-version": "v1.24", |
| 194 | + "pod-security.kubernetes.io/warn": "restricted", |
| 195 | + "pod-security.kubernetes.io/warn-version": "v1.24", |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + { |
| 200 | + ObjectMeta: metav1.ObjectMeta{ |
| 201 | + Name: "kube-system", |
| 202 | + Labels: map[string]string{}, |
| 203 | + }, |
| 204 | + }, |
| 205 | + }, |
| 206 | + expected: map[string]operatorv1.ConditionStatus{ |
| 207 | + "PodSecurityCustomerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 208 | + "PodSecurityOpenshiftEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 209 | + "PodSecurityRunLevelZeroEvaluationConditionsDetected": operatorv1.ConditionTrue, |
| 210 | + "PodSecurityDisabledSyncerEvaluationConditionsDetected": operatorv1.ConditionFalse, |
| 211 | + }, |
| 212 | + }, |
| 213 | + } { |
| 214 | + tt := tt |
| 215 | + t.Run(tt.name, func(t *testing.T) { |
| 216 | + cond := podSecurityOperatorConditions{} |
| 217 | + |
| 218 | + for _, ns := range tt.namespace { |
| 219 | + cond.addViolation(ns) |
| 220 | + } |
| 221 | + |
| 222 | + status := &operatorv1.OperatorStatus{} |
| 223 | + for _, f := range cond.toConditionFuncs() { |
| 224 | + if err := f(status); err != nil { |
| 225 | + t.Fatalf("unexpected error: %v", err) |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + for expectedType, expectedStatus := range tt.expected { |
| 230 | + found := false |
| 231 | + |
| 232 | + for _, condition := range status.Conditions { |
| 233 | + if condition.Type == expectedType { |
| 234 | + found = true |
| 235 | + if condition.Status != expectedStatus { |
| 236 | + t.Errorf("expected %s to be %v, have %v", expectedType, expectedStatus, condition.Status) |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + if !found { |
| 242 | + t.Errorf("expected condition %s not found", expectedType) |
| 243 | + } |
| 244 | + } |
| 245 | + }) |
| 246 | + } |
69 | 247 | } |
0 commit comments