From 421f9429ec4ff41708093f48d6eba9dfb8b7a83a Mon Sep 17 00:00:00 2001 From: Tullio Sebastiani Date: Fri, 3 May 2024 15:24:50 +0200 Subject: [PATCH] added github action linting added html2text removed distribution added build removed useless step updated quay quay user redhat-chaos removed useless log --- .github/workflows/test.yaml | 115 ++++++++++++++++++++++++++++++++++ app.py | 38 ++++++----- poetry.lock | 12 +++- pyproject.toml | 1 + tests/functional/test_plan.py | 9 ++- 5 files changed, 157 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..af8fe34 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,115 @@ +name: Build +on: + push: + branches: + - main + tags: + - '*' + pull_request: +permissions: + # https://github.community/t/permissions-nesecary-to-comment-on-a-pr/179047/5 + pull-requests: write + contents: write +jobs: + test: + name: Execute Functional & Unit Tests + strategy: + matrix: + python-version: [ '3.9' ] + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Check isort, black, and flake8 + run: | + pip install black flake8 isort + isort --profile black . + black --line-length 79 . + flake8 . + curl -sSL https://install.python-poetry.org | python3 - + - name: Install project dependencies + run: poetry install --no-interaction + - name: Run tests with coverage + env: + TEST_PLAN_PATH: tests/functional/testdata/plan.yaml + run: | + poetry run python3 -m coverage run -a -m pytest + poetry run python3 -m coverage html + poetry run python3 -m coverage json + + - name: Publish coverage report to job summary + if: ${{ matrix.python-version == '3.9'}} + run: | + poetry run html2text --ignore-images --ignore-links -b 0 htmlcov/index.html >> $GITHUB_STEP_SUMMARY + - name: Upload json coverage + uses: actions/upload-artifact@v3 + with: + name: coverage.json + path: coverage.json + if-no-files-found: error + - name: Upload coverage HTML artifact + uses: actions/upload-artifact@v3 + with: + name: coverage + path: htmlcov + if-no-files-found: error + build-image: + name: Build and push container image + runs-on: ubuntu-latest + needs: + - test + if: startsWith(github.ref, 'refs/tags/') + steps: + - name: Check out code + uses: actions/checkout@v3 + - name: Build Docker image + run: | + docker build --no-cache -t quay.io/redhat-chaos/krkn-service-hijacking:latest . + docker tag quay.io/redhat-chaos/krkn-service-hijacking:latest quay.io/redhat-chaos/krkn-service-hijacking:${GITHUB_REF##*/} + - name: Login in quay + run: docker login quay.io -u ${QUAY_USER} -p ${QUAY_TOKEN} + env: + QUAY_USER: ${{ secrets.RH_USERNAME }} + QUAY_TOKEN: ${{ secrets.RH_PASSWORD }} + - name: Push Docker image + run: | + docker push quay.io/redhat-chaos/krkn-service-hijacking:latest + docker push quay.io/redhat-chaos/krkn-service-hijacking:${GITHUB_REF##*/} + + +# publish: +# name: Publish Library on PyPi +# runs-on: ubuntu-latest +# needs: +# - build +# if: startsWith(github.ref, 'refs/tags/') +# steps: +# - name: Check out code +# uses: actions/checkout@v3 +# - name: Download artifacts +# uses: actions/download-artifact@v3 +# with: +# name: dist +# path: dist +# - name: Install twine +# run: pip install -U twine +# - name: Publish +# env: +# TWINE_USERNAME: __token__ +# TWINE_PASSWORD: ${{secrets.PYPI_TOKEN}} +# TWINE_NON_INTERACTIVE: true +# run: twine upload dist/* +# build_krkn: +# name: Build Krkn Image +# runs-on: ubuntu-latest +# steps: +# - name: Rebuild krkn +# if: github.ref == 'refs/heads/main' && github.event_name == 'push' +# uses: redhat-chaos/actions/krkn@main +# with: +# QUAY_USER: ${{ secrets.RH_USERNAME }} +# QUAY_TOKEN: ${{ secrets.RH_PASSWORD }} \ No newline at end of file diff --git a/app.py b/app.py index 3dc6745..1486420 100644 --- a/app.py +++ b/app.py @@ -10,33 +10,37 @@ from utils import validate_step, TimeKeeper app = Flask(__name__) -logging.error("*************************INSTANCE") -method_steps: dict[(str, str), list[dict[any]]] = dict[(str, str), list[dict[any]]]() +method_steps: dict[(str, str), list[dict[any]]] = dict[ + (str, str), list[dict[any]] +]() time_keeper = TimeKeeper() -def restart_time(): - start_time = time.time() - - stats: list[Resource] = list[Resource]() test_plan_path = os.environ.get("TEST_PLAN_PATH") stats_endpoint = ( - os.environ.get("STATS_ROUTE") if os.environ.get("STATS_ROUTE") else "/stats" + os.environ.get("STATS_ROUTE") + if os.environ.get("STATS_ROUTE") + else "/stats" ) def request_handler(params: str = None): - path = request.path if not params else request.path.replace(f"/{params}", "") + path = ( + request.path if not params else request.path.replace(f"/{params}", "") + ) steps = method_steps[(request.method, path)] for request_step in steps: if validate_step(request_step): return Response( - f"[KRKN ERROR] invalid plan step for route {path}, method {request.method}. " - f'The following parameters are missing : "{validate_step(request_step)}". Please ' - f"refer to the documentation on https://github.com/krkn-chaos/krkn-service-hijacking", + f"[KRKN ERROR] invalid plan step for " + f"route {path}, method {request.method}. " + f"The following parameters are missing : " + f'"{validate_step(request_step)}". ' + f"Please refer to the documentation on " + f"https://github.com/krkn-chaos/krkn-service-hijacking", status=500, ) @@ -64,7 +68,9 @@ def request_handler(params: str = None): request_full_path=request.full_path, ) return Response( - steps[-1]["payload"], steps[-1]["status"], mimetype=steps[-1]["mime_type"] + steps[-1]["payload"], + steps[-1]["status"], + mimetype=steps[-1]["mime_type"], ) @@ -115,9 +121,13 @@ def __add_stat_row( for step in test_plan: for i, method in enumerate(step["steps"]): - app.add_url_rule(step["resource"], view_func=request_handler, methods=[method]) app.add_url_rule( - f'{step["resource"]}/', view_func=request_handler, methods=[method] + step["resource"], view_func=request_handler, methods=[method] + ) + app.add_url_rule( + f'{step["resource"]}/', + view_func=request_handler, + methods=[method], ) method_steps[(method, step["resource"])] = step["steps"][method] stats.append(Resource(step["resource"], method)) diff --git a/poetry.lock b/poetry.lock index 0031e5c..e970b41 100644 --- a/poetry.lock +++ b/poetry.lock @@ -137,6 +137,16 @@ Werkzeug = ">=2.3.7" async = ["asgiref (>=3.2)"] dotenv = ["python-dotenv"] +[[package]] +name = "html2text" +version = "2024.2.26" +description = "Turn HTML into equivalent Markdown-structured text." +optional = false +python-versions = ">=3.8" +files = [ + {file = "html2text-2024.2.26.tar.gz", hash = "sha256:05f8e367d15aaabc96415376776cdd11afd5127a77fce6e36afc60c563ca2c32"}, +] + [[package]] name = "importlib-metadata" version = "7.1.0" @@ -407,4 +417,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "b73334cdaf6a460f23f3a3e898da1b206f8f949cd266f825ac7a400bcac73d12" +content-hash = "a429af6034d76b30020f91eeb2467782dbe8128b4e9704be44374de02b0af30d" diff --git a/pyproject.toml b/pyproject.toml index da1b523..b22dd32 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ flask = "^2.3.2" PyYAML = "^6.0.1" pytest = "^8.2.0" coverage = "^7.5.0" +html2text = "^2024.2.26" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" diff --git a/tests/functional/test_plan.py b/tests/functional/test_plan.py index bb76fe4..cc1d4ff 100644 --- a/tests/functional/test_plan.py +++ b/tests/functional/test_plan.py @@ -1,5 +1,4 @@ import json -import os import time @@ -70,12 +69,16 @@ def test_stats(app, client, time_keeper): post = [r for r in stats_json if r["method"] == "POST"][0] patch = [r for r in stats_json if r["method"] == "PATCH"][0] - get_stat = [r for r in get["requests"] if get_resource in r["request_full_path"]][0] + get_stat = [ + r for r in get["requests"] if get_resource in r["request_full_path"] + ][0] post_stat = [ r for r in post["requests"] if post_resource in r["request_full_path"] ][0] patch_stat = [ - r for r in patch["requests"] if patch_resource in r["request_full_path"] + r + for r in patch["requests"] + if patch_resource in r["request_full_path"] ][0] assert get_stat["mime_type"] == "application/json"