mirror of
https://github.com/taigrr/systemctl.git
synced 2026-04-02 02:28:50 -07:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
7bd5bef0cb
|
|||
| a82f845b84 | |||
| c9e7f79f8c | |||
| 0075dc6b4d |
@@ -5,7 +5,6 @@ This library aims at providing idiomatic `systemctl` bindings for go developers,
|
|||||||
This tool tries to take guesswork out of arbitrarily shelling out to `systemctl` by providing a structured, thoroughly-tested wrapper for the `systemctl` functions most-likely to be used in a system program.
|
This tool tries to take guesswork out of arbitrarily shelling out to `systemctl` by providing a structured, thoroughly-tested wrapper for the `systemctl` functions most-likely to be used in a system program.
|
||||||
|
|
||||||
If your system isn't running (or targeting another system running) `systemctl`, this library will be of little use to you.
|
If your system isn't running (or targeting another system running) `systemctl`, this library will be of little use to you.
|
||||||
In fact, if `systemctl` isn't found in the `PATH`, this library will panic.
|
|
||||||
|
|
||||||
## What is systemctl
|
## What is systemctl
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ var (
|
|||||||
// Masked units can only be unmasked, but something else was attempted
|
// Masked units can only be unmasked, but something else was attempted
|
||||||
// Unmask the unit before enabling or disabling it
|
// Unmask the unit before enabling or disabling it
|
||||||
ErrMasked = errors.New("unit masked")
|
ErrMasked = errors.New("unit masked")
|
||||||
// If this error occurs, the library isn't entirely useful, as it causes a panic
|
|
||||||
// Make sure systemctl is in the PATH before calling again
|
// Make sure systemctl is in the PATH before calling again
|
||||||
ErrNotInstalled = errors.New("systemctl not in $PATH")
|
ErrNotInstalled = errors.New("systemctl not in $PATH")
|
||||||
// A unit was expected to be running but was found inactive
|
// A unit was expected to be running but was found inactive
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -63,7 +64,7 @@ func TestErrorFuncs(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := f(ctx, tc.unit, tc.opts)
|
err := f(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module github.com/taigrr/systemctl
|
module github.com/taigrr/systemctl
|
||||||
|
|
||||||
go 1.17
|
go 1.12
|
||||||
|
|||||||
52
helpers.go
52
helpers.go
@@ -2,7 +2,9 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/taigrr/systemctl/properties"
|
"github.com/taigrr/systemctl/properties"
|
||||||
@@ -52,3 +54,53 @@ func GetPID(ctx context.Context, unit string, opts Options) (int, error) {
|
|||||||
}
|
}
|
||||||
return strconv.Atoi(value)
|
return strconv.Atoi(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetMaskedUnits(ctx context.Context, opts Options) ([]string, error) {
|
||||||
|
args := []string{"list-unit-files", "--state=masked"}
|
||||||
|
if opts.UserMode {
|
||||||
|
args = append(args, "--user")
|
||||||
|
}
|
||||||
|
stdout, stderr, _, err := execute(ctx, args)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, errors.Join(err, filterErr(stderr))
|
||||||
|
}
|
||||||
|
lines := strings.Split(stdout, "\n")
|
||||||
|
units := []string{}
|
||||||
|
for _, line := range lines {
|
||||||
|
if !strings.Contains(line, "masked") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entry := strings.Split(line, " ")
|
||||||
|
if len(entry) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if entry[1] == "masked" {
|
||||||
|
unit := entry[0]
|
||||||
|
uName := strings.Split(unit, ".")
|
||||||
|
unit = uName[0]
|
||||||
|
units = append(units, unit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return units, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if a service is masked
|
||||||
|
func IsMasked(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
units, err := GetMaskedUnits(ctx, opts)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
for _, u := range units {
|
||||||
|
if u == unit {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if a service is running
|
||||||
|
// https://unix.stackexchange.com/a/396633
|
||||||
|
func IsRunning(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
status, err := Show(ctx, unit, properties.SubState, opts)
|
||||||
|
return status == "running", err
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -58,13 +59,13 @@ func TestGetStartTime(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := GetStartTime(ctx, tc.unit, tc.opts)
|
_, err := GetStartTime(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Prove start time changes after a restart
|
// Prove start time changes after a restart
|
||||||
t.Run(fmt.Sprintf("prove start time changes"), func(t *testing.T) {
|
t.Run("prove start time changes", func(t *testing.T) {
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
t.Skip("skipping superuser test while running as user")
|
t.Skip("skipping superuser test while running as user")
|
||||||
}
|
}
|
||||||
@@ -93,12 +94,13 @@ func TestGetStartTime(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetNumRestarts(t *testing.T) {
|
func TestGetNumRestarts(t *testing.T) {
|
||||||
testCases := []struct {
|
type testCase struct {
|
||||||
unit string
|
unit string
|
||||||
err error
|
err error
|
||||||
opts Options
|
opts Options
|
||||||
runAsUser bool
|
runAsUser bool
|
||||||
}{
|
}
|
||||||
|
testCases := []testCase{
|
||||||
// Run these tests only as a user
|
// Run these tests only as a user
|
||||||
|
|
||||||
// try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
@@ -118,6 +120,7 @@ func TestGetNumRestarts(t *testing.T) {
|
|||||||
{"nginx", nil, Options{UserMode: false}, false},
|
{"nginx", nil, Options{UserMode: false}, false},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
func(tc testCase) {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
@@ -128,13 +131,14 @@ func TestGetNumRestarts(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := GetNumRestarts(ctx, tc.unit, tc.opts)
|
_, err := GetNumRestarts(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}(tc)
|
||||||
}
|
}
|
||||||
// Prove restart count increases by one after a restart
|
// Prove restart count increases by one after a restart
|
||||||
t.Run(fmt.Sprintf("prove restart count increases by one after a restart"), func(t *testing.T) {
|
t.Run("prove restart count increases by one after a restart", func(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping in short mode")
|
t.Skip("skipping in short mode")
|
||||||
}
|
}
|
||||||
@@ -154,9 +158,9 @@ func TestGetNumRestarts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
syscall.Kill(pid, syscall.SIGKILL)
|
syscall.Kill(pid, syscall.SIGKILL)
|
||||||
for {
|
for {
|
||||||
running, err := IsActive(ctx, "nginx", Options{UserMode: false})
|
running, errIsActive := IsActive(ctx, "nginx", Options{UserMode: false})
|
||||||
if err != nil {
|
if errIsActive != nil {
|
||||||
t.Errorf("error asserting nginx is up: %v", err)
|
t.Errorf("error asserting nginx is up: %v", errIsActive)
|
||||||
break
|
break
|
||||||
} else if running {
|
} else if running {
|
||||||
break
|
break
|
||||||
@@ -173,12 +177,13 @@ func TestGetNumRestarts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetMemoryUsage(t *testing.T) {
|
func TestGetMemoryUsage(t *testing.T) {
|
||||||
testCases := []struct {
|
type testCase struct {
|
||||||
unit string
|
unit string
|
||||||
err error
|
err error
|
||||||
opts Options
|
opts Options
|
||||||
runAsUser bool
|
runAsUser bool
|
||||||
}{
|
}
|
||||||
|
testCases := []testCase{
|
||||||
// Run these tests only as a user
|
// Run these tests only as a user
|
||||||
|
|
||||||
// try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
@@ -198,6 +203,7 @@ func TestGetMemoryUsage(t *testing.T) {
|
|||||||
{"nginx", nil, Options{UserMode: false}, false},
|
{"nginx", nil, Options{UserMode: false}, false},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
func(tc testCase) {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
@@ -208,13 +214,14 @@ func TestGetMemoryUsage(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := GetMemoryUsage(ctx, tc.unit, tc.opts)
|
_, err := GetMemoryUsage(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}(tc)
|
||||||
}
|
}
|
||||||
// Prove memory usage values change across services
|
// Prove memory usage values change across services
|
||||||
t.Run(fmt.Sprintf("prove memory usage values change across services"), func(t *testing.T) {
|
t.Run("prove memory usage values change across services", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
bytes, err := GetMemoryUsage(ctx, "nginx", Options{UserMode: false})
|
bytes, err := GetMemoryUsage(ctx, "nginx", Options{UserMode: false})
|
||||||
@@ -232,12 +239,14 @@ func TestGetMemoryUsage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetPID(t *testing.T) {
|
func TestGetPID(t *testing.T) {
|
||||||
testCases := []struct {
|
type testCase struct {
|
||||||
unit string
|
unit string
|
||||||
err error
|
err error
|
||||||
opts Options
|
opts Options
|
||||||
runAsUser bool
|
runAsUser bool
|
||||||
}{
|
}
|
||||||
|
|
||||||
|
testCases := []testCase{
|
||||||
// Run these tests only as a user
|
// Run these tests only as a user
|
||||||
|
|
||||||
// try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
@@ -257,6 +266,7 @@ func TestGetPID(t *testing.T) {
|
|||||||
{"nginx", nil, Options{UserMode: false}, false},
|
{"nginx", nil, Options{UserMode: false}, false},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
func(tc testCase) {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
@@ -267,12 +277,13 @@ func TestGetPID(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := GetPID(ctx, tc.unit, tc.opts)
|
_, err := GetPID(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}(tc)
|
||||||
}
|
}
|
||||||
t.Run(fmt.Sprintf("prove pid changes"), func(t *testing.T) {
|
t.Run("prove pid changes", func(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping in short mode")
|
t.Skip("skipping in short mode")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
@@ -41,7 +42,6 @@ func TestMain(m *testing.M) {
|
|||||||
|
|
||||||
func TestDaemonReload(t *testing.T) {
|
func TestDaemonReload(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
unit string
|
|
||||||
err error
|
err error
|
||||||
opts Options
|
opts Options
|
||||||
runAsUser bool
|
runAsUser bool
|
||||||
@@ -49,22 +49,26 @@ func TestDaemonReload(t *testing.T) {
|
|||||||
/* Run these tests only as a user */
|
/* Run these tests only as a user */
|
||||||
|
|
||||||
// fail to reload system daemon as user
|
// fail to reload system daemon as user
|
||||||
{"", ErrInsufficientPermissions, Options{UserMode: false}, true},
|
{ErrInsufficientPermissions, Options{UserMode: false}, true},
|
||||||
// reload user's scope daemon
|
// reload user's scope daemon
|
||||||
{"", nil, Options{UserMode: true}, true},
|
{nil, Options{UserMode: true}, true},
|
||||||
/* End user tests*/
|
/* End user tests*/
|
||||||
|
|
||||||
/* Run these tests only as a superuser */
|
/* Run these tests only as a superuser */
|
||||||
|
|
||||||
// succeed to reload daemon
|
// succeed to reload daemon
|
||||||
{"", nil, Options{UserMode: false}, false},
|
{nil, Options{UserMode: false}, false},
|
||||||
// fail to connect to user bus as system
|
// fail to connect to user bus as system
|
||||||
{"", ErrBusFailure, Options{UserMode: true}, false},
|
{ErrBusFailure, Options{UserMode: true}, false},
|
||||||
|
|
||||||
/* End superuser tests*/
|
/* End superuser tests*/
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
mode := "user"
|
||||||
|
if tc.opts.UserMode == false {
|
||||||
|
mode = "system"
|
||||||
|
}
|
||||||
|
t.Run(fmt.Sprintf("DaemonReload as %s, %s mode", userString, mode), func(t *testing.T) {
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
t.Skip("skipping user test while running as superuser")
|
t.Skip("skipping user test while running as superuser")
|
||||||
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
||||||
@@ -73,7 +77,7 @@ func TestDaemonReload(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := DaemonReload(ctx, tc.opts)
|
err := DaemonReload(ctx, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -81,7 +85,6 @@ func TestDaemonReload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDisable(t *testing.T) {
|
func TestDisable(t *testing.T) {
|
||||||
t.Run(fmt.Sprintf(""), func(t *testing.T) {
|
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
t.Skip("skipping superuser test while running as user")
|
t.Skip("skipping superuser test while running as user")
|
||||||
}
|
}
|
||||||
@@ -94,7 +97,7 @@ func TestDisable(t *testing.T) {
|
|||||||
t.Errorf("Unable to mask %s", unit)
|
t.Errorf("Unable to mask %s", unit)
|
||||||
}
|
}
|
||||||
err = Disable(ctx, unit, Options{UserMode: false})
|
err = Disable(ctx, unit, Options{UserMode: false})
|
||||||
if err != ErrMasked {
|
if !errors.Is(err, ErrMasked) {
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
||||||
}
|
}
|
||||||
@@ -102,11 +105,9 @@ func TestDisable(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to unmask %s", unit)
|
t.Errorf("Unable to unmask %s", unit)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReenable(t *testing.T) {
|
func TestReenable(t *testing.T) {
|
||||||
t.Run(fmt.Sprintf(""), func(t *testing.T) {
|
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
t.Skip("skipping superuser test while running as user")
|
t.Skip("skipping superuser test while running as user")
|
||||||
}
|
}
|
||||||
@@ -119,7 +120,7 @@ func TestReenable(t *testing.T) {
|
|||||||
t.Errorf("Unable to mask %s", unit)
|
t.Errorf("Unable to mask %s", unit)
|
||||||
}
|
}
|
||||||
err = Reenable(ctx, unit, Options{UserMode: false})
|
err = Reenable(ctx, unit, Options{UserMode: false})
|
||||||
if err != ErrMasked {
|
if !errors.Is(err, ErrMasked) {
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
||||||
}
|
}
|
||||||
@@ -127,11 +128,9 @@ func TestReenable(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to unmask %s", unit)
|
t.Errorf("Unable to unmask %s", unit)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEnable(t *testing.T) {
|
func TestEnable(t *testing.T) {
|
||||||
t.Run(fmt.Sprintf(""), func(t *testing.T) {
|
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
t.Skip("skipping superuser test while running as user")
|
t.Skip("skipping superuser test while running as user")
|
||||||
}
|
}
|
||||||
@@ -144,7 +143,7 @@ func TestEnable(t *testing.T) {
|
|||||||
t.Errorf("Unable to mask %s", unit)
|
t.Errorf("Unable to mask %s", unit)
|
||||||
}
|
}
|
||||||
err = Enable(ctx, unit, Options{UserMode: false})
|
err = Enable(ctx, unit, Options{UserMode: false})
|
||||||
if err != ErrMasked {
|
if !errors.Is(err, ErrMasked) {
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
||||||
}
|
}
|
||||||
@@ -152,7 +151,6 @@ func TestEnable(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to unmask %s", unit)
|
t.Errorf("Unable to unmask %s", unit)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEnable() {
|
func ExampleEnable() {
|
||||||
@@ -161,16 +159,16 @@ func ExampleEnable() {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err := Enable(ctx, unit, Options{UserMode: true})
|
err := Enable(ctx, unit, Options{UserMode: true})
|
||||||
switch err {
|
switch {
|
||||||
case ErrMasked:
|
case errors.Is(err, ErrMasked):
|
||||||
fmt.Printf("%s is masked, unmask it before enabling\n", unit)
|
fmt.Printf("%s is masked, unmask it before enabling\n", unit)
|
||||||
case ErrDoesNotExist:
|
case errors.Is(err, ErrDoesNotExist):
|
||||||
fmt.Printf("%s does not exist\n", unit)
|
fmt.Printf("%s does not exist\n", unit)
|
||||||
case ErrInsufficientPermissions:
|
case errors.Is(err, ErrInsufficientPermissions):
|
||||||
fmt.Printf("permission to enable %s denied\n", unit)
|
fmt.Printf("permission to enable %s denied\n", unit)
|
||||||
case ErrBusFailure:
|
case errors.Is(err, ErrBusFailure):
|
||||||
fmt.Printf("Cannot communicate with the bus\n")
|
fmt.Printf("Cannot communicate with the bus\n")
|
||||||
case nil:
|
case err == nil:
|
||||||
fmt.Printf("%s enabled successfully\n", unit)
|
fmt.Printf("%s enabled successfully\n", unit)
|
||||||
default:
|
default:
|
||||||
fmt.Printf("Error: %v", err)
|
fmt.Printf("Error: %v", err)
|
||||||
@@ -179,7 +177,7 @@ func ExampleEnable() {
|
|||||||
|
|
||||||
func TestIsActive(t *testing.T) {
|
func TestIsActive(t *testing.T) {
|
||||||
unit := "nginx"
|
unit := "nginx"
|
||||||
t.Run(fmt.Sprintf("check active"), func(t *testing.T) {
|
t.Run("check active", func(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping in short mode")
|
t.Skip("skipping in short mode")
|
||||||
}
|
}
|
||||||
@@ -195,10 +193,10 @@ func TestIsActive(t *testing.T) {
|
|||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
isActive, err := IsActive(ctx, unit, Options{UserMode: false})
|
isActive, err := IsActive(ctx, unit, Options{UserMode: false})
|
||||||
if !isActive {
|
if !isActive {
|
||||||
t.Errorf("IsActive didn't return true for %s", unit)
|
t.Errorf("IsActive didn't return true for %s: %v", unit, err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("check masked"), func(t *testing.T) {
|
t.Run("check masked", func(t *testing.T) {
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
t.Skip("skipping superuser test while running as user")
|
t.Skip("skipping superuser test while running as user")
|
||||||
}
|
}
|
||||||
@@ -214,7 +212,7 @@ func TestIsActive(t *testing.T) {
|
|||||||
}
|
}
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("check masked"), func(t *testing.T) {
|
t.Run("check masked", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := IsActive(ctx, "nonexistant", Options{UserMode: false})
|
_, err := IsActive(ctx, "nonexistant", Options{UserMode: false})
|
||||||
@@ -231,7 +229,7 @@ func TestIsEnabled(t *testing.T) {
|
|||||||
userMode = true
|
userMode = true
|
||||||
unit = "syncthing"
|
unit = "syncthing"
|
||||||
}
|
}
|
||||||
t.Run(fmt.Sprintf("check enabled"), func(t *testing.T) {
|
t.Run("check enabled", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Enable(ctx, unit, Options{UserMode: userMode})
|
err := Enable(ctx, unit, Options{UserMode: userMode})
|
||||||
@@ -240,10 +238,10 @@ func TestIsEnabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
isEnabled, err := IsEnabled(ctx, unit, Options{UserMode: userMode})
|
isEnabled, err := IsEnabled(ctx, unit, Options{UserMode: userMode})
|
||||||
if !isEnabled {
|
if !isEnabled {
|
||||||
t.Errorf("IsEnabled didn't return true for %s", unit)
|
t.Errorf("IsEnabled didn't return true for %s: %v", unit, err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("check disabled"), func(t *testing.T) {
|
t.Run("check disabled", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Disable(ctx, unit, Options{UserMode: userMode})
|
err := Disable(ctx, unit, Options{UserMode: userMode})
|
||||||
@@ -259,7 +257,7 @@ func TestIsEnabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
Enable(ctx, unit, Options{UserMode: false})
|
Enable(ctx, unit, Options{UserMode: false})
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("check masked"), func(t *testing.T) {
|
t.Run("check masked", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Mask(ctx, unit, Options{UserMode: userMode})
|
err := Mask(ctx, unit, Options{UserMode: userMode})
|
||||||
@@ -320,13 +318,13 @@ func TestMask(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Mask(ctx, tc.unit, tc.opts)
|
err := Mask(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
Unmask(ctx, tc.unit, tc.opts)
|
Unmask(ctx, tc.unit, tc.opts)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
t.Run(fmt.Sprintf("test double masking existing"), func(t *testing.T) {
|
t.Run("test double masking existing", func(t *testing.T) {
|
||||||
unit := "nginx"
|
unit := "nginx"
|
||||||
userMode := false
|
userMode := false
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
@@ -346,17 +344,15 @@ func TestMask(t *testing.T) {
|
|||||||
}
|
}
|
||||||
Unmask(ctx, unit, opts)
|
Unmask(ctx, unit, opts)
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("test double masking nonexisting"), func(t *testing.T) {
|
t.Run("test double masking nonexisting", func(t *testing.T) {
|
||||||
unit := "nonexistant"
|
unit := "nonexistant"
|
||||||
userMode := false
|
userMode := userString != "root" && userString != "system"
|
||||||
if userString != "root" && userString != "system" {
|
|
||||||
userMode = true
|
|
||||||
}
|
|
||||||
opts := Options{UserMode: userMode}
|
opts := Options{UserMode: userMode}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Mask(ctx, unit, opts)
|
err := Mask(ctx, unit, opts)
|
||||||
if err != ErrDoesNotExist {
|
if !errors.Is(err, ErrDoesNotExist) {
|
||||||
t.Errorf("error on initial masking is %v, but should have been %v", err, ErrDoesNotExist)
|
t.Errorf("error on initial masking is %v, but should have been %v", err, ErrDoesNotExist)
|
||||||
}
|
}
|
||||||
err = Mask(ctx, unit, opts)
|
err = Mask(ctx, unit, opts)
|
||||||
@@ -388,9 +384,9 @@ func TestRestart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
syscall.Kill(pid, syscall.SIGKILL)
|
syscall.Kill(pid, syscall.SIGKILL)
|
||||||
for {
|
for {
|
||||||
running, err := IsActive(ctx, unit, opts)
|
running, errIsActive := IsActive(ctx, unit, opts)
|
||||||
if err != nil {
|
if errIsActive != nil {
|
||||||
t.Errorf("error asserting %s is up: %v", unit, err)
|
t.Errorf("error asserting %s is up: %v", unit, errIsActive)
|
||||||
break
|
break
|
||||||
} else if running {
|
} else if running {
|
||||||
break
|
break
|
||||||
@@ -415,6 +411,7 @@ func TestShow(t *testing.T) {
|
|||||||
UserMode: false,
|
UserMode: false,
|
||||||
}
|
}
|
||||||
for _, x := range properties.Properties {
|
for _, x := range properties.Properties {
|
||||||
|
func(x properties.Property) {
|
||||||
t.Run(fmt.Sprintf("show property %s", string(x)), func(t *testing.T) {
|
t.Run(fmt.Sprintf("show property %s", string(x)), func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -424,6 +421,7 @@ func TestShow(t *testing.T) {
|
|||||||
t.Errorf("error is %v, but should have been %v", err, nil)
|
t.Errorf("error is %v, but should have been %v", err, nil)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}(x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -551,13 +549,13 @@ func TestUnmask(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Mask(ctx, tc.unit, tc.opts)
|
err := Mask(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
Unmask(ctx, tc.unit, tc.opts)
|
Unmask(ctx, tc.unit, tc.opts)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
t.Run(fmt.Sprintf("test double unmasking existing"), func(t *testing.T) {
|
t.Run("test double unmasking existing", func(t *testing.T) {
|
||||||
unit := "nginx"
|
unit := "nginx"
|
||||||
userMode := false
|
userMode := false
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
@@ -577,12 +575,10 @@ func TestUnmask(t *testing.T) {
|
|||||||
}
|
}
|
||||||
Unmask(ctx, unit, opts)
|
Unmask(ctx, unit, opts)
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("test double unmasking nonexisting"), func(t *testing.T) {
|
t.Run("test double unmasking nonexisting", func(t *testing.T) {
|
||||||
unit := "nonexistant"
|
unit := "nonexistant"
|
||||||
userMode := false
|
userMode := userString != "root" && userString != "system"
|
||||||
if userString != "root" && userString != "system" {
|
|
||||||
userMode = true
|
|
||||||
}
|
|
||||||
opts := Options{UserMode: userMode}
|
opts := Options{UserMode: userMode}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -592,7 +588,7 @@ func TestUnmask(t *testing.T) {
|
|||||||
t.Errorf("error on initial unmasking is %v, but should have been %v", err, nil)
|
t.Errorf("error on initial unmasking is %v, but should have been %v", err, nil)
|
||||||
}
|
}
|
||||||
err = Unmask(ctx, unit, opts)
|
err = Unmask(ctx, unit, opts)
|
||||||
if err != ErrDoesNotExist {
|
if !errors.Is(err, ErrDoesNotExist) {
|
||||||
t.Errorf("error on second unmasking is %v, but should have been %v", err, ErrDoesNotExist)
|
t.Errorf("error on second unmasking is %v, but should have been %v", err, ErrDoesNotExist)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
61
util.go
61
util.go
@@ -3,10 +3,10 @@ package systemctl
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var systemctl string
|
var systemctl string
|
||||||
@@ -14,12 +14,7 @@ var systemctl string
|
|||||||
const killed = 130
|
const killed = 130
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
path, err := exec.LookPath("systemctl")
|
path, _ := exec.LookPath("systemctl")
|
||||||
if err != nil {
|
|
||||||
log.Printf("%v", ErrNotInstalled)
|
|
||||||
systemctl = ""
|
|
||||||
return
|
|
||||||
}
|
|
||||||
systemctl = path
|
systemctl = path
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +29,7 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if systemctl == "" {
|
if systemctl == "" {
|
||||||
panic(ErrNotInstalled)
|
return "", "", 1, ErrNotInstalled
|
||||||
}
|
}
|
||||||
cmd := exec.CommandContext(ctx, systemctl, args...)
|
cmd := exec.CommandContext(ctx, systemctl, args...)
|
||||||
cmd.Stdout = &stdout
|
cmd.Stdout = &stdout
|
||||||
@@ -56,32 +51,26 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func filterErr(stderr string) error {
|
func filterErr(stderr string) error {
|
||||||
if matched, _ := regexp.MatchString(`does not exist`, stderr); matched {
|
switch {
|
||||||
return ErrDoesNotExist
|
case strings.Contains(stderr, `does not exist`):
|
||||||
}
|
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||||
if matched, _ := regexp.MatchString(`not found.`, stderr); matched {
|
case strings.Contains(stderr, `not found.`):
|
||||||
return ErrDoesNotExist
|
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||||
}
|
case strings.Contains(stderr, `not loaded.`):
|
||||||
if matched, _ := regexp.MatchString(`not loaded.`, stderr); matched {
|
return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr))
|
||||||
return ErrUnitNotLoaded
|
case strings.Contains(stderr, `No such file or directory`):
|
||||||
}
|
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||||
if matched, _ := regexp.MatchString(`No such file or directory`, stderr); matched {
|
case strings.Contains(stderr, `Interactive authentication required`):
|
||||||
return ErrDoesNotExist
|
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
|
||||||
}
|
case strings.Contains(stderr, `Access denied`):
|
||||||
if matched, _ := regexp.MatchString(`Interactive authentication required`, stderr); matched {
|
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
|
||||||
return ErrInsufficientPermissions
|
case strings.Contains(stderr, `DBUS_SESSION_BUS_ADDRESS`):
|
||||||
}
|
return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr))
|
||||||
if matched, _ := regexp.MatchString(`Access denied`, stderr); matched {
|
case strings.Contains(stderr, `is masked`):
|
||||||
return ErrInsufficientPermissions
|
return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr))
|
||||||
}
|
case strings.Contains(stderr, `Failed`):
|
||||||
if matched, _ := regexp.MatchString(`DBUS_SESSION_BUS_ADDRESS`, stderr); matched {
|
return errors.Join(ErrUnspecified, fmt.Errorf("stderr: %s", stderr))
|
||||||
return ErrBusFailure
|
default:
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`is masked`, stderr); matched {
|
|
||||||
return ErrMasked
|
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`Failed`, stderr); matched {
|
|
||||||
return ErrUnspecified
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user