Skip to content

Commit afd8a26

Browse files
committed
added github action
linting added html2text removed distribution added build removed useless step updated quay quay user
1 parent e8c2797 commit afd8a26

File tree

5 files changed

+157
-17
lines changed

5 files changed

+157
-17
lines changed

.github/workflows/test.yaml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Build
2+
on:
3+
push:
4+
branches:
5+
- main
6+
tags:
7+
- '*'
8+
pull_request:
9+
permissions:
10+
# https://github.community/t/permissions-nesecary-to-comment-on-a-pr/179047/5
11+
pull-requests: write
12+
contents: write
13+
jobs:
14+
test:
15+
name: Execute Functional & Unit Tests
16+
strategy:
17+
matrix:
18+
python-version: [ '3.9' ]
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Check out code
22+
uses: actions/checkout@v3
23+
- name: Set up Python
24+
uses: actions/setup-python@v4
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
- name: Check isort, black, and flake8
28+
run: |
29+
pip install black flake8 isort
30+
isort --profile black .
31+
black --line-length 79 .
32+
flake8 .
33+
curl -sSL https://install.python-poetry.org | python3 -
34+
- name: Install project dependencies
35+
run: poetry install --no-interaction
36+
- name: Run tests with coverage
37+
env:
38+
TEST_PLAN_PATH: tests/functional/testdata/plan.yaml
39+
run: |
40+
poetry run python3 -m coverage run -a -m pytest
41+
poetry run python3 -m coverage html
42+
poetry run python3 -m coverage json
43+
44+
- name: Publish coverage report to job summary
45+
if: ${{ matrix.python-version == '3.9'}}
46+
run: |
47+
poetry run html2text --ignore-images --ignore-links -b 0 htmlcov/index.html >> $GITHUB_STEP_SUMMARY
48+
- name: Upload json coverage
49+
uses: actions/upload-artifact@v3
50+
with:
51+
name: coverage.json
52+
path: coverage.json
53+
if-no-files-found: error
54+
- name: Upload coverage HTML artifact
55+
uses: actions/upload-artifact@v3
56+
with:
57+
name: coverage
58+
path: htmlcov
59+
if-no-files-found: error
60+
build-image:
61+
name: Build and push container image
62+
runs-on: ubuntu-latest
63+
needs:
64+
- test
65+
if: startsWith(github.ref, 'refs/tags/')
66+
steps:
67+
- name: Check out code
68+
uses: actions/checkout@v3
69+
- name: Build Docker image
70+
run: |
71+
docker build --no-cache -t quay.io/krkn-chaos/krkn-service-hijacking:latest .
72+
docker tag quay.io/krkn-chaos/krkn-service-hijacking:latest quay.io/krkn-chaos/krkn-service-hijacking:${GITHUB_REF##*/}
73+
- name: Login in quay
74+
run: docker login quay.io -u ${QUAY_USER} -p ${QUAY_TOKEN}
75+
env:
76+
QUAY_USER: ${{ secrets.RH_USERNAME }}
77+
QUAY_TOKEN: ${{ secrets.RH_PASSWORD }}
78+
- name: Push Docker image
79+
run: |
80+
docker push quay.io/redhat-chaos/krkn-service-hijacking:latest
81+
docker push quay.io/redhat-chaos/krkn-service-hijacking:${GITHUB_REF##*/}
82+
83+
84+
# publish:
85+
# name: Publish Library on PyPi
86+
# runs-on: ubuntu-latest
87+
# needs:
88+
# - build
89+
# if: startsWith(github.ref, 'refs/tags/')
90+
# steps:
91+
# - name: Check out code
92+
# uses: actions/checkout@v3
93+
# - name: Download artifacts
94+
# uses: actions/download-artifact@v3
95+
# with:
96+
# name: dist
97+
# path: dist
98+
# - name: Install twine
99+
# run: pip install -U twine
100+
# - name: Publish
101+
# env:
102+
# TWINE_USERNAME: __token__
103+
# TWINE_PASSWORD: ${{secrets.PYPI_TOKEN}}
104+
# TWINE_NON_INTERACTIVE: true
105+
# run: twine upload dist/*
106+
# build_krkn:
107+
# name: Build Krkn Image
108+
# runs-on: ubuntu-latest
109+
# steps:
110+
# - name: Rebuild krkn
111+
# if: github.ref == 'refs/heads/main' && github.event_name == 'push'
112+
# uses: redhat-chaos/actions/krkn@main
113+
# with:
114+
# QUAY_USER: ${{ secrets.RH_USERNAME }}
115+
# QUAY_TOKEN: ${{ secrets.RH_PASSWORD }}

app.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,37 @@
1111

1212
app = Flask(__name__)
1313
logging.error("*************************INSTANCE")
14-
method_steps: dict[(str, str), list[dict[any]]] = dict[(str, str), list[dict[any]]]()
14+
method_steps: dict[(str, str), list[dict[any]]] = dict[
15+
(str, str), list[dict[any]]
16+
]()
1517
time_keeper = TimeKeeper()
1618

1719

18-
def restart_time():
19-
start_time = time.time()
20-
21-
2220
stats: list[Resource] = list[Resource]()
2321
test_plan_path = os.environ.get("TEST_PLAN_PATH")
2422
stats_endpoint = (
25-
os.environ.get("STATS_ROUTE") if os.environ.get("STATS_ROUTE") else "/stats"
23+
os.environ.get("STATS_ROUTE")
24+
if os.environ.get("STATS_ROUTE")
25+
else "/stats"
2626
)
2727

2828

2929
def request_handler(params: str = None):
3030

31-
path = request.path if not params else request.path.replace(f"/{params}", "")
31+
path = (
32+
request.path if not params else request.path.replace(f"/{params}", "")
33+
)
3234
steps = method_steps[(request.method, path)]
3335

3436
for request_step in steps:
3537
if validate_step(request_step):
3638
return Response(
37-
f"[KRKN ERROR] invalid plan step for route {path}, method {request.method}. "
38-
f'The following parameters are missing : "{validate_step(request_step)}". Please '
39-
f"refer to the documentation on https://github.com/krkn-chaos/krkn-service-hijacking",
39+
f"[KRKN ERROR] invalid plan step for "
40+
f"route {path}, method {request.method}. "
41+
f"The following parameters are missing : "
42+
f'"{validate_step(request_step)}". '
43+
f"Please refer to the documentation on "
44+
f"https://github.com/krkn-chaos/krkn-service-hijacking",
4045
status=500,
4146
)
4247

@@ -64,7 +69,9 @@ def request_handler(params: str = None):
6469
request_full_path=request.full_path,
6570
)
6671
return Response(
67-
steps[-1]["payload"], steps[-1]["status"], mimetype=steps[-1]["mime_type"]
72+
steps[-1]["payload"],
73+
steps[-1]["status"],
74+
mimetype=steps[-1]["mime_type"],
6875
)
6976

7077

@@ -115,9 +122,13 @@ def __add_stat_row(
115122

116123
for step in test_plan:
117124
for i, method in enumerate(step["steps"]):
118-
app.add_url_rule(step["resource"], view_func=request_handler, methods=[method])
119125
app.add_url_rule(
120-
f'{step["resource"]}/<params>', view_func=request_handler, methods=[method]
126+
step["resource"], view_func=request_handler, methods=[method]
127+
)
128+
app.add_url_rule(
129+
f'{step["resource"]}/<params>',
130+
view_func=request_handler,
131+
methods=[method],
121132
)
122133
method_steps[(method, step["resource"])] = step["steps"][method]
123134
stats.append(Resource(step["resource"], method))

poetry.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ flask = "^2.3.2"
1414
PyYAML = "^6.0.1"
1515
pytest = "^8.2.0"
1616
coverage = "^7.5.0"
17+
html2text = "^2024.2.26"
1718
[build-system]
1819
requires = ["poetry-core"]
1920
build-backend = "poetry.core.masonry.api"

tests/functional/test_plan.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import os
32
import time
43

54

@@ -70,12 +69,16 @@ def test_stats(app, client, time_keeper):
7069
post = [r for r in stats_json if r["method"] == "POST"][0]
7170
patch = [r for r in stats_json if r["method"] == "PATCH"][0]
7271

73-
get_stat = [r for r in get["requests"] if get_resource in r["request_full_path"]][0]
72+
get_stat = [
73+
r for r in get["requests"] if get_resource in r["request_full_path"]
74+
][0]
7475
post_stat = [
7576
r for r in post["requests"] if post_resource in r["request_full_path"]
7677
][0]
7778
patch_stat = [
78-
r for r in patch["requests"] if patch_resource in r["request_full_path"]
79+
r
80+
for r in patch["requests"]
81+
if patch_resource in r["request_full_path"]
7982
][0]
8083

8184
assert get_stat["mime_type"] == "application/json"

0 commit comments

Comments
 (0)