mirror of
https://github.com/taigrr/systemctl.git
synced 2026-04-02 10:38:48 -07:00
Compare commits
2 Commits
master
...
cd/ci-impr
| Author | SHA1 | Date | |
|---|---|---|---|
| c966da674a | |||
| 1b451ee8b2 |
16
.github/workflows/ci.yml
vendored
16
.github/workflows/ci.yml
vendored
@@ -25,24 +25,14 @@ jobs:
|
||||
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: Run tests with race detection and coverage
|
||||
run: go test -race -coverprofile=coverage.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
|
||||
files: coverage.out
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
fail_ci_if_error: false
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ If your system isn't running (or targeting another system running) `systemctl`,
|
||||
- [x] `systemctl is-enabled`
|
||||
- [x] `systemctl is-failed`
|
||||
- [x] `systemctl mask`
|
||||
- [x] `systemctl reload`
|
||||
- [x] `systemctl restart`
|
||||
- [x] `systemctl show`
|
||||
- [x] `systemctl start`
|
||||
|
||||
@@ -61,21 +61,6 @@ func TestFilterErr(t *testing.T) {
|
||||
stderr: "Failed to do something unknown",
|
||||
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",
|
||||
stderr: "Warning: something benign happened",
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -104,13 +104,6 @@ func Restart(ctx context.Context, unit string, opts Options, args ...string) err
|
||||
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
|
||||
// properties subpackage to guarantee properties are valid and assist code-completion.
|
||||
//
|
||||
|
||||
@@ -44,10 +44,6 @@ func restart(_ context.Context, _ string, _ Options, _ ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func reload(_ context.Context, _ string, _ Options, _ ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func show(_ context.Context, _ string, _ properties.Property, _ Options, _ ...string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -115,12 +115,6 @@ func restart(ctx context.Context, unit string, opts Options, args ...string) 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) {
|
||||
extra := append([]string{unit, "--property", string(property)}, args...)
|
||||
a := prepareArgs("show", opts, extra...)
|
||||
|
||||
@@ -290,7 +290,7 @@ func TestMask(t *testing.T) {
|
||||
// try existing unit in user mode as user
|
||||
{"syncthing", nil, Options{UserMode: true}, true},
|
||||
// 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
|
||||
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},
|
||||
|
||||
@@ -521,7 +521,7 @@ func TestUnmask(t *testing.T) {
|
||||
// try existing unit in user mode as user
|
||||
{"syncthing", nil, Options{UserMode: true}, true},
|
||||
// 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
|
||||
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},
|
||||
|
||||
|
||||
22
util.go
22
util.go
@@ -70,21 +70,7 @@ func prepareArgs(base string, opts Options, extra ...string) []string {
|
||||
}
|
||||
|
||||
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 {
|
||||
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`):
|
||||
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||
case strings.Contains(stderr, `not found.`):
|
||||
@@ -93,6 +79,14 @@ func filterErr(stderr string) error {
|
||||
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`):
|
||||
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`):
|
||||
return errors.Join(ErrUnspecified, fmt.Errorf("stderr: %s", stderr))
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user