podman

Форк
0
/
container_internal_test.go 
215 строк · 5.4 Кб
1
//go:build !remote
2

3
package libpod
4

5
import (
6
	"context"
7
	"fmt"
8
	"os"
9
	"path/filepath"
10
	"runtime"
11
	"strings"
12
	"testing"
13

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"
18
)
19

20
// hookPath is the path to an example hook executable.
21
var hookPath string
22

23
func TestParseOptionIDs(t *testing.T) {
24
	idMap := []idtools.IDMap{
25
		{
26
			ContainerID: 0,
27
			HostID:      1,
28
			Size:        10000,
29
		},
30
	}
31

32
	_, err := parseOptionIDs(idMap, "uids=100-200-2")
33
	assert.NotNil(t, err)
34

35
	mappings, err := parseOptionIDs(idMap, "100-200-2")
36
	assert.Nil(t, err)
37
	assert.NotNil(t, mappings)
38

39
	assert.Equal(t, len(mappings), 1)
40

41
	assert.Equal(t, mappings[0].ContainerID, 100)
42
	assert.Equal(t, mappings[0].HostID, 200)
43
	assert.Equal(t, mappings[0].Size, 2)
44

45
	mappings, err = parseOptionIDs(idMap, "100-200-2#300-400-5")
46
	assert.Nil(t, err)
47
	assert.NotNil(t, mappings)
48

49
	assert.Equal(t, len(mappings), 2)
50

51
	assert.Equal(t, mappings[0].ContainerID, 100)
52
	assert.Equal(t, mappings[0].HostID, 200)
53
	assert.Equal(t, mappings[0].Size, 2)
54

55
	assert.Equal(t, mappings[1].ContainerID, 300)
56
	assert.Equal(t, mappings[1].HostID, 400)
57
	assert.Equal(t, mappings[1].Size, 5)
58

59
	mappings, err = parseOptionIDs(idMap, "@100-200-2#@300-400-5")
60
	assert.Nil(t, err)
61
	assert.NotNil(t, mappings)
62

63
	assert.Equal(t, len(mappings), 2)
64

65
	assert.Equal(t, mappings[0].ContainerID, 100)
66
	assert.Equal(t, mappings[0].HostID, 201)
67
	assert.Equal(t, mappings[0].Size, 2)
68

69
	assert.Equal(t, mappings[1].ContainerID, 300)
70
	assert.Equal(t, mappings[1].HostID, 401)
71
	assert.Equal(t, mappings[1].Size, 5)
72

73
	_, err = parseOptionIDs(idMap, "@10000-20000-2")
74
	assert.NotNil(t, err)
75

76
	_, err = parseOptionIDs(idMap, "100-200-3###400-500-6")
77
	assert.NotNil(t, err)
78
}
79

80
func TestParseIDMapMountOption(t *testing.T) {
81
	uidMap := []idtools.IDMap{
82
		{
83
			ContainerID: 0,
84
			HostID:      1000,
85
			Size:        10000,
86
		},
87
	}
88
	gidMap := []idtools.IDMap{
89
		{
90
			ContainerID: 0,
91
			HostID:      2000,
92
			Size:        10000,
93
		},
94
	}
95
	options := stypes.IDMappingOptions{
96
		UIDMap: uidMap,
97
		GIDMap: gidMap,
98
	}
99
	uids, gids, err := parseIDMapMountOption(options, "idmap")
100
	assert.Nil(t, err)
101
	assert.Equal(t, len(uids), 1)
102
	assert.Equal(t, len(gids), 1)
103

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))
107

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))
111

112
	uids, gids, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10")
113
	assert.Nil(t, err)
114
	assert.Equal(t, len(uids), 2)
115
	assert.Equal(t, len(gids), 1)
116

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))
120

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))
124

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))
128

129
	_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10;foobar=bar")
130
	assert.NotNil(t, err)
131

132
	_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10#0-12")
133
	assert.NotNil(t, err)
134

135
	_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10#0-12--12")
136
	assert.NotNil(t, err)
137

138
	_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10#-1-12-12")
139
	assert.NotNil(t, err)
140

141
	_, _, err = parseIDMapMountOption(options, "idmap=uids=0-1-10#10-11-10;gids=0-3-10#0--12-0")
142
	assert.NotNil(t, err)
143
}
144

145
func TestPostDeleteHooks(t *testing.T) {
146
	ctx := context.Background()
147
	dir := t.TempDir()
148

149
	statePath := filepath.Join(dir, "state")
150
	copyPath := filepath.Join(dir, "copy")
151
	cwdPath := filepath.Join(dir, "cwd")
152
	c := Container{
153
		runtime: &Runtime{},
154
		config: &ContainerConfig{
155
			ID: "123abc",
156
			Spec: &rspec.Spec{
157
				Annotations: map[string]string{
158
					"a": "b",
159
				},
160
			},
161
			ContainerRootFSConfig: ContainerRootFSConfig{
162
				StaticDir: dir, // not the bundle, but good enough for this test
163
			},
164
		},
165
		state: &ContainerState{
166
			ExtensionStageHooks: map[string][]rspec.Hook{
167
				"poststop": {
168
					rspec.Hook{
169
						Path: hookPath,
170
						Args: []string{"sh", "-c", fmt.Sprintf("cat >%s", statePath)},
171
					},
172
					rspec.Hook{
173
						Path: "/does/not/exist",
174
					},
175
					rspec.Hook{
176
						Path: hookPath,
177
						Args: []string{"sh", "-c", fmt.Sprintf("cp %s %s", statePath, copyPath)},
178
					},
179
					rspec.Hook{
180
						Path: hookPath,
181
						Args: []string{"sh", "-c", fmt.Sprintf("pwd >%s", cwdPath)},
182
					},
183
				},
184
			},
185
		},
186
	}
187
	err := c.postDeleteHooks(ctx)
188
	if err != nil {
189
		t.Fatal(err)
190
	}
191
	stateRegexp := `{"ociVersion":"[0-9]+\.[0-9]+\..*","id":"123abc","status":"stopped","bundle":"` + dir + `","annotations":{"a":"b"}}`
192
	for _, p := range []string{statePath, copyPath} {
193
		path := p
194
		t.Run(path, func(t *testing.T) {
195
			content, err := os.ReadFile(path)
196
			if err != nil {
197
				t.Fatal(err)
198
			}
199
			assert.Regexp(t, stateRegexp, string(content))
200
		})
201
	}
202
	content, err := os.ReadFile(cwdPath)
203
	if err != nil {
204
		t.Fatal(err)
205
	}
206
	assert.Equal(t, strings.TrimSuffix(string(content), "\n"), dir)
207
}
208

209
func init() {
210
	if runtime.GOOS != "windows" {
211
		hookPath = "/bin/sh"
212
	} else {
213
		panic("we need a reliable executable path on Windows")
214
	}
215
}
216

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.