Skip to content

Commit

Permalink
tests/screentest: improve run.sh
Browse files Browse the repository at this point in the history
- Use single-hyphen flags to conform to Go style.

- Remove unnecessary quotation.

- Minor shell style nits.

- Change the "local" case to run all components, instead of assuming
  that some are already running.

The last change makes the "local" case much more useful, and faster
than the "ci" case for local testing.

Change-Id: Id3412fac8bd4a29539563e3cc568a3c7edd06c2e
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/626855
Reviewed-by: Robert Findley <[email protected]>
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
kokoro-CI: kokoro <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
jba committed Nov 13, 2024
1 parent de75df5 commit 71b0feb
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 39 deletions.
2 changes: 1 addition & 1 deletion deploy/screentest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ main() {
if [ -z $idtoken ]; then
idtoken=$(cat _ID_TOKEN)
fi
./tests/screentest/run.sh --idtoken $idtoken --concurrency 1 $env
./tests/screentest/run.sh -idtoken $idtoken -concurrency 1 $env
}

main $@
2 changes: 1 addition & 1 deletion devtools/ci/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ print_duration_and_reset
echo "----------------------------------------"
echo "Running screentest"
echo "----------------------------------------"
./tests/screentest/run.sh --rm ci --concurrency 1
./tests/screentest/run.sh -rm ci -concurrency 1
print_duration_and_reset

echo "----------------------------------------"
Expand Down
157 changes: 120 additions & 37 deletions tests/screentest/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright 2021 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

set -e

source devtools/lib.sh || { echo "Are you at repo root?"; exit 1; }
Expand All @@ -12,6 +13,10 @@ screentest_version=v0.0.0-20241108174919-3a761022ad6f
# This should match the version we are using in devtools/docker/compose.yaml.
chromedp_version=97.0.4692.71

chromedp_port=9222
frontend_port=8080
postgres_port=5432

usage() {
>&2 cat <<EOUSAGE
Usage: $0 [OPTIONS] [ci|local|exp|dev|staging|prod]
Expand All @@ -21,24 +26,36 @@ Usage: $0 [OPTIONS] [ci|local|exp|dev|staging|prod]
CI/kokoro and should always pass on master.
[local]
Run tests against a local server started with ./devtools/run_local.sh <env>
frontend.
Run ci tests without using 'docker compose'.
Docker is used to run Postgres and headless Chrome, but the frontend and screentest
binaries are run outside of docker. The database is created and seeded if necessary,
but not brought down, so subsequent runs can reuse its state. Headless Chrome is
also left running, because it takes a while to bring it down. Use 'docker container list'
followed by 'docker container stop' to stop these docker containers.
This is a good choice for testing locally before mailing a CL.
[exp|dev|staging|prod]
Run the tests against live instance of the given env. Use to verify that there
are no unexpected changes after a deploy is complete.
Options:
--concurrency <N>
-concurrency <N>
Set the number of testcases to run concurrently. Defaults to 1. Setting this too
high in lower memory environments may cause instability in tests.
--update
Recapture every snapshot during this test run.
-idtoken <TOKEN>
Provide an identity token to pass to servers that require one.
Generate a token with 'gcloud auth print-identity-token'.
--rm
-rm
Remove containers and volumes after tests are finished.
You can provide this with no command-line argument to remove resources from a previous
command that did not specify -rm.
-update
Recapture every snapshot during this test run.
EOUSAGE
exit 1
Expand All @@ -55,13 +72,19 @@ dcompose() {
$cmd "$@"
}

rm=false
env=

cleanup() {
dcompose stop
if [ "$rm" = true ]; then
dcompose down --volumes --remove-orphans
if [[ $env != local ]]; then
dcompose stop
if $rm; then
dcompose down --volumes --remove-orphans
fi
fi
if [ ! -z $chromedp ]; then
runcmd docker container stop $chromedp
if [ ! -z $frontend_pid ]; then
# The pid we captured is that of the 'go run' command; we want to kill its child.
runcmd pkill --parent $frontend_pid
fi
}

Expand All @@ -72,21 +95,18 @@ main() {
local update
while [[ $1 = -* ]]; do
case "$1" in
"--concurrency"|"-c")
-concurrency)
shift
concurrency="-c $1"
;;
"--idtoken")
-idtoken)
shift
idtoken=$1
;;
"--seeddb")
die "The seeddb flag is no longer supported. It is safe to remove it."
;;
"--update"|"-u")
-update)
update="-u"
;;
"--rm")
-rm)
rm=true
;;
*)
Expand All @@ -96,24 +116,27 @@ main() {
shift
done

local env=$1
local debugger_url="-d ws://localhost:9222"
# -rm by itself brings down previous containers (see cleanup).
if [[ $1 = '' && $rm ]]; then
exit 0
fi

env=$1
local debugger_url="-d ws://localhost:$chromedp_port"
local vars
case $env in
ci)
debugger_url="-d ws://chromedp:9222"
vars="-v Origin:http://frontend:8080"
;;
local)
vars="-v Origin:http://localhost:8080"
debugger_url="-d ws://chromedp:$chromedp_port"
vars="-v Origin:http://frontend:$frontend_port"
;;
exp|dev|staging)
debugger_url="-d ws://chromedp:9222"
debugger_url="-d ws://chromedp:$chromedp_port"
vars="-v Origin:https://$env-pkg.go.dev,QuotaBypass:$GO_DISCOVERY_E2E_QUOTA_BYPASS,Token:$idtoken"
;;
prod)
vars="-v Origin:https://pkg.go.dev,QuotaBypass:$bypass"
;;
local) ;;
*)
usage
;;
Expand All @@ -134,18 +157,10 @@ main() {
dcompose up --detach --force-recreate frontend
dcompose run --rm --entrypoint bash go -c "
go install golang.org/x/website/cmd/screentest@$screentest_version
go run ./devtools/cmd/wait_available --timeout 120s frontend:8080 -- \
go run ./devtools/cmd/wait_available --timeout 120s frontend:$frontend_port -- \
$(echo $cmd)"
elif [ "$env" = local ]; then
if ! nc -z localhost 9222; then
chromedp=$(runcmd docker run --detach --rm --network host --shm-size 8G \
--name headless-shell chromedp/headless-shell:$chromedp_version)
timeout 3s bash -c -- 'while ! nc -z localhost 9222; do sleep 1; done'
fi
if ! command -v screentest &> /dev/null; then
runcmd go install golang.org/x/website/cmd/screentest@$screentest_version
fi
runcmd $cmd
elif [[ "$env" == local ]]; then
run_locally $concurrency $update
else
dcompose up --detach chromedp
dcompose run --rm --entrypoint bash go -c "
Expand All @@ -154,4 +169,72 @@ main() {
fi
}


# run_locally: run outside of the docker compose network, but use
# docker for each component.
run_locally() {
local concurrency=$1
local update=$2

if ! command -v screentest &> /dev/null; then
runcmd go install golang.org/x/website/cmd/screentest@screentest_version
fi

export GO_DISCOVERY_DATABASE_NAME=discovery-db
export GO_DISCOVERY_DATABASE_HOST=localhost
export GO_DISCOVERY_DATABASE_PORT=$postgres_port
export GO_DISCOVERY_DATABASE_USER=postgres
export GO_DISCOVERY_DATABASE_PASSWORD=postgres
export GO_DISCOVERY_LOG_LEVEL=warning
export GO_DISCOVERY_VULN_DB=file:///$PWD/tests/screentest/testdata/vulndb-v1

if ! listening $postgres_port; then
info setting up postgres DB
runcmd docker run --detach \
-e POSTGRES_DB=$GO_DISCOVERY_DATABASE_NAME \
-e POSTGRES_USER=$GO_DISCOVERY_DATABASE_USER \
-e POSTGRES_PASSWORD=$GO_DISCOVERY_DATABASE_PASSWORD \
-e LANG=C \
-p $postgres_port:$postgres_port \
--rm \
postgres:11.12
wait_for $postgres_port
# Postgres can take some time to start up even after it is listening to the port.
runcmd sleep 4
runcmd go run ./devtools/cmd/db create
runcmd go run ./devtools/cmd/db migrate
fi

info seeding DB
go run ./devtools/cmd/seeddb -seed tests/screentest/seed.txt

if ! listening $frontend_port; then
info starting frontend

go run ./cmd/frontend -host localhost:$frontend_port &
wait_for $frontend_port
frontend_pid=$!
fi
if ! listening $chromedp_port; then
info starting chromedp
runcmd docker run --detach --rm --network host --shm-size 8G \
--name headless-shell chromedp/headless-shell:$chromedp_version
wait_for $chromedp_port
fi

info "running screentest"
screentest $concurrency $update \
-v Origin:http://localhost:$frontend_port \
-d ws://localhost:$chromedp_port \
'tests/screentest/testcases.*'
}

listening() {
nc -z localhost $1
}

wait_for() {
timeout 5s bash -c -- "while ! nc -z localhost $1; do sleep 1; done"
}

main $@

0 comments on commit 71b0feb

Please sign in to comment.