|
| 1 | +#! /bin/sh |
| 2 | + |
| 3 | +# Run 'consensus-specs' tests from a docker container instance. |
| 4 | +# *Be sure to launch Docker before running this script.* |
| 5 | +# |
| 6 | +# It does the below: |
| 7 | +# 1. Run pytest for consensus-specs in a container. |
| 8 | +# 2. Copy and paste the coverage report. |
| 9 | +# 3. Remove all exited containers that use the consensus-specs:<TAG> images. |
| 10 | + |
| 11 | + |
| 12 | +# Set variables |
| 13 | +ALL_EXECUTABLE_SPECS=("phase0" "altair" "bellatrix" "capella" "deneb" "eip6110" "whisk") |
| 14 | +TEST_PRESET_TYPE=minimal |
| 15 | +FORK_TO_TEST=phase0 |
| 16 | +NUMBER_OF_CORES=4 |
| 17 | +WORKDIR="//consensus-specs//tests//core//pyspec" |
| 18 | +ETH2SPEC_FOLDER_NAME="eth2spec" |
| 19 | +CONTAINER_NAME="consensus-specs-tests" |
| 20 | +DATE=$(date +"%Y%m%d-%H-%M") |
| 21 | +# Default flag values |
| 22 | +version=$(git log --pretty=format:'%h' -n 1) |
| 23 | +IMAGE_NAME="consensus-specs:$version" |
| 24 | +number_of_core=4 |
| 25 | + |
| 26 | +# displays the available options |
| 27 | +display_help() { |
| 28 | + echo "Run 'consensus-specs' tests from a container instance." |
| 29 | + echo "Be sure to launch Docker before running this script." |
| 30 | + echo |
| 31 | + echo "Syntax: build_run_test.sh [--v TAG | --n NUMBER_OF_CORE | --f FORK_TO_TEST | --p PRESET_TYPE | --a | --h HELP]" |
| 32 | + echo " --f <fork> Specify the fork to test" |
| 33 | + echo " --i <image_name> Specify the docker image to use" |
| 34 | + echo " --n <number> Specify the number of cores" |
| 35 | + echo " --p <type> Specify the test preset type" |
| 36 | + echo " --a Test all forks" |
| 37 | + echo " --h Display this help and exit" |
| 38 | +} |
| 39 | + |
| 40 | +# Stop and remove the 'consensus-specs-dockerfile-test' container. |
| 41 | +# If this container doesn't exist, then a error message is printed |
| 42 | +# (but the process is not stopped). |
| 43 | +cleanup() { |
| 44 | + echo "Stop and remove the 'consensus-specs-tests' container." |
| 45 | + docker stop $CONTAINER_NAME || true && docker rm $CONTAINER_NAME || true |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +# Copy the results from the container to a local folder |
| 50 | +copy_test_results() { |
| 51 | + local fork_name="$1" # Storing the first argument in a variable |
| 52 | + |
| 53 | + docker cp $CONTAINER_NAME:$WORKDIR/test-reports/test_results.xml ./testResults/test-results-$fork_name-$DATE.xml |
| 54 | +} |
| 55 | + |
| 56 | +# Function to check if the Docker image already exists |
| 57 | +image_exists() { |
| 58 | + docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "$1" |
| 59 | +} |
| 60 | + |
| 61 | +# Parse command line arguments |
| 62 | +while [[ "$#" -gt 0 ]]; do |
| 63 | + case $1 in |
| 64 | + --f) FORK_TO_TEST="$2"; shift ;; |
| 65 | + --v) IMAGE_NAME="$2"; shift ;; |
| 66 | + --n) NUMBER_OF_CORES="$2"; shift ;; |
| 67 | + --p) TEST_PRESET_TYPE="$2"; shift ;; |
| 68 | + --a) FORK_TO_TEST="all" ;; |
| 69 | + --h) display_help; exit 0 ;; |
| 70 | + *) echo "Unknown parameter: $1"; display_help; exit 1 ;; |
| 71 | + esac |
| 72 | + shift |
| 73 | +done |
| 74 | + |
| 75 | +# initialize a test result directory |
| 76 | +mkdir -p ./testResults |
| 77 | + |
| 78 | +# Only clean container after user exit console |
| 79 | +trap cleanup SIGINT |
| 80 | + |
| 81 | +# Build Docker container if it doesn't exist |
| 82 | +if ! image_exists "$IMAGE_NAME"; then |
| 83 | + echo "Image $IMAGE_NAME does not exist. Building Docker image..." |
| 84 | + docker build ../ -t $IMAGE_NAME -f ../docker/Dockerfile |
| 85 | +else |
| 86 | + echo "Image $IMAGE_NAME already exists. Skipping build..." |
| 87 | +fi |
| 88 | + |
| 89 | +# Equivalent to `make citest with the subsequent flags` |
| 90 | +if [ "$FORK_TO_TEST" == "all" ]; then |
| 91 | + for fork in "${ALL_EXECUTABLE_SPECS[@]}"; do |
| 92 | + docker run --name $CONTAINER_NAME $IMAGE_NAME \ |
| 93 | + make citest fork=$fork TEST_PRESET_TYPE=$TEST_PRESET_TYPE NUMBER_OF_CORES=$NUMBER_OF_CORES |
| 94 | + copy_test_results $fork |
| 95 | + done |
| 96 | +else |
| 97 | + docker run --name $CONTAINER_NAME $IMAGE_NAME \ |
| 98 | + make citest fork=$FORK_TO_TEST TEST_PRESET_TYPE=$TEST_PRESET_TYPE NUMBER_OF_CORES=$NUMBER_OF_CORES |
| 99 | + copy_test_results $FORK_TO_TEST |
| 100 | +fi |
| 101 | + |
| 102 | +# Stop and remove the container |
| 103 | +cleanup |
0 commit comments