summaryrefslogtreecommitdiff
path: root/internal/namespace/lifecycle_test.go
blob: 9962e149be4d6162bd40948098e9345143891145 (plain)
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
126
package namespace

import (
	"os"
	"path/filepath"
	"strconv"
	"testing"

	"git.theodohertyfamily.com/wg-wrap/internal/paths"
)

func TestLifecycleReferenceCounting(t *testing.T) {
	// Use a temporary directory to avoid polluting the system
	tmpDir := t.TempDir()
	pm := paths.NewPathManager("", tmpDir)

	profile := "test-vpn"

	t.Run("RegisterAndUnregister", func(t *testing.T) {
		err := RegisterProcess(pm, profile)
		if err != nil {
			t.Fatalf("failed to register: %v", err)
		}

		pidsDir := GetPidsDirPath(pm, profile)
		pidFile := filepath.Join(pidsDir, strconv.Itoa(os.Getpid()))
		if _, err := os.Stat(pidFile); os.IsNotExist(err) {
			t.Errorf("PID file should exist at %s", pidFile)
		}

		err = UnregisterProcess(pm, profile)
		if err != nil {
			t.Fatalf("failed to unregister: %v", err)
		}

		if _, err := os.Stat(pidFile); err == nil {
			t.Errorf("PID file should have been removed at %s", pidFile)
		}
	})

	t.Run("PruneStalePids", func(t *testing.T) {
		pidsDir := GetPidsDirPath(pm, profile)
		if err := os.MkdirAll(pidsDir, 0755); err != nil {
			t.Fatal(err)
		}

		fakePid := "9999999"
		fakePidFile := filepath.Join(pidsDir, fakePid)
		if err := os.WriteFile(fakePidFile, []byte(""), 0644); err != nil {
			t.Fatal(err)
		}

		if err := RegisterProcess(pm, profile); err != nil {
			t.Fatal(err)
		}

		err := PruneStalePids(pm, profile)
		if err != nil {
			t.Fatalf("prune failed: %v", err)
		}

		if _, err := os.Stat(fakePidFile); err == nil {
			t.Errorf("Stale PID file %s should have been pruned", fakePidFile)
		}

		currentPidFile := filepath.Join(pidsDir, strconv.Itoa(os.Getpid()))
		if _, err := os.Stat(currentPidFile); os.IsNotExist(err) {
			t.Errorf("Current PID file %s should not have been pruned", currentPidFile)
		}

		if err := UnregisterProcess(pm, profile); err != nil {
			t.Fatal(err)
		}
	})

	t.Run("IsLastProcess", func(t *testing.T) {
		pidsDir := GetPidsDirPath(pm, profile)
		if err := os.RemoveAll(pidsDir); err != nil {
			t.Fatal(err)
		}

		isLast, err := IsLastProcess(pm, profile)
		if err != nil || !isLast {
			t.Errorf("Expected IsLastProcess to be true for empty profile, got %v, err: %v", isLast, err)
		}

		if err := RegisterProcess(pm, profile); err != nil {
			t.Fatal(err)
		}
		
		// Simulate the application flow: Unregister before checking if we are the last one
		if err := UnregisterProcess(pm, profile); err != nil {
			t.Fatal(err)
		}

		isLast, err = IsLastProcess(pm, profile)
		if err != nil || !isLast {
			t.Errorf("Expected IsLastProcess to be true after unregistering the only process, got %v, err: %v", isLast, err)
		}

		// Add a "stale" process to ensure it's pruned and doesn't count as active
		if err := os.WriteFile(filepath.Join(pidsDir, "1234567"), []byte(""), 0644); err != nil {
			t.Fatal(err)
		}
		
		// Register a real process so that pruning has something to do
		if err := RegisterProcess(pm, profile); err != nil {
			t.Fatal(err)
		}
		
		// Prune the stale one
		if err := PruneStalePids(pm, profile); err != nil {
			t.Fatal(err)
		}

		// Unregister the real one
		if err := UnregisterProcess(pm, profile); err != nil {
			t.Fatal(err)
		}

		isLast, err = IsLastProcess(pm, profile)
		if err != nil || !isLast {
			t.Errorf("Expected IsLastProcess to be true after pruning stale and unregistering current, got %v, err: %v", isLast, err)
		}
	})
}