fix broken error filtration

This commit is contained in:
2023-06-28 23:43:20 -07:00
parent a82f845b84
commit 7bd5bef0cb
2 changed files with 18 additions and 15 deletions

View File

@@ -42,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
@@ -50,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 {

18
util.go
View File

@@ -52,23 +52,23 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
func filterErr(stderr string) error { func filterErr(stderr string) error {
switch { switch {
case strings.Contains(`does not exist`, stderr): case strings.Contains(stderr, `does not exist`):
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(`not found.`, stderr): case strings.Contains(stderr, `not found.`):
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(`not loaded.`, stderr): case strings.Contains(stderr, `not loaded.`):
return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(`No such file or directory`, stderr): case strings.Contains(stderr, `No such file or directory`):
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(`Interactive authentication required`, stderr): case strings.Contains(stderr, `Interactive authentication required`):
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(`Access denied`, stderr): case strings.Contains(stderr, `Access denied`):
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(`DBUS_SESSION_BUS_ADDRESS`, stderr): case strings.Contains(stderr, `DBUS_SESSION_BUS_ADDRESS`):
return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(`is masked`, stderr): case strings.Contains(stderr, `is masked`):
return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(`Failed`, stderr): case strings.Contains(stderr, `Failed`):
return errors.Join(ErrUnspecified, fmt.Errorf("stderr: %s", stderr)) return errors.Join(ErrUnspecified, fmt.Errorf("stderr: %s", stderr))
default: default:
return nil return nil