Skip to content

Commit 1ca0be7

Browse files
authored
Merge pull request #31 from codeuniversity/feature/custom-testing-base-url
testing if the api is working and add the test to the deployment script
2 parents 3250704 + 3aeeebe commit 1ca0be7

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

.github/workflows/deploy.yml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ jobs:
1010
runs-on: ubuntu-latest
1111

1212
env:
13+
GOOGLE_PROJECT_ID: ${{ vars.GOOGLE_PROJECT_ID }}
14+
GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS }}
15+
GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL }}
16+
ENVIRONMENT: ${{ vars.ENVIRONMENT }}
1317
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GCP_SA_KEY }}
1418
BACKEND_IMAGE_NAME: europe-west3-docker.pkg.dev/thf-climate-cloud/thf-climate/thf-climate
1519
FRONTEND_IMAGE_NAME: europe-west3-docker.pkg.dev/thf-climate-cloud/thf-climate-frontend/thf-climate-frontend
1620

1721
steps:
18-
# Step 1: Checkout the code
19-
- name: Checkout code
20-
uses: actions/checkout@v2
22+
- uses: actions/checkout@v3
2123

2224
# Step 2: Decode and Write the Service Account Key to a file properly
2325
- name: Set up Google Cloud credentials
@@ -43,11 +45,28 @@ jobs:
4345
run: |
4446
gcloud auth configure-docker europe-west3-docker.pkg.dev
4547
46-
# Step 6: Build and Deploy the backend
47-
- name: Build Docker image (backend)
48+
# Step 6: Build and the run the backend
49+
- name: Build and Run the Docker image (backend)
4850
working-directory: ./backend
4951
run: |
5052
docker buildx build --platform linux/amd64 -t $BACKEND_IMAGE_NAME:latest .
53+
docker run -d -p 8000:8000 --name thf-climate-backend $BACKEND_IMAGE_NAME
54+
- name: Wait for the application to start
55+
working-directory: ./backend
56+
run: |
57+
timeout 30 bash -c 'until curl -s http://127.0.0.1:8000/; do sleep 1; done'
58+
59+
# Step 7: Run the tests against the local application
60+
- name: Install dependencies and Run tests (backend)
61+
working-directory: ./backend
62+
env:
63+
API_BASE_URL: "http://127.0.0.1:8000"
64+
run: |
65+
python -m pip install --upgrade pip
66+
pip install -r requirements-dev.txt
67+
pytest
68+
69+
# Step 8: Push and Deploy the backend on Google cloud
5170
- name: Push Docker image to Artifact Registry (backend)
5271
working-directory: ./backend
5372
run: |
@@ -64,7 +83,7 @@ jobs:
6483
--min-instances=1 \
6584
--max-instances=5
6685
67-
# Step 7: Build and Deploy the frontend
86+
# Step 9: Build and Deploy the frontend
6887
- name: Build Docker image (frontend)
6988
working-directory: ./frontend
7089
run: |

.github/workflows/tests-main.yaml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@ jobs:
1616
ENVIRONMENT: ${{ vars.ENVIRONMENT }}
1717
steps:
1818
- uses: actions/checkout@v3
19-
- name: Set up Python
20-
uses: actions/setup-python@v4
21-
with:
22-
python-version: '3.11'
23-
- name: Install dependencies
19+
20+
# Build and the run the backend docker image
21+
- name: Build and Run the Docker image (backend)
22+
working-directory: ./backend
23+
run: |
24+
docker buildx build --platform linux/amd64 -t thf-climate-backend .
25+
docker run -d -p 8000:8000 --name thf-climate-backend thf-climate-backend
26+
- name: Wait for the application to start
2427
working-directory: ./backend
28+
run: |
29+
timeout 30 bash -c 'until curl -s http://127.0.0.1:8000/; do sleep 1; done'
30+
31+
# Run the tests against the local application
32+
- name: Install dependencies and Run tests
33+
working-directory: ./backend
34+
env:
35+
API_BASE_URL: "http://127.0.0.1:8000"
2536
run: |
2637
python -m pip install --upgrade pip
2738
pip install -r requirements-dev.txt
28-
- name: Run tests
29-
working-directory: ./backend
30-
run: pytest
39+
pytest

backend/tests/test_api_response.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import requests
3+
4+
5+
def test_api_response():
6+
# Make a GET request to the API
7+
response = requests.get(os.getenv("API_BASE_URL"))
8+
9+
# Verify the response status code
10+
assert response.status_code == 200, f"Expected 200, got {response.status_code}"
11+
12+
# Verify the response body
13+
expected_response = {"Hello": "World"}
14+
assert response.json() == expected_response, f"Expected {expected_response}, got {response.json()}"

0 commit comments

Comments
 (0)