File: spec_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (337 lines) | stat: -rw-r--r-- 13,273 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
// -*- 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 mount_test

import (
	"strings"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/interfaces"
	"github.com/snapcore/snapd/interfaces/ifacetest"
	"github.com/snapcore/snapd/interfaces/mount"
	"github.com/snapcore/snapd/logger"
	"github.com/snapcore/snapd/osutil"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/snap/snaptest"
)

type specSuite struct {
	iface    *ifacetest.TestInterface
	spec     *mount.Specification
	plugInfo *snap.PlugInfo
	plug     *interfaces.ConnectedPlug
	slotInfo *snap.SlotInfo
	slot     *interfaces.ConnectedSlot
}

var _ = Suite(&specSuite{
	iface: &ifacetest.TestInterface{
		InterfaceName: "test",
		MountConnectedPlugCallback: func(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
			return spec.AddMountEntry(osutil.MountEntry{Dir: "dir-a", Name: "connected-plug"})
		},
		MountConnectedSlotCallback: func(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
			return spec.AddMountEntry(osutil.MountEntry{Dir: "dir-b", Name: "connected-slot"})
		},
		MountPermanentPlugCallback: func(spec *mount.Specification, plug *snap.PlugInfo) error {
			return spec.AddMountEntry(osutil.MountEntry{Dir: "dir-c", Name: "permanent-plug"})
		},
		MountPermanentSlotCallback: func(spec *mount.Specification, slot *snap.SlotInfo) error {
			return spec.AddMountEntry(osutil.MountEntry{Dir: "dir-d", Name: "permanent-slot"})
		},
	},
	plugInfo: &snap.PlugInfo{
		Snap:      &snap.Info{SuggestedName: "snap"},
		Name:      "name",
		Interface: "test",
	},
	slotInfo: &snap.SlotInfo{
		Snap:      &snap.Info{SuggestedName: "snap"},
		Name:      "name",
		Interface: "test",
	},
})

func (s *specSuite) SetUpTest(c *C) {
	s.spec = &mount.Specification{}

	const plugYaml = `name: snap
version: 1
apps:
 app:
  plugs: [name]
`
	s.plug, s.plugInfo = ifacetest.MockConnectedPlug(c, plugYaml, nil, "name")

	const slotYaml = `name: snap
version: 1
slots:
 name:
  interface: test
`
	s.slot, s.slotInfo = ifacetest.MockConnectedSlot(c, slotYaml, nil, "name")
}

// AddMountEntry and AddUserMountEntry are not broken
func (s *specSuite) TestSmoke(c *C) {
	ent0 := osutil.MountEntry{Dir: "dir-a", Name: "fs1"}
	ent1 := osutil.MountEntry{Dir: "dir-b", Name: "fs2"}
	ent2 := osutil.MountEntry{Dir: "dir-c", Name: "fs3"}

	uent0 := osutil.MountEntry{Dir: "per-user-a", Name: "fs1"}
	uent1 := osutil.MountEntry{Dir: "per-user-b", Name: "fs2"}

	c.Assert(s.spec.AddMountEntry(ent0), IsNil)
	c.Assert(s.spec.AddMountEntry(ent1), IsNil)
	c.Assert(s.spec.AddMountEntry(ent2), IsNil)

	c.Assert(s.spec.AddUserMountEntry(uent0), IsNil)
	c.Assert(s.spec.AddUserMountEntry(uent1), IsNil)

	c.Assert(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{ent0, ent1, ent2})
	c.Assert(s.spec.UserMountEntries(), DeepEquals, []osutil.MountEntry{uent0, uent1})
}

// Added entries can clash and are automatically renamed by MountEntries
func (s *specSuite) TestMountEntriesDeclash(c *C) {
	buf, restore := logger.MockLogger()
	defer restore()

	c.Assert(s.spec.AddMountEntry(osutil.MountEntry{Dir: "foo", Name: "fs1"}), IsNil)
	c.Assert(s.spec.AddMountEntry(osutil.MountEntry{Dir: "foo", Name: "fs2"}), IsNil)
	c.Assert(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{
		{Dir: "foo", Name: "fs1"},
		{Dir: "foo-2", Name: "fs2"},
	})

	c.Assert(s.spec.AddUserMountEntry(osutil.MountEntry{Dir: "bar", Name: "fs1"}), IsNil)
	c.Assert(s.spec.AddUserMountEntry(osutil.MountEntry{Dir: "bar", Name: "fs2"}), IsNil)
	c.Assert(s.spec.AddUserMountEntry(osutil.MountEntry{Dir: "bar", Name: "", Options: []string{"x-snapd.kind=ensure-dir"}}), IsNil)
	c.Assert(s.spec.AddUserMountEntry(osutil.MountEntry{Dir: "bar", Name: "", Options: []string{"x-snapd.kind=ensure-dir"}}), IsNil)

	c.Assert(s.spec.UserMountEntries(), DeepEquals, []osutil.MountEntry{
		// First entry: leave intact
		{Dir: "bar", Name: "fs1"},
		// Different name: rename
		{Dir: "bar-2", Name: "fs2"},
		// Different name, Kind ensure-dir: leave intact, append to end
		{Dir: "bar", Options: []string{"x-snapd.kind=ensure-dir"}},
		// Same name , Kind ensure-dir: leave intact, append to end
		{Dir: "bar", Options: []string{"x-snapd.kind=ensure-dir"}},
	})

	// extract the relevant part of the log
	loggedMsgs := strings.Split(buf.String(), "\n")
	msg := strings.SplitAfter(strings.TrimSpace(loggedMsgs[0]), ": ")[1]
	c.Assert(msg, Equals, `renaming mount entry for directory "foo" to "foo-2" to avoid a clash`)
	msg = strings.SplitAfter(strings.TrimSpace(loggedMsgs[1]), ": ")[1]
	c.Assert(msg, Equals, `renaming mount entry for directory "bar" to "bar-2" to avoid a clash`)
}

// The mount.Specification can be used through the interfaces.Specification interface
func (s *specSuite) TestSpecificationIface(c *C) {
	var r interfaces.Specification = s.spec
	c.Assert(r.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
	c.Assert(r.AddConnectedSlot(s.iface, s.plug, s.slot), IsNil)
	c.Assert(r.AddPermanentPlug(s.iface, s.plugInfo), IsNil)
	c.Assert(r.AddPermanentSlot(s.iface, s.slotInfo), IsNil)
	c.Assert(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{
		{Dir: "dir-a", Name: "connected-plug"},
		{Dir: "dir-b", Name: "connected-slot"},
		{Dir: "dir-c", Name: "permanent-plug"},
		{Dir: "dir-d", Name: "permanent-slot"}})
}

const snapWithLayout = `
name: vanguard
version: 0
layout:
  /usr:
    bind: $SNAP/usr
  /lib/mytmp:
    type: tmpfs
    mode: 1777
  /lib/mylink:
    symlink: $SNAP/link/target
  /etc/foo.conf:
    bind-file: $SNAP/foo.conf
`

func (s *specSuite) TestMountEntryFromLayout(c *C) {
	snapInfo := snaptest.MockInfo(c, snapWithLayout, &snap.SideInfo{Revision: snap.R(42)})
	s.spec.AddLayout(snapInfo)
	c.Assert(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{
		// Layout result is sorted by mount path.
		{Dir: "/etc/foo.conf", Name: "/snap/vanguard/42/foo.conf", Options: []string{"bind", "rw", "x-snapd.kind=file", "x-snapd.origin=layout"}},
		{Dir: "/lib/mylink", Options: []string{"x-snapd.kind=symlink", "x-snapd.symlink=/snap/vanguard/42/link/target", "x-snapd.origin=layout"}},
		{Dir: "/lib/mytmp", Name: "tmpfs", Type: "tmpfs", Options: []string{"x-snapd.mode=01777", "x-snapd.origin=layout"}},
		{Dir: "/usr", Name: "/snap/vanguard/42/usr", Options: []string{"rbind", "rw", "x-snapd.origin=layout"}},
	})
}

func (s *specSuite) TestMountEntryFromExtraLayouts(c *C) {
	extraLayouts := []snap.Layout{
		{
			Path: "/test",
			Bind: "/usr/home/test",
			Mode: 0755,
		},
	}

	s.spec.AddExtraLayouts(extraLayouts)
	c.Assert(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{
		{Dir: "/test", Name: "/usr/home/test", Options: []string{"rbind", "rw", "x-snapd.origin=layout"}},
	})
}

func (s *specSuite) TestParallelInstanceMountEntryFromLayout(c *C) {
	snapInfo := snaptest.MockInfo(c, snapWithLayout, &snap.SideInfo{Revision: snap.R(42)})
	snapInfo.InstanceKey = "instance"
	s.spec.AddLayout(snapInfo)
	s.spec.AddOvername(snapInfo)
	c.Assert(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{
		// Parallel instance mappings come first
		{Dir: "/snap/vanguard", Name: "/snap/vanguard_instance", Options: []string{"rbind", "x-snapd.origin=overname"}},
		{Dir: "/var/snap/vanguard", Name: "/var/snap/vanguard_instance", Options: []string{"rbind", "x-snapd.origin=overname"}},
		// Layout result is sorted by mount path.
		{Dir: "/etc/foo.conf", Name: "/snap/vanguard/42/foo.conf", Options: []string{"bind", "rw", "x-snapd.kind=file", "x-snapd.origin=layout"}},
		{Dir: "/lib/mylink", Options: []string{"x-snapd.kind=symlink", "x-snapd.symlink=/snap/vanguard/42/link/target", "x-snapd.origin=layout"}},
		{Dir: "/lib/mytmp", Name: "tmpfs", Type: "tmpfs", Options: []string{"x-snapd.mode=01777", "x-snapd.origin=layout"}},
		{Dir: "/usr", Name: "/snap/vanguard/42/usr", Options: []string{"rbind", "rw", "x-snapd.origin=layout"}},
	})
}

func (s *specSuite) TestSpecificationUberclash(c *C) {
	// When everything clashes for access to /usr/foo, what happens?
	const uberclashYaml = `name: uberclash
version: 0
layout:
  /usr/foo:
    type: tmpfs
`
	snapInfo := snaptest.MockInfo(c, uberclashYaml, &snap.SideInfo{Revision: snap.R(42)})
	entry := osutil.MountEntry{Dir: "/usr/foo", Type: "tmpfs", Name: "tmpfs"}
	s.spec.AddMountEntry(entry)
	s.spec.AddUserMountEntry(entry)
	s.spec.AddLayout(snapInfo)
	c.Assert(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{
		{Dir: "/usr/foo", Type: "tmpfs", Name: "tmpfs", Options: []string{"x-snapd.origin=layout"}},
		// This is the non-layout entry, it was renamed to "foo-2"
		{Dir: "/usr/foo-2", Type: "tmpfs", Name: "tmpfs"},
	})
	c.Assert(s.spec.UserMountEntries(), DeepEquals, []osutil.MountEntry{
		// This is the user entry, it was _not_ renamed and it would clash with
		// /foo but there is no way to request things like that for now.
		{Dir: "/usr/foo", Type: "tmpfs", Name: "tmpfs"},
	})
}

func (s *specSuite) TestSpecificationMergedClash(c *C) {
	defaultEntry := osutil.MountEntry{
		Dir:  "/usr/foo",
		Type: "tmpfs",
		Name: "/here",
	}
	for _, td := range []struct {
		// Options for all the clashing mount entries
		Options [][]string
		// Expected options for the merged mount entry
		ExpectedOptions []string
	}{
		{
			// If all entries are read-only, the merged entry is also RO
			Options:         [][]string{{"noatime", "ro"}, {"ro"}},
			ExpectedOptions: []string{"noatime", "ro"},
		},
		{
			// If one entry is rbind, the recursiveness is preserved
			Options:         [][]string{{"bind", "rw"}, {"rbind", "ro"}},
			ExpectedOptions: []string{"rbind"},
		},
		{
			// With simple bind, no recursiveness is added
			Options:         [][]string{{"bind", "noatime"}, {"bind", "noexec"}},
			ExpectedOptions: []string{"noatime", "noexec", "bind"},
		},
		{
			// Ordinary flags are preserved
			Options:         [][]string{{"noexec", "noatime"}, {"noatime", "nomand"}, {"nodev"}},
			ExpectedOptions: []string{"noexec", "noatime", "nomand", "nodev"},
		},
	} {
		for _, options := range td.Options {
			entry := defaultEntry
			entry.Options = options
			s.spec.AddMountEntry(entry)
		}
		c.Check(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{
			{Dir: "/usr/foo", Name: "/here", Type: "tmpfs", Options: td.ExpectedOptions},
		}, Commentf("Clashing entries: %q", td.Options))

		// reset the spec after each iteration, or flags will leak
		s.spec = &mount.Specification{}
	}
}

func (s *specSuite) TestParallelInstanceMountEntriesNoInstanceKey(c *C) {
	snapInfo := &snap.Info{SideInfo: snap.SideInfo{RealName: "foo", Revision: snap.R(42)}}
	s.spec.AddOvername(snapInfo)
	c.Assert(s.spec.MountEntries(), HasLen, 0)
	c.Assert(s.spec.UserMountEntries(), HasLen, 0)
}

func (s *specSuite) TestParallelInstanceMountEntriesReal(c *C) {
	snapInfo := &snap.Info{SideInfo: snap.SideInfo{RealName: "foo", Revision: snap.R(42)}, InstanceKey: "instance"}
	s.spec.AddOvername(snapInfo)
	c.Assert(s.spec.MountEntries(), DeepEquals, []osutil.MountEntry{
		// /snap/foo_instance -> /snap/foo
		{Name: "/snap/foo_instance", Dir: "/snap/foo", Options: []string{"rbind", "x-snapd.origin=overname"}},
		// /var/snap/foo_instance -> /var/snap/foo
		{Name: "/var/snap/foo_instance", Dir: "/var/snap/foo", Options: []string{"rbind", "x-snapd.origin=overname"}},
	})
	c.Assert(s.spec.UserMountEntries(), HasLen, 0)
}

func (s *specSuite) TestAddUserEnsureDirHappy(c *C) {
	ensureDirSpecs := []*interfaces.EnsureDirSpec{
		{MustExistDir: "$HOME", EnsureDir: "$HOME/.local/share"},
		{MustExistDir: "$HOME", EnsureDir: "$HOME/other/other"},
		{MustExistDir: "/dir1", EnsureDir: "/dir1/dir2"},
	}
	err := s.spec.AddUserEnsureDirs(ensureDirSpecs)
	c.Assert(err, IsNil)
	c.Assert(s.spec.UserMountEntries(), DeepEquals, []osutil.MountEntry{
		{Dir: "$HOME/.local/share", Options: []string{"x-snapd.kind=ensure-dir", "x-snapd.must-exist-dir=$HOME"}},
		{Dir: "$HOME/other/other", Options: []string{"x-snapd.kind=ensure-dir", "x-snapd.must-exist-dir=$HOME"}},
		{Dir: "/dir1/dir2", Options: []string{"x-snapd.kind=ensure-dir", "x-snapd.must-exist-dir=/dir1"}},
	})
}

func (s *specSuite) TestAddUserEnsureErrorValidate(c *C) {
	ensureDirSpecs := []*interfaces.EnsureDirSpec{
		{MustExistDir: "$HOME", EnsureDir: "$HOME/.local/share"},
		{MustExistDir: "$HOME", EnsureDir: "$HOME/other/other"},
		{MustExistDir: "$SNAP_HOME", EnsureDir: "$SNAP_HOME/dir"},
		{MustExistDir: "/dir1", EnsureDir: "/dir1/dir2"},
	}
	err := s.spec.AddUserEnsureDirs(ensureDirSpecs)
	c.Assert(err, ErrorMatches, `internal error: cannot use ensure-dir mount specification: directory that must exist "\$SNAP_HOME" prefix "\$SNAP_HOME" is not allowed`)
}