From 27c2ad77c8977ea62c0effce75eb5b4ff7ffd776 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sun, 16 May 2021 22:32:24 -0700 Subject: [PATCH] improves IsActive testing --- helpers_test.go | 23 +++++++++++++++++++++++ systemctl_test.go | 4 ++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/helpers_test.go b/helpers_test.go index a1debbb..d4a3049 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -267,4 +267,27 @@ func TestGetPID(t *testing.T) { } }) } + t.Run(fmt.Sprintf("prove pid changes"), 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(), 1*time.Second) + defer cancel() + Restart(ctx, unit, Options{UserMode: true}) + pid, err := GetPID(ctx, unit, Options{UserMode: false}) + if err != nil { + t.Errorf("issue getting MainPID for nginx as %s: %v", userString, err) + } + syscall.Kill(pid, syscall.SIGKILL) + secondPid, err := GetPID(ctx, unit, Options{UserMode: false}) + if err != nil { + t.Errorf("issue getting second MainPID for nginx as %s: %v", userString, err) + } + if pid == secondPid { + t.Errorf("Expected pid != secondPid, but both were: %d", pid) + } + + }) + } diff --git a/systemctl_test.go b/systemctl_test.go index 538bf73..7b403ab 100644 --- a/systemctl_test.go +++ b/systemctl_test.go @@ -152,9 +152,9 @@ func TestIsActive(t *testing.T) { } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - err := Start(ctx, unit, Options{UserMode: false}) + err := Restart(ctx, unit, Options{UserMode: false}) if err != nil { - t.Errorf("Unable to restart %s", unit) + t.Errorf("Unable to restart %s: %v", unit, err) } time.Sleep(time.Second) isActive, err := IsActive(ctx, unit, Options{UserMode: false})