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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
package e2e
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
)
func waitForPids(t *testing.T, pidsDir string, expectedCount int) {
timeout := time.After(2 * time.Second)
tick := time.NewTicker(50 * time.Millisecond)
defer tick.Stop()
for {
select {
case <-timeout:
files, err := os.ReadDir(pidsDir)
if err != nil {
t.Fatalf("Failed to read pids dir during timeout: %v", err)
}
t.Fatalf("Timed out waiting for %d PID files, got %d", expectedCount, len(files))
case <-tick.C:
files, err := os.ReadDir(pidsDir)
if err != nil {
if os.IsNotExist(err) {
continue
}
t.Fatalf("Failed to read pids dir: %v", err)
}
if len(files) == expectedCount {
return
}
}
}
}
func TestNamespaceLifecycleAutomation(t *testing.T) {
// 1. Setup Environment
binaryPath, err := GetBinaryPath()
if err != nil {
t.Skipf("Skipping test: %v", err)
}
// 2. Override the runtime base dir to a temporary location
tmpRuntimeDir := t.TempDir()
profile := "default"
pidsDir := filepath.Join(tmpRuntimeDir, "profiles", profile, "pids")
// Clean up before starting
if err := os.RemoveAll(filepath.Join(tmpRuntimeDir, "profiles", profile)); err != nil {
t.Fatalf("failed to remove profile directory: %v", err)
}
t.Run("ReferenceCounting", func(t *testing.T) {
// Start a process that exits quickly
cmd1 := exec.Command(binaryPath, "--profile", "default", "--", "sleep", "0.1")
cmd1.Env = append(os.Environ(), fmt.Sprintf("XDG_RUNTIME_DIR=%s", tmpRuntimeDir))
if err := cmd1.Start(); err != nil {
t.Fatalf("Failed to start cmd1: %v", err)
}
// Verify PID file exists using polling
waitForPids(t, pidsDir, 1)
// Start a second process using the same profile
cmd2 := exec.Command(binaryPath, "--profile", "default", "--", "sleep", "0.1")
cmd2.Env = append(os.Environ(), fmt.Sprintf("XDG_RUNTIME_DIR=%s", tmpRuntimeDir))
if err := cmd2.Start(); err != nil {
t.Fatalf("Failed to start cmd2: %v", err)
}
waitForPids(t, pidsDir, 2)
// Wait for first process to exit naturally (triggering defer)
if err := cmd1.Wait(); err != nil {
t.Fatalf("cmd1 failed: %v", err)
}
// Poll for the count to drop back to 1
timeout := time.After(2 * time.Second)
found := false
for !found {
select {
case <-timeout:
t.Fatalf("Timed out waiting for first process to unregister")
default:
files, err := os.ReadDir(pidsDir)
if err == nil && len(files) == 1 {
found = true
break
}
time.Sleep(50 * time.Millisecond)
}
}
// Wait for second process to exit naturally
if err := cmd2.Wait(); err != nil {
t.Fatalf("cmd2 failed: %v", err)
}
// Verify a clean state (expect 0 files)
timeout = time.After(2 * time.Second)
found = false
for !found {
select {
case <-timeout:
files, _ := os.ReadDir(pidsDir)
t.Fatalf("Expected 0 PID files after all exits, got %d", len(files))
default:
files, err := os.ReadDir(pidsDir)
if err != nil && os.IsNotExist(err) {
found = true
break
}
if err == nil && len(files) == 0 {
found = true
break
}
time.Sleep(50 * time.Millisecond)
}
}
})
}
|