Finish tests for helper methods
This commit is contained in:
parent
64249bf8e6
commit
b1346b7992
@ -23,4 +23,6 @@ var (
|
|||||||
ErrUnitNotRunning = errors.New("Unit isn't running")
|
ErrUnitNotRunning = errors.New("Unit isn't running")
|
||||||
// ErrBusFailure means $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR were not defined
|
// ErrBusFailure means $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR were not defined
|
||||||
ErrBusFailure = errors.New("Failure to connect to bus, did you run in usermode as root?")
|
ErrBusFailure = errors.New("Failure to connect to bus, did you run in usermode as root?")
|
||||||
|
// ErrValueNotSet means an expected value is unavailable, but the unit may be running
|
||||||
|
ErrValueNotSet = errors.New("Value not set or unavailable")
|
||||||
)
|
)
|
||||||
|
@ -30,6 +30,9 @@ func GetMemoryUsage(ctx context.Context, unit string, opts Options) (int, error)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
|
if value == "[not set]" {
|
||||||
|
return -1, ErrValueNotSet
|
||||||
|
}
|
||||||
return strconv.Atoi(value)
|
return strconv.Atoi(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
106
helpers_test.go
106
helpers_test.go
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
// Testing assumptions
|
// Testing assumptions
|
||||||
// - there's no unit installed named `nonexistant`
|
// - there's no unit installed named `nonexistant`
|
||||||
// - the syncthing unit to be available on the tester's system.
|
// - the syncthing unit is available but the binary is not on the tester's system
|
||||||
// this is just what was available on mine, should you want to change it,
|
// this is just what was available on mine, should you want to change it,
|
||||||
// either to something in this repo or more common, feel free to submit a PR.
|
// either to something in this repo or more common, feel free to submit a PR.
|
||||||
// - your 'user' isn't root
|
// - your 'user' isn't root
|
||||||
@ -23,7 +23,6 @@ func TestGetStartTime(t *testing.T) {
|
|||||||
runAsUser bool
|
runAsUser bool
|
||||||
}{
|
}{
|
||||||
// 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
|
||||||
{"nonexistant", ErrUnitNotRunning, Options{usermode: false}, true},
|
{"nonexistant", ErrUnitNotRunning, Options{usermode: false}, true},
|
||||||
// try existing unit in user mode as user
|
// try existing unit in user mode as user
|
||||||
@ -56,7 +55,7 @@ func TestGetStartTime(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// 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(fmt.Sprintf("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")
|
||||||
}
|
}
|
||||||
@ -83,6 +82,105 @@ func TestGetStartTime(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStartTime(ctx context.Context, unit string, opts Options) (time.Time, error) {
|
func TestGetMemoryUsage(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", ErrValueNotSet, Options{usermode: false}, true},
|
||||||
|
// try existing unit in user mode as user
|
||||||
|
{"syncthing", ErrValueNotSet, Options{usermode: true}, true},
|
||||||
|
// try existing unit in system mode as user
|
||||||
|
{"nginx", nil, Options{usermode: false}, true},
|
||||||
|
|
||||||
|
// Run these tests only as a superuser
|
||||||
|
|
||||||
|
// try nonexistant unit in system mode as system
|
||||||
|
{"nonexistant", ErrValueNotSet, 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},
|
||||||
|
}
|
||||||
|
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 := GetMemoryUsage(ctx, tc.unit, tc.opts)
|
||||||
|
if err != tc.err {
|
||||||
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// Prove start time changes after a restart
|
||||||
|
t.Run(fmt.Sprintf("prove start time changes"), func(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
bytes, err := GetMemoryUsage(ctx, "nginx", Options{usermode: false})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("issue getting memory usage of nginx: %v", err)
|
||||||
|
}
|
||||||
|
secondBytes, err := GetMemoryUsage(ctx, "user.slice", Options{usermode: false})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("issue getting second memort usage reading of nginx: %v", err)
|
||||||
|
}
|
||||||
|
if bytes == secondBytes {
|
||||||
|
t.Errorf("Expected memory usage between nginx and user.slice to differ, but both were: %d", bytes)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
func TestGetPID(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", nil, Options{usermode: false}, true},
|
||||||
|
// try existing unit in user mode as user
|
||||||
|
{"syncthing", nil, Options{usermode: true}, true},
|
||||||
|
// try existing unit in system mode as user
|
||||||
|
{"nginx", nil, Options{usermode: false}, true},
|
||||||
|
|
||||||
|
// Run these tests only as a superuser
|
||||||
|
|
||||||
|
// try nonexistant unit in system mode as system
|
||||||
|
{"nonexistant", nil, 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},
|
||||||
|
}
|
||||||
|
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 := GetPID(ctx, tc.unit, tc.opts)
|
||||||
|
if err != tc.err {
|
||||||
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GetMemoryUsage(ctx context.Context, unit string, opts Options) (int, error) {
|
// GetMemoryUsage(ctx context.Context, unit string, opts Options) (int, error) {
|
||||||
// GetPID(ctx context.Context, unit string, opts Options) (int, error) {
|
// GetPID(ctx context.Context, unit string, opts Options) (int, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user