Skip to content

Commit

Permalink
systemd: improve error handling for systemd-sysctl command
Browse files Browse the repository at this point in the history
From a comment by @mvo5 on a code review.
  • Loading branch information
mardy committed Jun 1, 2022
1 parent ca83bdb commit b7d12d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion systemd/sysctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ var (
var systemdSysctlCmd = func(args ...string) error {
bs, err := exec.Command(systemdSysctlPath, args...).CombinedOutput()
if err != nil {
exitCode, _ := osutil.ExitCode(err)
exitCode, err := osutil.ExitCode(err)
if err != nil {
return err
}
return fmt.Errorf("%s invoked with %v failed with exit status %d: %s", systemdSysctlPath, args, exitCode, bs)
}
return nil
Expand Down
7 changes: 7 additions & 0 deletions systemd/sysctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ exit 1
c.Assert(err, ErrorMatches, `(?m)systemd-sysctl invoked with \[--prefix net\] failed with exit status 1: foo`)
}

func (s *sysctlSuite) TestSysctlFailedExec(c *C) {
defer systemd.MockSystemdSysctlPath("/i/bet/this/does/not/exist/systemd-sysctl")()

err := systemd.Sysctl(nil)
c.Assert(err, ErrorMatches, `fork/exec /i/bet/this/does/not/exist/systemd-sysctl: no such file or directory`)
}

func (s *sysctlSuite) TestMockSystemdSysctl(c *C) {
var capturedArgs []string
var sysctlErr error
Expand Down

0 comments on commit b7d12d3

Please sign in to comment.