Skip to content

Commit e525946

Browse files
authored
WIP: Update dockerisation (#3477)
* 🐳 add Dockerfile and dockerfile based script for consensus-spec tests
1 parent 877817c commit e525946

File tree

7 files changed

+165
-2
lines changed

7 files changed

+165
-2
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**/venv
2+
**/.venv

.github/workflows/run-tests.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,37 @@ jobs:
6666
- name: Run linter for test generators
6767
run: make lint_generators
6868

69+
dockerfile-test:
70+
runs-on: self-hosted
71+
needs: preclear
72+
services:
73+
registry:
74+
image: registry:2
75+
ports:
76+
- 5000:5000
77+
steps:
78+
- name: Checkout this repo
79+
uses: actions/[email protected]
80+
- name: get git commit hash
81+
id: git_commit_hash
82+
shell: bash
83+
run: |
84+
echo "git_commit_hash=$(echo $(git log --pretty=format:'%h' -n 1))" >> $GITHUB_OUTPUT
85+
- name: Set up Docker Buildx
86+
uses: docker/setup-buildx-action@v2
87+
with:
88+
driver-opts: network=host
89+
- name: Build and push to local registry
90+
uses: docker/build-push-action@v4
91+
with:
92+
context: .
93+
file: ./docker/Dockerfile
94+
push: true
95+
tags: localhost:5000/consensus-specs-dockerfile-test:${{ steps.git_commit_hash.outputs.git_commit_hash }}
96+
- name: Test the image by running the linter
97+
run: |
98+
docker run localhost:5000/consensus-specs-dockerfile-test:${{ steps.git_commit_hash.outputs.git_commit_hash }} make lint
99+
69100
pyspec-tests:
70101
runs-on: self-hosted
71102
needs: [preclear,lint,codespell,table_of_contents]

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ docs/ssz
4646
docs/fork_choice
4747
docs/README.md
4848
site
49+
50+
# docker test results
51+
testResults

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SOLIDITY_FILE_NAME = deposit_contract.json
1414
DEPOSIT_CONTRACT_TESTER_DIR = ${SOLIDITY_DEPOSIT_CONTRACT_DIR}/web3_tester
1515
CONFIGS_DIR = ./configs
1616
TEST_PRESET_TYPE ?= minimal
17+
NUMBER_OF_CORES=16
1718
# Collect a list of generator names
1819
GENERATORS = $(sort $(dir $(wildcard $(GENERATOR_DIR)/*/.)))
1920
# Map this list of generator paths to "gen_{generator name}" entries
@@ -128,10 +129,10 @@ citest: pyspec
128129
mkdir -p $(TEST_REPORT_DIR);
129130
ifdef fork
130131
. venv/bin/activate; cd $(PY_SPEC_DIR); \
131-
python3 -m pytest -n 16 --bls-type=fastest --preset=$(TEST_PRESET_TYPE) --fork=$(fork) --junitxml=test-reports/test_results.xml eth2spec
132+
python3 -m pytest -n $(NUMBER_OF_CORES) --bls-type=fastest --preset=$(TEST_PRESET_TYPE) --fork=$(fork) --junitxml=test-reports/test_results.xml eth2spec
132133
else
133134
. venv/bin/activate; cd $(PY_SPEC_DIR); \
134-
python3 -m pytest -n 16 --bls-type=fastest --preset=$(TEST_PRESET_TYPE) --junitxml=test-reports/test_results.xml eth2spec
135+
python3 -m pytest -n $(NUMBER_OF_CORES) --bls-type=fastest --preset=$(TEST_PRESET_TYPE) --junitxml=test-reports/test_results.xml eth2spec
135136
endif
136137

137138

docker/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Rename the build stage from 'base' to 'builder' for clarification and code readability
2+
FROM python:3.11.0-slim-bullseye as builder
3+
4+
ENV DEBIAN_FRONTEND=noninteractive \
5+
WORKDIR=/consensus-specs \
6+
PIP_UPGRADE_CMD="python -m pip install --upgrade pip" \
7+
INSTALL_CMD="apt install -y git build-essential"
8+
9+
RUN mkdir ${WORKDIR}
10+
WORKDIR ${WORKDIR}
11+
12+
# Chain the commands together
13+
RUN apt update && ${INSTALL_CMD} && ${PIP_UPGRADE_CMD} && rm -rf /var/lib/apt/lists/*
14+
15+
# Copy the current directory contents into the builder
16+
COPY . .
17+
18+
# Inline installation commands
19+
RUN make install_test && \
20+
make preinstallation && \
21+
make pyspec
22+

requirements_preinstallation.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pip>=23.1.2
22
wheel>=0.40.0
33
setuptools>=68.0.0
4+
pylint>=3.0.0

scripts/build_run_docker_tests.sh

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)