mirror of
https://github.com/taigrr/systemctl.git
synced 2026-04-02 02:28:50 -07:00
Compare commits
3 Commits
cd/ci-impr
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| da8db0d3a3 | |||
| 038fbe1a17 | |||
| 7253c912ca |
65
.github/workflows/ci.yml
vendored
Normal file
65
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [master]
|
||||||
|
pull_request:
|
||||||
|
branches: [master]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: Test (Go ${{ matrix.go-version }})
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: ["1.26"]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Go ${{ matrix.go-version }}
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go-version }}
|
||||||
|
cache: true
|
||||||
|
|
||||||
|
- name: Install and start nginx
|
||||||
|
run: |
|
||||||
|
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
|
||||||
|
if: matrix.go-version == '1.26'
|
||||||
|
uses: codecov/codecov-action@v5
|
||||||
|
with:
|
||||||
|
files: coverage-user.out,coverage-root.out
|
||||||
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
fail_ci_if_error: false
|
||||||
|
|
||||||
|
lint:
|
||||||
|
name: Lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: "1.26"
|
||||||
|
cache: true
|
||||||
|
|
||||||
|
- name: Run staticcheck
|
||||||
|
uses: dominikh/staticcheck-action@v1
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
install-go: false
|
||||||
@@ -22,6 +22,7 @@ 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`
|
||||||
|
|||||||
@@ -61,6 +61,21 @@ 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
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module github.com/taigrr/systemctl
|
module github.com/taigrr/systemctl
|
||||||
|
|
||||||
go 1.26
|
go 1.26.1
|
||||||
|
|||||||
67
reload_linux_test.go
Normal file
67
reload_linux_test.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
//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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -104,6 +104,13 @@ 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.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -44,6 +44,10 @@ 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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,6 +115,12 @@ 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...)
|
||||||
|
|||||||
@@ -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", ErrDoesNotExist, Options{UserMode: false}, true},
|
{"nonexistant", ErrInsufficientPermissions, 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", ErrDoesNotExist, Options{UserMode: false}, true},
|
{"nonexistant", ErrInsufficientPermissions, 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
22
util.go
@@ -70,15 +70,13 @@ 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, `does not exist`):
|
|
||||||
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
|
||||||
case strings.Contains(stderr, `not found.`):
|
|
||||||
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
|
||||||
case strings.Contains(stderr, `not loaded.`):
|
|
||||||
return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr))
|
|
||||||
case strings.Contains(stderr, `No such file or directory`):
|
|
||||||
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
|
||||||
case strings.Contains(stderr, `Interactive authentication required`):
|
case strings.Contains(stderr, `Interactive authentication required`):
|
||||||
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
|
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
|
||||||
case strings.Contains(stderr, `Access denied`):
|
case strings.Contains(stderr, `Access denied`):
|
||||||
@@ -87,6 +85,14 @@ func filterErr(stderr string) error {
|
|||||||
return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr))
|
return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr))
|
||||||
case strings.Contains(stderr, `is masked`):
|
case strings.Contains(stderr, `is masked`):
|
||||||
return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr))
|
return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `does not exist`):
|
||||||
|
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `not found.`):
|
||||||
|
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `not loaded.`):
|
||||||
|
return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `No such file or directory`):
|
||||||
|
return errors.Join(ErrDoesNotExist, 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:
|
||||||
|
|||||||
Reference in New Issue
Block a user