Skip to content

Commit 374758a

Browse files
committed
systemd: do not fail 'stop' if service unit not installed
If a service unit configuration file doesn't exist on disk, 'systemctl stop' fails with error code 5 [1]. To keep 'git-bundle-server web-server stop' from failing when 'web-server start' was not called before it, do not cause the program to fail when the error code indicates the service isn't installed. Add a test covering this case. [1] https://freedesktop.org/software/systemd/man/systemd.exec.html#Process%20Exit%20Codes Signed-off-by: Victoria Dye <[email protected]>
1 parent db1c776 commit 374758a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

internal/daemon/systemd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Type=simple
1818
ExecStart={{sq_escape .Program}}{{range .Arguments}} {{sq_escape .}}{{end}}
1919
`
2020

21+
const SystemdUnitNotInstalledErrorCode int = 5
22+
2123
type systemd struct {
2224
user common.UserProvider
2325
cmdExec common.CommandExecutor
@@ -107,7 +109,7 @@ func (s *systemd) Stop(label string) error {
107109
return err
108110
}
109111

110-
if exitCode != 0 {
112+
if exitCode != 0 && exitCode != SystemdUnitNotInstalledErrorCode {
111113
return fmt.Errorf("'systemctl stop' exited with status %d", exitCode)
112114
}
113115

internal/daemon/systemd_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,19 @@ func TestSystemd_Stop(t *testing.T) {
313313
assert.NotNil(t, err)
314314
mock.AssertExpectationsForObjects(t, testCommandExecutor)
315315
})
316+
317+
// Reset the mock structure between tests
318+
testCommandExecutor.Mock = mock.Mock{}
319+
320+
// Test #3: service unit not found still succeeds
321+
t.Run("Succeeds if service unit not installed", func(t *testing.T) {
322+
testCommandExecutor.On("Run",
323+
mock.AnythingOfType("string"),
324+
mock.AnythingOfType("[]string"),
325+
).Return(daemon.SystemdUnitNotInstalledErrorCode, nil).Once()
326+
327+
err := systemd.Stop(basicDaemonConfig.Label)
328+
assert.Nil(t, err)
329+
mock.AssertExpectationsForObjects(t, testCommandExecutor)
330+
})
316331
}

0 commit comments

Comments
 (0)