Commit 4a078db2 authored by Javier Velázquez's avatar Javier Velázquez
Browse files

Script to run tests locally

parent 5c05180a
Loading
Loading
Loading
Loading

scripts/run_tests.sh

0 → 100644
+90 −0
Original line number Diff line number Diff line
#!/bin/bash
# Copyright 2022-2026 ETSI SDG TeraFlowSDN (TFS) (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.

# This file includes contributions from Telefonica Innovación Digital S.L.

IMAGE_NAME="nsc-test"
IMAGE_TAG="local-test"

# Get project root directory
PROJECTDIR=$(pwd)

echo "========================================="
echo " Starting Local Unit Tests & Coverage"
echo "========================================="

# 1. Clean up old test containers
echo ""
echo "Cleaning up any old test containers..."
echo "------------------------------------"
if [ -n "$(docker ps -a -q -f name=$IMAGE_NAME 2>/dev/null)" ]; then
    echo "Stopping and removing existing container '$IMAGE_NAME'..."
    docker stop $IMAGE_NAME >/dev/null 2>&1
    docker rm $IMAGE_NAME >/dev/null 2>&1
fi

# 2. Build the Docker image
echo ""
echo "Building Docker image '$IMAGE_NAME:$IMAGE_TAG'..."
echo "--------------------------------------------------"
docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./Dockerfile .
if [ $? -ne 0 ]; then
    echo "Error: Docker build failed."
    exit 1
fi

# 3. Run the container in detached mode
echo ""
echo "Starting test container '$IMAGE_NAME'..."
echo "--------------------------------------"
docker run --name "$IMAGE_NAME" -d "$IMAGE_NAME:$IMAGE_TAG"
if [ $? -ne 0 ]; then
    echo "Error: Failed to run container."
    exit 1
fi

# Give the container a few seconds to initialize
sleep 3

# 4. Run unit tests using pytest & coverage
echo ""
echo "Running unit tests inside the container..."
echo "-----------------------------------------"
docker exec -i "$IMAGE_NAME" bash -c "coverage run -m pytest --log-level=INFO --verbose"
TEST_EXIT_CODE=$?

# 5. Report coverage
echo ""
echo "Generating Coverage Report..."
echo "----------------------------"
docker exec -i "$IMAGE_NAME" bash -c "coverage report --show-missing"

# 6. Post-test clean-up
echo ""
echo "Post-test clean-up..."
echo "--------------------"
docker stop "$IMAGE_NAME" >/dev/null 2>&1
docker rm "$IMAGE_NAME" >/dev/null 2>&1

echo ""
echo "========================================="
if [ $TEST_EXIT_CODE -eq 0 ]; then
    echo " TESTS PASSED SUCCESSFULLY!"
else
    echo " TESTS FAILED! (Exit code: $TEST_EXIT_CODE)"
fi
echo "========================================="

exit $TEST_EXIT_CODE