|
| 1 | +// Copyright 2017 - 2025 Crunchy Data Solutions, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package util |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "gotest.tools/v3/assert" |
| 11 | + |
| 12 | + "github.com/crunchydata/postgres-operator/internal/testing/cmp" |
| 13 | +) |
| 14 | + |
| 15 | +func TestPodSecurityContext(t *testing.T) { |
| 16 | + t.Run("Non-Openshift", func(t *testing.T) { |
| 17 | + assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(2, []int64{}), ` |
| 18 | +fsGroup: 2 |
| 19 | +fsGroupChangePolicy: OnRootMismatch |
| 20 | + `)) |
| 21 | + |
| 22 | + supplementalGroups := []int64{3, 4} |
| 23 | + assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(26, supplementalGroups), ` |
| 24 | +fsGroup: 26 |
| 25 | +fsGroupChangePolicy: OnRootMismatch |
| 26 | +supplementalGroups: |
| 27 | +- 3 |
| 28 | +- 4 |
| 29 | + `)) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("OpenShift", func(t *testing.T) { |
| 33 | + assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(0, []int64{}), |
| 34 | + `fsGroupChangePolicy: OnRootMismatch`)) |
| 35 | + |
| 36 | + supplementalGroups := []int64{3, 4} |
| 37 | + assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(0, supplementalGroups), ` |
| 38 | +fsGroupChangePolicy: OnRootMismatch |
| 39 | +supplementalGroups: |
| 40 | +- 3 |
| 41 | +- 4 |
| 42 | + `)) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("NoRootGID", func(t *testing.T) { |
| 46 | + supplementalGroups := []int64{999, 0, 100, 0} |
| 47 | + assert.DeepEqual(t, []int64{999, 100}, PodSecurityContext(2, supplementalGroups).SupplementalGroups) |
| 48 | + |
| 49 | + supplementalGroups = []int64{0} |
| 50 | + assert.Assert(t, PodSecurityContext(2, supplementalGroups).SupplementalGroups == nil) |
| 51 | + }) |
| 52 | +} |
0 commit comments