Skip to content

Commit 45d8342

Browse files
author
Kairo de Araujo
committed
Add tests for tern-rest-api reports implementation
This commit adds all the tern-rest-api reports implementation tests and adjusts the repository to use Docker.
1 parent eb8e0b3 commit 45d8342

File tree

18 files changed

+644
-45
lines changed

18 files changed

+644
-45
lines changed

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[run]
2+
omit = *tests*,app.py

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
run: pip install tox tox-gh-actions
2323

2424
- name: Run Python tests
25-
run: tox
25+
run: tox -r

Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ RUN echo "deb http://deb.debian.org/debian bullseye main" > /etc/apt/sources.lis
1616
skopeo \
1717
&& rm -rf /var/lib/apt/lists/*
1818

19+
COPY requirements.txt requirements.txt
20+
RUN pip install --no-cache -r ./requirements.txt
21+
1922
RUN mkdir /opt/tern-rest-api
2023

2124
ADD . /opt/tern-rest-api
2225
WORKDIR /opt/tern-rest-api
23-
RUN pip install --no-cache -r ./requirements.txt
2426

25-
ENV TERN_API_CACHE_DIR=/var/opt/tern-rest-api/cached
27+
ENV TERN_API_CACHE_DIR=/var/opt/tern-rest-api/cache
2628
ENV TERN_DEFAULT_REGISTRY="registry.hub.docker.com"
2729

28-
ENV FLASK_APP=/opt/tern-rest-api/app.py
29-
CMD ["bash", "docker_start.sh"]
30+
ENTRYPOINT [ "bash", "docker_start.sh" ]

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55
# SPDX-License-Identifier: BSD-2-Clause
66

77
build-dev:
8-
docker build -t tern-rest-api:dev .
8+
docker-compose build --force-rm tern-rest-api
99

1010
serve-dev: build-dev
11-
docker run --rm --name tern-rest-api -e ENVIRONMENT=DEVELOPMENT --privileged -v /var/run/docker.sock:/var/run/docker.sock -v $(PWD):/opt/tern-rest-api -p 5001:80 tern-rest-api:dev
11+
docker-compose up --remove-orphans
12+
13+
tests: build-dev
14+
docker-compose run --rm --volume=$(PWD):/opt/tern-rest-api --entrypoint="/bin/sh" tern-rest-api -c 'pip install tox && tox'
15+
16+
stop:
17+
docker-compose down -v
18+
19+
update-requirements:
20+
pipenv lock -r > requirements.txt
21+
pipenv lock -r -d > requirements-dev.txt

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Open http://localhost/ in your browser.
6868
Changing the source code will automatically reload the server inside the
6969
container and makes the development easier.
7070

71+
You can stop the sever using ``Ctrl+C`` and running ``make stop``
72+
7173
## On your local machine
7274
Runing the API locally
7375

@@ -81,7 +83,12 @@ Open http://localhost:5000/ in your browser.
8183

8284
We use [Tox](https://tox.wiki/en/latest/) to manage running the tests.
8385

84-
Running tests
86+
## As a Docker Container
87+
```shell
88+
$ make tests
89+
```
90+
91+
## On your local machine
8592
```shell
8693
$ tox
8794
```
@@ -106,6 +113,5 @@ $ pipenv update
106113

107114
Updating the ``requirements.txt`` and ``requirements-dev.txt``
108115
```shell
109-
$ pipenv lock -r > requirements.txt
110-
$ pipenv lock -r -d > requirements-dev.txt
116+
$ make update-requirements
111117
```

app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from tern_api import __version__, tern_app
1212
from tern_api.api.v1.common_models import api_models_namespace
13-
from tern_api.api.v1.reports import ns as report_v1
13+
from tern_api.api.v1.reports import ns as reports_v1
1414
from tern_api.api.v1.version import ns as version_v1
1515

1616
logging.basicConfig(
@@ -35,7 +35,7 @@
3535

3636
api.add_namespace(api_models_namespace)
3737
api.add_namespace(version_v1, path="/api/v1/version")
38-
api.add_namespace(report_v1, path="/api/v1/report")
38+
api.add_namespace(reports_v1, path="/api/v1/reports")
3939

4040

4141
def export_swagger_json(filepath):

docker-compose.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3'
2+
3+
volumes:
4+
tern-cache:
5+
6+
services:
7+
tern-rest-api:
8+
build:
9+
context: .
10+
args:
11+
DEVEL: "yes"
12+
command: "bash bash_start.sh"
13+
environment:
14+
- ENVIRONMENT=development
15+
- TERN_DEFAULT_REGISTRY=registry.hub.docker.com
16+
volumes:
17+
- /var/run/docker.sock:/var/run/docker.sock:ro
18+
- tern-cache:/var/opt/tern-rest-api/cache:z
19+
- ./tern_api:/opt/tern-rest-api/tern_api:z
20+
ports:
21+
- "5001:80"

docker_start.sh

100644100755
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/bash
22

33
if [[ ${ENVIRONMENT^^} == "DEVELOPMENT" ]]; then
4-
flask run --reload --debugger -h 0.0.0.0 -p 80
4+
echo "Starting tern-rest-api in development mode"
5+
gunicorn --reload -b 0.0.0.0:80 app:tern_app
56
else
6-
gunicorn --workers=1 -b 0.0.0.0:80 app:tern_api
7+
gunicorn -b 0.0.0.0:80 app:tern_app
78
fi

tern_api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
tern_app.config["TERN_DEFAULT_REGISTRY"] = os.getenv("TERN_DEFAULT_REGISTRY")
1414

1515
tern_tasks = Executor(tern_app)
16+
tern_app.config["TERN_TASKS"] = tern_tasks

tern_api/api/v1/reports.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from flask import request
77
from flask_restx import Namespace, Resource, fields
88

9-
from tern_api import reports, tern_app
9+
from tern_api import tern_app, constants
10+
from tern_api.reports import submit, status
1011
from tern_api.api.v1.common_models import (
1112
async_response_model,
1213
error_model,
@@ -53,14 +54,14 @@ class Report(Resource):
5354
)
5455

5556
@ns.response(200, "OK", report_response_request)
56-
@ns.expect(report_parameters)
57+
@ns.expect(report_parameters, validate=True)
5758
def post(self):
5859
"""Tern BoM report
5960
6061
**Note**: This request will be processed assynchronous.
6162
"""
6263
payload = request.json
63-
response = reports.request(payload)
64+
response = submit(payload)
6465
return response.to_response()
6566

6667

@@ -79,13 +80,28 @@ class ReportStatus(Resource):
7980
data_status_response = ns.model(
8081
"data_status_response",
8182
{
83+
"cache": fields.Boolean(
84+
description="Requested using cache?",
85+
required=True,
86+
example=True,
87+
),
88+
"id": fields.String(
89+
description="Unique Identification for request",
90+
required=False,
91+
example="19f035a711644eab84ef5a38ceb5572e",
92+
),
93+
"message": fields.String(
94+
description="Message",
95+
required=False,
96+
exampple="Request is running",
97+
),
98+
"report": fields.Nested(report_model),
8299
"status": fields.String(
83100
description="Status of request",
84101
required=True,
85-
example="FINISH",
86-
enum=["UNKNOWN", "FINISH", "RUNNING", "FAIL"],
102+
example=constants.task_status.SUCCESS.value,
103+
enum=[s.value for s in constants.task_status],
87104
),
88-
"result": fields.Nested(report_model),
89105
},
90106
)
91107
report_status_response = ns.model(
@@ -97,10 +113,10 @@ class ReportStatus(Resource):
97113
)
98114

99115
@ns.response(200, "OK", report_status_response)
100-
@ns.expect(report_status_parameters)
116+
@ns.expect(report_status_parameters, validate=True)
101117
def post(self):
102118
"""Request Tern BoM report status/result"""
103119

104120
payload = request.json
105-
response = reports.status(payload.get("id"))
121+
response = status(payload.get("id"))
106122
return response.to_response()

0 commit comments

Comments
 (0)