From 22ada6251f6d16a1102f86860a81f12b91e2dae4 Mon Sep 17 00:00:00 2001 From: Henri Vasserman Date: Fri, 9 May 2025 20:58:55 +0300 Subject: [PATCH] test(playwright): Add Docker and Podman support (#433) * test(playwright): Add support to run tests in Docker/Podman * fix command name Co-authored-by: Xe Iaso Signed-off-by: Henri Vasserman * up the pw version as it is in package.json * add convenience npm scripts * chore: changelog update Also removed a period from my other item. * chore: fix spelling Signed-off-by: Xe Iaso --------- Signed-off-by: Henri Vasserman Signed-off-by: Xe Iaso Co-authored-by: Xe Iaso --- .github/actions/spelling/expect.txt | 5 ++++ docs/docs/CHANGELOG.md | 3 ++- internal/test/playwright_test.go | 40 ++++++++++++++++++++--------- package.json | 2 ++ 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index 8ac4764..892c6b6 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -117,6 +117,7 @@ loadbalancer lol maintainership malware +mcr memes mimi minica @@ -141,10 +142,13 @@ pids pipefail pki podkova +podman prebaked privkey promauto promhttp +pwcmd +pwuser qwant qwantbot rac @@ -196,6 +200,7 @@ webpage websecure websites workaround +workdir xcaddy Xeact xeiaso diff --git a/docs/docs/CHANGELOG.md b/docs/docs/CHANGELOG.md index 1e97ef3..22e5206 100644 --- a/docs/docs/CHANGELOG.md +++ b/docs/docs/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `check-spelling` for spell checking - Add `--target-insecure-skip-verify` flag/envvar to allow Anubis to hit a self-signed HTTPS backend. - Minor adjustments to FreeBSD rc.d script to allow for more flexible configuration. +- Added Podman and Docker support for running Playwright tests ## v1.18.0: Varis zos Galvus @@ -69,7 +70,7 @@ Other changes: - Use CSS variables to deduplicate styles - Fixed native packages not containing the stdlib and botPolicies.yaml - Change import syntax to allow multi-level imports -- Changed the startup logging to use JSON formatting as all the other logs do. +- Changed the startup logging to use JSON formatting as all the other logs do - Added the ability to do [expression matching with CEL](./admin/configuration/expressions.mdx) - Add a warning for clients that don't store cookies - Disable Open Graph passthrough by default ([#435](https://github.com/TecharoHQ/anubis/issues/435)) diff --git a/internal/test/playwright_test.go b/internal/test/playwright_test.go index 916a363..66c84df 100644 --- a/internal/test/playwright_test.go +++ b/internal/test/playwright_test.go @@ -38,6 +38,7 @@ var ( playwrightServer = flag.String("playwright", "ws://localhost:9001", "Playwright server URL") playwrightMaxTime = flag.Duration("playwright-max-time", 5*time.Second, "maximum time for Playwright requests") playwrightMaxHardTime = flag.Duration("playwright-max-hard-time", 5*time.Minute, "maximum time for hard Playwright requests") + playwrightRunner = flag.String("playwright-runner", "npx", "how to start Playwright, can be: none,npx,docker,podman") testCases = []testCase{ { @@ -98,7 +99,7 @@ const ( actionChallenge action = "CHALLENGE" placeholderIP = "fd11:5ee:bad:c0de::" - playwrightVersion = "1.51.1" + playwrightVersion = "1.52.0" ) type action string @@ -111,11 +112,11 @@ type testCase struct { isHard bool } -func doesNPXExist(t *testing.T) { +func doesCommandExist(t *testing.T, command string) { t.Helper() - if _, err := exec.LookPath("npx"); err != nil { - t.Skipf("npx not found in PATH, skipping integration smoke testing: %v", err) + if _, err := exec.LookPath(command); err != nil { + t.Skipf("%s not found in PATH, skipping integration smoke testing: %v", command, err) } } @@ -170,13 +171,30 @@ func daemonize(t *testing.T, command string) { func startPlaywright(t *testing.T) { t.Helper() - if os.Getenv("CI") == "true" { - run(t, fmt.Sprintf("npx --yes playwright@%s install --with-deps", playwrightVersion)) - } else { - run(t, fmt.Sprintf("npx --yes playwright@%s install", playwrightVersion)) - } + if *playwrightRunner == "npx" { + doesCommandExist(t, "npx") - daemonize(t, fmt.Sprintf("npx --yes playwright@%s run-server --port %d", playwrightVersion, *playwrightPort)) + if os.Getenv("CI") == "true" { + run(t, fmt.Sprintf("npx --yes playwright@%s install --with-deps", playwrightVersion)) + } else { + run(t, fmt.Sprintf("npx --yes playwright@%s install", playwrightVersion)) + } + + daemonize(t, fmt.Sprintf("npx --yes playwright@%s run-server --port %d", playwrightVersion, *playwrightPort)) + } else if *playwrightRunner == "docker" || *playwrightRunner == "podman" { + doesCommandExist(t, *playwrightRunner) + + // docs: https://playwright.dev/docs/docker + pwcmd := fmt.Sprintf("npx -y playwright@%s run-server --port %d --host 0.0.0.0", playwrightVersion, *playwrightPort) + container := run(t, fmt.Sprintf("%s run -d --ipc=host --user pwuser --workdir /home/pwuser --net=host mcr.microsoft.com/playwright:v%s-noble /bin/sh -c \"%s\"", *playwrightRunner, playwrightVersion, pwcmd)) + t.Cleanup(func() { + run(t, fmt.Sprintf("%s rm --force %s", *playwrightRunner, container)) + }) + } else if *playwrightRunner == "none" { + t.Log("not starting Playwright, assuming it is already running") + } else { + t.Skipf("unknown runner: %s, skipping", *playwrightRunner) + } for { if _, err := http.Get(fmt.Sprintf("http://localhost:%d", *playwrightPort)); err != nil { @@ -196,7 +214,6 @@ func TestPlaywrightBrowser(t *testing.T) { return } - doesNPXExist(t) startPlaywright(t) pw := setupPlaywright(t) @@ -274,7 +291,6 @@ func TestPlaywrightWithBasePrefix(t *testing.T) { t.Skip("NOTE(Xe)\\ these tests require HTTPS support in #364") - doesNPXExist(t) startPlaywright(t) pw := setupPlaywright(t) diff --git a/package.json b/package.json index ca9f06e..62c11a1 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,8 @@ "scripts": { "test": "npm run assets && go test ./...", "test:integration": "npm run assets && go test -v ./internal/test", + "test:integration:podman": "npm run assets && go test -v ./internal/test --playwright-runner=podman", + "test:integration:docker": "npm run assets && go test -v ./internal/test --playwright-runner=docker", "assets": "go generate ./... && ./web/build.sh && ./xess/build.sh", "build": "npm run assets && go build -o ./var/anubis ./cmd/anubis", "dev": "npm run assets && go run ./cmd/anubis --use-remote-address",