diff options
| author | Michael Vogt <mvo@ubuntu.com> | 2023-07-19 19:35:57 +0200 |
|---|---|---|
| committer | Michael Vogt <michael.vogt@gmail.com> | 2023-07-20 15:28:45 +0200 |
| commit | 2b3491b0390b1c92666308a32100bceb936e15f9 (patch) | |
| tree | 3f9c0c92f67e5f89a49ac41b2ec3b0662a01f4ba | |
| parent | 57c8e27e91e90d328422e82c60a907a8a1b7d200 (diff) | |
systemd: fix incorrect err handling in ensureMountUnitFile()
The ensureMountUnitFile() was not handling errors from osutil.EnsureFileState() correctly. This commit fixes this and adds a unit test.
| -rw-r--r-- | systemd/systemd.go | 4 | ||||
| -rw-r--r-- | systemd/systemd_test.go | 19 |
2 files changed, 21 insertions, 2 deletions
diff --git a/systemd/systemd.go b/systemd/systemd.go index 7bc92a46ca..48ca5f997e 100644 --- a/systemd/systemd.go +++ b/systemd/systemd.go @@ -1429,8 +1429,8 @@ func ensureMountUnitFile(u *MountUnitOptions) (mountUnitName string, modified mo if stateErr == osutil.ErrSameState { modified = mountUnchanged - } else if err != nil { - return "", mountUnchanged, err + } else if stateErr != nil { + return "", mountUnchanged, stateErr } return filepath.Base(mu), modified, nil diff --git a/systemd/systemd_test.go b/systemd/systemd_test.go index f177094f34..93e795417e 100644 --- a/systemd/systemd_test.go +++ b/systemd/systemd_test.go @@ -2411,3 +2411,22 @@ func (s *systemdErrorSuite) TestErrorStringNoSystemctl(c *C) { _, err := Version() c.Check(err, ErrorMatches, `systemctl command \[--version\] failed with: exec: "systemctl": executable file not found in \$PATH`) } + +func (s *systemdErrorSuite) TestEnsureMountUnitFileEnsureFileStateErr(c *C) { + dirs.SetRootDir(c.MkDir()) + defer dirs.SetRootDir("") + + restore := osutil.MockMountInfo("") + defer restore() + + mockSnapPath := filepath.Join(dirs.GlobalRootDir, "/var/lib/snappy/snaps/foo_1.0.snap") + makeMockFile(c, mockSnapPath) + + // trigger an error below by creating a directory with the + // same name as the mount unit file + err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/systemd/system/snap-snapname-123.mount"), 0755) + c.Assert(err, IsNil) + + _, err = New(SystemMode, nil).EnsureMountUnitFile("foo", "42", mockSnapPath, "/snap/snapname/123", "squashfs") + c.Assert(err, ErrorMatches, `rename .*/etc/systemd/system/snap-snapname-123.mount.* .*/etc/systemd/system/snap-snapname-123.mount: file exists`) +} |
