Migrates many tests to errors_test

This commit is contained in:
Tai Groot
2021-05-16 22:00:50 -07:00
parent dd48d610dc
commit e941c90f0d
6 changed files with 188 additions and 85 deletions

View File

@@ -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")
}
@@ -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 */
//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},
// 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
/* 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},
// 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*/
}
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")
@@ -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)
}
err = Unmask(ctx, unit, Options{UserMode: false})
if err != nil {
t.Errorf("Unable to unmask %s", unit)
time.Sleep(time.Second)
isActive, err := IsActive(ctx, unit, Options{UserMode: false})
if !isActive {
t.Errorf("IsActive didn't return true for %s", unit)
}
})
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 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