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

testing if the api is working and add the test to the deployment script #31

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 25 additions & 6 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ jobs:
runs-on: ubuntu-latest

env:
GOOGLE_PROJECT_ID: ${{ vars.GOOGLE_PROJECT_ID }}
GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS }}
GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL }}
ENVIRONMENT: ${{ vars.ENVIRONMENT }}
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GCP_SA_KEY }}
BACKEND_IMAGE_NAME: europe-west3-docker.pkg.dev/thf-climate-cloud/thf-climate/thf-climate
FRONTEND_IMAGE_NAME: europe-west3-docker.pkg.dev/thf-climate-cloud/thf-climate-frontend/thf-climate-frontend

steps:
# Step 1: Checkout the code
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/checkout@v3

# Step 2: Decode and Write the Service Account Key to a file properly
- name: Set up Google Cloud credentials
Expand All @@ -43,11 +45,28 @@ jobs:
run: |
gcloud auth configure-docker europe-west3-docker.pkg.dev

# Step 6: Build and Deploy the backend
- name: Build Docker image (backend)
# Step 6: Build and the run the backend
- name: Build and Run the Docker image (backend)
working-directory: ./backend
run: |
docker buildx build --platform linux/amd64 -t $BACKEND_IMAGE_NAME:latest .
docker run -d -p 8000:8000 --name thf-climate-backend $BACKEND_IMAGE_NAME
- name: Wait for the application to start
working-directory: ./backend
run: |
timeout 30 bash -c 'until curl -s http://127.0.0.1:8000/; do sleep 1; done'

# Step 7: Run the tests against the local application
- name: Install dependencies and Run tests (backend)
working-directory: ./backend
env:
API_BASE_URL: "http://127.0.0.1:8000"
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pytest

# Step 8: Push and Deploy the backend on Google cloud
- name: Push Docker image to Artifact Registry (backend)
working-directory: ./backend
run: |
Expand All @@ -64,7 +83,7 @@ jobs:
--min-instances=1 \
--max-instances=5

# Step 7: Build and Deploy the frontend
# Step 9: Build and Deploy the frontend
- name: Build Docker image (frontend)
working-directory: ./frontend
run: |
Expand Down
25 changes: 17 additions & 8 deletions .github/workflows/tests-main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ jobs:
ENVIRONMENT: ${{ vars.ENVIRONMENT }}
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies

# Build and the run the backend docker image
- name: Build and Run the Docker image (backend)
working-directory: ./backend
run: |
docker buildx build --platform linux/amd64 -t thf-climate-backend .
docker run -d -p 8000:8000 --name thf-climate-backend thf-climate-backend
- name: Wait for the application to start
working-directory: ./backend
run: |
timeout 30 bash -c 'until curl -s http://127.0.0.1:8000/; do sleep 1; done'

# Run the tests against the local application
- name: Install dependencies and Run tests
working-directory: ./backend
env:
API_BASE_URL: "http://127.0.0.1:8000"
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
- name: Run tests
working-directory: ./backend
run: pytest
pytest
14 changes: 14 additions & 0 deletions backend/tests/test_api_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import requests


def test_api_response():
# Make a GET request to the API
response = requests.get(os.getenv("API_BASE_URL"))

# Verify the response status code
assert response.status_code == 200, f"Expected 200, got {response.status_code}"

# Verify the response body
expected_response = {"Hello": "World"}
assert response.json() == expected_response, f"Expected {expected_response}, got {response.json()}"
Loading