2 Commits

Author SHA1 Message Date
c966da674a ci: drop Go 1.25 from matrix (go.mod requires 1.26) 2026-03-05 22:38:07 +00:00
1b451ee8b2 ci: add test pipeline with codecov, staticcheck, and race detection
- Build/test matrix: Go 1.25 + 1.26
- Race detection enabled for all tests
- Coverage uploaded to Codecov (latest Go only)
- staticcheck lint step
- Go module caching via setup-go
2026-03-05 22:36:39 +00:00
10 changed files with 14 additions and 130 deletions

View File

@@ -25,24 +25,14 @@ jobs:
go-version: ${{ matrix.go-version }} go-version: ${{ matrix.go-version }}
cache: true cache: true
- name: Install and start nginx - name: Run tests with race detection and coverage
run: | run: go test -race -coverprofile=coverage.out -covermode=atomic ./...
sudo apt-get update -qq
sudo apt-get install -y -qq nginx
sudo systemctl start nginx
sudo systemctl enable nginx
- name: Run tests (user)
run: go test -race -coverprofile=coverage-user.out -covermode=atomic ./...
- name: Run tests (root)
run: sudo go test -race -coverprofile=coverage-root.out -covermode=atomic ./...
- name: Upload coverage to Codecov - name: Upload coverage to Codecov
if: matrix.go-version == '1.26' if: matrix.go-version == '1.26'
uses: codecov/codecov-action@v5 uses: codecov/codecov-action@v5
with: with:
files: coverage-user.out,coverage-root.out files: coverage.out
token: ${{ secrets.CODECOV_TOKEN }} token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false fail_ci_if_error: false

View File

@@ -22,7 +22,6 @@ If your system isn't running (or targeting another system running) `systemctl`,
- [x] `systemctl is-enabled` - [x] `systemctl is-enabled`
- [x] `systemctl is-failed` - [x] `systemctl is-failed`
- [x] `systemctl mask` - [x] `systemctl mask`
- [x] `systemctl reload`
- [x] `systemctl restart` - [x] `systemctl restart`
- [x] `systemctl show` - [x] `systemctl show`
- [x] `systemctl start` - [x] `systemctl start`

View File

@@ -61,21 +61,6 @@ func TestFilterErr(t *testing.T) {
stderr: "Failed to do something unknown", stderr: "Failed to do something unknown",
want: ErrUnspecified, want: ErrUnspecified,
}, },
{
name: "does not exist with auth required prioritizes permission error",
stderr: "Unit nginx.service does not exist, proceeding anyway.\nFailed to mask unit: Interactive authentication required.",
want: ErrInsufficientPermissions,
},
{
name: "does not exist with access denied prioritizes permission error",
stderr: "Unit foo.service does not exist, proceeding anyway.\nAccess denied",
want: ErrInsufficientPermissions,
},
{
name: "does not exist with bus failure prioritizes bus error",
stderr: "Unit foo.service does not exist, proceeding anyway.\n$DBUS_SESSION_BUS_ADDRESS not set",
want: ErrBusFailure,
},
{ {
name: "unrecognized warning", name: "unrecognized warning",
stderr: "Warning: something benign happened", stderr: "Warning: something benign happened",

2
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/taigrr/systemctl module github.com/taigrr/systemctl
go 1.26.1 go 1.26

View File

@@ -1,67 +0,0 @@
//go:build linux
package systemctl
import (
"context"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
func TestReloadBuildsExpectedCommand(t *testing.T) {
tests := []struct {
name string
opts Options
want []string
}{
{
name: "system mode",
opts: Options{},
want: []string{"reload", "--system", "nginx.service"},
},
{
name: "user mode",
opts: Options{UserMode: true},
want: []string{"reload", "--user", "nginx.service"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
logFile := filepath.Join(tempDir, "args.log")
fakeSystemctl := filepath.Join(tempDir, "systemctl")
script := "#!/bin/sh\nprintf '%s\\n' \"$@\" > '" + logFile + "'\n"
if err := os.WriteFile(fakeSystemctl, []byte(script), 0o755); err != nil {
t.Fatalf("write fake systemctl: %v", err)
}
original := systemctl
systemctl = fakeSystemctl
t.Cleanup(func() {
systemctl = original
})
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := Reload(ctx, "nginx.service", tt.opts); err != nil {
t.Fatalf("Reload returned error: %v", err)
}
gotBytes, err := os.ReadFile(logFile)
if err != nil {
t.Fatalf("read captured args: %v", err)
}
got := strings.Fields(strings.TrimSpace(string(gotBytes)))
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("Reload args = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -104,13 +104,6 @@ func Restart(ctx context.Context, unit string, opts Options, args ...string) err
return restart(ctx, unit, opts, args...) return restart(ctx, unit, opts, args...)
} }
// Reload one or more units if they support reload.
//
// Any additional arguments are passed directly to the systemctl command.
func Reload(ctx context.Context, unit string, opts Options, args ...string) error {
return reload(ctx, unit, opts, args...)
}
// Show a selected property of a unit. Accepted properties are predefined in the // Show a selected property of a unit. Accepted properties are predefined in the
// properties subpackage to guarantee properties are valid and assist code-completion. // properties subpackage to guarantee properties are valid and assist code-completion.
// //

View File

@@ -44,10 +44,6 @@ func restart(_ context.Context, _ string, _ Options, _ ...string) error {
return nil return nil
} }
func reload(_ context.Context, _ string, _ Options, _ ...string) error {
return nil
}
func show(_ context.Context, _ string, _ properties.Property, _ Options, _ ...string) (string, error) { func show(_ context.Context, _ string, _ properties.Property, _ Options, _ ...string) (string, error) {
return "", nil return "", nil
} }

View File

@@ -115,12 +115,6 @@ func restart(ctx context.Context, unit string, opts Options, args ...string) err
return err return err
} }
func reload(ctx context.Context, unit string, opts Options, args ...string) error {
a := prepareArgs("reload", opts, append([]string{unit}, args...)...)
_, _, _, err := execute(ctx, a)
return err
}
func show(ctx context.Context, unit string, property properties.Property, opts Options, args ...string) (string, error) { func show(ctx context.Context, unit string, property properties.Property, opts Options, args ...string) (string, error) {
extra := append([]string{unit, "--property", string(property)}, args...) extra := append([]string{unit, "--property", string(property)}, args...)
a := prepareArgs("show", opts, extra...) a := prepareArgs("show", opts, extra...)

View File

@@ -290,7 +290,7 @@ func TestMask(t *testing.T) {
// try existing unit in user mode as user // try existing unit in user mode as user
{"syncthing", nil, Options{UserMode: true}, true}, {"syncthing", nil, Options{UserMode: true}, true},
// try nonexisting unit in system mode as user // try nonexisting unit in system mode as user
{"nonexistant", ErrInsufficientPermissions, Options{UserMode: false}, true}, {"nonexistant", ErrDoesNotExist, Options{UserMode: false}, true},
// try existing unit in system mode as user // try existing unit in system mode as user
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true}, {"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},
@@ -521,7 +521,7 @@ func TestUnmask(t *testing.T) {
// try existing unit in user mode as user // try existing unit in user mode as user
{"syncthing", nil, Options{UserMode: true}, true}, {"syncthing", nil, Options{UserMode: true}, true},
// try nonexisting unit in system mode as user // try nonexisting unit in system mode as user
{"nonexistant", ErrInsufficientPermissions, Options{UserMode: false}, true}, {"nonexistant", ErrDoesNotExist, Options{UserMode: false}, true},
// try existing unit in system mode as user // try existing unit in system mode as user
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true}, {"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},

22
util.go
View File

@@ -70,21 +70,7 @@ func prepareArgs(base string, opts Options, extra ...string) []string {
} }
func filterErr(stderr string) error { func filterErr(stderr string) error {
// Order matters: check higher-priority errors first.
// For example, `systemctl mask nginx` as a non-root user on a system
// without nginx prints both "does not exist, proceeding anyway" (a
// warning) and "Interactive authentication required" (the real error).
// Permission and bus errors must be checked before "does not exist" so
// the actual failure reason is returned.
switch { switch {
case strings.Contains(stderr, `Interactive authentication required`):
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `Access denied`):
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `DBUS_SESSION_BUS_ADDRESS`):
return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `is masked`):
return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `does not exist`): case strings.Contains(stderr, `does not exist`):
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `not found.`): case strings.Contains(stderr, `not found.`):
@@ -93,6 +79,14 @@ func filterErr(stderr string) error {
return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `No such file or directory`): case strings.Contains(stderr, `No such file or directory`):
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `Interactive authentication required`):
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `Access denied`):
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `DBUS_SESSION_BUS_ADDRESS`):
return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `is masked`):
return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `Failed`): case strings.Contains(stderr, `Failed`):
return errors.Join(ErrUnspecified, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrUnspecified, fmt.Errorf("stderr: %s", stderr))
default: default: