Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ All functions return a predefined error type, and it is highly recommended these
## Context support

All calls into this library support go's `context` functionality.
Therefore, blocking calls can time out according to the caller's needs, and the returned error should be checked to see if a timeout occurred (`ErrExecTimeout`).
Therefore, blocking calls can time out according to the caller's needs.
Use `errors.Is(err, ErrExecTimeout)` to check for context cancellation or timeout, and `errors.Is(err, context.Canceled)` or `errors.Is(err, context.DeadlineExceeded)` to distinguish the cause.


## Simple example
Expand Down
69 changes: 69 additions & 0 deletions execute_context_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//go:build linux

package systemctl

import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"time"
)

func TestExecuteContextErrors(t *testing.T) {
fakeSystemctl := filepath.Join(t.TempDir(), "systemctl")
// Replace the shell so cancellation cannot leave a child holding the pipes.
if err := os.WriteFile(fakeSystemctl, []byte("#!/bin/sh\nprintf 'Failed to connect to bus' >&2\nexec sleep 30\n"), 0o755); err != nil {
t.Fatal(err)
}
original := systemctl
systemctl = fakeSystemctl
t.Cleanup(func() { systemctl = original })

tests := []struct {
name string
context func() (context.Context, context.CancelFunc)
want error
}{
{
name: "already canceled",
context: func() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx, cancel
},
want: context.Canceled,
},
{
name: "already expired",
context: func() (context.Context, context.CancelFunc) {
return context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
},
want: context.DeadlineExceeded,
},
{
name: "deadline during execution",
context: func() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 200*time.Millisecond)
},
want: context.DeadlineExceeded,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := test.context()
defer cancel()
_, _, code, err := execute(ctx, []string{"start", "example.service"})
if !errors.Is(err, test.want) {
t.Errorf("execute() error = %v, want %v", err, test.want)
}
if !errors.Is(err, ErrExecTimeout) {
t.Errorf("execute() error = %v, want ErrExecTimeout", err)
}
if code == 0 {
t.Error("execute() exit code = 0, want failure")
}
})
}
}
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
code = 1
}

// Context cancellation takes precedence over stderr classification. A killed
// process may have emitted a diagnostic unrelated to why it was terminated.
if err != nil && ctx.Err() != nil {
return output, warnings, code, errors.Join(ErrExecTimeout, ctx.Err(), err)
}

if code == killed {
return output, warnings, code, ErrExecTimeout
}
Expand Down
Loading