-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_units_test.go
More file actions
43 lines (38 loc) · 1.12 KB
/
Copy pathtest_units_test.go
File metadata and controls
43 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package systemctl
import (
"context"
"strings"
"testing"
"time"
"github.com/taigrr/systemctl/properties"
)
func systemTestUnit(t *testing.T) string {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
units, err := GetUnits(ctx, Options{UserMode: false})
if err != nil {
t.Fatalf("get system units: %v", err)
}
for _, unit := range units {
if unit.Active != "active" || unit.Sub != "running" || !strings.HasSuffix(unit.Name, ".service") {
continue
}
name := strings.TrimSuffix(unit.Name, ".service")
startTime, err := Show(ctx, name, properties.ExecMainStartTimestamp, Options{UserMode: false})
if err != nil || startTime == "" {
continue
}
restarts, err := Show(ctx, name, properties.NRestarts, Options{UserMode: false})
if err != nil || restarts == "" || restarts == "[not set]" {
continue
}
memory, err := Show(ctx, name, properties.MemoryCurrent, Options{UserMode: false})
if err != nil || memory == "" || memory == "[not set]" {
continue
}
return name
}
t.Skip("no readable active system service found for read-only tests")
return ""
}