summaryrefslogtreecommitdiff
path: root/tests/e2e/resource_exhaustion_test.go
blob: b5cdaf9b676cf8e3c085cc9036404f6b20606d9d (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
package e2e

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"testing"
)

// TestResourceExhaustion ensures that repeatedly starting and stopping
// tunnels does not leak mounts, file descriptors, or namespaces.
func TestResourceExhaustion(t *testing.T) {
	binaryPath := EnsureBinary(t)

	tmpRuntimeDir := t.TempDir()
	tmpConfigDir := t.TempDir()
	profile := "stress-test"

	profilesDir := filepath.Join(tmpConfigDir, "wg-wrap", "profiles")
	if err := os.MkdirAll(profilesDir, 0755); err != nil {
		t.Fatal(err)
	}
	profileConfPath := filepath.Join(profilesDir, profile+".conf")
	conf := `[Interface]
Address = 10.0.0.2/24
PrivateKey = 0000000000000000000000000000000000000000000000000000000000000000
[Peer]
PublicKey = 0000000000000000000000000000000000000000000000000000000000000000
AllowedIPs = 0.0.0.0/0
Endpoint = 1.1.1.1:51820
`
	if err := os.WriteFile(profileConfPath, []byte(conf), 0644); err != nil {
		t.Fatal(err)
	}

	// We run a burst of short-lived commands to stress the lock and cleanup logic.
	iterations := 50
	for i := 0; i < iterations; i++ {
		cmd := exec.Command(binaryPath, "--profile", profile, "--", "true")
		cmd.Env = append(os.Environ(),
			fmt.Sprintf("XDG_RUNTIME_DIR=%s", tmpRuntimeDir),
			fmt.Sprintf("XDG_CONFIG_HOME=%s", tmpConfigDir),
		)
		if err := cmd.Run(); err != nil {
			t.Fatalf("Iteration %d failed: %v", i, err)
		}
	}

	// After all iterations, the pin file should be gone.
	nsPath := filepath.Join(tmpRuntimeDir, "profiles", profile+".ns")
	if _, err := os.Stat(nsPath); err == nil {
		t.Errorf("BUG: Namespace pin file %s still exists after %d iterations", nsPath, iterations)
	}

	// PIDs directory should be empty or gone.
	pidsDir := filepath.Join(tmpRuntimeDir, "profiles", profile, "pids")
	if files, err := os.ReadDir(pidsDir); err == nil && len(files) > 0 {
		t.Errorf("BUG: PIDs directory not empty after stress test: %d files remaining", len(files))
	}
}