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

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

// TestConfigHotSwap verifies that changing the configuration file on disk
// does not affect an active session. A process joining an existing session
// should use the established tunnel's state, not the updated file.
func TestConfigHotSwap(t *testing.T) {
	binaryPath, err := GetBinaryPath()
	if err != nil {
		t.Skipf("Skipping test: %v", err)
	}

	tmpRuntimeDir := t.TempDir()
	tmpConfigDir := t.TempDir()
	profile := "hotswap-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")

	// 1. Initial configuration
	conf1 := `[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(conf1), 0644); err != nil {
		t.Fatal(err)
	}

	// Start a process to establish the session
	cmdA := exec.Command(binaryPath, "--profile", profile, "--", "sleep", "0.1")
	cmdA.Env = append(os.Environ(),
		fmt.Sprintf("XDG_RUNTIME_DIR=%s", tmpRuntimeDir),
		fmt.Sprintf("XDG_CONFIG_HOME=%s", tmpConfigDir),
	)
	if err := cmdA.Start(); err != nil {
		t.Fatalf("Failed to start Process A: %v", err)
	}
	defer func() { _ = cmdA.Process.Kill() }()

	waitForLifecycle(t, binaryPath, tmpRuntimeDir, profile, true)

	// 2. "Hot-Swap" the configuration file while the tunnel is active.
	// We change the endpoint to something obviously different.
	conf2 := `[Interface]
Address = 10.0.0.2/24
PrivateKey = 0000000000000000000000000000000000000000000000000000000000000000
[Peer]
PublicKey = 0000000000000000000000000000000000000000000000000000000000000000
AllowedIPs = 0.0.0.0/0
Endpoint = 8.8.8.8:51820
`
	if err := os.WriteFile(profileConfPath, []byte(conf2), 0644); err != nil {
		t.Fatal(err)
	}

	// 3. Launch a second process. It should join the existing session
	// regardless of the fact that the .conf file has changed.
	cmdB := exec.Command(binaryPath, "--profile", profile, "--", "ls")
	cmdB.Env = append(os.Environ(),
		fmt.Sprintf("XDG_RUNTIME_DIR=%s", tmpRuntimeDir),
		fmt.Sprintf("XDG_CONFIG_HOME=%s", tmpConfigDir),
	)

	out, err := cmdB.CombinedOutput()
	if err != nil {
		t.Fatalf("Process B failed to join session after config change: %v\nOutput: %s", err, string(out))
	}

	if !strings.Contains(string(out), "Joining active WireGuard tunnel") {
		t.Errorf("Expected Process B to join active tunnel, but it re-initialized. Output: %s", string(out))
	}
}