Skip to content

Commit

Permalink
ARCH-95: add dedicated cypress test
Browse files Browse the repository at this point in the history
  • Loading branch information
finfa committed Oct 7, 2023
1 parent d3cc967 commit 1812b9b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 87 deletions.
8 changes: 8 additions & 0 deletions run-tests/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ inputs:
e2e:
description: 'E2e test commands'
required: false
cypress:
description: 'Cypress test commands'
required: false
e2e_port:
description: 'Port for e2e tests (required only if the image exposes multiple ports)'
required: false
Expand Down Expand Up @@ -89,6 +92,11 @@ runs:
if: inputs.e2e != ''
run: ${{ github.action_path }}/../scripts/docker-run-e2e.sh "${{ inputs.image_name }}" "${{ inputs.image_tag }}" "${{ inputs.docker_args }}" "${{ inputs.e2e }}" "${{ inputs.env_file }}" "${{ inputs.e2e_port }}" "${{ inputs.e2e_readiness_timeout }}" "${{ inputs.screenshots_path }}"
shell: sh
- name: Run Cypress tests
id: run-cypress
if: inputs.cypress != ''
run: ${{ github.action_path }}/../scripts/docker-run-cypress.sh "${{ inputs.image_name }}" "${{ inputs.image_tag }}" "${{ inputs.docker_args }}" "${{ inputs.e2e }}" "${{ inputs.env_file }}" "${{ inputs.e2e_port }}" "${{ inputs.e2e_readiness_timeout }}" "${{ inputs.screenshots_path }}"
shell: sh
- name: Upload screenshots
if: inputs.e2e != '' && inputs.screenshots_path != '' && failure() && steps.run-e2e.outcome == 'failure'
uses: actions/upload-artifact@v3
Expand Down
13 changes: 13 additions & 0 deletions scripts/docker-run-cypress.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

set -e

COMMAND="$4"
ENV_FILE="$5"
SERVICE_PORT="$6"

printenv
yarn install
yarn lint
yarn build --modern=client
yarn start-server-and-test 'yarn start' "http://localhost:$SERVICE_PORT" "$COMMAND"
82 changes: 0 additions & 82 deletions scripts/docker-run-e2e.back.sh

This file was deleted.

79 changes: 74 additions & 5 deletions scripts/docker-run-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,81 @@

set -e

IMAGE_NAME="$1"
IMAGE_TAG="$2"
DOCKER_ARGS="$3"
COMMAND="$4"
ENV_FILE="$5"
SERVICE_PORT="$6"
READINESS_TIMEOUT="$7"
SCREENSHOTS_PATH="$8"

HOST_PORT=3000
CONTAINER_NAME="${IMAGE_NAME}_${IMAGE_TAG}"

FAILED=0

if [ -z "$SERVICE_PORT" ]; then
CONTAINER_PORT=$(docker inspect --format='{{json .Config.ExposedPorts}}' "$IMAGE_NAME:$IMAGE_TAG" | jq 'keys[0]' | cut -d'/' -f1 | cut -d'"' -f2)
echo "Detected image port: $CONTAINER_PORT"
else
CONTAINER_PORT=$SERVICE_PORT
fi

if [ -z "$SCREENSHOTS_PATH" ]; then
MOUNTS_PART=""
else
SCREENSHOTS_PATH_LOCAL="${PWD}/test-artifacts/screenshots/${IMAGE_NAME}/"
mkdir -p "$SCREENSHOTS_PATH_LOCAL"
chmod 777 "$SCREENSHOTS_PATH_LOCAL"
MOUNTS_PART=" -v ${SCREENSHOTS_PATH_LOCAL}:${SCREENSHOTS_PATH}/"
echo "Set mount: $MOUNTS_PART"
fi

# Run the container
docker run --rm -d -p "$HOST_PORT:$CONTAINER_PORT" --cap-add=SYS_ADMIN --name "$CONTAINER_NAME" $MOUNTS_PART --env-file "$ENV_FILE" -e HOST="0.0.0.0" -e "TERM=xterm-color" $DOCKER_ARGS "$IMAGE_NAME:$IMAGE_TAG" || exit 1

# Wait for container to s
# start and run tests
start_time=$(date +%s)
elapsed=0
while [ $elapsed -lt $READINESS_TIMEOUT ]; do
if curl --connect-timeout 30 --head --silent --fail "http://localhost:$HOST_PORT/"; then
echo "Service started, running test..."
docker exec -u root "$CONTAINER_NAME" /bin/sh -c "mkdir -p /etc/sysctl.d/; echo 'kernel.unprivileged_userns_clone=1' > /etc/sysctl.d/userns.conf" || exit 1

# Print env vars
docker exec "$CONTAINER_NAME" /bin/sh -c "printenv" || exit 1

# Run tests
docker exec "$CONTAINER_NAME" /bin/sh -c "$COMMAND" || FAILED=1

break
else
echo "Waiting for service to start... ($elapsed)"
sleep 1
end_time=$(date +%s)
elapsed=$((elapsed + end_time - start_time))
fi
done
if [ $elapsed -gt $READINESS_TIMEOUT ]; then
echo "Timeout elapsed ($elapsed / max: $READINESS_TIMEOUT). Container still not operational."
echo "Response body:"
curl --connect-timeout 30 "http://localhost:$HOST_PORT/"
exit 1
fi

# Print container logs
echo "#########################################"
echo "########### SERVER LOGS #################"
echo "#########################################"
docker logs "$CONTAINER_NAME"
echo "#########################################"
echo "######### END OF SERVER LOGS ############"
echo "#########################################"

#echo "Listing artifacts on ${SCREENSHOTS_PATH_LOCAL}"
#ls -als "${SCREENSHOTS_PATH_LOCAL}" || echo "No artifacts."

exit $FAILED

printenv
yarn install
yarn lint
yarn build --modern=client
yarn start-server-and-test 'yarn start' "http://localhost:$SERVICE_PORT" "$COMMAND"

0 comments on commit 1812b9b

Please sign in to comment.