14
"github.com/containers/storage/pkg/idtools"
15
stypes "github.com/containers/storage/types"
16
rspec "github.com/opencontainers/runtime-spec/specs-go"
17
"github.com/stretchr/testify/assert"
20
// hookPath is the path to an example hook executable.
23
func TestParseOptionIDs(t *testing.T) {
24
idMap := []idtools.IDMap{
32
_, err := parseOptionIDs(idMap, "uids=100-200-2")
35
mappings, err := parseOptionIDs(idMap, "100-200-2")
37
assert.NotNil(t, mappings)
39
assert.Equal(t, len(mappings), 1)
41
assert.Equal(t, mappings[0].ContainerID, 100)
42
assert.Equal(t, mappings[0].HostID, 200)
43
assert.Equal(t, mappings[0].Size, 2)
45
mappings, err = parseOptionIDs(idMap, "100-200-2#300-400-5")
47
assert.NotNil(t, mappings)
49
assert.Equal(t, len(mappings), 2)
51
assert.Equal(t, mappings[0].ContainerID, 100)
52
assert.Equal(t, mappings[0].HostID, 200)
53
assert.Equal(t, mappings[0].Size, 2)
55
assert.Equal(t, mappings[1].ContainerID, 300)
56
assert.Equal(t, mappings[1].HostID, 400)
57
assert.Equal(t, mappings[1].Size, 5)
59
mappings, err = parseOptionIDs(idMap, "@100-200-2#@300-400-5")
61
assert.NotNil(t, mappings)
63
assert.Equal(t, len(mappings), 2)
65
assert.Equal(t, mappings[0].ContainerID, 100)
66
assert.Equal(t, mappings[0].HostID, 201)
67
assert.Equal(t, mappings[0].Size, 2)
69
assert.Equal(t, mappings[1].ContainerID, 300)
70
assert.Equal(t, mappings[1].HostID, 401)
71
assert.Equal(t, mappings[1].Size, 5)
73
_, err = parseOptionIDs(idMap, "@10000-20000-2")
76
_, err = parseOptionIDs(idMap, "100-200-3###400-500-6")
80
func TestParseIDMapMountOption(t *testing.T) {
81
uidMap := []idtools.IDMap{
88
gidMap := []idtools.IDMap{
95
options := stypes.IDMappingOptions{
99
uids, gids, err := parseIDMapMountOption(options, "idmap")
101
assert.Equal(t, len(uids), 1)
102
assert.Equal(t, len(gids), 1)
104
assert.Equal(t, uids[0].HostID, uint32(1000))
105
assert.Equal(t, uids[0].ContainerID, uint32(0))
106
assert.Equal(t, uids[0].Size, uint32(10000))
108
assert.Equal(t, gids[0].HostID, uint32(2000))
109
assert.Equal(t, gids[0].ContainerID, uint32(0))
110
assert.Equal(t, gids[0].Size, uint32(10000))
112
uids, gids, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10")
114
assert.Equal(t, len(uids), 2)
115
assert.Equal(t, len(gids), 1)
117
assert.Equal(t, uids[0].HostID, uint32(1))
118
assert.Equal(t, uids[0].ContainerID, uint32(0))
119
assert.Equal(t, uids[0].Size, uint32(10))
121
assert.Equal(t, uids[1].HostID, uint32(11))
122
assert.Equal(t, uids[1].ContainerID, uint32(10))
123
assert.Equal(t, uids[1].Size, uint32(10))
125
assert.Equal(t, gids[0].HostID, uint32(3))
126
assert.Equal(t, gids[0].ContainerID, uint32(0))
127
assert.Equal(t, gids[0].Size, uint32(10))
129
_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10;foobar=bar")
130
assert.NotNil(t, err)
132
_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10#0-12")
133
assert.NotNil(t, err)
135
_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10#0-12--12")
136
assert.NotNil(t, err)
138
_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10#-1-12-12")
139
assert.NotNil(t, err)
141
_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10#0--12-0")
142
assert.NotNil(t, err)
145
func TestPostDeleteHooks(t *testing.T) {
146
ctx := context.Background()
149
statePath := filepath.Join(dir, "state")
150
copyPath := filepath.Join(dir, "copy")
151
cwdPath := filepath.Join(dir, "cwd")
154
config: &ContainerConfig{
157
Annotations: map[string]string{
161
ContainerRootFSConfig: ContainerRootFSConfig{
162
StaticDir: dir, // not the bundle, but good enough for this test
165
state: &ContainerState{
166
ExtensionStageHooks: map[string][]rspec.Hook{
170
Args: []string{"sh", "-c", fmt.Sprintf("cat >%s", statePath)},
173
Path: "/does/not/exist",
177
Args: []string{"sh", "-c", fmt.Sprintf("cp %s %s", statePath, copyPath)},
181
Args: []string{"sh", "-c", fmt.Sprintf("pwd >%s", cwdPath)},
187
err := c.postDeleteHooks(ctx)
191
stateRegexp := `{"ociVersion":"[0-9]+\.[0-9]+\..*","id":"123abc","status":"stopped","bundle":"` + dir + `","annotations":{"a":"b"}}`
192
for _, p := range []string{statePath, copyPath} {
194
t.Run(path, func(t *testing.T) {
195
content, err := os.ReadFile(path)
199
assert.Regexp(t, stateRegexp, string(content))
202
content, err := os.ReadFile(cwdPath)
206
assert.Equal(t, strings.TrimSuffix(string(content), "\n"), dir)
210
if runtime.GOOS != "windows" {
213
panic("we need a reliable executable path on Windows")