|
| 1 | +//go:build !remote && (linux || freebsd) |
| 2 | + |
| 3 | +package generate |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/containers/podman/v6/libpod" |
| 9 | + "github.com/containers/podman/v6/pkg/specgen" |
| 10 | + spec "github.com/opencontainers/runtime-spec/specs-go" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestApplyInfraInheritMountOptionsDoNotLeak verifies that mount options from |
| 16 | +// one mount do not leak into another when calling applyInfraInherit. |
| 17 | +func TestApplyInfraInheritMountOptionsDoNotLeak(t *testing.T) { |
| 18 | + compatibleOptions := &libpod.InfraInherit{ |
| 19 | + Mounts: []spec.Mount{ |
| 20 | + {Destination: "/mylog", Source: "/a", Type: "bind"}, |
| 21 | + {Destination: "/mytmp", Source: "/b", Type: "bind", Options: []string{"ro"}}, |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + s := &specgen.SpecGenerator{} |
| 26 | + s.Mounts = []spec.Mount{ |
| 27 | + {Destination: "/mytmp", Source: "/b", Type: "bind", Options: []string{"ro"}}, |
| 28 | + {Destination: "/mylog", Source: "/a", Type: "bind"}, |
| 29 | + } |
| 30 | + |
| 31 | + err := applyInfraInherit(compatibleOptions, s) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + for _, m := range s.Mounts { |
| 35 | + if m.Destination == "/mylog" { |
| 36 | + assert.Empty(t, m.Options, |
| 37 | + "/mylog should have no options; ro from /mytmp leaked") |
| 38 | + } |
| 39 | + if m.Destination == "/mytmp" { |
| 40 | + assert.Equal(t, []string{"ro"}, m.Options, |
| 41 | + "/mytmp should keep its ro option") |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// TestApplyInfraInheritDoesNotOverwriteSeccomp verifies that applyInfraInherit |
| 47 | +// does not overwrite a pre-set SeccompProfilePath when the infra container has |
| 48 | +// no seccomp profile (empty string). |
| 49 | +func TestApplyInfraInheritDoesNotOverwriteSeccomp(t *testing.T) { |
| 50 | + compatibleOptions := &libpod.InfraInherit{} |
| 51 | + |
| 52 | + s := &specgen.SpecGenerator{} |
| 53 | + s.SeccompProfilePath = "localhost/seccomp.json" |
| 54 | + |
| 55 | + err := applyInfraInherit(compatibleOptions, s) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + assert.Equal(t, "localhost/seccomp.json", s.SeccompProfilePath, |
| 59 | + "SeccompProfilePath should not be overwritten by empty infra value") |
| 60 | +} |
0 commit comments