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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
package paths
import (
"os"
"path/filepath"
"testing"
)
func TestPathManager_ConfigDir(t *testing.T) {
tests := []struct {
name string
configOverride string
envConfigHome string
userHome string
expected string
}{
{
name: "Use override",
configOverride: "/tmp/custom-config",
expected: "/tmp/custom-config",
},
{
name: "Use XDG_CONFIG_HOME",
envConfigHome: "/tmp/xdg-config",
expected: "/tmp/xdg-config/wg-wrap/profiles",
},
{
name: "Fallback to home",
userHome: "/home/testuser",
expected: "/home/testuser/.config/wg-wrap/profiles",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Backup and restore env
oldXdg := os.Getenv("XDG_CONFIG_HOME")
defer func() {
if err := os.Setenv("XDG_CONFIG_HOME", oldXdg); err != nil {
t.Errorf("failed to restore XDG_CONFIG_HOME: %v", err)
}
}()
if tt.envConfigHome != "" {
if err := os.Setenv("XDG_CONFIG_HOME", tt.envConfigHome); err != nil {
t.Fatalf("failed to set XDG_CONFIG_HOME: %v", err)
}
} else {
if err := os.Unsetenv("XDG_CONFIG_HOME"); err != nil {
t.Fatalf("failed to unset XDG_CONFIG_HOME: %v", err)
}
}
// We can't easily mock os.UserHomeDir() without monkey patching or interfaces
// but we can test the other paths.
pm := NewPathManager(tt.configOverride, "")
got := pm.ConfigDir()
if tt.userHome == "" && got != tt.expected {
t.Errorf("ConfigDir() = %v, want %v", got, tt.expected)
}
})
}
}
func TestPathManager_RuntimeBaseDir(t *testing.T) {
tests := []struct {
name string
runtimeOverride string
envHostRuntime string
envXdgRuntime string
expected string
}{
{
name: "Use override",
runtimeOverride: "/tmp/custom-runtime",
expected: "/tmp/custom-runtime",
},
{
name: "Use WG_WRAP_HOST_RUNTIME_BASE_DIR",
envHostRuntime: "/tmp/host-runtime",
expected: "/tmp/host-runtime",
},
{
name: "Use XDG_RUNTIME_DIR",
envXdgRuntime: "/tmp/xdg-runtime",
expected: "/tmp/xdg-runtime",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Backup and restore env
oldHost := os.Getenv("WG_WRAP_HOST_RUNTIME_BASE_DIR")
defer func() {
if err := os.Setenv("WG_WRAP_HOST_RUNTIME_BASE_DIR", oldHost); err != nil {
t.Errorf("failed to restore WG_WRAP_HOST_RUNTIME_BASE_DIR: %v", err)
}
}()
oldXdg := os.Getenv("XDG_RUNTIME_DIR")
defer func() {
if err := os.Setenv("XDG_RUNTIME_DIR", oldXdg); err != nil {
t.Errorf("failed to restore XDG_RUNTIME_DIR: %v", err)
}
}()
if tt.envHostRuntime != "" {
if err := os.Setenv("WG_WRAP_HOST_RUNTIME_BASE_DIR", tt.envHostRuntime); err != nil {
t.Fatalf("failed to set WG_WRAP_HOST_RUNTIME_BASE_DIR: %v", err)
}
} else {
if err := os.Unsetenv("WG_WRAP_HOST_RUNTIME_BASE_DIR"); err != nil {
t.Fatalf("failed to unset WG_WRAP_HOST_RUNTIME_BASE_DIR: %v", err)
}
}
if tt.envXdgRuntime != "" {
if err := os.Setenv("XDG_RUNTIME_DIR", tt.envXdgRuntime); err != nil {
t.Fatalf("failed to set XDG_RUNTIME_DIR: %v", err)
}
} else {
if err := os.Unsetenv("XDG_RUNTIME_DIR"); err != nil {
t.Fatalf("failed to unset XDG_RUNTIME_DIR: %v", err)
}
}
pm := NewPathManager("", tt.runtimeOverride)
got := pm.RuntimeBaseDir()
if got != tt.expected {
t.Errorf("RuntimeBaseDir() = %v, want %v", got, tt.expected)
}
})
}
}
func TestPathManager_ProfilePaths(t *testing.T) {
pm := NewPathManager("", "/tmp/runtime")
profile := "test-profile"
t.Run("ProfileNamespacePath", func(t *testing.T) {
expected := filepath.Join("/tmp/runtime", "profiles", profile+".ns")
got := pm.ProfileNamespacePath(profile)
if got != expected {
t.Errorf("ProfileNamespacePath() = %v, want %v", got, expected)
}
})
t.Run("ProfilePidsDir", func(t *testing.T) {
expected := filepath.Join("/tmp/runtime", "profiles", profile, "pids")
got := pm.ProfilePidsDir(profile)
if got != expected {
t.Errorf("ProfilePidsDir() = %v, want %v", got, expected)
}
})
}
|