diff options
Diffstat (limited to 'internal/paths/paths_test.go')
| -rw-r--r-- | internal/paths/paths_test.go | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/internal/paths/paths_test.go b/internal/paths/paths_test.go new file mode 100644 index 0000000..caf103d --- /dev/null +++ b/internal/paths/paths_test.go @@ -0,0 +1,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) + } + }) +} |
