Skip to content

Commit

Permalink
test(smoketest): open services in browser tabs when ready (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores authored Nov 7, 2023
1 parent 3d30433 commit 7910714
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 18 deletions.
86 changes: 75 additions & 11 deletions smoketest.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,32 @@ FILES=(
./smoketest/compose/db.yml
)

USE_USERHOSTS=${USE_USERHOSTS:-true}
PULL_IMAGES=${PULL_IMAGES:-true}
KEEP_VOLUMES=${KEEP_VOLUMES:-false}
OPEN_TABS=${OPEN_TABS:-false}

display_usage() {
echo "Usage:"
echo -e "\t-O \t\t\t\tOffline mode, do not attempt to pull container images."
echo -e "\t-h\t\t\t\tprint this Help text."
echo -e "\t-O\t\t\t\tOffline mode, do not attempt to pull container images."
echo -e "\t-s [minio|localstack]\t\tS3 implementation to spin up (default \"minio\")."
echo -e "\t-g \t\t\t\tinclude Grafana dashboard and jfr-datasource in deployment."
echo -e "\t-t \t\t\t\tinclude sample applications for Testing."
echo -e "\t-V \t\t\t\tdo not discard data storage Volumes on exit."
echo -e "\t-X \t\t\t\tdeploy additional development aid tools."
echo -e "\t-g\t\t\t\tinclude Grafana dashboard and jfr-datasource in deployment."
echo -e "\t-t\t\t\t\tinclude sample applications for Testing."
echo -e "\t-V\t\t\t\tdo not discard data storage Volumes on exit."
echo -e "\t-X\t\t\t\tdeploy additional development aid tools."
echo -e "\t-c [podman|docker]\t\tUse Podman or Docker Container Engine (default \"podman\")."
echo -e "\t-b\t\t\t\tOpen a Browser tab for each running service's first mapped port (ex. Cryostat web client, Minio console)"
}

s3=minio
ce=podman
while getopts "s:gtOVXc" opt; do
while getopts "hs:gtOVXcb" opt; do
case $opt in
h)
display_usage
exit 0
;;
s)
s3="${OPTARG}"
;;
Expand All @@ -48,6 +56,9 @@ while getopts "s:gtOVXc" opt; do
c)
ce="${OPTARG}"
;;
b)
OPEN_TABS=true
;;
*)
display_usage
exit 1
Expand Down Expand Up @@ -82,26 +93,32 @@ for file in "${FILES[@]}"; do
CMD+=(-f "${file}")
done

PIDS=()

HOSTSFILE="${HOSTSFILE:-$HOME/.hosts}"

cleanup() {
DOWN_FLAGS=('--remove-orphans')
set +xe
local downFlags=('--remove-orphans')
if [ "${KEEP_VOLUMES}" != "true" ]; then
DOWN_FLAGS+=('--volumes')
downFlags=('--volumes')
fi
docker-compose \
"${CMD[@]}" \
down "${DOWN_FLAGS[@]}"
down "${downFlags[@]}"
# podman kill hoster || true
truncate -s 0 "${HOSTSFILE}"
for i in "${PIDS[@]}"; do
kill -0 "${i}" && kill "${i}"
done
set -xe
}
trap cleanup EXIT
cleanup

setupUserHosts() {
# FIXME this is broken: it puts the containers' bridge-internal IP addresses
# into the user hosts file, but these IPs are in a subnet not reachable from the host.
# This requires https://github.com/figiel/hosts to work. See README.
# podman run \
# --detach \
# --rm \
Expand All @@ -111,13 +128,60 @@ setupUserHosts() {
# -v "${XDG_RUNTIME_DIR}/podman/podman.sock:/tmp/docker.sock:Z" \
# -v "${HOME}/.hosts:/tmp/hosts" \
# dvdarias/docker-hoster
#
# This requires https://github.com/figiel/hosts to work. See README.
truncate -s 0 "${HOSTSFILE}"
for file in "${FILES[@]}" ; do
local hosts
hosts="$(yq '.services.*.hostname' "${file}" | grep -v null | sed -e 's/^/localhost /')"
echo "${hosts}" >> "${HOSTSFILE}"
done
}
setupUserHosts
if [ "${USE_USERHOSTS}" = "true" ]; then
setupUserHosts
fi

openBrowserTabs() {
# TODO find a way to use 'podman wait --condition=healthy $containerId' instead of polling with curl
set +xe
local urls=()
for file in "${FILES[@]}"; do
local yaml
yaml="$(yq '.services.* | [{"host": .hostname, "ports": .ports}]' "${file}")"
local length
length="$(echo "${yaml}" | yq 'length')"
for (( i=0; i<"${length}"; i+=1 ))
do
local host
local port
if [ "${USE_USERHOSTS}" = "true" ]; then
host="$(echo "${yaml}" | yq ".[${i}].host" | grep -v null)"
else
host="localhost"
fi
port="$(echo "${yaml}" | yq ".[${i}].ports[0]" | grep -v null | cut -d: -f1)"
if [ -n "${host}" ] && [ -n "${port}" ]; then
urls+=("http://${host}:${port}")
fi
done
done
set -xe
echo "Service URLs:" "${urls[@]}"
for url in "${urls[@]}"; do
(
until timeout 1s curl -s -f -o /dev/null "${url}"
do
sleep 5
done
xdg-open "${url}"
echo "Opened ${url} in default browser."
) &
PIDS+=($!)
done
}
if [ "${OPEN_TABS}" = "true" ]; then
openBrowserTabs
fi

if [ "${PULL_IMAGES}" = "true" ]; then
IMAGES=()
Expand Down
2 changes: 2 additions & 0 deletions smoketest/compose/db-viewer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
hostname: db-viewer
ports:
- "8989:8989"
expose:
- "8989"
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: admin
Expand Down
2 changes: 0 additions & 2 deletions smoketest/compose/jfr-datasource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ services:
limits:
cpus: '0.4'
memory: 512m
ports:
- "8080:8080"
expose:
- "8080"
labels:
Expand Down
6 changes: 3 additions & 3 deletions smoketest/compose/s3-localstack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ services:
s3:
image: docker.io/localstack/localstack:1.4.0
hostname: s3
ports:
- "4566:4566"
- "4577:4577"
expose:
- "4566"
- "4577"
environment:
SERVICES: s3
START_WEB: 1
Expand Down
4 changes: 3 additions & 1 deletion smoketest/compose/s3-minio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ services:
hostname: s3
ports:
- "9001:9001"
- "9000:9000"
expose:
- "9000"
- "9001"
command: server /data --console-address ":9001"
environment:
MINIO_ROOT_USER: minioroot
Expand Down
3 changes: 2 additions & 1 deletion smoketest/compose/sample-apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ services:
# do not add a depends_on:cryostat here, so that we can test that the agent is tolerant of that state
hostname: quarkus-test-agent
ports:
- "9977:9977"
- "10010:10010"
expose:
- "9977"
environment:
JAVA_OPTS: "-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager -javaagent:/deployments/app/cryostat-agent.jar"
QUARKUS_HTTP_PORT: 10010
Expand Down

0 comments on commit 7910714

Please sign in to comment.