|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/docker/docker/api/types/container" |
| 12 | + "github.com/docker/docker/api/types/mount" |
| 13 | + "github.com/shellhub-io/shellhub/tests/environment" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + tc "github.com/testcontainers/testcontainers-go" |
| 16 | + "github.com/testcontainers/testcontainers-go/wait" |
| 17 | +) |
| 18 | + |
| 19 | +func TestE2E(t *testing.T) { |
| 20 | + ctx := context.Background() |
| 21 | + |
| 22 | + // Start ShellHub environment with UI (using e2e compose) |
| 23 | + compose := environment.NewE2E(t).Up(ctx) |
| 24 | + t.Cleanup(compose.Down) |
| 25 | + |
| 26 | + // Get the base URL for Playwright - use gateway hostname inside Docker network |
| 27 | + baseURL := "http://gateway:80" |
| 28 | + |
| 29 | + // Run Playwright tests in container |
| 30 | + t.Run("homepage", func(t *testing.T) { |
| 31 | + runPlaywrightTest(t, ctx, compose, baseURL, "tests/homepage.spec.ts") |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func runPlaywrightTest(t *testing.T, ctx context.Context, compose *environment.DockerCompose, baseURL, testFile string) { |
| 36 | + // Get absolute path to e2e directory |
| 37 | + absPath, err := filepath.Abs(".") |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + // Get the ShellHub network name from compose environment |
| 41 | + networkName := compose.Env("SHELLHUB_NETWORK") |
| 42 | + |
| 43 | + // Create Playwright container that installs deps and runs tests |
| 44 | + req := tc.ContainerRequest{ |
| 45 | + Image: "mcr.microsoft.com/playwright:v1.57.0-jammy", |
| 46 | + Cmd: []string{ |
| 47 | + "sh", "-c", |
| 48 | + "npm install && npx playwright test " + testFile, |
| 49 | + }, |
| 50 | + Env: map[string]string{ |
| 51 | + "BASE_URL": baseURL, |
| 52 | + }, |
| 53 | + HostConfigModifier: func(hc *container.HostConfig) { |
| 54 | + hc.Mounts = []mount.Mount{ |
| 55 | + { |
| 56 | + Type: mount.TypeBind, |
| 57 | + Source: absPath, |
| 58 | + Target: "/app", |
| 59 | + }, |
| 60 | + } |
| 61 | + }, |
| 62 | + WorkingDir: "/app", |
| 63 | + Networks: []string{networkName}, |
| 64 | + WaitingFor: wait.ForExit().WithExitTimeout(5 * time.Minute), |
| 65 | + } |
| 66 | + |
| 67 | + container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ |
| 68 | + ContainerRequest: req, |
| 69 | + Started: true, |
| 70 | + }) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + // Wait for completion |
| 74 | + _, err = container.State(ctx) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + // Get and print logs |
| 78 | + logs, err := container.Logs(ctx) |
| 79 | + require.NoError(t, err) |
| 80 | + |
| 81 | + buf := new(strings.Builder) |
| 82 | + _, _ = io.Copy(buf, logs) |
| 83 | + t.Logf("Playwright output:\n%s", buf.String()) |
| 84 | + |
| 85 | + // Cleanup |
| 86 | + exitCode, err := container.State(ctx) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + _ = container.Terminate(ctx) |
| 90 | + |
| 91 | + // Check exit code |
| 92 | + require.Equal(t, 0, exitCode.ExitCode, "Playwright tests should pass") |
| 93 | +} |
0 commit comments