diff options
Diffstat (limited to 'tests/e2e')
| -rw-r--r-- | tests/e2e/e2e_test.go | 26 | ||||
| -rw-r--r-- | tests/e2e/lifecycle_test.go | 92 |
2 files changed, 97 insertions, 21 deletions
diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index fb763b3..7b5858c 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -1,10 +1,7 @@ package e2e import ( - "fmt" - "os" "os/exec" - "path/filepath" "strings" "testing" ) @@ -14,36 +11,23 @@ func TestDataPlaneConnectivity(t *testing.T) { } func TestNetworkIsolation(t *testing.T) { - // 1. Determine project root - cwd, err := os.Getwd() + // 1. Determine binary path + binaryPath, err := GetBinaryPath() if err != nil { - t.Fatalf("Failed to get cwd: %v", err) + t.Skipf("Skipping test: %v", err) } - root := filepath.Join(cwd, "..", "..") - // 2. Build the project to ensure we have a fresh binary - buildCmd := exec.Command("bash", "-c", fmt.Sprintf( - "cd %s && gcc -static -O2 internal/namespace/launcher_src/launcher.c -o internal/namespace/launcher.bin && export CGO_ENABLED=1 && go build -o wg-wrap cmd/wg-wrap/main.go", - root)) - if err := buildCmd.Run(); err != nil { - t.Fatalf("Failed to build project for E2E test: %v", err) - } - - // 3. Run the test-ns command using the binary in the root - binaryPath := filepath.Join(root, "wg-wrap") + // 2. Run the test-ns command using the binary cmd := exec.Command(binaryPath, "test-ns") out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("wg-wrap test-ns failed: %v\nOutput: %s", err, string(out)) } - // 4. Verify the success message + // 3. Verify the success message if !strings.Contains(string(out), "Isolation Verified: OK") { t.Errorf("Expected 'Isolation Verified: OK', got: %q", string(out)) } - - // Cleanup - _ = os.Remove(binaryPath) } func TestDNSLeakage(t *testing.T) { diff --git a/tests/e2e/lifecycle_test.go b/tests/e2e/lifecycle_test.go new file mode 100644 index 0000000..45890fa --- /dev/null +++ b/tests/e2e/lifecycle_test.go @@ -0,0 +1,92 @@ +package e2e + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "testing" + "time" +) + +func TestNamespaceLifecycleAutomation(t *testing.T) { + // 1. Setup Environment + binaryPath, err := GetBinaryPath() + if err != nil { + t.Skipf("Skipping test: %v", err) + } + + // 2. Override the runtime base dir to a temporary location + tmpRuntimeDir := t.TempDir() + profile := "e2e-lifecycle-test" + pidsDir := filepath.Join(tmpRuntimeDir, "profiles", profile, "pids") + + // Clean up before starting + os.RemoveAll(filepath.Join(tmpRuntimeDir, "profiles", profile)) + + t.Run("ReferenceCounting", func(t *testing.T) { + // Start a process that exits quickly + cmd1 := exec.Command(binaryPath, "--profile", profile, "--", "sleep", "0.1") + cmd1.Env = append(os.Environ(), fmt.Sprintf("WG_WRAP_RUNTIME_DIR=%s", tmpRuntimeDir)) + if err := cmd1.Start(); err != nil { + t.Fatalf("Failed to start cmd1: %v", err) + } + + // Allow a moment for the bootstrap loop to complete and register the PID + time.Sleep(500 * time.Millisecond) + + // Verify PID file exists + files, err := os.ReadDir(pidsDir) + if err != nil { + t.Fatalf("Failed to read pids dir: %v", err) + } + if len(files) != 1 { + t.Errorf("Expected 1 PID file, got %d", len(files)) + } + + // Start a second process using the same profile + cmd2 := exec.Command(binaryPath, "--profile", profile, "--", "sleep", "0.1") + cmd2.Env = append(os.Environ(), fmt.Sprintf("WG_WRAP_RUNTIME_DIR=%s", tmpRuntimeDir)) + if err := cmd2.Start(); err != nil { + t.Fatalf("Failed to start cmd2: %v", err) + } + time.Sleep(500 * time.Millisecond) + + files, err = os.ReadDir(pidsDir) + if err != nil { + t.Fatalf("Failed to read pids dir: %v", err) + } + if len(files) != 2 { + t.Errorf("Expected 2 PID files, got %d", len(files)) + } + + // Wait for first process to exit naturally (triggering defer) + if err := cmd1.Wait(); err != nil { + t.Fatalf("cmd1 failed: %v", err) + } + time.Sleep(500 * time.Millisecond) + + files, err = os.ReadDir(pidsDir) + if err != nil { + t.Fatalf("Failed to read pids dir: %v", err) + } + if len(files) != 1 { + t.Errorf("Expected 1 PID file after first exit, got %d", len(files)) + } + + // Wait for second process to exit naturally + if err := cmd2.Wait(); err != nil { + t.Fatalf("cmd2 failed: %v", err) + } + time.Sleep(500 * time.Millisecond) + + // Verify a clean state + files, err = os.ReadDir(pidsDir) + if err != nil && !os.IsNotExist(err) { + t.Fatalf("Failed to read pids dir: %v", err) + } + if err == nil && len(files) != 0 { + t.Errorf("Expected 0 PID files after all exits, got %d", len(files)) + } + }) +} |
