summaryrefslogtreecommitdiff
path: root/internal/namespace/pinning_test.go
blob: c65e1b101668df8b9424ec3ec809e8c65f5c8362 (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
package namespace

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

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

func TestUnpinNamespace(t *testing.T) {
	tmpDir := t.TempDir()
	pm := paths.NewPathManager("", tmpDir)
	profile := "test-profile"
	nsPath := GetProfileNamespacePath(pm, profile)

	// Create the base profiles directory first
	profilesDir := filepath.Dir(nsPath)
	if err := os.MkdirAll(profilesDir, 0755); err != nil {
		t.Fatalf("failed to create profiles dir: %v", err)
	}

	// Create dummy namespace file
	if err := os.WriteFile(nsPath, []byte("dummy"), 0644); err != nil {
		t.Fatalf("failed to create ns file: %v", err)
	}

	pidsDir := GetPidsDirPath(pm, profile)
	if err := os.MkdirAll(pidsDir, 0755); err != nil {
		t.Fatalf("failed to create pids dir: %v", err)
	}

	t.Run("successfully unpins", func(t *testing.T) {
		err := UnpinNamespace(pm, profile)
		if err != nil {
			t.Errorf("unexpected error: %v", err)
		}

		if _, err := os.Stat(nsPath); !os.IsNotExist(err) {
			t.Errorf("namespace file should have been deleted")
		}
		if _, err := os.Stat(pidsDir); !os.IsNotExist(err) {
			t.Errorf("pids directory should have been deleted")
		}
	})

	t.Run("handles non-existent namespace", func(t *testing.T) {
		err := UnpinNamespace(pm, profile)
		if err != nil {
			t.Errorf("unexpected error when unpinning non-existent namespace: %v", err)
		}
	})
}