Skip to content

Commit da4df5b

Browse files
many: centralize and use common systemd mock in snapshotmgr test (#17139)
* many: centralize and reuse the common systemd mock in tests * o/snapshotstate: use systemd mock in snapshotmgr test
1 parent 7dd3cc8 commit da4df5b

8 files changed

Lines changed: 184 additions & 260 deletions

File tree

overlord/hookstate/ctlcmd/mount_test.go

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,10 @@ import (
3232
"github.com/snapcore/snapd/overlord/state"
3333
"github.com/snapcore/snapd/snap"
3434
"github.com/snapcore/snapd/systemd"
35+
"github.com/snapcore/snapd/systemd/systemdtest"
3536
"github.com/snapcore/snapd/testutil"
3637
)
3738

38-
type ResultForEnsureMountUnitFile struct {
39-
path string
40-
err error
41-
}
42-
43-
type FakeSystemdForMount struct {
44-
systemd.Systemd
45-
46-
RemoveMountUnitFileCalls []string
47-
RemoveMountUnitFileResult error
48-
49-
EnsureMountUnitFileCalls []*systemd.MountUnitOptions
50-
EnsureMountUnitFileResult ResultForEnsureMountUnitFile
51-
}
52-
53-
func (s *FakeSystemdForMount) RemoveMountUnitFile(baseDir string) error {
54-
s.RemoveMountUnitFileCalls = append(s.RemoveMountUnitFileCalls, baseDir)
55-
return s.RemoveMountUnitFileResult
56-
}
57-
58-
func (s *FakeSystemdForMount) EnsureMountUnitFile(options *systemd.MountUnitOptions) (string, error) {
59-
s.EnsureMountUnitFileCalls = append(s.EnsureMountUnitFileCalls, options)
60-
return s.EnsureMountUnitFileResult.path, s.EnsureMountUnitFileResult.err
61-
}
62-
6339
func CopyMap(m map[string]any) map[string]any {
6440
cp := make(map[string]any)
6541
for k, v := range m {
@@ -96,7 +72,7 @@ type mountSuite struct {
9672
mockContext *hookstate.Context
9773
mockHandler *hooktest.MockHandler
9874
hookTask *state.Task
99-
sysd *FakeSystemdForMount
75+
sysd *systemdtest.FakeSystemd
10076
// A connection state for a snap using the mount interface with the plug
10177
// properly configured, which we'll be reusing in different test cases
10278
regularConnState map[string]any
@@ -158,7 +134,7 @@ func (s *mountSuite) SetUpTest(c *C) {
158134
}
159135
s.hookTask = task
160136

161-
s.sysd = &FakeSystemdForMount{}
137+
s.sysd = &systemdtest.FakeSystemd{}
162138
s.AddCleanup(systemd.MockNewSystemd(func(be systemd.Backend, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
163139
return s.sysd
164140
}))
@@ -308,7 +284,7 @@ func (s *mountSuite) TestMissingProperPlug(c *C) {
308284
func (s *mountSuite) TestUnitCreationFailure(c *C) {
309285
s.injectSnapWithProperPlug(c)
310286

311-
s.sysd.EnsureMountUnitFileResult = ResultForEnsureMountUnitFile{"", errors.New("creation error")}
287+
s.sysd.EnsureMountUnitFileResult.Err = errors.New("creation error")
312288

313289
_, _, err := ctlcmd.Run(s.mockContext, []string{"mount", "-t", "ext4", "/src", "/dest"}, 0, nil)
314290
c.Check(err, ErrorMatches, `cannot ensure mount unit: creation error`)
@@ -328,7 +304,7 @@ func (s *mountSuite) TestUnitCreationFailure(c *C) {
328304
func (s *mountSuite) TestHappy(c *C) {
329305
s.injectSnapWithProperPlug(c)
330306

331-
s.sysd.EnsureMountUnitFileResult = ResultForEnsureMountUnitFile{"/path/unit.mount", nil}
307+
s.sysd.EnsureMountUnitFileResult.Path = "/path/unit.mount"
332308

333309
_, _, err := ctlcmd.Run(s.mockContext, []string{"mount", "--persistent", "-t", "ext4", "-o", "sync,rw", "/src", "/dest"}, 0, nil)
334310
c.Check(err, IsNil)
@@ -349,7 +325,7 @@ func (s *mountSuite) TestHappy(c *C) {
349325
func (s *mountSuite) TestHappyWithVariableExpansion(c *C) {
350326
s.injectSnapWithProperPlug(c)
351327

352-
s.sysd.EnsureMountUnitFileResult = ResultForEnsureMountUnitFile{"/path/unit.mount", nil}
328+
s.sysd.EnsureMountUnitFileResult.Path = "/path/unit.mount"
353329

354330
// Now try with $SNAP_* variables in the paths
355331
snapDataDir := filepath.Join(dirs.SnapDataDir, "snap1", "1")
@@ -372,7 +348,7 @@ func (s *mountSuite) TestHappyWithVariableExpansion(c *C) {
372348
func (s *mountSuite) TestHappyWithCommasInPath(c *C) {
373349
s.injectSnapWithProperPlug(c)
374350

375-
s.sysd.EnsureMountUnitFileResult = ResultForEnsureMountUnitFile{"/path/unit.mount", nil}
351+
s.sysd.EnsureMountUnitFileResult.Path = "/path/unit.mount"
376352

377353
// Now try with commas in the paths
378354
_, _, err := ctlcmd.Run(s.mockContext, []string{"mount", "-o", "ro", "/dev/dma_heap/qcom,qseecom", "/dest,with,commas"}, 0, nil)
@@ -393,7 +369,7 @@ func (s *mountSuite) TestHappyWithCommasInPath(c *C) {
393369
func (s *mountSuite) TestHappyNFS(c *C) {
394370
s.injectSnapWithProperPlug(c)
395371

396-
s.sysd.EnsureMountUnitFileResult = ResultForEnsureMountUnitFile{"/path/unit.mount", nil}
372+
s.sysd.EnsureMountUnitFileResult.Path = "/path/unit.mount"
397373

398374
// Now try with commas in the paths
399375
_, _, err := ctlcmd.Run(s.mockContext, []string{"mount", "-o", "rw", "-t", "nfs", "localhost:/var/share", "/nfs-dest"}, 0, nil)
@@ -415,7 +391,7 @@ func (s *mountSuite) TestHappyNFS(c *C) {
415391
func (s *mountSuite) TestHappyCIFS(c *C) {
416392
s.injectSnapWithProperPlug(c)
417393

418-
s.sysd.EnsureMountUnitFileResult = ResultForEnsureMountUnitFile{"/path/unit.mount", nil}
394+
s.sysd.EnsureMountUnitFileResult.Path = "/path/unit.mount"
419395

420396
// Now try with commas in the paths
421397
_, _, err := ctlcmd.Run(s.mockContext, []string{"mount", "-o", "rw,guest", "-t", "cifs", "//10.0.0.1/share/path", "/cifs-dest"}, 0, nil)
@@ -437,7 +413,7 @@ func (s *mountSuite) TestHappyCIFS(c *C) {
437413
func (s *mountSuite) TestEnsureMountUnitFailed(c *C) {
438414
s.injectSnapWithProperPlug(c)
439415

440-
s.sysd.EnsureMountUnitFileResult = ResultForEnsureMountUnitFile{"", errors.New("some error")}
416+
s.sysd.EnsureMountUnitFileResult.Err = errors.New("some error")
441417

442418
_, _, err := ctlcmd.Run(s.mockContext, []string{"mount", "--persistent", "-t", "ext4", "-o", "sync,rw", "/src", "/dest"}, 0, nil)
443419
c.Check(err, ErrorMatches, `cannot ensure mount unit: some error`)
@@ -460,7 +436,7 @@ func (s *mountSuite) TestEnsureMountUnitFailed(c *C) {
460436
func (s *mountSuite) TestEnsureMountUnitFailedRemoveFailed(c *C) {
461437
s.injectSnapWithProperPlug(c)
462438

463-
s.sysd.EnsureMountUnitFileResult = ResultForEnsureMountUnitFile{"", errors.New("some error")}
439+
s.sysd.EnsureMountUnitFileResult.Err = errors.New("some error")
464440
s.sysd.RemoveMountUnitFileResult = errors.New("some other error")
465441

466442
_, _, err := ctlcmd.Run(s.mockContext, []string{"mount", "--persistent", "-t", "ext4", "-o", "sync,rw", "/src", "/dest"}, 0, nil)

overlord/hookstate/ctlcmd/umount_test.go

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,17 @@ import (
3131
"github.com/snapcore/snapd/overlord/state"
3232
"github.com/snapcore/snapd/snap"
3333
"github.com/snapcore/snapd/systemd"
34+
"github.com/snapcore/snapd/systemd/systemdtest"
3435
"github.com/snapcore/snapd/testutil"
3536
)
3637

37-
type ParamsForListMountUnits struct {
38-
snapName string
39-
origin string
40-
}
41-
42-
type ResultForListMountUnits struct {
43-
units []string
44-
err error
45-
}
46-
47-
type FakeSystemdForUmount struct {
48-
systemd.Systemd
49-
50-
RemoveMountUnitFileCalls []string
51-
RemoveMountUnitFileCallsResult error
52-
53-
ListMountUnitsCalls []*ParamsForListMountUnits
54-
ListMountUnitsCallsResult ResultForListMountUnits
55-
}
56-
57-
func (s *FakeSystemdForUmount) RemoveMountUnitFile(mountedDir string) error {
58-
s.RemoveMountUnitFileCalls = append(s.RemoveMountUnitFileCalls, mountedDir)
59-
return s.RemoveMountUnitFileCallsResult
60-
}
61-
62-
func (s *FakeSystemdForUmount) ListMountUnits(snapName, origin string) ([]string, error) {
63-
s.ListMountUnitsCalls = append(s.ListMountUnitsCalls,
64-
&ParamsForListMountUnits{snapName, origin})
65-
return s.ListMountUnitsCallsResult.units, s.ListMountUnitsCallsResult.err
66-
}
67-
6838
type umountSuite struct {
6939
testutil.BaseTest
7040
state *state.State
7141
mockContext *hookstate.Context
7242
mockHandler *hooktest.MockHandler
7343
hookTask *state.Task
74-
sysd *FakeSystemdForUmount
44+
sysd *systemdtest.FakeSystemd
7545
}
7646

7747
var _ = Suite(&umountSuite{})
@@ -94,7 +64,7 @@ func (s *umountSuite) SetUpTest(c *C) {
9464

9565
s.hookTask = task
9666

97-
s.sysd = &FakeSystemdForUmount{}
67+
s.sysd = &systemdtest.FakeSystemd{}
9868
s.AddCleanup(systemd.MockNewSystemd(func(be systemd.Backend, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
9969
return s.sysd
10070
}))
@@ -111,52 +81,52 @@ func (s *umountSuite) TestMissingParameters(c *C) {
11181
}
11282

11383
func (s *umountSuite) TestListUnitFailure(c *C) {
114-
s.sysd.ListMountUnitsCallsResult = ResultForListMountUnits{[]string{}, errors.New("list error")}
84+
s.sysd.ListMountUnitsResult.Err = errors.New("list error")
11585

11686
_, _, err := ctlcmd.Run(s.mockContext, []string{"umount", "/dest"}, 0, nil)
11787
c.Check(err, ErrorMatches, `cannot retrieve list of mount units: list error`)
118-
c.Check(s.sysd.ListMountUnitsCalls, DeepEquals, []*ParamsForListMountUnits{
119-
{"snap1", "mount-control"},
88+
c.Check(s.sysd.ListMountUnitsCalls, DeepEquals, []systemdtest.ParamsForListMountUnits{
89+
{SnapName: "snap1", Origin: "mount-control"},
12090
})
12191
c.Check(s.sysd.RemoveMountUnitFileCalls, HasLen, 0)
12292
}
12393

12494
func (s *umountSuite) TestUnitNotFound(c *C) {
125-
s.sysd.ListMountUnitsCallsResult = ResultForListMountUnits{[]string{
95+
s.sysd.ListMountUnitsResult.MountPoints = []string{
12696
"/this/is",
12797
"/not/our/mount/destination",
128-
}, nil}
98+
}
12999

130100
_, _, err := ctlcmd.Run(s.mockContext, []string{"umount", "/dest"}, 0, nil)
131101
c.Check(err, ErrorMatches, `cannot find the given mount`)
132-
c.Check(s.sysd.ListMountUnitsCalls, DeepEquals, []*ParamsForListMountUnits{
133-
{"snap1", "mount-control"},
102+
c.Check(s.sysd.ListMountUnitsCalls, DeepEquals, []systemdtest.ParamsForListMountUnits{
103+
{SnapName: "snap1", Origin: "mount-control"},
134104
})
135105
c.Check(s.sysd.RemoveMountUnitFileCalls, HasLen, 0)
136106
}
137107

138108
func (s *umountSuite) TestRemovalError(c *C) {
139-
s.sysd.ListMountUnitsCallsResult = ResultForListMountUnits{[]string{"/dest"}, nil}
109+
s.sysd.ListMountUnitsResult.MountPoints = []string{"/dest"}
140110

141-
s.sysd.RemoveMountUnitFileCallsResult = errors.New("remove error")
111+
s.sysd.RemoveMountUnitFileResult = errors.New("remove error")
142112

143113
_, _, err := ctlcmd.Run(s.mockContext, []string{"umount", "/dest"}, 0, nil)
144114
c.Check(err, ErrorMatches, `cannot remove mount unit: remove error`)
145-
c.Check(s.sysd.ListMountUnitsCalls, DeepEquals, []*ParamsForListMountUnits{
146-
{"snap1", "mount-control"},
115+
c.Check(s.sysd.ListMountUnitsCalls, DeepEquals, []systemdtest.ParamsForListMountUnits{
116+
{SnapName: "snap1", Origin: "mount-control"},
147117
})
148118
c.Check(s.sysd.RemoveMountUnitFileCalls, DeepEquals, []string{
149119
"/dest",
150120
})
151121
}
152122

153123
func (s *umountSuite) TestHappy(c *C) {
154-
s.sysd.ListMountUnitsCallsResult = ResultForListMountUnits{[]string{"/dest"}, nil}
124+
s.sysd.ListMountUnitsResult.MountPoints = []string{"/dest"}
155125

156126
_, _, err := ctlcmd.Run(s.mockContext, []string{"umount", "/dest"}, 0, nil)
157127
c.Check(err, IsNil)
158-
c.Check(s.sysd.ListMountUnitsCalls, DeepEquals, []*ParamsForListMountUnits{
159-
{"snap1", "mount-control"},
128+
c.Check(s.sysd.ListMountUnitsCalls, DeepEquals, []systemdtest.ParamsForListMountUnits{
129+
{SnapName: "snap1", Origin: "mount-control"},
160130
})
161131
c.Check(s.sysd.RemoveMountUnitFileCalls, DeepEquals, []string{
162132
"/dest",

overlord/snapshotstate/backend/mountunit_test.go

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/snapcore/snapd/overlord/snapshotstate/backend"
2828
"github.com/snapcore/snapd/systemd"
29+
"github.com/snapcore/snapd/systemd/systemdtest"
2930
"github.com/snapcore/snapd/testutil"
3031
)
3132

@@ -35,34 +36,13 @@ type mountunitSuite struct {
3536

3637
var _ = Suite(&mountunitSuite{})
3738

38-
type fakeSystemd struct {
39-
systemd.Systemd
40-
41-
listMountUnitsCalls []listMountUnitsCall
42-
listMountUnitsResult listMountUnitsResult
43-
}
44-
45-
type listMountUnitsCall struct {
46-
snapName, origin string
47-
}
48-
49-
type listMountUnitsResult struct {
50-
mountPoints []string
51-
err error
52-
}
53-
54-
func (s *fakeSystemd) ListMountUnits(snapName, origin string) ([]string, error) {
55-
s.listMountUnitsCalls = append(s.listMountUnitsCalls, listMountUnitsCall{snapName, origin})
56-
return s.listMountUnitsResult.mountPoints, s.listMountUnitsResult.err
57-
}
58-
5939
func (s *mountunitSuite) TestListMountControlMountPointsHappy(c *C) {
6040
returnedMounts := []string{"/var/snap/a-snap/x1/data/mymount", "/var/snap/a-snap/common/media"}
6141

62-
var sysd *fakeSystemd
63-
restore := systemd.MockNewSystemd(func(_ systemd.Backend, _ string, mode systemd.InstanceMode, rep systemd.Reporter) systemd.Systemd {
64-
sysd = &fakeSystemd{}
65-
sysd.listMountUnitsResult = listMountUnitsResult{mountPoints: returnedMounts}
42+
var sysd *systemdtest.FakeSystemd
43+
restore := systemd.MockNewSystemd(func(_ systemd.Backend, _ string, _ systemd.InstanceMode, _ systemd.Reporter) systemd.Systemd {
44+
sysd = &systemdtest.FakeSystemd{}
45+
sysd.ListMountUnitsResult.MountPoints = returnedMounts
6646
return sysd
6747
})
6848
defer restore()
@@ -71,16 +51,16 @@ func (s *mountunitSuite) TestListMountControlMountPointsHappy(c *C) {
7151
c.Assert(err, IsNil)
7252
c.Check(mountPts, DeepEquals, returnedMounts)
7353

74-
c.Check(sysd.listMountUnitsCalls, DeepEquals, []listMountUnitsCall{{snapName: "a-snap", origin: "mount-control"}})
54+
c.Check(sysd.ListMountUnitsCalls, DeepEquals, []systemdtest.ParamsForListMountUnits{{SnapName: "a-snap", Origin: "mount-control"}})
7555
}
7656

7757
func (s *mountunitSuite) TestListMountControlMountPointsError(c *C) {
7858
expectedErr := errors.New("systemd failure")
7959

80-
var sysd *fakeSystemd
81-
restore := systemd.MockNewSystemd(func(_ systemd.Backend, _ string, mode systemd.InstanceMode, rep systemd.Reporter) systemd.Systemd {
82-
sysd = &fakeSystemd{}
83-
sysd.listMountUnitsResult = listMountUnitsResult{err: expectedErr}
60+
var sysd *systemdtest.FakeSystemd
61+
restore := systemd.MockNewSystemd(func(_ systemd.Backend, _ string, _ systemd.InstanceMode, _ systemd.Reporter) systemd.Systemd {
62+
sysd = &systemdtest.FakeSystemd{}
63+
sysd.ListMountUnitsResult.Err = expectedErr
8464
return sysd
8565
})
8666
defer restore()
@@ -89,14 +69,14 @@ func (s *mountunitSuite) TestListMountControlMountPointsError(c *C) {
8969
c.Check(err, Equals, expectedErr)
9070
c.Check(mountPts, IsNil)
9171

92-
c.Check(sysd.listMountUnitsCalls, DeepEquals, []listMountUnitsCall{{snapName: "a-snap", origin: "mount-control"}})
72+
c.Check(sysd.ListMountUnitsCalls, DeepEquals, []systemdtest.ParamsForListMountUnits{{SnapName: "a-snap", Origin: "mount-control"}})
9373
}
9474

9575
func (s *mountunitSuite) TestListMountControlMountPointsEmpty(c *C) {
96-
var sysd *fakeSystemd
76+
var sysd *systemdtest.FakeSystemd
9777
restore := systemd.MockNewSystemd(func(_ systemd.Backend, _ string, _ systemd.InstanceMode, _ systemd.Reporter) systemd.Systemd {
98-
sysd = &fakeSystemd{}
99-
sysd.listMountUnitsResult = listMountUnitsResult{mountPoints: []string{}}
78+
sysd = &systemdtest.FakeSystemd{}
79+
sysd.ListMountUnitsResult.MountPoints = []string{}
10080
return sysd
10181
})
10282
defer restore()
@@ -105,5 +85,5 @@ func (s *mountunitSuite) TestListMountControlMountPointsEmpty(c *C) {
10585
c.Assert(err, IsNil)
10686
c.Check(mountPts, DeepEquals, []string{})
10787

108-
c.Check(sysd.listMountUnitsCalls, DeepEquals, []listMountUnitsCall{{snapName: "a-snap", origin: "mount-control"}})
88+
c.Check(sysd.ListMountUnitsCalls, DeepEquals, []systemdtest.ParamsForListMountUnits{{SnapName: "a-snap", Origin: "mount-control"}})
10989
}

overlord/snapshotstate/export_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,3 @@ func MockGetSnapDirOptions(f func(*state.State, string) (*dirs.SnapDirOptions, e
216216
getSnapDirOpts = old
217217
}
218218
}
219-
220-
func MockListMountControlMountPoints(f func(string) ([]string, error)) (restore func()) {
221-
old := listMountControlMountPoints
222-
listMountControlMountPoints = f
223-
return func() {
224-
listMountControlMountPoints = old
225-
}
226-
}

overlord/snapshotstate/snapshotmgr.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ var (
5656

5757
autoExpirationInterval = time.Hour * 24 // interval between forgetExpiredSnapshots runs as part of Ensure()
5858

59-
getSnapDirOpts = snapstate.GetSnapDirOpts
60-
listMountControlMountPoints = backend.ListMountControlMountPoints
59+
getSnapDirOpts = snapstate.GetSnapDirOpts
6160
)
6261

6362
// SnapshotManager takes snapshots of active snaps
@@ -272,7 +271,7 @@ func mapMountPointsInGlobalDataDirsToExcludes(si *snap.Info, mountPoints []strin
272271
// excludeMountControlMountPoints appends any currently-active mount-control
273272
// mount points under the snap's data directories to s.Options.
274273
func (s *snapshotSetup) excludeMountControlMountPoints(si *snap.Info) error {
275-
mountPts, err := listMountControlMountPoints(si.InstanceName())
274+
mountPts, err := backend.ListMountControlMountPoints(si.InstanceName())
276275
if err != nil {
277276
return fmt.Errorf("cannot list mount-control units for %q: %v", si.InstanceName(), err)
278277
}

0 commit comments

Comments
 (0)