From 22c01b3d50a0061c56c9a6659f17f00256ac9bf9 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 18 Nov 2023 14:55:15 +0100 Subject: [PATCH] Change gui-tests script to run all steps inside docker containers --- .github/workflows/ci.yml | 14 +--- docker-compose.yml | 58 ++++++++++++---- dockerfiles/run-gui-tests.sh | 40 ----------- .../Dockerfile | 8 +-- gui-tests/README.md | 57 +++++++++++++++ gui-tests/in-docker | 69 +++++++++++++++++++ 6 files changed, 177 insertions(+), 69 deletions(-) delete mode 100755 dockerfiles/run-gui-tests.sh rename dockerfiles/Dockerfile-gui-tests => gui-tests/Dockerfile (95%) create mode 100644 gui-tests/README.md create mode 100755 gui-tests/in-docker diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index af9388035..3d206fcf2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -119,21 +119,11 @@ jobs: needs: build steps: - uses: actions/checkout@v4 - - id: install - run: | - rustup override set stable - rustup update stable - - - name: restore build & cargo cache - uses: Swatinem/rust-cache@v2 - - name: Launch postgres and min.io + - name: Run GUI tests run: | touch .docker.env - docker compose up --wait --wait-timeout 30 db s3 - - - name: Run GUI tests - run: ./dockerfiles/run-gui-tests.sh + gui-tests/in-docker build_tests: runs-on: ubuntu-latest diff --git a/docker-compose.yml b/docker-compose.yml index ccdacd62a..233330bac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,11 @@ x-environment: &environment DOCSRS_BUILD_CPU_LIMIT: 2 DOCSRS_INCLUDE_DEFAULT_TARGETS: false +x-gui-tests-environment: &gui-tests-environment + << : *environment + DOCSRS_DATABASE_URL: postgresql://cratesfyi:password@db/gui-tests + DOCSRS_S3_BUCKET: gui-tests + x-builder: &builder build: context: . @@ -47,7 +52,7 @@ x-builder: &builder test: curl --silent --fail localhost:3000/about/metrics services: - web: + web: &web build: context: . dockerfile: ./dockerfiles/Dockerfile @@ -98,7 +103,7 @@ services: - ".rustwide-docker/builder-b:/opt/docsrs/rustwide" - "/var/run/docker.sock:/var/run/docker.sock" - cli: + cli: &cli build: context: . dockerfile: ./dockerfiles/Dockerfile @@ -134,8 +139,16 @@ services: image: minio/minio entrypoint: > /bin/sh -c " - mkdir -p /data/rust-docs-rs; - minio server /data --console-address ":9001"; + set -meu + mc alias rm s3 || true + mc alias rm gcs || true + mc alias rm local || true + mc alias rm play || true + minio server /data --console-address ":9001" & + sleep 1 + mc alias set local http://s3:9000 cratesfyi secret_key + mc mb --ignore-existing s3/rust-docs-rs + fg " ports: - "127.0.0.1:9000:9000" @@ -159,15 +172,36 @@ services: << : *healthcheck-interval test: promtool check healthy - gui_tests: - build: - context: . - dockerfile: ./dockerfiles/Dockerfile-gui-tests - network_mode: "host" - extra_hosts: - - "host.docker.internal:host-gateway" + gui-tests-cli: + << : *cli + environment: *gui-tests-environment + profiles: + - all + + gui-tests-builder: + << : *builder + environment: *gui-tests-environment volumes: - - "${PWD}:/build/out" + - ".rustwide-docker/builder-gui-tests:/opt/docsrs/rustwide" + - "/var/run/docker.sock:/var/run/docker.sock" + profiles: + - all + + gui-tests-web: + << : *web + ports: + - "3001:80" + environment: *gui-tests-environment + profiles: + - all + + gui-tests: + build: + context: gui-tests + depends_on: + - gui-tests-web + environment: + SERVER_URL: http://gui-tests-web profiles: - all diff --git a/dockerfiles/run-gui-tests.sh b/dockerfiles/run-gui-tests.sh deleted file mode 100755 index 8c1940ae3..000000000 --- a/dockerfiles/run-gui-tests.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bash - -set -e - -# Just in case it's running, we stop the web server. -docker compose stop web - -docker compose up --wait --wait-timeout 30 db s3 - -# If we have a .env file, we need to temporarily move it so -# it doesn't make sqlx fail compilation. -if [ -f .env ]; then - mv .env .tmp.env -fi - -# We add the information we need. -cargo run -- database migrate -cargo run -- build update-toolchain -cargo run -- build crate sysinfo 0.23.4 -cargo run -- build crate sysinfo 0.23.5 -cargo run -- build add-essential-files - -if [ -f .tmp.env ]; then - mv .tmp.env .env -fi - -# In case we don't have a `.env`, we create one. -if [ ! -f .env ]; then - cp .env.sample .env -fi - -. .env - -cargo run -- start-web-server & -SERVER_PID=$! - -# status="docker run . -v `pwd`:/build/out:ro gui_tests" -docker compose run gui_tests -status=$? -exit $status diff --git a/dockerfiles/Dockerfile-gui-tests b/gui-tests/Dockerfile similarity index 95% rename from dockerfiles/Dockerfile-gui-tests rename to gui-tests/Dockerfile index 4e4108d2d..6563249ba 100644 --- a/dockerfiles/Dockerfile-gui-tests +++ b/gui-tests/Dockerfile @@ -65,9 +65,7 @@ RUN curl -sL https://nodejs.org/dist/v14.4.0/node-v14.4.0-linux-x64.tar.xz | tar ENV PATH="/node-v14.4.0-linux-x64/bin:${PATH}" ENV NODE_PATH="/node-v14.4.0-linux-x64/lib/node_modules/" -WORKDIR /build - -RUN mkdir out +WORKDIR /gui-tests # For now, we need to use `--unsafe-perm=true` to go around an issue when npm tries # to create a new folder. For reference: @@ -76,6 +74,6 @@ RUN mkdir out # We also specify the version in case we need to update it to go around cache limitations. RUN npm install -g browser-ui-test@0.16.10 --unsafe-perm=true -EXPOSE 3000 +COPY . /gui-tests -CMD ["node", "/build/out/gui-tests/tester.js"] +CMD ["node", "tester.js"] diff --git a/gui-tests/README.md b/gui-tests/README.md new file mode 100644 index 000000000..b7e5e564f --- /dev/null +++ b/gui-tests/README.md @@ -0,0 +1,57 @@ +## Running gui-tests in docker + +The easiest way to run the gui-tests in a stable environment setup as they +expect is by spinning up a temporary web service with the correct data in +docker. + +This is supported by the `in-docker` script. It has three phases that can be +run, to allow quicker feedback when editing the gui-tests themselves. + +```console +# initialize a temporary database and web service and builds some crates in it +> gui-tests/in-docker init +... + ✔ Container docsrs-db-1 Healthy + ✔ Container docsrs-s3-1 Healthy + ✔ Container docsrs-gui-tests-web-1 Healthy + +# while you have changes to make +# do + # edit your tests + > vim gui-tests/basic.goml + + # run the tests against the temporary web service + > gui-tests/in-docker run + ... + Running 2 docs.rs GUI (2 concurrently) ... + .. (2/2) +# done + +# tear down the temporary database and web service +> gui-tests/in-docker cleanup +... +Removed `local/gui-tests` successfully. +``` + +Running with `all` or without an argument will run all steps in sequence, +skipping the cleanup if it fails so you can inspect the failure. Useful if +you've done some other changes and want to run the gui-tests but aren't +expecting them to fail. + +If you are changing the web service or doc builder, take a look in the script at +the steps that `init` takes, you can likely run just one of these steps manually +within your edit-test loop rather than having to fully reinit the setup +(remember to use `--build` to ensure docker-compose rebuilds the image from your +updated source). + +```console +# e.g. after editing the doc builder +docker compose run --build --rm gui-tests-builder build crate sysinfo 0.23.4 +docker compose run --build --rm gui-tests-builder build crate sysinfo 0.23.5 + +# or after editing the web service +docker compose up --build --wait --wait-timeout 10 gui-tests-web +``` + +The web service is also bound onto `localhost:3001` so that you can manually +inspect the pages if necessary. diff --git a/gui-tests/in-docker b/gui-tests/in-docker new file mode 100755 index 000000000..5546bae45 --- /dev/null +++ b/gui-tests/in-docker @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +set -euo pipefail + +run() { + cmd="$1" + shift + printf ' \e[36;1mRunning\e[0m `%q' "$cmd" >&2 + printf ' %q' "$@" >&2 + printf '`\n' >&2 + "$cmd" "$@" +} + +cleanup() { + run docker compose rm --stop --volumes --force gui-tests-web + + run docker compose up --wait --wait-timeout 10 db s3 + + run docker compose exec db dropdb --user cratesfyi gui-tests || true + run docker compose exec s3 mc rb --force local/gui-tests || true +} + +init() { + run docker compose up --wait --wait-timeout 10 db s3 + + run docker compose exec db createdb --user cratesfyi gui-tests + run docker compose exec s3 mc mb local/gui-tests + + # Pre-build the images needed + run docker compose build gui-tests-cli gui-tests-builder gui-tests-web + + # Add the information we need + run docker compose run --rm gui-tests-cli database migrate + run docker compose run --rm gui-tests-builder build update-toolchain + run docker compose run --rm gui-tests-builder build crate sysinfo 0.23.4 + run docker compose run --rm gui-tests-builder build crate sysinfo 0.23.5 + + # Start the web server up + run docker compose up --wait --wait-timeout 10 gui-tests-web +} + +execute() { + run docker compose build gui-tests + run docker compose run --rm gui-tests +} + +case "${1:-all}" +in + cleanup) + cleanup + ;; + init) + cleanup + init + ;; + run) + execute + ;; + all) + cleanup + init + execute + cleanup + ;; + *) + echo 'unknown command `'"$1"'`, expected one of [init, run, cleanup, all]' >&2 + exit 1 + ;; +esac