hako/e2e/e2eutil/testserver.go
Felipe M. f47010cd0f
Some checks failed
CI / goreleaser-lint (pull_request) Successful in 11s
CI / format (pull_request) Successful in 2m38s
CI / test (pull_request) Successful in 5m0s
CI / lint (pull_request) Successful in 3m5s
CI / build (pull_request) Successful in 3m16s
CI / e2e (pull_request) Failing after 28m15s
fix(e2e): make link creation reach test web server via shared network
The test web server and Hako container previously ran on isolated Docker
networks, so Hako could not fetch URLs like http://localhost:32865 (the
host-mapped port) from inside its container. Most tests passed vacuously
because they only checked DOM containers; the Archive tests failed
because they tried to click an actual link item that was never created.

Changes:
- Add a shared Docker network so both containers can communicate by alias
- Test web server registers as hako-testserver and returns that as its URL
- Hako container joins the same network and can resolve the alias
- CreateLink now navigates to /home before each call so it can be invoked
  multiple times in a row (the form redirects to /links after submission)
- IsVisible uses .First() to avoid Playwright strict-mode errors when a
  selector matches multiple elements
- Archive tests use .link-item instead of the never-matching
  a[href*='/links/'] selector
- Bump Dockerfile.e2e Alpine to 3.23 to match the production Containerfile
2026-04-08 16:42:42 +02:00

70 lines
2.1 KiB
Go

package e2eutil
import (
"context"
"fmt"
"path/filepath"
"testing"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// StartTestWebServer starts an nginx container with test content for E2E testing.
// The container is attached to the shared network with the testServerAlias hostname,
// and the returned URL uses that alias so the Hako container can fetch from it.
func StartTestWebServer(ctx context.Context, t *testing.T) (testcontainers.Container, string, error) {
t.Helper()
// Get the project root (one level up from e2e)
projectRoot, err := filepath.Abs(filepath.Join("..", ".."))
if err != nil {
return nil, "", fmt.Errorf("failed to get project root: %w", err)
}
// Ensure shared network exists so Hako can reach this server by alias.
nw, err := getSharedNetwork(ctx)
if err != nil {
return nil, "", err
}
// Create container request attached to the shared network.
req := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: filepath.Join(projectRoot, "e2e", "testserver"),
Dockerfile: "Dockerfile",
PrintBuildLog: false,
},
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForHTTP("/").WithStartupTimeout(10 * time.Second),
Networks: []string{nw.Name},
NetworkAliases: map[string][]string{
nw.Name: {testServerAlias},
},
}
// Start container
t.Log("Starting test web server container...")
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, "", fmt.Errorf("failed to start test web server container: %w", err)
}
// Register cleanup
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Logf("Failed to terminate test web server container: %v", err)
}
})
// Use the network alias as the base URL so Hako can reach this server
// from inside its container via Docker DNS.
baseURL := fmt.Sprintf("http://%s", testServerAlias)
t.Logf("Test web server started at %s (network alias)", baseURL)
return container, baseURL, nil
}