hako/e2e/e2eutil/testserver.go
Felipe M. d71915a3fb
test(e2e): expand test suite with comprehensive coverage
- Add e2e tests for admin pages (archivers, extractors, rules)
- Add tests for archives, links, navigation, and error handling
- Add search and pagination test coverage
- Implement test web server with Docker container
- Add seed data and helper utilities for tests
- Update webapp components for permission handling
- Generate HTML reports with embedded screenshots

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-29 12:49:40 +01:00

71 lines
2 KiB
Go

package e2eutil
import (
"context"
"fmt"
"path/filepath"
"testing"
"time"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// StartTestWebServer starts an nginx container with test content for E2E testing.
// It builds the test web server image and returns the container instance and base URL.
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)
}
// Create container request
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),
}
// 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)
}
})
// Get the mapped port
mappedPort, err := container.MappedPort(ctx, nat.Port("80/tcp"))
if err != nil {
return nil, "", fmt.Errorf("failed to get mapped port: %w", err)
}
// Get the host
host, err := container.Host(ctx)
if err != nil {
return nil, "", fmt.Errorf("failed to get container host: %w", err)
}
// Construct base URL
baseURL := fmt.Sprintf("http://%s:%s", host, mappedPort.Port())
t.Logf("Test web server started at %s", baseURL)
return container, baseURL, nil
}