Add masked unit tests for enable and disable

This commit is contained in:
Tai Groot 2021-05-15 20:02:05 -07:00
parent 5865d30ada
commit 34f11bfcd1
Signed by: taigrr
GPG Key ID: D00C269A87614812
4 changed files with 55 additions and 2 deletions

View File

@ -25,4 +25,6 @@ var (
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")
// ErrMasked means a unit cannot be enabled because it is masked
ErrMasked = errors.New("Unit is masked")
)

View File

@ -109,6 +109,7 @@ func TestGetMemoryUsage(t *testing.T) {
}
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 {
@ -123,7 +124,7 @@ func TestGetMemoryUsage(t *testing.T) {
})
}
// Prove start time changes after a restart
t.Run(fmt.Sprintf("prove start time changes"), func(t *testing.T) {
t.Run(fmt.Sprintf("prove memory usage values change across services"), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
bytes, err := GetMemoryUsage(ctx, "nginx", Options{usermode: false})
@ -137,6 +138,7 @@ func TestGetMemoryUsage(t *testing.T) {
if bytes == secondBytes {
t.Errorf("Expected memory usage between nginx and user.slice to differ, but both were: %d", bytes)
}
t.Parallel()
})
}
@ -167,6 +169,7 @@ func TestGetPID(t *testing.T) {
}
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 {

View File

@ -62,12 +62,13 @@ func TestEnable(t *testing.T) {
}
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(), 1*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := Enable(ctx, tc.unit, tc.opts)
if err != tc.err {
@ -75,6 +76,28 @@ 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")
}
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 = Enable(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)
}
})
}
@ -120,6 +143,28 @@ 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)
}
})
}

View File

@ -65,6 +65,9 @@ func filterErr(stderr string) error {
if matched, _ := regexp.MatchString(`DBUS_SESSION_BUS_ADDRESS`, stderr); matched {
return ErrBusFailure
}
if matched, _ := regexp.MatchString(`is masked`, stderr); matched {
return ErrMasked
}
if matched, _ := regexp.MatchString(`Failed`, stderr); matched {
return ErrUnspecified
}