From dadecb4f8bd695ebd8fc4f779ca1b487c5b10d9f Mon Sep 17 00:00:00 2001
From: "agbarneo@optaresolutions.com" <agbarneo@optaresolutions.com>
Date: Mon, 28 Oct 2024 16:04:31 +0000
Subject: [PATCH] Added qkd app tests

---
 src/qkd_app/.gitlab-ci.yml                    | 44 +++++++++++++++++++
 .../tests}/test_create_apps.py                |  2 +-
 .../tests}/test_set_new_configuration.py      |  2 +-
 src/tests/tools/mock_qkd_nodes/wsgi.py        |  3 ++
 4 files changed, 49 insertions(+), 2 deletions(-)
 rename src/{device/tests/qkd/unit => qkd_app/tests}/test_create_apps.py (98%)
 rename src/{device/tests/qkd/unit => qkd_app/tests}/test_set_new_configuration.py (98%)

diff --git a/src/qkd_app/.gitlab-ci.yml b/src/qkd_app/.gitlab-ci.yml
index 85ee2b5e0..ec07719aa 100644
--- a/src/qkd_app/.gitlab-ci.yml
+++ b/src/qkd_app/.gitlab-ci.yml
@@ -58,6 +58,50 @@ unit_test app:
     - docker logs $IMAGE_NAME
     - docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=INFO --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/opt/results/${IMAGE_NAME}_report.xml"
     - docker exec -i $IMAGE_NAME bash -c "coverage report --include='${IMAGE_NAME}/*' --show-missing"
+
+    # Mock QKD Nodes Deployment
+    - |
+      echo "Starting stage: deploy_mock_nodes"
+    - pip install flask  # Install Flask to ensure it is available
+    - |
+      for port in 11111 22222 33333; do
+        if lsof -i:$port >/dev/null 2>&1; then
+          echo "Freeing up port $port..."
+          fuser -k $port/tcp
+        fi
+      done
+      MOCK_NODES_DIR="$PWD/src/tests/tools/mock_qkd_nodes"
+      if [ -d "$MOCK_NODES_DIR" ]; then
+        cd "$MOCK_NODES_DIR" || exit
+        ./start.sh &
+        MOCK_NODES_PID=$!
+      else
+        echo "Error: Mock QKD nodes directory '$MOCK_NODES_DIR' not found."
+        exit 1
+      fi
+      echo "Waiting for mock nodes to be up..."
+      RETRY_COUNT=0
+      MAX_RETRIES=15
+      while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
+        if curl -s http://127.0.0.1:11111 > /dev/null && \
+           curl -s http://127.0.0.1:22222 > /dev/null && \
+           curl -s http://127.0.0.1:33333 > /dev/null; then
+            echo "Mock nodes are up!"
+            break
+        else
+            echo "Mock nodes not ready, retrying in 5 seconds..."
+            RETRY_COUNT=$((RETRY_COUNT + 1))
+            sleep 5
+        fi
+      done
+      if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then
+        echo "Error: Mock nodes failed to start after multiple attempts."
+        exit 1
+      fi
+      
+  # Run additional QKD unit tests
+    - docker exec -i $IMAGE_NAME bash -c "pytest --log-level=INFO --verbose $IMAGE_NAME/tests/test_create_apps.py"
+    - docker exec -i $IMAGE_NAME bash -c "pytest --log-level=INFO --verbose $IMAGE_NAME/tests/test_Set_new_configuration.py"
   coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/'
   after_script:
     - docker rm -f $IMAGE_NAME
diff --git a/src/device/tests/qkd/unit/test_create_apps.py b/src/qkd_app/tests/test_create_apps.py
similarity index 98%
rename from src/device/tests/qkd/unit/test_create_apps.py
rename to src/qkd_app/tests/test_create_apps.py
index 4724e5c4a..4d05c0e93 100644
--- a/src/device/tests/qkd/unit/test_create_apps.py
+++ b/src/qkd_app/tests/test_create_apps.py
@@ -14,7 +14,7 @@
 
 import requests
 
-QKD_ADDRESS = '10.0.2.10'
+QKD_ADDRESS = '127.0.0.1'
 QKD_URL     = 'http://{:s}/qkd_app/create_qkd_app'.format(QKD_ADDRESS)
 
 QKD_REQUEST_1 = {
diff --git a/src/device/tests/qkd/unit/test_set_new_configuration.py b/src/qkd_app/tests/test_set_new_configuration.py
similarity index 98%
rename from src/device/tests/qkd/unit/test_set_new_configuration.py
rename to src/qkd_app/tests/test_set_new_configuration.py
index 438e46d74..a042a14df 100644
--- a/src/device/tests/qkd/unit/test_set_new_configuration.py
+++ b/src/qkd_app/tests/test_set_new_configuration.py
@@ -53,7 +53,7 @@ def create_qkd_app(driver, qkdn_id, backing_qkdl_id, client_app_id=None):
         print(f"Sending payload to {driver.address}: {app_payload}")
 
         # Send POST request to create the application
-        response = requests.post(f'http://{driver.address}/app/create_qkd_app', json=app_payload)
+        response = requests.post(f'http://{driver.address}/qkd_app/create_qkd_app', json=app_payload)
         
         # Check if the request was successful (HTTP 2xx)
         response.raise_for_status()
diff --git a/src/tests/tools/mock_qkd_nodes/wsgi.py b/src/tests/tools/mock_qkd_nodes/wsgi.py
index 2a3d48adb..28ba78260 100644
--- a/src/tests/tools/mock_qkd_nodes/wsgi.py
+++ b/src/tests/tools/mock_qkd_nodes/wsgi.py
@@ -183,6 +183,9 @@ def get_side_effect(url):
     module = header_splitted[0]
     assert(module == 'etsi-qkd-sdn-node')
 
+    if ip_port.startswith('127.0.0.1'):
+        ip_port = ip_port.replace('127.0.0.1', current_ip)
+
     tree = {'qkd_node': nodes[ip_port]['node'].copy()}
 
     if len(header_splitted) == 1 or not header_splitted[1]:
-- 
GitLab