From 871f4de78be581f938a98b040fab1049e45e3652 Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Fri, 15 Mar 2024 21:02:26 +0000
Subject: [PATCH] ECOC'22 test:

- Updated and containerized test
- Adapted CI/CD pipeline
- Activated CI/CD pipeline
---
 src/tests/.gitlab-ci.yml                      |   2 +-
 src/tests/ecoc22/.gitlab-ci.yml               |  62 +++++++++--
 src/tests/ecoc22/Dockerfile                   | 101 ++++++++++++++++++
 src/tests/ecoc22/requirements.in              |  15 +++
 .../ecoc22/tests/test_functional_bootstrap.py |   4 +-
 .../ecoc22/tests/test_functional_cleanup.py   |   4 +-
 .../tests/test_functional_create_service.py   |   4 +-
 .../tests/test_functional_delete_service.py   |   4 +-
 8 files changed, 178 insertions(+), 18 deletions(-)
 create mode 100644 src/tests/ecoc22/Dockerfile
 create mode 100644 src/tests/ecoc22/requirements.in

diff --git a/src/tests/.gitlab-ci.yml b/src/tests/.gitlab-ci.yml
index d48456b83..41b8bb36c 100644
--- a/src/tests/.gitlab-ci.yml
+++ b/src/tests/.gitlab-ci.yml
@@ -16,7 +16,7 @@
 include:
   - local: '/src/tests/ofc22/.gitlab-ci.yml'
   #- local: '/src/tests/oeccpsc22/.gitlab-ci.yml'
-  #- local: '/src/tests/ecoc22/.gitlab-ci.yml'
+  - local: '/src/tests/ecoc22/.gitlab-ci.yml'
   #- local: '/src/tests/nfvsdn22/.gitlab-ci.yml'
   #- local: '/src/tests/ofc23/.gitlab-ci.yml'
   #- local: '/src/tests/ofc24/.gitlab-ci.yml'
diff --git a/src/tests/ecoc22/.gitlab-ci.yml b/src/tests/ecoc22/.gitlab-ci.yml
index af0a7669e..8bf2929e6 100644
--- a/src/tests/ecoc22/.gitlab-ci.yml
+++ b/src/tests/ecoc22/.gitlab-ci.yml
@@ -13,32 +13,76 @@
 # limitations under the License.
 
 # Build, tag, and push the Docker image to the GitLab Docker registry
-end2end ecoc22:
+build ecoc22:
+  variables:
+    TEST_NAME: 'ecoc22'
+  stage: build
+  before_script:
+    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
+  script:
+    - docker build -t "${TEST_NAME}:latest" -f ./src/tests/${TEST_NAME}/Dockerfile .
+    - docker tag "${TEST_NAME}:latest" "$CI_REGISTRY_IMAGE/${TEST_NAME}:latest"
+    - docker push "$CI_REGISTRY_IMAGE/${TEST_NAME}:latest"
+  after_script:
+    - docker images --filter="dangling=true" --quiet | xargs -r docker rmi
+  rules:
+    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
+    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
+    - changes:
+      - src/common/**/*.py
+      - proto/*.proto
+      - src/tests/${TEST_NAME}/**/*.{py,in,sh,yml}
+      - src/tests/${TEST_NAME}/Dockerfile
+      - .gitlab-ci.yml
+
+# Deploy TeraFlowSDN and Execute end-2-end test
+end2end_test ecoc22:
   variables:
     TEST_NAME: 'ecoc22'
   stage: end2end_test
   before_script:
     - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
   script:
+    # Download Docker image to run the test
+    - docker pull "${CI_REGISTRY_IMAGE}/${TEST_NAME}:latest"
+
+    # Check MicroK8s is ready
+    - microk8s status --wait-ready
+    - kubectl get pods --all-namespaces
+
+    # Configure TeraFlowSDN deployment
     - source src/tests/${TEST_NAME}/deploy_specs.sh
-    - export TFS_REGISTRY_IMAGES="labs.etsi.org:5050/tfs/controller"
+    - export TFS_REGISTRY_IMAGES="${CI_REGISTRY_IMAGE}"
     - export TFS_SKIP_BUILD="YES"
     - export TFS_IMAGE_TAG="latest"
+    - echo "TFS_REGISTRY_IMAGES=${CI_REGISTRY_IMAGE}"
+
+    # Deploy TeraFlowSDN
     - ./deploy/crdb.sh
     - ./deploy/nats.sh
     - ./deploy/qdb.sh
     - ./deploy/expose_dashboard.sh
     - ./deploy/tfs.sh
     - ./deploy/show.sh
+    
+    # Wait for Context to be subscribed to NATS
     - while ! kubectl --namespace $TFS_K8S_NAMESPACE logs deployment/contextservice -c server 2>&1 | grep -q 'Subscriber is Ready? True'; do sleep 1; done
     - kubectl --namespace $TFS_K8S_NAMESPACE logs deployment/contextservice -c server
-    # Run the tests
-    - source tfs_runtime_env_vars.sh
-    - pytest --verbose --log-level=INFO ./src/tests/${TEST_NAME}/tests/test_functional_bootstrap.py --junitxml=./src/tests/${TEST_NAME}/report_bootstrap.xml
-    - pytest --verbose --log-level=INFO ./src/tests/${TEST_NAME}/tests/test_functional_create_service.py --junitxml=./src/tests/${TEST_NAME}/report_create_service.xml
-    - pytest --verbose --log-level=INFO ./src/tests/${TEST_NAME}/tests/test_functional_delete_service.py --junitxml=./src/tests/${TEST_NAME}/report_delete_service.xml
-    - pytest --verbose --log-level=INFO ./src/tests/${TEST_NAME}/tests/test_functional_cleanup.py --junitxml=./src/tests/${TEST_NAME}/report_cleanup.xml
+
+    # Run end-to-end tests
+    - if docker ps -a | grep ${TEST_NAME}; then docker rm -f ${TEST_NAME}; fi
+    - docker create --name ${TEST_NAME} --network=host -v "$PWD/src/tests/${TEST_NAME}:/opt/results" $CI_REGISTRY_IMAGE/${TEST_NAME}:latest
+    - docker cp ./tfs_runtime_env_vars.sh ${TEST_NAME}:/var/teraflow/
+    - docker start ${TEST_NAME}
+    - docker wait ${TEST_NAME}
+    - docker logs ${TEST_NAME}
+    #- source tfs_runtime_env_vars.sh
+    #- pytest --verbose --log-level=INFO ./src/tests/${TEST_NAME}/tests/test_functional_bootstrap.py --junitxml=./src/tests/${TEST_NAME}/report_bootstrap.xml
+    #- pytest --verbose --log-level=INFO ./src/tests/${TEST_NAME}/tests/test_functional_create_service.py --junitxml=./src/tests/${TEST_NAME}/report_create_service.xml
+    #- pytest --verbose --log-level=INFO ./src/tests/${TEST_NAME}/tests/test_functional_delete_service.py --junitxml=./src/tests/${TEST_NAME}/report_delete_service.xml
+    #- pytest --verbose --log-level=INFO ./src/tests/${TEST_NAME}/tests/test_functional_cleanup.py --junitxml=./src/tests/${TEST_NAME}/report_cleanup.xml
   after_script:
+    - if docker ps -a | grep ${TEST_NAME}; then docker rm -f ${TEST_NAME}; fi
     - docker images --filter="dangling=true" --quiet | xargs -r docker rmi
   #coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/'
   rules:
@@ -47,4 +91,4 @@ end2end ecoc22:
   artifacts:
       when: always
       reports:
-        junit: ./src/tests/${TEST_NAME}/report*.xml
+        junit: ./src/tests/${TEST_NAME}/report_*.xml
diff --git a/src/tests/ecoc22/Dockerfile b/src/tests/ecoc22/Dockerfile
new file mode 100644
index 000000000..3ac134a38
--- /dev/null
+++ b/src/tests/ecoc22/Dockerfile
@@ -0,0 +1,101 @@
+# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+FROM python:3.9-slim
+
+# Install dependencies
+RUN apt-get --yes --quiet --quiet update && \
+    apt-get --yes --quiet --quiet install wget g++ git && \
+    rm -rf /var/lib/apt/lists/*
+
+# Set Python to show logs as they occur
+ENV PYTHONUNBUFFERED=0
+
+# Download the gRPC health probe
+RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \
+    wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
+    chmod +x /bin/grpc_health_probe
+
+# Get generic Python packages
+RUN python3 -m pip install --upgrade pip
+RUN python3 -m pip install --upgrade setuptools wheel
+RUN python3 -m pip install --upgrade pip-tools
+
+# Get common Python packages
+# Note: this step enables sharing the previous Docker build steps among all the Python components
+WORKDIR /var/teraflow
+COPY common_requirements.in common_requirements.in
+RUN pip-compile --quiet --output-file=common_requirements.txt common_requirements.in
+RUN python3 -m pip install -r common_requirements.txt
+
+# Add common files into working directory
+WORKDIR /var/teraflow/common
+COPY src/common/. ./
+RUN rm -rf proto
+
+# Create proto sub-folder, copy .proto files, and generate Python code
+RUN mkdir -p /var/teraflow/common/proto
+WORKDIR /var/teraflow/common/proto
+RUN touch __init__.py
+COPY proto/*.proto ./
+RUN python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. *.proto
+RUN rm *.proto
+RUN find . -type f -exec sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' {} \;
+
+# Create component sub-folders, get specific Python packages
+RUN mkdir -p /var/teraflow/tests/ecoc22
+WORKDIR /var/teraflow/tests/ecoc22
+COPY src/tests/ecoc22/requirements.in requirements.in
+RUN pip-compile --quiet --output-file=requirements.txt requirements.in
+RUN python3 -m pip install -r requirements.txt
+
+# Add component files into working directory
+WORKDIR /var/teraflow
+COPY src/__init__.py ./__init__.py
+COPY src/common/*.py ./common/
+COPY src/common/tests/. ./common/tests/
+COPY src/common/tools/. ./common/tools/
+COPY src/context/__init__.py context/__init__.py
+COPY src/context/client/. context/client/
+COPY src/device/__init__.py device/__init__.py
+COPY src/device/client/. device/client/
+COPY src/monitoring/__init__.py monitoring/__init__.py
+COPY src/monitoring/client/. monitoring/client/
+COPY src/monitoring/__init__.py monitoring/__init__.py
+COPY src/monitoring/client/. monitoring/client/
+COPY src/e2e_orchestrator/__init__.py e2e_orchestrator/__init__.py
+COPY src/e2e_orchestrator/client/. e2e_orchestrator/client/
+COPY src/service/__init__.py service/__init__.py
+COPY src/service/client/. service/client/
+COPY src/slice/__init__.py slice/__init__.py
+COPY src/slice/client/. slice/client/
+COPY src/tests/*.py ./tests/
+COPY src/tests/ecoc22/__init__.py ./tests/ecoc22/__init__.py
+COPY src/tests/ecoc22/descriptors_emulated.json ./tests/ecoc22/descriptors_emulated.json
+COPY src/tests/ecoc22/tests/. ./tests/ecoc22/tests/
+COPY src/tests/tools/. ./tests/tools/
+
+RUN tee ./run_tests.sh <<EOF
+#!/bin/bash
+source /var/teraflow/tfs_runtime_env_vars.sh
+export PYTHONPATH=/var/teraflow
+pytest --verbose --log-level=INFO /var/teraflow/tests/ecoc22/tests/test_functional_bootstrap.py      --junitxml=/opt/results/report_bootstrap.xml
+pytest --verbose --log-level=INFO /var/teraflow/tests/ecoc22/tests/test_functional_create_service.py --junitxml=/opt/results/report_create_service.xml
+pytest --verbose --log-level=INFO /var/teraflow/tests/ecoc22/tests/test_functional_delete_service.py --junitxml=/opt/results/report_delete_service.xml
+pytest --verbose --log-level=INFO /var/teraflow/tests/ecoc22/tests/test_functional_cleanup.py        --junitxml=/opt/results/report_cleanup.xml
+EOF
+RUN chmod ug+x ./run_tests.sh
+
+# Run the tests
+ENTRYPOINT ["./run_tests.sh"]
diff --git a/src/tests/ecoc22/requirements.in b/src/tests/ecoc22/requirements.in
new file mode 100644
index 000000000..30b11e653
--- /dev/null
+++ b/src/tests/ecoc22/requirements.in
@@ -0,0 +1,15 @@
+# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+requests==2.27.*
diff --git a/src/tests/ecoc22/tests/test_functional_bootstrap.py b/src/tests/ecoc22/tests/test_functional_bootstrap.py
index 05691d0b2..60df8d8a2 100644
--- a/src/tests/ecoc22/tests/test_functional_bootstrap.py
+++ b/src/tests/ecoc22/tests/test_functional_bootstrap.py
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging
+import logging, os, time
 from common.Constants import DEFAULT_CONTEXT_NAME
 from common.proto.context_pb2 import ContextId
 from common.tools.descriptor.Loader import DescriptorLoader, check_descriptor_load_results, validate_empty_scenario
@@ -24,7 +24,7 @@ from tests.Fixtures import context_client, device_client # pylint: disable=unuse
 LOGGER = logging.getLogger(__name__)
 LOGGER.setLevel(logging.DEBUG)
 
-DESCRIPTOR_FILE = 'ecoc22/descriptors_emulated.json'
+DESCRIPTOR_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'descriptors_emulated.json')
 ADMIN_CONTEXT_ID = ContextId(**json_context_id(DEFAULT_CONTEXT_NAME))
 
 def test_scenario_bootstrap(
diff --git a/src/tests/ecoc22/tests/test_functional_cleanup.py b/src/tests/ecoc22/tests/test_functional_cleanup.py
index 088c19799..b1396f49a 100644
--- a/src/tests/ecoc22/tests/test_functional_cleanup.py
+++ b/src/tests/ecoc22/tests/test_functional_cleanup.py
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging
+import logging, os
 from common.Constants import DEFAULT_CONTEXT_NAME
 from common.proto.context_pb2 import ContextId
 from common.tools.descriptor.Loader import DescriptorLoader, validate_empty_scenario
@@ -24,7 +24,7 @@ from tests.Fixtures import context_client, device_client    # pylint: disable=un
 LOGGER = logging.getLogger(__name__)
 LOGGER.setLevel(logging.DEBUG)
 
-DESCRIPTOR_FILE = 'ecoc22/descriptors_emulated.json'
+DESCRIPTOR_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'descriptors_emulated.json')
 ADMIN_CONTEXT_ID = ContextId(**json_context_id(DEFAULT_CONTEXT_NAME))
 
 def test_scenario_cleanup(
diff --git a/src/tests/ecoc22/tests/test_functional_create_service.py b/src/tests/ecoc22/tests/test_functional_create_service.py
index dab9c7eb1..b4976f15d 100644
--- a/src/tests/ecoc22/tests/test_functional_create_service.py
+++ b/src/tests/ecoc22/tests/test_functional_create_service.py
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging
+import logging, os
 from common.Constants import DEFAULT_CONTEXT_NAME
 from common.proto.context_pb2 import ContextId, ServiceTypeEnum
 from common.tools.descriptor.Loader import DescriptorLoader
@@ -27,7 +27,7 @@ from .Objects import WIM_SERVICE_CONNECTION_POINTS, WIM_SERVICE_TYPE
 LOGGER = logging.getLogger(__name__)
 LOGGER.setLevel(logging.DEBUG)
 
-DESCRIPTOR_FILE = 'ecoc22/descriptors_emulated.json'
+DESCRIPTOR_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'descriptors_emulated.json')
 ADMIN_CONTEXT_ID = ContextId(**json_context_id(DEFAULT_CONTEXT_NAME))
 
 def test_service_creation(context_client : ContextClient, osm_wim : MockOSM): # pylint: disable=redefined-outer-name
diff --git a/src/tests/ecoc22/tests/test_functional_delete_service.py b/src/tests/ecoc22/tests/test_functional_delete_service.py
index 710e1a817..d55f3acc0 100644
--- a/src/tests/ecoc22/tests/test_functional_delete_service.py
+++ b/src/tests/ecoc22/tests/test_functional_delete_service.py
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging
+import logging, os
 from common.Constants import DEFAULT_CONTEXT_NAME
 from common.proto.context_pb2 import ContextId, ServiceTypeEnum
 from common.tools.descriptor.Loader import DescriptorLoader
@@ -26,7 +26,7 @@ from .Fixtures import osm_wim
 LOGGER = logging.getLogger(__name__)
 LOGGER.setLevel(logging.DEBUG)
 
-DESCRIPTOR_FILE = 'ecoc22/descriptors_emulated.json'
+DESCRIPTOR_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'descriptors_emulated.json')
 ADMIN_CONTEXT_ID = ContextId(**json_context_id(DEFAULT_CONTEXT_NAME))
 
 def test_service_removal(context_client : ContextClient, osm_wim : MockOSM): # pylint: disable=redefined-outer-name
-- 
GitLab