From b7d12d3c62d864fe46491f9990b23b25d1cef9d8 Mon Sep 17 00:00:00 2001 From: Alberto Mardegan Date: Wed, 1 Jun 2022 15:06:15 +0300 Subject: [PATCH] systemd: improve error handling for systemd-sysctl command From a comment by @mvo5 on a code review. --- systemd/sysctl.go | 5 ++++- systemd/sysctl_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/systemd/sysctl.go b/systemd/sysctl.go index bc78cb34113..9d9bc5ec64f 100644 --- a/systemd/sysctl.go +++ b/systemd/sysctl.go @@ -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 diff --git a/systemd/sysctl_test.go b/systemd/sysctl_test.go index 48c8adf5bb5..d78696e1883 100644 --- a/systemd/sysctl_test.go +++ b/systemd/sysctl_test.go @@ -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