-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
186 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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*/ | ||
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters