-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathportable_user_unit_test.go
More file actions
72 lines (64 loc) · 1.82 KB
/
Copy pathportable_user_unit_test.go
File metadata and controls
72 lines (64 loc) · 1.82 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package systemctl
import (
"context"
"os"
"path/filepath"
"testing"
"time"
)
func userBusAvailable(t *testing.T) bool {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_, err := GetUnits(ctx, Options{UserMode: true})
return err == nil
}
func requireUserBus(t *testing.T) {
t.Helper()
if !userBusAvailable(t) {
t.Skip("skipping user-mode lifecycle test without a reachable user systemd bus")
}
}
func userTestUnit(t *testing.T) string {
t.Helper()
homeDir, err := os.UserHomeDir()
if err != nil {
t.Fatalf("get user home dir: %v", err)
}
unitDir := filepath.Join(homeDir, ".local", "share", "systemd", "user")
if err := os.MkdirAll(unitDir, 0o755); err != nil {
t.Fatalf("create user systemd dir: %v", err)
}
unitName := "openclaw-test"
unitPath := filepath.Join(unitDir, unitName+".service")
unitFile := `[Unit]
Description=OpenClaw portable test service
[Service]
Type=simple
ExecStart=/bin/sh -c 'sleep 300'
Restart=on-failure
[Install]
WantedBy=default.target
`
if err := os.WriteFile(unitPath, []byte(unitFile), 0o644); err != nil {
t.Fatalf("write user test unit: %v", err)
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = Stop(ctx, unitName, Options{UserMode: true})
_ = Disable(ctx, unitName, Options{UserMode: true})
_ = Unmask(ctx, unitName, Options{UserMode: true})
_ = os.Remove(filepath.Join(unitDir, "default.target.wants", unitName+".service"))
_ = os.Remove(unitPath)
_ = DaemonReload(ctx, Options{UserMode: true})
})
if userBusAvailable(t) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := DaemonReload(ctx, Options{UserMode: true}); err != nil {
t.Fatalf("reload user daemon: %v", err)
}
}
return unitName
}