From 135f6edbd9389bc4783f13c26aed0a74d3c8aca0 Mon Sep 17 00:00:00 2001 From: James O'Doherty Date: Fri, 22 May 2026 16:17:55 -0400 Subject: refactor: unify path management and complete profile management system - Create internal/paths package for unified config and runtime directory resolution - Implement robust WireGuard config parsing in pkg/wgconf - Implement profile management subcommands: list, import, configure, delete, stop - Fix namespace pinning path collisions (separating .ns files from pids directories) - Implement and verify namespace unpinning logic - Fix linting errors and improve error handling across the project --- internal/cli/profile_test.go | 125 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 internal/cli/profile_test.go (limited to 'internal/cli/profile_test.go') diff --git a/internal/cli/profile_test.go b/internal/cli/profile_test.go new file mode 100644 index 0000000..d256cb0 --- /dev/null +++ b/internal/cli/profile_test.go @@ -0,0 +1,125 @@ +package cli + +import ( + "os" + "path/filepath" + "testing" +) + +func TestProfileList(t *testing.T) { + tmpDir := t.TempDir() + + // Create some dummy profile files + profiles := []string{"home.conf", "work.conf", "not-a-conf.txt"} + for _, p := range profiles { + err := os.WriteFile(filepath.Join(tmpDir, p), []byte("test content"), 0644) + if err != nil { + t.Fatalf("failed to create test profile %s: %v", p, err) + } + } + + app := NewApp([]string{"wg-wrap", "profile", "list"}) + app.ConfigDir = tmpDir + + err := app.Route() + if err != nil { + t.Errorf("expected no error, got %v", err) + } +} + +func TestProfileImport(t *testing.T) { + tmpDir := t.TempDir() + profilesDir := filepath.Join(tmpDir, "profiles") + err := os.MkdirAll(profilesDir, 0755) + if err != nil { + t.Fatalf("failed to create profiles dir: %v", err) + } + + srcFile := filepath.Join(tmpDir, "source.conf") + err = os.WriteFile(srcFile, []byte("[Interface]\nPrivateKey = test\n"), 0644) + if err != nil { + t.Fatalf("failed to create source conf: %v", err) + } + + app := NewApp([]string{"wg-wrap", "profile", "import", srcFile}) + app.ConfigDir = profilesDir + + err = app.Route() + if err != nil { + t.Errorf("expected no error, got %v", err) + } + + // Verify the file was actually copied + destFile := filepath.Join(profilesDir, "source.conf") + if _, err := os.Stat(destFile); os.IsNotExist(err) { + t.Errorf("expected profile to be imported to %s", destFile) + } +} + +func TestProfileDelete(t *testing.T) { + tmpDir := t.TempDir() + profilesDir := filepath.Join(tmpDir, "profiles") + err := os.MkdirAll(profilesDir, 0755) + if err != nil { + t.Fatalf("failed to create profiles dir: %v", err) + } + + profileName := "test-profile" + profileFile := filepath.Join(profilesDir, profileName+".conf") + err = os.WriteFile(profileFile, []byte("[Interface]\nPrivateKey = test\n"), 0644) + if err != nil { + t.Fatalf("failed to create profile file: %v", err) + } + + app := NewApp([]string{"wg-wrap", "profile", "delete", profileName}) + app.ConfigDir = profilesDir + + err = app.Route() + if err != nil { + t.Errorf("expected no error, got %v", err) + } + + if _, err := os.Stat(profileFile); !os.IsNotExist(err) { + t.Errorf("expected profile file %s to be deleted", profileFile) + } +} + +func TestProfileDeleteNotFound(t *testing.T) { + tmpDir := t.TempDir() + app := NewApp([]string{"wg-wrap", "profile", "delete", "non-existent"}) + app.ConfigDir = tmpDir + + err := app.Route() + if err == nil { + t.Errorf("expected error when deleting non-existent profile, got nil") + } +} + +func TestProfileConfigure(t *testing.T) { + // profile configure is intended to modify existing configs. + // For now, we just want to ensure it doesn't crash and we can + // eventually implement it. + + tmpDir := t.TempDir() + profilesDir := filepath.Join(tmpDir, "profiles") + err := os.MkdirAll(profilesDir, 0755) + if err != nil { + t.Fatalf("failed to create profiles dir: %v", err) + } + + profileName := "test-profile" + profileFile := filepath.Join(profilesDir, profileName+".conf") + err = os.WriteFile(profileFile, []byte("[Interface]\nPrivateKey = test\n"), 0644) + if err != nil { + t.Fatalf("failed to create profile file: %v", err) + } + + app := NewApp([]string{"wg-wrap", "profile", "configure", profileName}) + app.ConfigDir = profilesDir + + err = app.Route() + // This will currently return "not yet implemented" error, which is expected for now. + if err == nil { + t.Errorf("expected 'not yet implemented' error, got nil") + } +} -- cgit v1.2.3