File: linux_parser_test.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (358 lines) | stat: -rw-r--r-- 10,846 bytes parent folder | download
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
package mounts

import (
	"fmt"
	"strings"
	"testing"

	"github.com/docker/docker/api/types/mount"
	"github.com/google/go-cmp/cmp/cmpopts"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestLinuxParseMountRaw(t *testing.T) {
	valid := []string{
		"/home",
		"/home:/home",
		"/home:/something/else",
		"/with space",
		"/home:/with space",
		"relative:/absolute-path",
		"hostPath:/containerPath:ro",
		"/hostPath:/containerPath:rw",
		"/rw:/ro",
		"/hostPath:/containerPath:shared",
		"/hostPath:/containerPath:rshared",
		"/hostPath:/containerPath:slave",
		"/hostPath:/containerPath:rslave",
		"/hostPath:/containerPath:private",
		"/hostPath:/containerPath:rprivate",
		"/hostPath:/containerPath:ro,shared",
		"/hostPath:/containerPath:ro,slave",
		"/hostPath:/containerPath:ro,private",
		"/hostPath:/containerPath:ro,z,shared",
		"/hostPath:/containerPath:ro,Z,slave",
		"/hostPath:/containerPath:Z,ro,slave",
		"/hostPath:/containerPath:slave,Z,ro",
		"/hostPath:/containerPath:Z,slave,ro",
		"/hostPath:/containerPath:slave,ro,Z",
		"/hostPath:/containerPath:rslave,ro,Z",
		"/hostPath:/containerPath:ro,rshared,Z",
		"/hostPath:/containerPath:ro,Z,rprivate",
	}

	invalid := map[string]string{
		"":                                "invalid volume specification",
		"./":                              "mount path must be absolute",
		"../":                             "mount path must be absolute",
		"/:../":                           "mount path must be absolute",
		"/:path":                          "mount path must be absolute",
		":":                               "invalid volume specification",
		"/tmp:":                           "invalid volume specification",
		":test":                           "invalid volume specification",
		":/test":                          "invalid volume specification",
		"tmp:":                            "invalid volume specification",
		":test:":                          "invalid volume specification",
		"::":                              "invalid volume specification",
		":::":                             "invalid volume specification",
		"/tmp:::":                         "invalid volume specification",
		":/tmp::":                         "invalid volume specification",
		"/path:rw":                        "invalid volume specification",
		"/path:ro":                        "invalid volume specification",
		"/rw:rw":                          "invalid volume specification",
		"path:ro":                         "invalid volume specification",
		"/path:/path:sw":                  `invalid mode`,
		"/path:/path:rwz":                 `invalid mode`,
		"/path:/path:ro,rshared,rslave":   `invalid mode`,
		"/path:/path:ro,z,rshared,rslave": `invalid mode`,
		"/path:shared":                    "invalid volume specification",
		"/path:slave":                     "invalid volume specification",
		"/path:private":                   "invalid volume specification",
		"name:/absolute-path:shared":      "invalid volume specification",
		"name:/absolute-path:rshared":     "invalid volume specification",
		"name:/absolute-path:slave":       "invalid volume specification",
		"name:/absolute-path:rslave":      "invalid volume specification",
		"name:/absolute-path:private":     "invalid volume specification",
		"name:/absolute-path:rprivate":    "invalid volume specification",
	}

	parser := NewLinuxParser()
	if p, ok := parser.(*linuxParser); ok {
		p.fi = mockFiProvider{}
	}

	for _, path := range valid {
		if _, err := parser.ParseMountRaw(path, "local"); err != nil {
			t.Errorf("ParseMountRaw(%q) should succeed: error %q", path, err)
		}
	}

	for path, expectedError := range invalid {
		if mp, err := parser.ParseMountRaw(path, "local"); err == nil {
			t.Errorf("ParseMountRaw(%q) should have failed validation. Err '%v' - MP: %v", path, err, mp)
		} else {
			if !strings.Contains(err.Error(), expectedError) {
				t.Errorf("ParseMountRaw(%q) error should contain %q, got %v", path, expectedError, err.Error())
			}
		}
	}
}

func TestLinuxParseMountRawSplit(t *testing.T) {
	tests := []struct {
		bind     string
		driver   string
		expected *MountPoint
		expErr   string
	}{
		{
			bind: "/tmp:/tmp1",
			expected: &MountPoint{
				Source:      "/tmp",
				Destination: "/tmp1",
				RW:          true,
				Type:        mount.TypeBind,
				Propagation: "rprivate",
				Spec: mount.Mount{
					Source:   "/tmp",
					Target:   "/tmp1",
					ReadOnly: false,
					Type:     mount.TypeBind,
				},
			},
		},
		{
			bind: "/tmp:/tmp2:ro",
			expected: &MountPoint{
				Source:      "/tmp",
				Destination: "/tmp2",
				RW:          false,
				Type:        mount.TypeBind,
				Mode:        "ro",
				Propagation: "rprivate",
				Spec: mount.Mount{
					Source:   "/tmp",
					Target:   "/tmp2",
					ReadOnly: true,
					Type:     mount.TypeBind,
				},
			},
		},
		{
			bind: "/tmp:/tmp3:rw",
			expected: &MountPoint{
				Source:      "/tmp",
				Destination: "/tmp3",
				RW:          true,
				Type:        mount.TypeBind,
				Mode:        "rw",
				Propagation: "rprivate",
				Spec: mount.Mount{
					Source:   "/tmp",
					Target:   "/tmp3",
					ReadOnly: false,
					Type:     mount.TypeBind,
				},
			},
		},
		{
			bind:   "/tmp:/tmp4:foo",
			expErr: `invalid mode: foo`,
		},
		{
			bind: "name:/named1",
			expected: &MountPoint{
				Destination: "/named1",
				RW:          true,
				Name:        "name",
				Type:        mount.TypeVolume,
				Mode:        "", // FIXME(thaJeztah): why is this different than an explicit "rw" ?
				Propagation: "",
				CopyData:    true,
				Spec: mount.Mount{
					Source:   "name",
					Target:   "/named1",
					ReadOnly: false,
					Type:     mount.TypeVolume,
				},
			},
		},
		{
			bind:   "name:/named2",
			driver: "external",
			expected: &MountPoint{
				Destination: "/named2",
				RW:          true,
				Name:        "name",
				Driver:      "external",
				Type:        mount.TypeVolume,
				Mode:        "", // FIXME(thaJeztah): why is this different than an explicit "rw" ?
				Propagation: "",
				CopyData:    true,
				Spec: mount.Mount{
					Source:        "name",
					Target:        "/named2",
					ReadOnly:      false,
					Type:          mount.TypeVolume,
					VolumeOptions: &mount.VolumeOptions{DriverConfig: &mount.Driver{Name: "external"}},
				},
			},
		},
		{
			bind:   "name:/named3:ro",
			driver: "local",
			expected: &MountPoint{
				Destination: "/named3",
				RW:          false,
				Name:        "name",
				Driver:      "local",
				Type:        mount.TypeVolume,
				Mode:        "ro",
				Propagation: "",
				CopyData:    true,
				Spec: mount.Mount{
					Source:        "name",
					Target:        "/named3",
					ReadOnly:      true,
					Type:          mount.TypeVolume,
					VolumeOptions: &mount.VolumeOptions{DriverConfig: &mount.Driver{Name: "local"}},
				},
			},
		},
		{
			bind: "local/name:/tmp:rw",
			expected: &MountPoint{
				Destination: "/tmp",
				RW:          true,
				Name:        "local/name",
				Type:        mount.TypeVolume,
				Mode:        "rw",
				Propagation: "",
				CopyData:    true,
				Spec: mount.Mount{
					Source:   "local/name",
					Target:   "/tmp",
					ReadOnly: false,
					Type:     mount.TypeVolume,
				},
			},
		},
		{
			bind:   "/tmp:tmp",
			expErr: `invalid volume specification: '/tmp:tmp': invalid mount config for type "bind": invalid mount path: 'tmp' mount path must be absolute`,
		},
	}

	parser := NewLinuxParser()
	if p, ok := parser.(*linuxParser); ok {
		p.fi = mockFiProvider{}
	}

	for _, tc := range tests {
		t.Run(tc.bind, func(t *testing.T) {
			m, err := parser.ParseMountRaw(tc.bind, tc.driver)
			if tc.expErr != "" {
				assert.Check(t, is.Nil(m))
				assert.Check(t, is.Error(err, tc.expErr))
				return
			}

			assert.NilError(t, err)
			assert.Check(t, is.DeepEqual(*m, *tc.expected, cmpopts.IgnoreUnexported(MountPoint{})))
		})
	}
}

// TestLinuxParseMountSpecBindWithFileinfoError makes sure that the parser returns
// the error produced by the fileinfo provider.
//
// Some extra context for the future in case of changes and possible wtf are we
// testing this for:
//
// Currently this "fileInfoProvider" returns (bool, bool, error)
// The 1st bool is "does this path exist"
// The 2nd bool is "is this path a dir"
// Then of course the error is an error.
//
// The issue is the parser was ignoring the error and only looking at the
// "does this path exist" boolean, which is always false if there is an error.
// Then the error returned to the caller was a (slightly, maybe) friendlier
// error string than what comes from `os.Stat`
// So ...the caller was always getting an error saying the path doesn't exist
// even if it does exist but got some other error (like a permission error).
// This is confusing to users.
func TestLinuxParseMountSpecBindWithFileinfoError(t *testing.T) {
	parser := NewLinuxParser()
	testErr := fmt.Errorf("some crazy error")
	if pr, ok := parser.(*linuxParser); ok {
		pr.fi = &mockFiProviderWithError{err: testErr}
	}

	_, err := parser.ParseMountSpec(mount.Mount{
		Type:   mount.TypeBind,
		Source: `/bananas`,
		Target: `/bananas`,
	})
	assert.ErrorContains(t, err, testErr.Error())
}

func TestConvertTmpfsOptions(t *testing.T) {
	type testCase struct {
		opt                  mount.TmpfsOptions
		readOnly             bool
		expectedSubstrings   []string
		unexpectedSubstrings []string
		err                  bool
	}
	cases := []testCase{
		{
			opt:                  mount.TmpfsOptions{SizeBytes: 1024 * 1024, Mode: 0o700},
			readOnly:             false,
			expectedSubstrings:   []string{"size=1m", "mode=700"},
			unexpectedSubstrings: []string{"ro"},
		},
		{
			opt:                  mount.TmpfsOptions{},
			readOnly:             true,
			expectedSubstrings:   []string{"ro"},
			unexpectedSubstrings: []string{},
		},
		{
			opt:                  mount.TmpfsOptions{Options: [][]string{{"exec"}}},
			readOnly:             true,
			expectedSubstrings:   []string{"ro", "exec"},
			unexpectedSubstrings: []string{"noexec"},
		},
		{
			opt: mount.TmpfsOptions{Options: [][]string{{"INVALID"}}},
			err: true,
		},
	}
	p := NewLinuxParser()
	for _, tc := range cases {
		opt := tc.opt
		data, err := p.ConvertTmpfsOptions(&opt, tc.readOnly)
		if tc.err {
			if err == nil {
				t.Fatalf("expected error for %+v, got nil", opt)
			}
			continue
		}
		if err != nil {
			t.Fatalf("could not convert %+v (readOnly: %v) to string: %v",
				tc.opt, tc.readOnly, err)
		}
		t.Logf("data=%q", data)
		for _, s := range tc.expectedSubstrings {
			if !strings.Contains(data, s) {
				t.Fatalf("expected substring: %s, got %v (case=%+v)", s, data, tc)
			}
		}
		for _, s := range tc.unexpectedSubstrings {
			if strings.Contains(data, s) {
				t.Fatalf("unexpected substring: %s, got %v (case=%+v)", s, data, tc)
			}
		}
	}
}