|
| 1 | +/* |
| 2 | +Copyright 2026 Red Hat |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package pod |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | +) |
| 24 | + |
| 25 | +func TestRestrictiveSecurityContext(t *testing.T) { |
| 26 | + var uid int64 = 42457 |
| 27 | + sc := RestrictiveSecurityContext(uid) |
| 28 | + |
| 29 | + if sc.RunAsUser == nil || *sc.RunAsUser != uid { |
| 30 | + t.Errorf("expected RunAsUser %d, got %v", uid, sc.RunAsUser) |
| 31 | + } |
| 32 | + if sc.RunAsGroup == nil || *sc.RunAsGroup != uid { |
| 33 | + t.Errorf("expected RunAsGroup %d, got %v", uid, sc.RunAsGroup) |
| 34 | + } |
| 35 | + if sc.RunAsNonRoot == nil || !*sc.RunAsNonRoot { |
| 36 | + t.Error("expected RunAsNonRoot true") |
| 37 | + } |
| 38 | + if sc.AllowPrivilegeEscalation == nil || *sc.AllowPrivilegeEscalation { |
| 39 | + t.Error("expected AllowPrivilegeEscalation false") |
| 40 | + } |
| 41 | + if sc.Capabilities == nil || len(sc.Capabilities.Drop) != 1 || sc.Capabilities.Drop[0] != "ALL" { |
| 42 | + t.Errorf("expected Capabilities.Drop [ALL], got %v", sc.Capabilities) |
| 43 | + } |
| 44 | + if sc.SeccompProfile == nil || sc.SeccompProfile.Type != corev1.SeccompProfileTypeRuntimeDefault { |
| 45 | + t.Errorf("expected SeccompProfile RuntimeDefault, got %v", sc.SeccompProfile) |
| 46 | + } |
| 47 | + if sc.ReadOnlyRootFilesystem == nil || !*sc.ReadOnlyRootFilesystem { |
| 48 | + t.Error("expected ReadOnlyRootFilesystem true") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestRestrictiveSecurityContextWithGID(t *testing.T) { |
| 53 | + var uid int64 = 42415 |
| 54 | + var gid int64 = 42416 |
| 55 | + sc := RestrictiveSecurityContextWithGID(uid, gid) |
| 56 | + |
| 57 | + if sc.RunAsUser == nil || *sc.RunAsUser != uid { |
| 58 | + t.Errorf("expected RunAsUser %d, got %v", uid, sc.RunAsUser) |
| 59 | + } |
| 60 | + if sc.RunAsGroup == nil || *sc.RunAsGroup != gid { |
| 61 | + t.Errorf("expected RunAsGroup %d, got %v", gid, sc.RunAsGroup) |
| 62 | + } |
| 63 | + if sc.RunAsNonRoot == nil || !*sc.RunAsNonRoot { |
| 64 | + t.Error("expected RunAsNonRoot true") |
| 65 | + } |
| 66 | + if sc.AllowPrivilegeEscalation == nil || *sc.AllowPrivilegeEscalation { |
| 67 | + t.Error("expected AllowPrivilegeEscalation false") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestRestrictiveSecurityContextNoAddCaps(t *testing.T) { |
| 72 | + sc := RestrictiveSecurityContext(42457) |
| 73 | + if sc.Capabilities.Add != nil { |
| 74 | + t.Errorf("expected no Add capabilities, got %v", sc.Capabilities.Add) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestRestrictiveSecurityContextWithAddCaps(t *testing.T) { |
| 79 | + sc := RestrictiveSecurityContext(42457, "NET_BIND_SERVICE", "CHOWN") |
| 80 | + |
| 81 | + if len(sc.Capabilities.Drop) != 1 || sc.Capabilities.Drop[0] != "ALL" { |
| 82 | + t.Errorf("expected Drop [ALL], got %v", sc.Capabilities.Drop) |
| 83 | + } |
| 84 | + if len(sc.Capabilities.Add) != 2 { |
| 85 | + t.Fatalf("expected 2 Add capabilities, got %d", len(sc.Capabilities.Add)) |
| 86 | + } |
| 87 | + if sc.Capabilities.Add[0] != "NET_BIND_SERVICE" || sc.Capabilities.Add[1] != "CHOWN" { |
| 88 | + t.Errorf("expected Add [NET_BIND_SERVICE CHOWN], got %v", sc.Capabilities.Add) |
| 89 | + } |
| 90 | +} |
0 commit comments