From 1812b9bb3119ffdf02dd3de7821a65868b4d71dd Mon Sep 17 00:00:00 2001 From: Federico Infanti Date: Sat, 7 Oct 2023 21:13:47 +0200 Subject: [PATCH] ARCH-95: add dedicated cypress test --- run-tests/action.yaml | 8 ++++ scripts/docker-run-cypress.sh | 13 ++++++ scripts/docker-run-e2e.back.sh | 82 ---------------------------------- scripts/docker-run-e2e.sh | 79 +++++++++++++++++++++++++++++--- 4 files changed, 95 insertions(+), 87 deletions(-) create mode 100755 scripts/docker-run-cypress.sh delete mode 100755 scripts/docker-run-e2e.back.sh diff --git a/run-tests/action.yaml b/run-tests/action.yaml index 4037ef9..9b518de 100644 --- a/run-tests/action.yaml +++ b/run-tests/action.yaml @@ -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 @@ -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 diff --git a/scripts/docker-run-cypress.sh b/scripts/docker-run-cypress.sh new file mode 100755 index 0000000..a06fe98 --- /dev/null +++ b/scripts/docker-run-cypress.sh @@ -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" diff --git a/scripts/docker-run-e2e.back.sh b/scripts/docker-run-e2e.back.sh deleted file mode 100755 index e927db4..0000000 --- a/scripts/docker-run-e2e.back.sh +++ /dev/null @@ -1,82 +0,0 @@ -#!/bin/sh - -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 - diff --git a/scripts/docker-run-e2e.sh b/scripts/docker-run-e2e.sh index a06fe98..e927db4 100755 --- a/scripts/docker-run-e2e.sh +++ b/scripts/docker-run-e2e.sh @@ -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"