Skip to content

Commit cf2c696

Browse files
committed
feat: Build test app with Dokcer
1 parent bffd7d9 commit cf2c696

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

.github/workflows/pull_request.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ jobs:
3636

3737
- name: Lint code
3838
run: uv run inv lint
39+
40+
- name: Build and run docker test image
41+
run: uv run inv test-app

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM python:3.13-slim-bookworm
2+
3+
RUN apt-get update && apt-get install -y \
4+
&& rm -rf /var/lib/apt/lists/*
5+
6+
WORKDIR /code
7+
8+
COPY pyproject.toml /code/pyproject.toml
9+
COPY uv.lock /code/uv.lock
10+
11+
RUN pip install --no-cache-dir uv
12+
13+
RUN uv sync --locked --all-extras --dev
14+
15+
COPY ./test_app /code/test_app
16+
17+
CMD ["uv", "run", "fastapi", "run", "/code/test_app/main.py", "--port", "80"]

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dev = [
1616
"invoke>=2.2.0",
1717
"mypy>=1.15.0",
1818
"pytest>=8.3.5",
19+
"requests>=2.32.3",
1920
"robotframework-tidy>=4.16.0",
2021
"ruff>=0.11.8",
2122
]

tasks.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from invoke import task
14+
from pathlib import Path
15+
import time
16+
17+
import requests
18+
from invoke.tasks import task
19+
20+
ROOT = Path(__file__).parent
21+
DOCKER_IMAGE = "schemathesis-library-test"
22+
DOCKER_CONTAINER = "schemathesis-library-test-app"
1523

1624

1725
@task
@@ -25,3 +33,39 @@ def lint(ctx):
2533
ctx.run("uv run mypy SchemathesisLibrary")
2634
print("Run RoboTidy")
2735
ctx.run("uv run robotidy")
36+
37+
38+
@task
39+
def stop(ctx):
40+
"""Stop and remove the test app container+image."""
41+
try:
42+
ctx.run(f"docker stop {DOCKER_CONTAINER}")
43+
except Exception as error:
44+
print(f"Error stopping container: {error}")
45+
try:
46+
ctx.run(f"docker rm {DOCKER_CONTAINER}")
47+
except Exception as error:
48+
print(f"Error removing container: {error}")
49+
try:
50+
ctx.run(f"docker image rm {DOCKER_IMAGE}")
51+
except Exception as error:
52+
print(f"Error removing image: {error}")
53+
54+
55+
@task(pre=[stop])
56+
def test_app(ctx):
57+
"""Build docker image and start the test app."""
58+
ctx.run(f"docker build -t {DOCKER_IMAGE} .")
59+
ctx.run(f"docker run -d --name {DOCKER_CONTAINER} -p 80:80 {DOCKER_IMAGE}")
60+
for i in range(300):
61+
time.sleep(1)
62+
try:
63+
response = requests.get("http://127.0.0.1")
64+
if response.status_code == 200:
65+
print("Test app is running")
66+
break
67+
time.sleep(1)
68+
except requests.ConnectionError as error:
69+
print(f"Connection error: {error}")
70+
if i == 299:
71+
raise RuntimeError("Test app did not start in time")

test_app/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class ItemUpdateResponse(BaseModel):
3232
price: float
3333

3434

35+
class ItemDeleteResponse(BaseModel):
36+
message: str
37+
38+
3539
@app.get("/")
3640
async def read_root() -> Root:
3741
return Root(message="Hello World")
@@ -49,3 +53,8 @@ def update_item(item_id: int, item: Item) -> ItemUpdateResponse:
4953
item_id=item_id,
5054
price=item.price,
5155
)
56+
57+
58+
@app.delete("/items/{item_id}")
59+
def delete_item(item_id: int) -> ItemDeleteResponse:
60+
return ItemDeleteResponse(message=f"Item {item_id} deleted successfully")

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)