Skip to content

Commit

Permalink
Migrates many tests to errors_test
Browse files Browse the repository at this point in the history
  • Loading branch information
taigrr committed May 17, 2021
1 parent dd48d61 commit e941c90
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 83 deletions.
5 changes: 4 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ var (
// 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
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
// This can happen when calling GetStartTime on a dead unit, for example
ErrUnitNotActive = errors.New("unit not active")
// A unit was expected to be loaded, but was not.
// This can happen when trying to Stop a unit which does not exist, for example
ErrUnitNotLoaded = errors.New("unit not loaded")
// An expected value is unavailable, but the unit may be running
// This can happen when calling GetMemoryUsage on systemd itself, for example
ErrValueNotSet = errors.New("value not set")
Expand Down
92 changes: 92 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package systemctl

import (
"context"
"fmt"
"reflect"
"runtime"
"strings"
"testing"
"time"
)

func TestErrorFuncs(t *testing.T) {
errFuncs := []func(ctx context.Context, unit string, opts Options) error{
Enable,
Disable,
Restart,
Start,
}
errCases := []struct {
unit string
err error
opts Options
runAsUser bool
}{
/* Run these tests only as an unpriviledged user */

//try nonexistant unit in user mode as user
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
// 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},
// try existing unit in system mode as user
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},

/* End user tests*/

/* Run these tests only as a superuser */

// try nonexistant unit in system mode as system
{"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false},
// try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system
{"nginx", nil, Options{UserMode: false}, false},

/* End superuser tests*/

}

for _, f := range errFuncs {
fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
fName = strings.TrimPrefix(fName, "github.com/taigrr/")
t.Run(fmt.Sprintf("Errorcheck %s", fName), func(t *testing.T) {

for _, tc := range errCases {
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := f(ctx, tc.unit, tc.opts)
if err != tc.err {
t.Errorf("error is %v, but should have been %v", err, tc.err)
}
})
}
})
}
}

// /* Run these tests only as a user */
//
// // fail to reload system daemon as user
// {"", ErrInsufficientPermissions, Options{UserMode: false}, true},
// // reload user's scope daemon
// {"", nil, Options{UserMode: true}, true},
// /* End user tests*/
//
// /* Run these tests only as a superuser */
//
// // succeed to reload daemon
// {"", nil, Options{UserMode: false}, false},
// // fail to connect to user bus as system
// {"", ErrBusFailure, Options{UserMode: true}, false},
//
// /* End superuser tests*/
//
11 changes: 9 additions & 2 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ func TestGetStartTime(t *testing.T) {
// try existing unit in system mode as system
{"nginx", nil, Options{UserMode: false}, false},
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
Restart(ctx, "syncthing", Options{UserMode: true})
Stop(ctx, "syncthing", Options{UserMode: true})
time.Sleep(1 * time.Second)
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
t.Run(fmt.Sprintf("%s as %s, UserMode=%v", tc.unit, userString, tc.opts.UserMode), func(t *testing.T) {
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
Expand All @@ -61,16 +66,18 @@ func TestGetStartTime(t *testing.T) {
t.Skip("skipping superuser test while running as user")
}

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
startTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting start time of nginx: %v", err)
}
time.Sleep(1 * time.Second)
err = Restart(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue restarting nginx as %s: %v", userString, err)
}
time.Sleep(100 * time.Millisecond)
newStartTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting second start time of nginx: %v", err)
Expand Down
8 changes: 8 additions & 0 deletions systemctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {

// Mask one or more units, as specified on the command line. This will link
// these unit files to /dev/null, making it impossible to start them.
//
// Notably, Mask may return ErrDoesNotExist if a unit doesn't exist, but it will
// ocntinue masking anyway.
func Mask(ctx context.Context, unit string, opts Options) error {
var args = []string{"mask", "--system", unit}
if opts.UserMode {
Expand Down Expand Up @@ -213,6 +216,11 @@ func Stop(ctx context.Context, unit string, opts Options) error {

// Unmask one or more unit files, as specified on the command line.
// This will undo the effect of Mask.
//
// In line with systemd, Unmask will return ErrDoesNotExist if the unit
// doesn't exist, but only if it's not already masked.
// If the unit doesn't exist but it's masked anyway, no error will be
// returned. Gross, I know. Take it up with Poettering.
func Unmask(ctx context.Context, unit string, opts Options) error {
var args = []string{"unmask", "--system", unit}
if opts.UserMode {
Expand Down
147 changes: 67 additions & 80 deletions systemctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var userString string

func TestMain(m *testing.M) {
curUser, err := user.Current()

if err != nil {
fmt.Println("Could not determine running user")
}
Expand All @@ -32,50 +31,73 @@ func TestMain(m *testing.M) {
fmt.Println("Don't forget to run both root and user tests.")
os.Exit(m.Run())
}

func TestEnable(t *testing.T) {
func TestDaemonReload(t *testing.T) {
testCases := []struct {
unit string
err error
opts Options
runAsUser bool
}{
// Run these tests only as a user
/* Run these tests only as a user */

// fail to reload system daemon as user
{"", ErrInsufficientPermissions, Options{UserMode: false}, true},
// reload user's scope daemon
{"", nil, Options{UserMode: true}, true},
/* End user tests*/

//try nonexistant unit in user mode as user
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
// 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},
// try existing unit in system mode as user
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},
/* Run these tests only as a superuser */

// Run these tests only as a superuser
// succeed to reload daemon
{"", nil, Options{UserMode: false}, false},
// fail to connect to user bus as system
{"", ErrBusFailure, Options{UserMode: true}, false},

// try nonexistant unit in system mode as system
{"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false},
// try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system
{"nginx", nil, Options{UserMode: false}, false},
/* End superuser tests*/
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
t.Parallel()
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := Enable(ctx, tc.unit, tc.opts)
err := DaemonReload(ctx, tc.opts)
if err != tc.err {
t.Errorf("error is %v, but should have been %v", err, tc.err)
}
})
}
}
func TestDisable(t *testing.T) {
t.Run(fmt.Sprintf(""), func(t *testing.T) {
if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user")
}
unit := "nginx"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
err := Mask(ctx, unit, Options{UserMode: false})
defer cancel()
if err != nil {
Unmask(ctx, unit, Options{UserMode: false})
t.Errorf("Unable to mask %s", unit)
}
err = Disable(ctx, unit, Options{UserMode: false})
if err != ErrMasked {
Unmask(ctx, unit, Options{UserMode: false})
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
}
err = Unmask(ctx, unit, Options{UserMode: false})
if err != nil {
t.Errorf("Unable to unmask %s", unit)
}
})

}
func TestEnable(t *testing.T) {

t.Run(fmt.Sprintf(""), func(t *testing.T) {
if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user")
Expand Down Expand Up @@ -122,75 +144,40 @@ func ExampleEnable() {
}
}

func TestDisable(t *testing.T) {
testCases := []struct {
unit string
err error
opts Options
runAsUser bool
}{
/* Run these tests only as a user */

//try nonexistant unit in user mode as user
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
// 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},
// try existing unit in system mode as user
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},

/* End user tests*/

/* Run these tests only as a superuser */

// try nonexistant unit in system mode as system
{"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false},
// try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system
{"nginx", nil, Options{UserMode: false}, false},

/* End superuser tests*/
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := Disable(ctx, tc.unit, tc.opts)
if err != tc.err {
t.Errorf("error is %v, but should have been %v", err, tc.err)
}
})
}
t.Run(fmt.Sprintf(""), func(t *testing.T) {
func TestIsActive(t *testing.T) {
unit := "nginx"
t.Run(fmt.Sprintf("check active"), func(t *testing.T) {
if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user")
}
unit := "nginx"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
err := Mask(ctx, unit, Options{UserMode: false})
defer cancel()
err := Start(ctx, unit, Options{UserMode: false})
if err != nil {
Unmask(ctx, unit, Options{UserMode: false})
t.Errorf("Unable to mask %s", unit)
t.Errorf("Unable to restart %s", unit)
}
err = Disable(ctx, unit, Options{UserMode: false})
if err != ErrMasked {
Unmask(ctx, unit, Options{UserMode: false})
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
time.Sleep(time.Second)
isActive, err := IsActive(ctx, unit, Options{UserMode: false})
if !isActive {
t.Errorf("IsActive didn't return true for %s", unit)
}
err = Unmask(ctx, unit, Options{UserMode: false})
})
t.Run(fmt.Sprintf("check masked"), func(t *testing.T) {
if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := Mask(ctx, unit, Options{UserMode: false})
if err != nil {
t.Errorf("Unable to unmask %s", unit)
t.Errorf("Unable to mask %s", unit)
}
_, err = IsActive(ctx, unit, Options{UserMode: false})
if err != nil {
t.Errorf("error is %v, but should have been %v", err, nil)
}
Unmask(ctx, unit, Options{UserMode: false})
})

}

// Runs through all defined Properties in parallel and checks for error cases
Expand Down
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func filterErr(stderr string) error {
if matched, _ := regexp.MatchString(`does not exist`, stderr); matched {
return ErrDoesNotExist
}
if matched, _ := regexp.MatchString(`not found.`, stderr); matched {
return ErrDoesNotExist
}
if matched, _ := regexp.MatchString(`not loaded.`, stderr); matched {
return ErrUnitNotLoaded
}
if matched, _ := regexp.MatchString(`No such file or directory`, stderr); matched {
return ErrDoesNotExist
}
Expand Down

0 comments on commit e941c90

Please sign in to comment.