Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests #99

Merged
merged 10 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
name: Deploy to ECR
name: Test and deploy to ECR

on:
push:
branches: [main]
paths:
- "app/**"
- ".github/workflows/test_and_deploy_to_ecr.yaml"
pull_request:
paths:
- "app/**"
- ".github/workflows/test_and_deploy_to_ecr.yaml"
workflow_dispatch:

env:
ECR_REPOSITORY: devops-boot
CONTAINER_NAME: fastapi_app
AWS_REGION: us-east-1 # Used for a public registry
AWS_REGION: us-east-1 # Used for a public registry

jobs:
test:
name: Application unittests
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r app/requirements.txt

- name: Install pytest
run: |
pip install pytest

- name: Run tests
run: |
pytest

build:
name: Build Image
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' # Only run on push or manual dispatch
needs: test # Only run if tests execute successfully

steps:
- name: Check out code
Expand All @@ -35,7 +73,7 @@ jobs:
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr-public.outputs.registry }}
ECR_REGISTRY_ALIAS: v4m5s6o6 # Alias generated by AWS: https://gallery.ecr.aws/v4m5s6o6/devops-boot
ECR_REGISTRY_ALIAS: v4m5s6o6 # Alias generated by AWS: https://gallery.ecr.aws/v4m5s6o6/devops-boot
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REGISTRY_ALIAS/$ECR_REPOSITORY:$IMAGE_TAG app/
Expand Down
18 changes: 18 additions & 0 deletions app/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from fastapi.testclient import TestClient
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def read_main():
return {"message": "Hello World!"}


client = TestClient(app)


def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World!"}