tests/screentest: improve run.sh

- 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 <rfindley@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Jonathan Amsterdam 2024-11-09 11:29:13 -05:00
Родитель de75df57e5
Коммит 71b0feb8eb
3 изменённых файлов: 122 добавлений и 39 удалений

Просмотреть файл

@ -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 $@

Просмотреть файл

@ -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 "----------------------------------------"

Просмотреть файл

@ -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; }
@ -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]
@ -21,8 +26,13 @@ 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
@ -30,15 +40,22 @@ Usage: $0 [OPTIONS] [ci|local|exp|dev|staging|prod]
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
-idtoken <TOKEN>
Provide an identity token to pass to servers that require one.
Generate a token with 'gcloud auth print-identity-token'.
-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.
--rm
Remove containers and volumes after tests are finished.
EOUSAGE
exit 1
@ -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
}
@ -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
;;
*)
@ -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
;;
@ -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 "
@ -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 $@