2019-01-03 10:12:12 +00:00
|
|
|
package oci
|
|
|
|
|
|
|
|
import (
|
2020-07-27 17:56:33 +00:00
|
|
|
"runtime"
|
2019-01-03 10:12:12 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-07-28 07:11:42 +00:00
|
|
|
"github.com/containerd/containerd/oci"
|
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2020-07-27 17:48:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-03 10:12:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHasPrefix(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
path string
|
|
|
|
prefix string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
path: "/foo/bar",
|
|
|
|
prefix: "/foo",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/foo/bar",
|
|
|
|
prefix: "/foo/",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/foo/bar",
|
|
|
|
prefix: "/",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/foo",
|
|
|
|
prefix: "/foo",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/foo/bar",
|
|
|
|
prefix: "/bar",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/foo/bar",
|
|
|
|
prefix: "foo",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/foobar",
|
|
|
|
prefix: "/foo",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
2020-07-27 17:56:33 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
testCases = append(testCases,
|
|
|
|
testCase{
|
|
|
|
path: "C:\\foo\\bar",
|
|
|
|
prefix: "C:\\foo",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
testCase{
|
|
|
|
path: "C:\\foo\\bar",
|
|
|
|
prefix: "C:\\foo\\",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
testCase{
|
|
|
|
path: "C:\\foo\\bar",
|
|
|
|
prefix: "C:\\",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
testCase{
|
|
|
|
path: "C:\\foo",
|
|
|
|
prefix: "C:\\foo",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
testCase{
|
|
|
|
path: "C:\\foo\\bar",
|
|
|
|
prefix: "C:\\bar",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
testCase{
|
|
|
|
path: "C:\\foo\\bar",
|
|
|
|
prefix: "foo",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
testCase{
|
|
|
|
path: "C:\\foobar",
|
|
|
|
prefix: "C:\\foo",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2019-01-03 10:12:12 +00:00
|
|
|
for i, tc := range testCases {
|
|
|
|
actual := hasPrefix(tc.path, tc.prefix)
|
2020-07-27 17:48:02 +00:00
|
|
|
assert.Equal(t, tc.expected, actual, "#%d: under(%q,%q)", i, tc.path, tc.prefix)
|
2019-01-03 10:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 07:11:42 +00:00
|
|
|
|
|
|
|
func TestWithRemovedMounts(t *testing.T) {
|
|
|
|
// The default mount-list from containerd
|
|
|
|
s := oci.Spec{
|
|
|
|
Mounts: []specs.Mount{
|
|
|
|
{
|
|
|
|
Destination: "/proc",
|
|
|
|
Type: "proc",
|
|
|
|
Source: "proc",
|
|
|
|
Options: []string{"nosuid", "noexec", "nodev"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/dev",
|
|
|
|
Type: "tmpfs",
|
|
|
|
Source: "tmpfs",
|
|
|
|
Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/dev/pts",
|
|
|
|
Type: "devpts",
|
|
|
|
Source: "devpts",
|
|
|
|
Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/dev/shm",
|
|
|
|
Type: "tmpfs",
|
|
|
|
Source: "shm",
|
|
|
|
Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/dev/mqueue",
|
|
|
|
Type: "mqueue",
|
|
|
|
Source: "mqueue",
|
|
|
|
Options: []string{"nosuid", "noexec", "nodev"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/sys",
|
|
|
|
Type: "sysfs",
|
|
|
|
Source: "sysfs",
|
|
|
|
Options: []string{"nosuid", "noexec", "nodev", "ro"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/run",
|
|
|
|
Type: "tmpfs",
|
|
|
|
Source: "tmpfs",
|
|
|
|
Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
oldLen := len(s.Mounts)
|
|
|
|
err := withRemovedMount("/run")(appcontext.Context(), nil, nil, &s)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, oldLen-1, len(s.Mounts))
|
|
|
|
}
|