From 34f11bfcd1503baa2293733cf187002a47286cbc Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sat, 15 May 2021 20:02:05 -0700 Subject: [PATCH] Add masked unit tests for enable and disable --- errors.go | 2 ++ helpers_test.go | 5 ++++- systemctl_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- util.go | 3 +++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/errors.go b/errors.go index e5dd078..c652d0d 100644 --- a/errors.go +++ b/errors.go @@ -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") ) diff --git a/helpers_test.go b/helpers_test.go index a021ced..caf60ce 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -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 { diff --git a/systemctl_test.go b/systemctl_test.go index 5a6af82..a8eb67f 100644 --- a/systemctl_test.go +++ b/systemctl_test.go @@ -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) + } + }) } diff --git a/util.go b/util.go index ee38e62..d8d0ce4 100644 --- a/util.go +++ b/util.go @@ -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 }