From fb1f4a725feb1f78c1c219c7911c32488e146e6a Mon Sep 17 00:00:00 2001 From: Marek Wydmuch Date: Thu, 7 Sep 2023 02:24:16 +0200 Subject: [PATCH] Add tests for wheels built using cibuildwheel --- .gitignore | 3 + scripts/install_and_test_wheel.sh | 29 ++++++++++ tests/README.md | 5 ++ tests/build_test_cibuildwheel_linux.sh | 55 ++++++++++++++++++ ...ly.sh => build_test_local_linux_builds.sh} | 57 +++++++++---------- .../apt-based.Dockerfile | 0 .../conda-based.Dockerfile | 0 .../dnf-based.Dockerfile | 0 .../apt-based.Dockerfile | 12 ++++ .../conda-based.Dockerfile | 6 ++ .../dnf-based.Dockerfile | 9 +++ 11 files changed, 147 insertions(+), 29 deletions(-) create mode 100644 scripts/install_and_test_wheel.sh create mode 100644 tests/README.md create mode 100644 tests/build_test_cibuildwheel_linux.sh rename tests/{dockerfiles/run_all_localy.sh => build_test_local_linux_builds.sh} (65%) rename tests/{dockerfiles => local_builds_dockerfiles}/apt-based.Dockerfile (100%) rename tests/{dockerfiles => local_builds_dockerfiles}/conda-based.Dockerfile (100%) rename tests/{dockerfiles => local_builds_dockerfiles}/dnf-based.Dockerfile (100%) create mode 100644 tests/wheels_test_dockerfiles/apt-based.Dockerfile create mode 100644 tests/wheels_test_dockerfiles/conda-based.Dockerfile create mode 100644 tests/wheels_test_dockerfiles/dnf-based.Dockerfile diff --git a/.gitignore b/.gitignore index 908fcad78..d9b17428b 100644 --- a/.gitignore +++ b/.gitignore @@ -52,6 +52,9 @@ venv vizdoom.egg-info wheelhouse +# Tests +test_dockerfiles + # Copied from the original ZDoom repository (and modified) *.cbp *.ncb diff --git a/scripts/install_and_test_wheel.sh b/scripts/install_and_test_wheel.sh new file mode 100644 index 000000000..ff64ba6d4 --- /dev/null +++ b/scripts/install_and_test_wheel.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -e + +# Set working dir to the root of the repo +cd $( dirname "${BASH_SOURCE[0]}" )/.. + +# Report directory +ls -lha . + +# Report python version +python3 --version +python3 -c "import sys; print('Python', sys.version)" + +# Find matching wheel file in wheelhouse +PYTHON_VERSION=$(python3 -c "import sys; print('{}{}'.format(sys.version_info.major, sys.version_info.minor))") +PYTHON_WHEEL=$(ls wheelhouse/vizdoom-*-cp${PYTHON_VERSION}-cp${PYTHON_VERSION}*.whl) + +# Updgrade pip and install test deps +python3 -m pip install --upgrade pip +python3 -m pip install pytest psutil + +# Install wheel +python3 -m pip install ${PYTHON_WHEEL} + +# Test import +python3 -c "import vizdoom" + +# Run tests +pytest tests diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 000000000..f4fc33b42 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,5 @@ +# Tests + +This directory contains the tests for the project that can be run with pytest or by running the `test_*.py` or `manual_test_*.py` files directly. +Manual tests require significant amount of time, so they are not run by default by CI/CD. +The `build_test_*.sh` scripts test the build process of the project under different distrubutions and enviroments. To run them docker and cibuildwheels is required. diff --git a/tests/build_test_cibuildwheel_linux.sh b/tests/build_test_cibuildwheel_linux.sh new file mode 100644 index 000000000..ffae5e36f --- /dev/null +++ b/tests/build_test_cibuildwheel_linux.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +set -e + +NC='\033[0m' +RED='\033[0;31m' +GREEN='\033[0;32m' + +DOCKERFILES_DIR=$( dirname ${BASH_SOURCE[0]} )/wheels_test_dockerfiles +GENERATED_DOCKERFILES_DIR=tests/test_dockerfiles +IMAGE_PREFIX="vizdoom_wheels" + +# Generate and run dockerfiles +# Array in format " " +DOCKERFILES_TO_BUILD_AND_RUN=( + "almalinux:9 dnf-based.Dockerfile" # Python 3.9 + "fedora:36 dnf-based.Dockerfile" # Python 3.10 + "fedora:37 dnf-based.Dockerfile" # Python 3.11 + "rockylinux:9 dnf-based.Dockerfile" # Python 3.9 + "debian:11.6 apt-based.Dockerfile ENV LANG C.UTF-8" # Python 3.9 + "ubuntu:20.04 apt-based.Dockerfile" # Python 3.8 + "ubuntu:22.04 apt-based.Dockerfile" # Python 3.10 + "continuumio/miniconda3:latest conda-based.Dockerfile" # Python 3.10 +) + +function create_dockerfile ( ) { + local all_args=("$@") + local base_image=$1 + local base_name=$( basename "$( echo ${base_image} | tr ':' '_' )" ) + local base_dockerfile=$2 + local add_commands=("${all_args[@]:2}") + + mkdir -p $GENERATED_DOCKERFILES_DIR + dockerfile=${GENERATED_DOCKERFILES_DIR}/${IMAGE_PREFIX}_${base_name}.Dockerfile + + echo "FROM $base_image" > $dockerfile + echo "" >> $dockerfile + echo -e "${add_commands[@]}" >> $dockerfile + cat ${DOCKERFILES_DIR}/$base_dockerfile | tail -n +2 >> $dockerfile +} + +for dockerfile_setting in "${DOCKERFILES_TO_BUILD_AND_RUN[@]}"; do + create_dockerfile $dockerfile_setting + + echo -n "Building and running $dockerfile, saving output to $dockerfile.log ... " + filename=$( basename "$dockerfile" ) + dockerfile_dir=$( dirname "$dockerfile" ) + without_ext="${filename%.*}" + tag="${without_ext}:latest" + log="${dockerfile_dir}/${without_ext}.log" + + docker build -t $tag -f $dockerfile . &> $log || ( echo -e "${RED}FAILED${NC}"; exit 1 ) + docker run -it $tag &>> $log || ( echo -e "${RED}FAILED${NC}"; exit 1 ) + + echo -e "${GREEN}OK${NC}" +done diff --git a/tests/dockerfiles/run_all_localy.sh b/tests/build_test_local_linux_builds.sh similarity index 65% rename from tests/dockerfiles/run_all_localy.sh rename to tests/build_test_local_linux_builds.sh index d2edcea85..23e0fe15e 100755 --- a/tests/dockerfiles/run_all_localy.sh +++ b/tests/build_test_local_linux_builds.sh @@ -5,9 +5,31 @@ NC='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' -DOCKERFILES_DIR=$( dirname "${BASH_SOURCE[0]}" ) -GENERATED_DOCKERFILES_DIR=tmp_dockerfiles +DOCKERFILES_DIR=$( dirname ${BASH_SOURCE[0]} )/local_builds_dockerfiles +GENERATED_DOCKERFILES_DIR=tests/test_dockerfiles +IMAGE_PREFIX="vizdoom_local" +# Array in format " " +DOCKERFILES_TO_BUILD_AND_RUN=( + "almalinux:9 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled crb" + "tgagor/centos:stream9 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled crb" + "fedora:36 dnf-based.Dockerfile" + "fedora:37 dnf-based.Dockerfile" + "rockylinux:9 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled crb" + "debian:11.6 apt-based.Dockerfile ENV LANG C.UTF-8" + "ubuntu:18.04 apt-based.Dockerfile" + "ubuntu:20.04 apt-based.Dockerfile" + "ubuntu:22.04 apt-based.Dockerfile" + "ubuntu:latest apt-based.Dockerfile" + #"continuumio/miniconda3:latest conda-based.Dockerfile" # Does not work at the moment +) + + +# Build wheels using cibuildwheel +cibuildwheel --platform linux --arch $(uname -m) + + +# Test wheels inside docker containers function create_dockerfile ( ) { local all_args=("$@") local base_image=$1 @@ -16,7 +38,7 @@ function create_dockerfile ( ) { local add_commands=("${all_args[@]:2}") mkdir -p $GENERATED_DOCKERFILES_DIR - dockerfile=${GENERATED_DOCKERFILES_DIR}/${base_name}.Dockerfile + dockerfile=${GENERATED_DOCKERFILES_DIR}/${IMAGE_PREFIX}_${base_name}.Dockerfile echo "FROM $base_image" > $dockerfile echo "" >> $dockerfile @@ -24,41 +46,18 @@ function create_dockerfile ( ) { cat ${DOCKERFILES_DIR}/$base_dockerfile | tail -n +2 >> $dockerfile } -# Generate and run dockerfiles -# Array in florma " " -DOCKERFILES_TO_BUILD_AND_RUN=( - #"almalinux:8 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled powertools" - "almalinux:9 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled crb" - #"tgagor/centos:stream8 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled powertools" - "tgagor/centos:stream9 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled crb" - #"fedora:34 dnf-based.Dockerfile" # EOL - #"fedora:35 dnf-based.Dockerfile" # EOL - "fedora:36 dnf-based.Dockerfile" - "fedora:37 dnf-based.Dockerfile" - #"rockylinux:8 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled devel" - "rockylinux:9 dnf-based.Dockerfile RUN dnf install -y 'dnf-command(config-manager)' && dnf config-manager --set-enabled crb" - #"debian:10.13 apt-based.Dockerfile ENV LANG C.UTF-8" # EOL - "debian:11.6 apt-based.Dockerfile ENV LANG C.UTF-8" - #"ubuntu:18.04 apt-based.Dockerfile" # EOL - "ubuntu:20.04 apt-based.Dockerfile" - "ubuntu:22.04 apt-based.Dockerfile" - #"continuumio/miniconda3:latest conda-based.Dockerfile" # Does not work at the moment -) - for dockerfile_setting in "${DOCKERFILES_TO_BUILD_AND_RUN[@]}"; do create_dockerfile $dockerfile_setting echo -n "Building and running $dockerfile, saving output to $dockerfile.log ... " filename=$( basename "$dockerfile" ) + dockerfile_dir=$( dirname "$dockerfile" ) without_ext="${filename%.*}" - tag="vizdoom_${without_ext}:latest" - log="vizdoom_${without_ext}.log" + tag="${without_ext}:latest" + log="${dockerfile_dir}/${without_ext}.log" docker build -t $tag -f $dockerfile . &> $log || ( echo -e "${RED}FAILED${NC}"; exit 1 ) docker run -it $tag &>> $log || ( echo -e "${RED}FAILED${NC}"; exit 1 ) echo -e "${GREEN}OK${NC}" - done - -rm -rf $GENERATED_DOCKERFILES_DIR diff --git a/tests/dockerfiles/apt-based.Dockerfile b/tests/local_builds_dockerfiles/apt-based.Dockerfile similarity index 100% rename from tests/dockerfiles/apt-based.Dockerfile rename to tests/local_builds_dockerfiles/apt-based.Dockerfile diff --git a/tests/dockerfiles/conda-based.Dockerfile b/tests/local_builds_dockerfiles/conda-based.Dockerfile similarity index 100% rename from tests/dockerfiles/conda-based.Dockerfile rename to tests/local_builds_dockerfiles/conda-based.Dockerfile diff --git a/tests/dockerfiles/dnf-based.Dockerfile b/tests/local_builds_dockerfiles/dnf-based.Dockerfile similarity index 100% rename from tests/dockerfiles/dnf-based.Dockerfile rename to tests/local_builds_dockerfiles/dnf-based.Dockerfile diff --git a/tests/wheels_test_dockerfiles/apt-based.Dockerfile b/tests/wheels_test_dockerfiles/apt-based.Dockerfile new file mode 100644 index 000000000..e5f2063df --- /dev/null +++ b/tests/wheels_test_dockerfiles/apt-based.Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:latest + +ARG DEBIAN_FRONTEND=noninteractive +ENV TZ=Europe/Warsaw + +WORKDIR vizdoom + +# MINIMAL +RUN apt update && apt install -y python3-dev python3-pip + +COPY . ./ +CMD ["bash", "./scripts/install_and_test_wheel.sh"] diff --git a/tests/wheels_test_dockerfiles/conda-based.Dockerfile b/tests/wheels_test_dockerfiles/conda-based.Dockerfile new file mode 100644 index 000000000..9b4cdff3c --- /dev/null +++ b/tests/wheels_test_dockerfiles/conda-based.Dockerfile @@ -0,0 +1,6 @@ +FROM continuumio/miniconda3:latest + +WORKDIR vizdoom + +COPY . ./ +CMD ["bash", "./scripts/install_and_test_wheel.sh"] diff --git a/tests/wheels_test_dockerfiles/dnf-based.Dockerfile b/tests/wheels_test_dockerfiles/dnf-based.Dockerfile new file mode 100644 index 000000000..d35640ff7 --- /dev/null +++ b/tests/wheels_test_dockerfiles/dnf-based.Dockerfile @@ -0,0 +1,9 @@ +FROM fedora:latest + +WORKDIR vizdoom + +# MINIMAL +RUN dnf update -y && dnf clean all && dnf install -y python3-devel python3-pip + +COPY . ./ +CMD ["bash", "./scripts/install_and_test_wheel.sh"]