File: update_test.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (495 lines) | stat: -rw-r--r-- 17,941 bytes parent folder | download | duplicates (2)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2017 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package main_test

import (
	"bytes"
	"fmt"
	"path/filepath"
	"syscall"

	. "gopkg.in/check.v1"

	update "github.com/snapcore/snapd/cmd/snap-update-ns"
	"github.com/snapcore/snapd/logger"
	"github.com/snapcore/snapd/osutil"
	"github.com/snapcore/snapd/osutil/sys"
	"github.com/snapcore/snapd/testutil"
)

type updateSuite struct {
	testutil.BaseTest
	log *bytes.Buffer
}

var _ = Suite(&updateSuite{})

func (s *updateSuite) SetUpTest(c *C) {
	s.BaseTest.SetUpTest(c)
	buf, restore := logger.MockLogger()
	s.BaseTest.AddCleanup(restore)
	s.log = buf
}

func (s *updateSuite) TestSmoke(c *C) {
	upCtx := &testProfileUpdateContext{}
	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
}

func (s *updateSuite) TestUpdateFlow(c *C) {
	// The flow of update is as follows:
	// - the current profile and the desired profiles are loaded
	// - the needed changes are computed
	// - the needed changes are performed (one by one)
	// - the updated current profile is saved
	var funcsCalled []string
	var nChanges int
	upCtx := &testProfileUpdateContext{
		loadCurrentProfile: func() (*osutil.MountProfile, error) {
			funcsCalled = append(funcsCalled, "loaded-current")
			return &osutil.MountProfile{}, nil
		},
		loadDesiredProfile: func() (*osutil.MountProfile, error) {
			funcsCalled = append(funcsCalled, "loaded-desired")
			return &osutil.MountProfile{}, nil
		},
		neededChanges: func(old, new *osutil.MountProfile) []*update.Change {
			funcsCalled = append(funcsCalled, "changes-computed")
			return []*update.Change{{}, {}}
		},
		performChange: func(change *update.Change, as *update.Assumptions) ([]*update.Change, error) {
			nChanges++
			funcsCalled = append(funcsCalled, fmt.Sprintf("change-%d-performed", nChanges))
			return nil, nil
		},
		saveCurrentProfile: func(*osutil.MountProfile) error {
			funcsCalled = append(funcsCalled, "saved-current")
			return nil
		},
	}
	restore := upCtx.MockRelatedFunctions()
	defer restore()
	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
	c.Assert(funcsCalled, DeepEquals, []string{"loaded-desired", "loaded-current",
		"changes-computed", "change-1-performed", "change-2-performed", "saved-current"})
	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
}

func (s *updateSuite) TestResultingProfile(c *C) {
	// When the mount namespace is changed by performing actions the updated
	// current profile is comprised of the past changes that were reused (kept
	// unchanged) as well as newly mounted entries. Unmounted entries simple
	// cease to be.
	var saved *osutil.MountProfile
	upCtx := &testProfileUpdateContext{
		neededChanges: func(old, new *osutil.MountProfile) []*update.Change {
			return []*update.Change{
				{Action: update.Keep, Entry: osutil.MountEntry{Dir: "/keep"}},
				{Action: update.Unmount, Entry: osutil.MountEntry{Dir: "/unmount"}},
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/mount"}},
			}
		},
		saveCurrentProfile: func(profile *osutil.MountProfile) error {
			saved = profile
			return nil
		},
	}
	restore := upCtx.MockRelatedFunctions()
	defer restore()
	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
	c.Check(saved, DeepEquals, &osutil.MountProfile{Entries: []osutil.MountEntry{
		{Dir: "/keep"},
		{Dir: "/mount"},
	}})
}

func (s *updateSuite) TestSynthesizedPastChanges(c *C) {
	// When an mount update is performed it runs under the assumption
	// that past changes (i.e. the current profile) did occur. This is used
	// by the trespassing detector.
	text := `tmpfs /usr tmpfs 0 0`
	entry, err := osutil.ParseMountEntry(text)
	c.Assert(err, IsNil)
	as := &update.Assumptions{}
	upCtx := &testProfileUpdateContext{
		loadCurrentProfile: func() (*osutil.MountProfile, error) { return osutil.LoadMountProfileText(text) },
		loadDesiredProfile: func() (*osutil.MountProfile, error) { return osutil.LoadMountProfileText(text) },
		assumptions:        func() *update.Assumptions { return as },
	}
	restore := upCtx.MockRelatedFunctions()
	defer restore()

	// Perform the update, this will modify assumptions.
	c.Check(as.PastChanges(), HasLen, 0)
	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
	c.Check(as.PastChanges(), HasLen, 1)
	c.Check(as.PastChanges(), DeepEquals, []*update.Change{{
		Action: update.Mount,
		Entry:  entry,
	}})
}

func (s *updateSuite) TestSyntheticChanges(c *C) {
	// When a mount change is performed it may cause additional mount changes
	// to be performed, that were needed internally. Such changes are recorded
	// and saved into the current profile.
	var saved *osutil.MountProfile
	upCtx := &testProfileUpdateContext{
		loadDesiredProfile: func() (*osutil.MountProfile, error) {
			return &osutil.MountProfile{Entries: []osutil.MountEntry{
				{Dir: "/subdir/mount"},
			}}, nil
		},
		saveCurrentProfile: func(profile *osutil.MountProfile) error {
			saved = profile
			return nil
		},
		neededChanges: func(old, new *osutil.MountProfile) []*update.Change {
			return []*update.Change{
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/subdir/mount"}},
			}
		},
		performChange: func(change *update.Change, as *update.Assumptions) ([]*update.Change, error) {
			// If we are trying to mount /subdir/mount then synthesize a change
			// for making a tmpfs on /subdir.
			if change.Action == update.Mount && change.Entry.Dir == "/subdir/mount" {
				return []*update.Change{
					{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/subdir", Type: "tmpfs"}},
				}, nil
			}
			return nil, nil
		},
	}
	restore := upCtx.MockRelatedFunctions()
	defer restore()
	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
	c.Check(saved, DeepEquals, &osutil.MountProfile{Entries: []osutil.MountEntry{
		{Dir: "/subdir", Type: "tmpfs"},
		{Dir: "/subdir/mount"},
	}})
}

func (s *updateSuite) TestCannotPerformContentInterfaceChange(c *C) {
	// When performing a mount change for a content interface fails, we simply
	// ignore the error carry on. Such changes are not stored in the updated
	// current profile.
	var saved *osutil.MountProfile
	upCtx := &testProfileUpdateContext{
		saveCurrentProfile: func(profile *osutil.MountProfile) error {
			saved = profile
			return nil
		},
		neededChanges: func(old, new *osutil.MountProfile) []*update.Change {
			return []*update.Change{
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-1"}},
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-2"}},
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-3"}},
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-4"}},
			}
		},
		performChange: func(change *update.Change, as *update.Assumptions) ([]*update.Change, error) {
			// The change to /dir-2 cannot be made.
			if change.Action == update.Mount && change.Entry.Dir == "/dir-2" {
				return nil, errTesting
			}
			// The change to /dir-4 cannot be made either but with a special reason.
			if change.Action == update.Mount && change.Entry.Dir == "/dir-4" {
				return nil, update.ErrIgnoredMissingMount
			}
			return nil, nil
		},
	}
	restore := upCtx.MockRelatedFunctions()
	defer restore()
	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
	c.Check(saved, DeepEquals, &osutil.MountProfile{Entries: []osutil.MountEntry{
		{Dir: "/dir-1"},
		{Dir: "/dir-3"},
	}})
	// A message is logged though, unless specifically silenced with a crafted error.
	c.Check(s.log.String(), testutil.Contains, "cannot change mount namespace according to change mount (none /dir-2 none defaults 0 0): testing")
	c.Check(s.log.String(), Not(testutil.Contains), "cannot change mount namespace according to change mount (none /dir-4 none defaults 0 0): ")
}

func (s *updateSuite) TestCannotPerformLayoutChange(c *C) {
	// When performing a mount change for a layout, errors are immediately fatal.
	var saved *osutil.MountProfile
	upCtx := &testProfileUpdateContext{
		saveCurrentProfile: func(profile *osutil.MountProfile) error {
			saved = profile
			return nil
		},
		neededChanges: func(old, new *osutil.MountProfile) []*update.Change {
			return []*update.Change{
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-1"}},
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-2", Options: []string{"x-snapd.origin=layout"}}},
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-3"}},
			}
		},
		performChange: func(change *update.Change, as *update.Assumptions) ([]*update.Change, error) {
			// The change to /dir-2 cannot be made.
			if change.Action == update.Mount && change.Entry.Dir == "/dir-2" {
				return nil, errTesting
			}
			return nil, nil
		},
	}
	restore := upCtx.MockRelatedFunctions()
	defer restore()
	err := update.ExecuteMountProfileUpdate(upCtx)
	c.Check(err, Equals, errTesting)
	c.Check(saved, IsNil)
}

func (s *updateSuite) TestCannotPerformOvermountChange(c *C) {
	// When performing a mount change for an "overname", errors are immediately fatal.
	var saved *osutil.MountProfile
	upCtx := &testProfileUpdateContext{
		saveCurrentProfile: func(profile *osutil.MountProfile) error {
			saved = profile
			return nil
		},
		neededChanges: func(old, new *osutil.MountProfile) []*update.Change {
			return []*update.Change{
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-1"}},
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-2", Options: []string{"rbind", "x-snapd.origin=overname"}}},
				{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-3"}},
			}
		},
		performChange: func(change *update.Change, as *update.Assumptions) ([]*update.Change, error) {
			// The change to /dir-2 cannot be made.
			if change.Action == update.Mount && change.Entry.Dir == "/dir-2" {
				return nil, errTesting
			}
			return nil, nil
		},
	}
	restore := upCtx.MockRelatedFunctions()
	defer restore()
	err := update.ExecuteMountProfileUpdate(upCtx)
	c.Check(err, Equals, errTesting)
	c.Check(saved, IsNil)
}

func (s *updateSuite) TestKeepSyntheticMountsLP2043993(c *C) {
	baseSourceDir := c.MkDir()
	sourceFile := filepath.Join(baseSourceDir, "rofs/dir/source")

	baseTargetDir := c.MkDir()
	targetFile := filepath.Join(baseTargetDir, "target")

	as := update.Assumptions{}
	restore := as.MockUnrestrictedPaths("/")
	defer restore()

	// mock for permission errors
	restore = update.MockSysFchown(func(fd int, uid sys.UserID, gid sys.GroupID) error {
		return nil
	})
	defer restore()

	// mock for permission errors
	restore = update.MockSysMount(func(source, target, fstype string, flags uintptr, data string) (err error) {
		return nil
	})
	defer restore()

	// mock for permission errors
	restore = update.MockSysUnmount(func(target string, flags int) (err error) {
		return nil
	})
	defer restore()

	var mkdiratFailed bool
	restore = update.MockSysMkdirat(func(dirfd int, path string, mode uint32) (err error) {
		if path == "dir" && !mkdiratFailed {
			mkdiratFailed = true
			return syscall.EROFS
		}
		return syscall.Mkdirat(dirfd, path, mode)
	})
	defer restore()

	desiredMountEntry := osutil.MountEntry{
		Name:    sourceFile,
		Dir:     targetFile,
		Options: []string{"rbind", "rw", "x-snapd.id=test-id", osutil.XSnapdKindFile(), osutil.XSnapdOriginLayout()},
	}

	var saved osutil.MountProfile
	upCtx := &testProfileUpdateContext{
		assumptions: func() *update.Assumptions {
			return &as
		},
		loadDesiredProfile: func() (*osutil.MountProfile, error) {
			return &osutil.MountProfile{Entries: []osutil.MountEntry{desiredMountEntry}}, nil
		},
		loadCurrentProfile: func() (*osutil.MountProfile, error) {
			return &saved, nil
		},
		saveCurrentProfile: func(profile *osutil.MountProfile) error {
			saved.Entries = profile.Entries
			return nil
		},
	}

	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
	c.Assert(saved.Entries, HasLen, 2)
	// synth mount created due to read-only fs
	c.Check(saved.Entries[0].Type, Equals, "tmpfs")
	c.Check(saved.Entries[0].Name, Equals, "tmpfs")
	c.Check(saved.Entries[0].Dir, Equals, filepath.Join(baseSourceDir, "rofs"))
	c.Check(saved.Entries[0].XSnapdSynthetic(), Equals, true)
	c.Check(saved.Entries[0].XSnapdNeededBy(), Equals, "test-id")
	// desired mount exists
	c.Check(saved.Entries[1], DeepEquals, desiredMountEntry)

	c.Assert(update.ExecuteMountProfileUpdate(upCtx), IsNil)
	c.Assert(saved.Entries, HasLen, 2)
	c.Check(saved.Entries[0].Type, Equals, "tmpfs")
	c.Check(saved.Entries[0].Name, Equals, "tmpfs")
	c.Check(saved.Entries[0].Dir, Equals, filepath.Join(baseSourceDir, "rofs"))
	c.Check(saved.Entries[0].XSnapdSynthetic(), Equals, true)
	c.Check(saved.Entries[0].XSnapdNeededBy(), Equals, "test-id")
	c.Check(saved.Entries[1], DeepEquals, desiredMountEntry)
}

func (s *updateSuite) TestCurrentProfileFromChangesMade(c *C) {
	// Changes are computed back-to-front, starting from the last entry in
	// the mount profile.
	changes := []*update.Change{
		{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-1"}},
		{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-2"}},
		{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/dir-3"}},
	}
	profile := update.CurrentProfileFromChangesMade(changes)
	c.Check(profile, DeepEquals, osutil.MountProfile{Entries: []osutil.MountEntry{
		{Dir: "/dir-1"},
		{Dir: "/dir-2"},
		{Dir: "/dir-3"},
	}})

	// When we keep a number of entries we are not still processing them
	// back-to-front but the order of actual changes is front-to-back.
	changes = []*update.Change{
		{Action: update.Keep, Entry: osutil.MountEntry{Dir: "/dir-3"}},
		{Action: update.Keep, Entry: osutil.MountEntry{Dir: "/dir-2"}},
		{Action: update.Keep, Entry: osutil.MountEntry{Dir: "/dir-1"}},
	}
	profile = update.CurrentProfileFromChangesMade(changes)
	c.Check(profile, DeepEquals, osutil.MountProfile{Entries: []osutil.MountEntry{
		{Dir: "/dir-1"},
		{Dir: "/dir-2"},
		{Dir: "/dir-3"},
	}})

	// When we unmount things they just don't appear in the resulting profile.
	changes = []*update.Change{
		{Action: update.Unmount, Entry: osutil.MountEntry{Dir: "/dir-1"}},
		{Action: update.Unmount, Entry: osutil.MountEntry{Dir: "/dir-2"}},
		{Action: update.Unmount, Entry: osutil.MountEntry{Dir: "/dir-3"}},
	}
	profile = update.CurrentProfileFromChangesMade(changes)
	c.Check(profile, DeepEquals, osutil.MountProfile{})

	// When we have a mixture of changes unmount entries are removed, keep
	// entries are retained first, back to front and then mount entries are
	// retained in the order in which they were executed.
	changes = []*update.Change{
		{Action: update.Unmount, Entry: osutil.MountEntry{Dir: "/old-2"}},
		{Action: update.Unmount, Entry: osutil.MountEntry{Dir: "/old-1"}},
		{Action: update.Keep, Entry: osutil.MountEntry{Dir: "/same-2"}},
		{Action: update.Keep, Entry: osutil.MountEntry{Dir: "/same-1"}},
		{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/new-1"}},
		{Action: update.Mount, Entry: osutil.MountEntry{Dir: "/new-2"}},
	}
	profile = update.CurrentProfileFromChangesMade(changes)
	c.Check(profile, DeepEquals, osutil.MountProfile{Entries: []osutil.MountEntry{
		{Dir: "/same-1"},
		{Dir: "/same-2"},
		{Dir: "/new-1"},
		{Dir: "/new-2"},
	}})
}

// testProfileUpdateContext implements MountProfileUpdateContext and is suitable for testing.
type testProfileUpdateContext struct {
	loadCurrentProfile func() (*osutil.MountProfile, error)
	loadDesiredProfile func() (*osutil.MountProfile, error)
	saveCurrentProfile func(*osutil.MountProfile) error
	assumptions        func() *update.Assumptions

	// The remaining functions are defined for consistency but are installed by
	// calling their mock helpers. They are not a part of the interface.
	neededChanges func(*osutil.MountProfile, *osutil.MountProfile) []*update.Change
	performChange func(*update.Change, *update.Assumptions) ([]*update.Change, error)
}

// MockRelatedFunctions mocks NeededChanges and Change.Perform for the purpose of testing.
func (upCtx *testProfileUpdateContext) MockRelatedFunctions() (restore func()) {
	neededChanges := func(*osutil.MountProfile, *osutil.MountProfile) []*update.Change { return nil }
	if upCtx.neededChanges != nil {
		neededChanges = upCtx.neededChanges
	}
	restore1 := update.MockNeededChanges(neededChanges)

	performChange := func(*update.Change, *update.Assumptions) ([]*update.Change, error) { return nil, nil }
	if upCtx.performChange != nil {
		performChange = upCtx.performChange
	}
	restore2 := update.MockChangePerform(performChange)

	return func() {
		restore1()
		restore2()
	}
}

func (upCtx *testProfileUpdateContext) Lock() (unlock func(), err error) {
	return func() {}, nil
}

func (upCtx *testProfileUpdateContext) Assumptions() *update.Assumptions {
	if upCtx.assumptions != nil {
		return upCtx.assumptions()
	}
	return &update.Assumptions{}
}

func (upCtx *testProfileUpdateContext) LoadCurrentProfile() (*osutil.MountProfile, error) {
	if upCtx.loadCurrentProfile != nil {
		return upCtx.loadCurrentProfile()
	}
	return &osutil.MountProfile{}, nil
}

func (upCtx *testProfileUpdateContext) LoadDesiredProfile() (*osutil.MountProfile, error) {
	if upCtx.loadDesiredProfile != nil {
		return upCtx.loadDesiredProfile()
	}
	return &osutil.MountProfile{}, nil
}

func (upCtx *testProfileUpdateContext) SaveCurrentProfile(profile *osutil.MountProfile) error {
	if upCtx.saveCurrentProfile != nil {
		return upCtx.saveCurrentProfile(profile)
	}
	return nil
}