From 8deb1064a88d66176e62486f0b9895b3d5486411 Mon Sep 17 00:00:00 2001 From: LukeShirnia Date: Thu, 25 Apr 2024 15:09:31 +0100 Subject: [PATCH] Make actions use docker compose file --- .github/workflows/main.yml | 1 + .github/workflows/pytest.yml | 113 ++++++++++++++++++++++++----------- Dockerfile | 2 +- docker-compose.yml | 6 +- tests/test_system.py | 24 ++++++++ 5 files changed, 108 insertions(+), 38 deletions(-) create mode 100644 tests/test_system.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d59dac2..8e7bb95 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -40,3 +40,4 @@ jobs: - detect-file-changes uses: ./.github/workflows/pytest.yml if: needs.detect-file-changes.outputs.oom_investigate == 'true' + secrets: inherit diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 0c417a7..a9e2ac9 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -1,51 +1,96 @@ -name: Run Unit Tests on Multiple Python Versions + +name: Run Unit Tests on Multiple Python Versions and Distributions on: workflow_call: + +env: + REPOSITORY: lukeshirnia/out-of-memory + jobs: - pytest_legacy_python: - name: python:${{ matrix.python-version }} - runs-on: ubuntu-20.04 - container: - image: python:${{ matrix.python-version }}-buster + + # Lets not build and push images every time. This can be done manually. Leaving here for reference. + + # build_and_push: + # runs-on: ubuntu-latest + # steps: + # - name: Checkout code + # uses: actions/checkout@v3 + + # - name: Set up Docker Buildx + # uses: docker/setup-buildx-action@v2 + + # - name: Login to Docker Hub + # uses: docker/login-action@v2 + # with: + # username: ${{ secrets.DOCKER_HUB_USERNAME }} + # password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + # - name: Pull Docker images (if exists) + # run: | + # docker-compose config | grep 'image:' | awk '{print $2}' | while read image; do + # base_image_name=$(echo $image | awk -F: '{print $1}') + # version_tag=$(echo $image | awk -F: '{print $2}') + # existing_tag="${{ env.REPOSITORY }}:${base_image_name#*/}" + # echo "Trying to pull existing image $existing_tag" + # docker pull $existing_tag || true # Continue even if pull fails + # done + + # - name: Build Docker images + # run: | + # docker-compose build --pull + + # - name: Push Docker images + # run: | + # docker-compose config | grep 'image:' | awk '{print $2}' | while read image; do + # echo "Processing image: $image" + # base_image_name=$(echo $image | awk -F: '{print $1}') + # version_tag=$(echo $image | awk -F: '{print $2}') + # new_tag="${{ env.REPOSITORY }}:${base_image_name#*/}" + # echo "Tagging image $image as $new_tag" + # docker tag $image $new_tag + # docker push $new_tag + # done + + test-python-versions: + # needs: build_and_push + runs-on: ubuntu-latest strategy: - fail-fast: true matrix: - python-version: ["2.7", "3.6"] + python-version: ["python27", "python36", "python310"] steps: - - name: Checkout Code - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v3 - - name: Display Python version - run: python -c "import sys; print(sys.version)" - - - name: Test with pytest + - name: Run pytest run: | - pip install pytest pytest-cov - pytest tests/ + docker run --rm \ + -v ${{ github.workspace }}:/app \ + ${{ env.REPOSITORY }}:${{ matrix.python-version }}-pytest \ + pytest -v --ignore=tests/test_system.py - pytest_latest_python: - name: ${{ matrix.os }} ${{ matrix.python-version }} - runs-on: ${{ matrix.os }} + test-systems: + # needs: build_and_push + runs-on: ubuntu-latest strategy: - fail-fast: true matrix: - os: [ubuntu-latest] - python-version: ["3.11"] + distro: ["amazonlinux", "centos7", "osx"] steps: - - name: Checkout Code - uses: actions/checkout@v4 - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python-version }} + - name: Checkout code + uses: actions/checkout@v3 - - name: Display Python version - run: python -c "import sys; print(sys.version)" + - name: Setup test command + id: setup + run: | + if [ "${{ matrix.distro }}" == "osx" ]; then + echo "::set-output name=command::python -m pytest" + else + echo "::set-output name=command::pytest" + fi - - name: Test with pytest + - name: Run system tests run: | - pip install pytest pytest-cov - pytest tests/ + docker run --rm -v ${{ github.workspace }}:/app \ + ${{ env.REPOSITORY }}:${{ matrix.distro }}-pytest \ + ${{ steps.setup.outputs.command }} -v tests/test_system.py -p no:cacheprovider diff --git a/Dockerfile b/Dockerfile index 6a596ad..82a0972 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,6 @@ RUN if echo ${IMAGE} | grep -q "amazon"; then \ RUN ${PIP} install pytest # Only install black on Python 3.x as it doesn't exist on 2.x -RUN if [ $(${PYTHON_VERSION}) -ge 3 ]; then ${PIP} install black isort; fi +RUN if [ $PYTHON_VERSION -ge 3 ]; then ${PIP} install black isort; fi WORKDIR /app diff --git a/docker-compose.yml b/docker-compose.yml index bd4f087..4a87c64 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -39,7 +39,7 @@ services: IMAGE: amazonlinux PIP: pip3 PYTHON_VERSION: 3 - image: amazonlinux + image: amazonlinux-pytest volumes: - .:/app @@ -50,7 +50,7 @@ services: IMAGE: centos/python-27-centos7 PIP: pip PYTHON_VERSION: 2 - image: centos7 + image: centos7-pytest volumes: - .:/app @@ -62,6 +62,6 @@ services: IMAGE: sickcodes/docker-osx PIP: python -m pip PYTHON_VERSION: 3 - image: osx + image: osx-pytest volumes: - .:/app diff --git a/tests/test_system.py b/tests/test_system.py new file mode 100644 index 0000000..ba235c2 --- /dev/null +++ b/tests/test_system.py @@ -0,0 +1,24 @@ +import os +import sys + +import pytest + +# Add the parent directory to the path so we can import the latest version of the script +oom_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) +sys.path.insert(0, oom_dir) + +from oom_investigate import System + +# Ignore DeprecationWarning and PendingDeprecationWarning warnings +pytestmark = pytest.mark.filterwarnings("ignore::DeprecationWarning") +pytestmark = pytest.mark.filterwarnings("ignore::PendingDeprecationWarning") + + +class TestSystem: + def test_distro_info_imports(self): + # very basic test to check if the distro info is returned + # Designed to run on docker containers for python 2.7, 3.6, 3.10 + system = System() + distro_info = system.get_distro_info() + assert distro_info[0] is not None + assert distro_info[1] is not None