forked from containers/container-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay_test.go
More file actions
369 lines (328 loc) · 10 KB
/
overlay_test.go
File metadata and controls
369 lines (328 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//go:build linux
package overlay
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
graphdriver "go.podman.io/storage/drivers"
"go.podman.io/storage/drivers/graphtest"
"go.podman.io/storage/drivers/quota"
"go.podman.io/storage/pkg/archive"
"go.podman.io/storage/pkg/idtools"
"go.podman.io/storage/pkg/reexec"
)
const driverName = "overlay"
// check that Driver correctly implements the ApplyDiffTemporary interface
var _ graphdriver.ApplyDiffStaging = &Driver{}
func init() {
// Do not sure chroot to speed run time and allow archive
// errors or hangs to be debugged directly from the test process.
untar = archive.UntarUncompressed
graphdriver.ApplyUncompressedLayer = archive.ApplyUncompressedLayer
reexec.Init()
}
func skipIfNaive(t *testing.T) {
td := t.TempDir()
if err := doesSupportNativeDiff(td, ""); err != nil {
t.Skipf("Cannot run test with naive diff")
}
}
// Ensure that a layer created with force_mask will keep the root directory mode
// with user.containers.override_stat. This preserved mode should also be
// inherited by the upper layer, whether force_mask is set or not.
//
// This test is placed before TestOverlaySetup() because it uses driver options
// different from the other tests.
func TestContainersOverlayXattr(t *testing.T) {
driver := graphtest.GetDriver(t, driverName, "force_mask=700")
require.NoError(t, driver.Create("lower", "", nil))
graphtest.ReconfigureDriver(t, driverName)
require.NoError(t, driver.Create("upper", "lower", nil))
root, err := driver.Get("upper", graphdriver.MountOpts{})
require.NoError(t, err)
fi, err := os.Stat(root)
require.NoError(t, err)
assert.Equal(t, 0o555&os.ModePerm, fi.Mode()&os.ModePerm, root)
}
func TestSupportsShifting(t *testing.T) {
contiguousMap := []idtools.IDMap{
{
ContainerID: 0,
HostID: 1000,
Size: 65536,
},
}
nonContiguousMap := []idtools.IDMap{
{
ContainerID: 0,
HostID: 0,
Size: 1,
},
{
ContainerID: 2,
HostID: 2,
Size: 1,
},
}
t.Run("no mount program", func(t *testing.T) {
driver := graphtest.GetDriver(t, driverName)
supported := driver.SupportsShifting(nil, nil)
assert.Equal(t, supported, driver.SupportsShifting(contiguousMap, contiguousMap), "contiguous map with no mount program")
assert.Equal(t, supported, driver.SupportsShifting(nonContiguousMap, nonContiguousMap), "non-contiguous map with no mount program")
})
t.Run("with mount program", func(t *testing.T) {
driver := graphtest.GetDriver(t, driverName, "mount_program=/usr/bin/true")
assert.True(t, driver.SupportsShifting(nil, nil), "nil map with mount program")
assert.True(t, driver.SupportsShifting(contiguousMap, contiguousMap), "contiguous map with mount program")
// If a mount program is specified, SupportsShifting must return false
assert.False(t, driver.SupportsShifting(nonContiguousMap, nonContiguousMap), "non-contiguous map with mount program")
})
}
// This avoids creating a new driver for each test if all tests are run
// Make sure to put new tests between TestOverlaySetup and TestOverlayTeardown
func TestOverlaySetup(t *testing.T) {
graphtest.GetDriver(t, driverName)
}
func TestOverlayCreateEmpty(t *testing.T) {
graphtest.DriverTestCreateEmpty(t, driverName)
}
func TestOverlayCreateBase(t *testing.T) {
graphtest.DriverTestCreateBase(t, driverName)
}
func TestOverlayCreateSnap(t *testing.T) {
graphtest.DriverTestCreateSnap(t, driverName)
}
func TestOverlayCreateFromTemplate(t *testing.T) {
graphtest.DriverTestCreateFromTemplate(t, driverName)
}
func TestOverlay500LayerRead(t *testing.T) {
graphtest.DriverTestDeepLayerRead(t, 500, driverName)
}
func TestOverlayDiffApply10Files(t *testing.T) {
skipIfNaive(t)
graphtest.DriverTestDiffApply(t, 10, driverName)
}
func TestOverlayChanges(t *testing.T) {
skipIfNaive(t)
graphtest.DriverTestChanges(t, driverName)
}
func TestOverlayEcho(t *testing.T) {
graphtest.DriverTestEcho(t, driverName)
}
func TestOverlayListLayers(t *testing.T) {
graphtest.DriverTestListLayers(t, driverName)
}
func TestOverlayTeardown(t *testing.T) {
graphtest.PutDriver(t)
}
// Benchmarks should always setup new driver
func BenchmarkExists(b *testing.B) {
graphtest.DriverBenchExists(b, driverName)
}
func BenchmarkGetEmpty(b *testing.B) {
graphtest.DriverBenchGetEmpty(b, driverName)
}
func BenchmarkDiffBase(b *testing.B) {
graphtest.DriverBenchDiffBase(b, driverName)
}
func BenchmarkDiffSmallUpper(b *testing.B) {
graphtest.DriverBenchDiffN(b, 10, 10, driverName)
}
func BenchmarkDiff10KFileUpper(b *testing.B) {
graphtest.DriverBenchDiffN(b, 10, 10000, driverName)
}
func BenchmarkDiff10KFilesBottom(b *testing.B) {
graphtest.DriverBenchDiffN(b, 10000, 10, driverName)
}
func BenchmarkDiffApply100(b *testing.B) {
graphtest.DriverBenchDiffApplyN(b, 100, driverName)
}
func BenchmarkDiff20Layers(b *testing.B) {
graphtest.DriverBenchDeepLayerDiff(b, 20, driverName)
}
func BenchmarkRead20Layers(b *testing.B) {
graphtest.DriverBenchDeepLayerRead(b, 20, driverName)
}
func Test_parseOptions(t *testing.T) {
var (
sharedMask = os.FileMode(0o755)
privateMask = os.FileMode(0o700)
customMask = os.FileMode(0o644)
)
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test-file")
f, err := os.Create(tmpFile)
require.NoError(t, err)
require.NoError(t, f.Close())
tests := []struct {
name string // description of this test case
// Named input parameters for target function.
options []string
want *overlayOptions
wantErr string
}{
{
name: "no opts",
options: []string{},
want: &overlayOptions{},
},
{
name: "mountopt",
options: []string{"mountopt=abc"},
want: &overlayOptions{mountOptions: "abc"},
},
{
name: "overlay prefix handling",
options: []string{"overlay.mountopt=def"},
want: &overlayOptions{mountOptions: "def"},
},
{
name: "overlay2 prefix handling",
options: []string{"overlay2.mountopt=ghi"},
want: &overlayOptions{mountOptions: "ghi"},
},
{
name: "size",
options: []string{"size=50kb"},
want: &overlayOptions{quota: quota.Quota{Size: 51200}},
},
{
name: "size - invalid",
options: []string{"size=abc"},
wantErr: "invalid size",
},
{
name: "inodes",
options: []string{"inodes=1024"},
want: &overlayOptions{quota: quota.Quota{Inodes: 1024}},
},
{
name: "inodes - invalid",
options: []string{"inodes=abc"},
wantErr: "invalid syntax",
},
{
name: "override_kernel_check",
options: []string{"override_kernel_check=true"},
want: &overlayOptions{}, // Has no effect other than log output
},
{
name: "imagestore - valid directory",
options: []string{"imagestore=" + tmpDir},
want: &overlayOptions{imageStores: []string{tmpDir}},
},
{
name: "imagestore - invalid (relative path)",
options: []string{"imagestore=./relative/path"},
wantErr: "is not absolute",
},
{
name: "imagestore - invalid (file instead of dir)",
options: []string{"imagestore=" + tmpFile},
wantErr: "must be a directory",
},
{
name: "additionallayerstore - valid directory",
options: []string{"additionallayerstore=" + tmpDir},
want: &overlayOptions{layerStores: []additionalLayerStore{{path: tmpDir, withReference: false}}},
},
{
name: "additionallayerstore - with ref",
options: []string{"additionallayerstore=" + tmpDir + ":ref"},
want: &overlayOptions{layerStores: []additionalLayerStore{{path: tmpDir, withReference: true}}},
},
{
name: "additionallayerstore - ref used twice",
options: []string{"additionallayerstore=" + tmpDir + ":ref:ref"},
wantErr: "contains \"ref\" option twice",
},
{
name: "additionallayerstore - unknown option",
options: []string{"additionallayerstore=" + tmpDir + ":unknown"},
wantErr: "contains unknown option \"unknown\"",
},
{
name: "mount_program - valid file",
options: []string{"mount_program=" + tmpFile},
want: &overlayOptions{mountProgram: tmpFile},
},
{
name: "mount_program - missing file",
options: []string{"mount_program=/does/not/exist"},
wantErr: "can't stat program",
},
{
name: "use_composefs",
options: []string{"use_composefs=true"},
want: &overlayOptions{useComposefs: true},
},
{
name: "use_composefs - invalid",
options: []string{"use_composefs=notabool"},
wantErr: "invalid syntax",
},
{
name: "skip_mount_home",
options: []string{"skip_mount_home=true"},
want: &overlayOptions{skipMountHome: true},
},
{
name: "ignore_chown_errors",
options: []string{"ignore_chown_errors=true"},
want: &overlayOptions{ignoreChownErrors: true},
},
{
name: "force_mask - shared",
options: []string{"force_mask=shared"},
want: &overlayOptions{forceMask: &sharedMask},
},
{
name: "force_mask - private",
options: []string{"force_mask=private"},
want: &overlayOptions{forceMask: &privateMask},
},
{
name: "force_mask - custom octal",
options: []string{"force_mask=644"},
want: &overlayOptions{forceMask: &customMask},
},
{
name: "force_mask - invalid syntax",
options: []string{"force_mask=hello"},
wantErr: "invalid syntax",
},
{
name: "unknown option",
options: []string{"unknown=value"},
wantErr: `unknown option "unknown" ("unknown=value")`,
},
{
name: "unknown overlay prefix option",
options: []string{"overlay.123=value"},
wantErr: `unknown option "123" ("overlay.123=value")`,
},
{
name: "other driver option should not error",
options: []string{"vfs.unknown=value"},
want: &overlayOptions{},
},
{
name: "unknown driver name must error",
options: []string{"driver123.mountopt=value"},
wantErr: `unknown driver "driver123" in option "driver123.mountopt=value"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, gotErr := parseOptions(tt.options)
if tt.wantErr != "" {
require.ErrorContains(t, gotErr, tt.wantErr)
return
}
require.NoError(t, gotErr)
assert.Equal(t, tt.want, got)
})
}
}