Skip to content

Commit f0e5ca3

Browse files
committed
Merge branch 'feature/add-tests' into develop
2 parents fbf3043 + 24b58f6 commit f0e5ca3

8 files changed

Lines changed: 7417 additions & 4818 deletions

File tree

.github/workflows/gke-cd.yml

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,60 @@
1-
name: Deploy FNS API to Cloud Run (Separated Jobs)
1+
name: Branch-Specific Deploy FNS API to Cloud Run
22

33
on:
44
push:
55
branches:
66
- master
7+
- feature/*
8+
- develop
79
pull_request:
810
branches:
911
- master
12+
- feature/*
13+
- develop
1014

1115
jobs:
16+
test:
17+
name: Run Backend Tests
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v2
22+
23+
- name: Set up Python 3.9
24+
uses: actions/setup-python@v2
25+
with:
26+
python-version: 3.9
27+
28+
- name: Install dependencies
29+
run: pip install -r requirements.txt
30+
31+
- name: Run tests
32+
run: python -m unittest tests/test_backend.py
33+
1234
build-and-push-image:
1335
name: Build and Push Docker Image
1436
runs-on: ubuntu-latest
37+
needs: test
38+
if: github.ref == 'refs/heads/master' # Condition: Only run on master branch
1539
steps:
1640
- name: Checkout code
1741
uses: actions/checkout@v2
1842

19-
- name: Google Cloud Auth # Authenticate gcloud CLI
43+
- name: Google Cloud Auth
2044
uses: google-github-actions/auth@v2
2145
with:
2246
credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
2347

24-
- name: Set up Google Cloud CLI # Set up gcloud CLI and Docker
48+
- name: Set up Google Cloud CLI
2549
uses: google-github-actions/setup-gcloud@v2
2650
with:
2751
version: latest
2852
project_id: ${{ secrets.GOOGLE_PROJECT }}
2953

30-
- name: Configure Docker to push to Artifact Registry # Configure docker auth
31-
run: |
32-
gcloud auth configure-docker us-central1-docker.pkg.dev
54+
- name: Configure Docker to push to Artifact Registry
55+
run: gcloud auth configure-docker us-central1-docker.pkg.dev
3356

34-
- name: Build and Push Docker image to Artifact Registry # Build and push image
57+
- name: Build and Push Docker image to Artifact Registry
3558
env:
3659
GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }}
3760
IMAGE_NAME: us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api
@@ -43,20 +66,21 @@ jobs:
4366
deploy-to-cloud-run:
4467
name: Deploy to Cloud Run
4568
runs-on: ubuntu-latest
46-
needs: build-and-push-image # Ensure this job runs after build-and-push-image
69+
needs: build-and-push-image
70+
if: github.ref == 'refs/heads/master' # Condition: Only run on master branch
4771
steps:
48-
- name: Checkout code (again, if needed for deploy steps - optional)
72+
- name: Checkout code (again, if needed)
4973
uses: actions/checkout@v2
5074

51-
- name: Google Cloud Auth # Authenticate gcloud CLI
75+
- name: Google Cloud Auth
5276
uses: google-github-actions/auth@v2
5377
with:
5478
credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
5579

5680
- name: Deploy to Cloud Run
5781
uses: google-github-actions/deploy-cloudrun@v1
5882
with:
59-
image: us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api:latest # Use the same image as built
83+
image: us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api:latest
6084
service: fns-api-cloud-run
6185
region: us-central1
6286
project_id: ${{ secrets.GOOGLE_PROJECT }}

backend.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from flask import Flask, request, jsonify
33
import subprocess
44
import os
5-
import logging # Import the logging module
6-
import json
5+
import logging
76

87
# --- Flask Setup ---
98
app = Flask(__name__)
@@ -29,14 +28,23 @@ def compile_simulation():
2928
os.remove(SIMULATION_EXECUTABLE)
3029

3130
logger.info("Compiling simulation...")
31+
32+
# Check if main.cpp exists
33+
if not os.path.exists("./src/main.cpp"):
34+
COMPILE_ERROR = {"error": "main.cpp not found", "details": "Ensure main.cpp is in the correct directory."}
35+
logger.error(f"Compilation failed: {COMPILE_ERROR['error']} - Details: {COMPILE_ERROR['details']}")
36+
return False
3237
compile_result = subprocess.run(["g++", "-O3", "-o", SIMULATION_EXECUTABLE, "./src/main.cpp"], capture_output=True, text=True)
3338

39+
# Check if compilation was successful
3440
if compile_result.returncode != 0:
3541
COMPILE_ERROR = {"error": "Compilation failed", "details": compile_result.stderr}
3642
logger.error(f"Compilation failed: {COMPILE_ERROR['error']} - Details: {COMPILE_ERROR['details']}") # Use logger.error for errors
3743
return False
3844

45+
COMPILE_ERROR = None
3946
logger.info("Simulation compiled successfully.")
47+
4048
return True
4149

4250
# --- Run Simulation Endpoint ---
@@ -48,12 +56,10 @@ def run_simulation():
4856
return jsonify(COMPILE_ERROR), 500
4957

5058
if not os.path.exists(SIMULATION_EXECUTABLE):
51-
return jsonify({"error": "Simulation executable not found. Contact developer."}), 400
59+
return jsonify({"error": "Simulation executable not found. Contact developer."}), 500
5260

5361
try:
5462
data = request.get_json()
55-
# if not data:
56-
# return jsonify({"error": "Missing JSON parameters in request body."}), 400
5763

5864
# Parameters, use default values if not provided
5965
algorithm = data.get("algorithm", "FirstFit")

0 commit comments

Comments
 (0)