From 6f205ccad98476a3d07d78e9ccbcc76e6b993434 Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Thu, 16 Oct 2025 17:58:33 +0200 Subject: [PATCH] Refactor test scripts into scripts/ directory and improve CI - Move run_tests_no_ot.sh and run_tests_with_ot.sh to scripts/ directory - Create scripts/run_tests_with_coverage.py for coverage measurement - Update CI to use scripts from scripts/ directory - Eliminate code duplication between shell scripts and CI configuration - Use Python script with coverage run for proper coverage measurement --- .gitlab-ci.yml | 4 +-- scripts/run_tests_no_ot.sh | 30 ++++++++++++++++++ scripts/run_tests_with_coverage.py | 50 ++++++++++++++++++++++++++++++ scripts/run_tests_with_ot.sh | 33 ++++++++++++++++++++ 4 files changed, 115 insertions(+), 2 deletions(-) create mode 100755 scripts/run_tests_no_ot.sh create mode 100755 scripts/run_tests_with_coverage.py create mode 100755 scripts/run_tests_with_ot.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b8d4e74..25536a5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -43,7 +43,7 @@ test_without_opentelemetry: - python manage.py sqldsn - python manage.py collectstatic --noinput - echo "Running tests without OpenTelemetry..." - - python manage.py test ivatar.ivataraccount.test_auth ivatar.ivataraccount.test_views ivatar.ivataraccount.test_views_bluesky ivatar.test_auxiliary ivatar.test_file_security ivatar.test_static_pages ivatar.test_utils ivatar.test_views ivatar.test_views_stats ivatar.tools.test_views ivatar.test_wsgi -v2 + - ./scripts/run_tests_no_ot.sh # Test with OpenTelemetry enabled and measure coverage test_with_opentelemetry_and_coverage: @@ -83,7 +83,7 @@ test_with_opentelemetry_and_coverage: - python manage.py sqldsn - python manage.py collectstatic --noinput - echo "Running tests with OpenTelemetry enabled and measuring coverage..." - - coverage run --source . manage.py test ivatar.ivataraccount.test_auth ivatar.ivataraccount.test_views ivatar.ivataraccount.test_views_bluesky ivatar.test_auxiliary ivatar.test_file_security ivatar.test_opentelemetry ivatar.test_static_pages ivatar.test_utils ivatar.test_views ivatar.test_views_stats ivatar.tools.test_views ivatar.test_wsgi -v2 + - coverage run --source . scripts/run_tests_with_coverage.py - coverage report --fail-under=70 - coverage html artifacts: diff --git a/scripts/run_tests_no_ot.sh b/scripts/run_tests_no_ot.sh new file mode 100755 index 0000000..4720101 --- /dev/null +++ b/scripts/run_tests_no_ot.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# Run tests without OpenTelemetry enabled (default mode) +# This is the default test mode for most users + +set -e + +echo "Running tests without OpenTelemetry (default mode)..." +echo "=====================================================" + +# Ensure OpenTelemetry is disabled +export ENABLE_OPENTELEMETRY=false +export OTEL_ENABLED=false + +# Run Django tests excluding OpenTelemetry-specific tests +python3 manage.py test \ + ivatar.ivataraccount.test_auth \ + ivatar.ivataraccount.test_views \ + ivatar.ivataraccount.test_views_bluesky \ + ivatar.test_auxiliary \ + ivatar.test_file_security \ + ivatar.test_static_pages \ + ivatar.test_utils \ + ivatar.test_views \ + ivatar.test_views_stats \ + ivatar.tools.test_views \ + ivatar.test_wsgi \ + -v2 + +echo "" +echo "Tests completed successfully (OpenTelemetry disabled)" diff --git a/scripts/run_tests_with_coverage.py b/scripts/run_tests_with_coverage.py new file mode 100755 index 0000000..a5f3ed9 --- /dev/null +++ b/scripts/run_tests_with_coverage.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +""" +Run tests with OpenTelemetry enabled and coverage measurement. +This script is designed to be used with 'coverage run' command. +""" + +import os +import subprocess +import sys + +def main(): + # Enable OpenTelemetry + os.environ['ENABLE_OPENTELEMETRY'] = 'true' + os.environ['OTEL_ENABLED'] = 'true' + os.environ['OTEL_SERVICE_NAME'] = 'ivatar-test' + os.environ['OTEL_ENVIRONMENT'] = 'test' + + print("Running tests with OpenTelemetry enabled...") + print("==========================================") + + # Test modules to run (including OpenTelemetry-specific tests) + test_modules = [ + 'ivatar.ivataraccount.test_auth', + 'ivatar.ivataraccount.test_views', + 'ivatar.ivataraccount.test_views_bluesky', + 'ivatar.test_auxiliary', + 'ivatar.test_file_security', + 'ivatar.test_opentelemetry', + 'ivatar.test_static_pages', + 'ivatar.test_utils', + 'ivatar.test_views', + 'ivatar.test_views_stats', + 'ivatar.tools.test_views', + 'ivatar.test_wsgi', + ] + + # Run Django tests + cmd = ['python3', 'manage.py', 'test'] + test_modules + ['-v2'] + + try: + result = subprocess.run(cmd, check=True) + print("") + print("Tests completed successfully (OpenTelemetry enabled)") + return result.returncode + except subprocess.CalledProcessError as e: + print(f"Tests failed with exit code {e.returncode}") + return e.returncode + +if __name__ == '__main__': + sys.exit(main()) diff --git a/scripts/run_tests_with_ot.sh b/scripts/run_tests_with_ot.sh new file mode 100755 index 0000000..63de521 --- /dev/null +++ b/scripts/run_tests_with_ot.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# Run tests with OpenTelemetry enabled +# This is used in CI to test OpenTelemetry functionality + +set -e + +echo "Running tests with OpenTelemetry enabled..." +echo "==========================================" + +# Enable OpenTelemetry +export ENABLE_OPENTELEMETRY=true +export OTEL_ENABLED=true +export OTEL_SERVICE_NAME=ivatar-test +export OTEL_ENVIRONMENT=test + +# Run Django tests including OpenTelemetry-specific tests +python3 manage.py test \ + ivatar.ivataraccount.test_auth \ + ivatar.ivataraccount.test_views \ + ivatar.ivataraccount.test_views_bluesky \ + ivatar.test_auxiliary \ + ivatar.test_file_security \ + ivatar.test_opentelemetry \ + ivatar.test_static_pages \ + ivatar.test_utils \ + ivatar.test_views \ + ivatar.test_views_stats \ + ivatar.tools.test_views \ + ivatar.test_wsgi \ + -v2 + +echo "" +echo "Tests completed successfully (OpenTelemetry enabled)"