From 04dca5dada8c2d971ff3b54eeedc5ab6e53a29ac Mon Sep 17 00:00:00 2001 From: James O'Doherty Date: Thu, 4 Jun 2026 22:57:35 -0400 Subject: refactor: decouple namespace operations and improve test coverage - Introduce `namespace.Ops` interface to decouple `Manager` from system-level namespace operations, enabling easier unit testing via mocks. - Add unit tests for `internal/paths` to verify path resolution logic across different environment configurations. - Implement `EnsureBinary` helper in E2E tests to gracefully skip tests when `WG_WRAP_BIN` is not set, allowing `go test ./...` to pass in non-build environments. - Apply project-wide formatting and fix linting issues. --- tests/e2e/e2e_test.go | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) (limited to 'tests/e2e/e2e_test.go') diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 939d231..907bb22 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -13,10 +13,7 @@ import ( func TestDataPlaneConnectivity(t *testing.T) { // 1. Determine binary path - binaryPath, err := GetBinaryPath() - if err != nil { - t.Skipf("Skipping test: %v", err) - } + binaryPath := EnsureBinary(t) // 2. Setup isolated config & runtime folders for testing tmpDir := t.TempDir() @@ -78,10 +75,7 @@ AllowedIPs = 10.0.0.0/24 } }() - err = cmd.Run() - // We expect the command (ping) to fail because our mock peer does not complete - // the handshake or reply to pings, but we log the error for diagnostic purposes. - if err != nil { + if err := cmd.Run(); err != nil { t.Logf("wg-wrap command returned error (expected since mock peer doesn't reply): %v", err) } @@ -100,10 +94,7 @@ AllowedIPs = 10.0.0.0/24 } func TestNetworkIsolation(t *testing.T) { - binaryPath, err := GetBinaryPath() - if err != nil { - t.Skipf("Skipping test: %v", err) - } + binaryPath := EnsureBinary(t) cmd := exec.Command(binaryPath, "test-ns") out, err := cmd.CombinedOutput() @@ -117,10 +108,7 @@ func TestNetworkIsolation(t *testing.T) { } func TestDNSIsolation(t *testing.T) { - binaryPath, err := GetBinaryPath() - if err != nil { - t.Skipf("Skipping test: %v", err) - } + binaryPath := EnsureBinary(t) // 1. Start Mock DNS Server dnsServer, port := StartMockDNSServer(t) @@ -190,10 +178,7 @@ AllowedIPs = 10.0.0.0/24 } func TestDNSPrecedence(t *testing.T) { - binaryPath, err := GetBinaryPath() - if err != nil { - t.Skipf("Skipping test: %v", err) - } + binaryPath := EnsureBinary(t) tmpDir := t.TempDir() profileName := "test-dns-precedence" @@ -276,10 +261,7 @@ AllowedIPs = 10.0.0.0/24 } func TestMTUFragmentation(t *testing.T) { - binaryPath, err := GetBinaryPath() - if err != nil { - t.Skipf("Skipping test: %v", err) - } + binaryPath := EnsureBinary(t) cmd := exec.Command(binaryPath, "--profile", "default", "--", "true") if err := cmd.Run(); err != nil { @@ -288,14 +270,11 @@ func TestMTUFragmentation(t *testing.T) { } func TestExitCodePropagation(t *testing.T) { - binaryPath, err := GetBinaryPath() - if err != nil { - t.Skipf("Skipping test: %v", err) - } + binaryPath := EnsureBinary(t) // Run a command that exits with code 42 cmd := exec.Command(binaryPath, "--profile", "default", "--", "sh", "-c", "exit 42") - err = cmd.Run() + err := cmd.Run() if err == nil { t.Fatalf("expected command to fail with exit status 42, but it succeeded") } -- cgit v1.2.3