Files
ivatar/ivatar/test_telemetry_integration.py

138 lines
5.2 KiB
Python

"""
Tests for OpenTelemetry integration and graceful degradation.
"""
import unittest
from unittest.mock import patch
from django.test import TestCase, RequestFactory
from ivatar.telemetry_utils import (
get_telemetry_decorators,
get_telemetry_metrics,
is_telemetry_available,
)
class TelemetryIntegrationTestCase(TestCase):
"""Test OpenTelemetry integration and graceful degradation"""
def setUp(self):
self.factory = RequestFactory()
def test_telemetry_utils_import(self):
"""Test that telemetry utils can be imported safely"""
# This should work regardless of whether OpenTelemetry is installed
trace_avatar, trace_file, trace_auth = get_telemetry_decorators()
metrics = get_telemetry_metrics()
available = is_telemetry_available()
# All should be callable/usable
self.assertTrue(callable(trace_avatar))
self.assertTrue(callable(trace_file))
self.assertTrue(callable(trace_auth))
self.assertTrue(hasattr(metrics, "record_avatar_generated"))
self.assertIsInstance(available, bool)
def test_decorators_work_as_no_op(self):
"""Test that decorators work even when OpenTelemetry is not available"""
trace_avatar, trace_file, trace_auth = get_telemetry_decorators()
@trace_avatar("test_operation")
def test_function():
return "success"
@trace_file("test_upload")
def test_upload():
return "uploaded"
@trace_auth("test_login")
def test_login():
return "logged_in"
# Functions should work normally
self.assertEqual(test_function(), "success")
self.assertEqual(test_upload(), "uploaded")
self.assertEqual(test_login(), "logged_in")
def test_metrics_work_as_no_op(self):
"""Test that metrics work even when OpenTelemetry is not available"""
metrics = get_telemetry_metrics()
# These should not raise exceptions
metrics.record_avatar_generated(size="80", format_type="png", source="test")
metrics.record_cache_hit(size="80", format_type="png")
metrics.record_cache_miss(size="80", format_type="png")
metrics.record_external_request("test_service", 200)
metrics.record_file_upload(1024, "image/png", True)
def test_telemetry_available_true(self):
"""Test behavior when telemetry is available"""
# This test assumes OpenTelemetry is available
available = is_telemetry_available()
# The actual value depends on whether OpenTelemetry is installed
self.assertIsInstance(available, bool)
def test_views_import_telemetry_safely(self):
"""Test that views can import telemetry utilities safely"""
# This should not raise ImportError
from ivatar.views import avatar_metrics
from ivatar.ivataraccount.views import avatar_metrics as account_metrics
# Both should have the required methods
self.assertTrue(hasattr(avatar_metrics, "record_avatar_generated"))
self.assertTrue(hasattr(account_metrics, "record_file_upload"))
class MockTelemetryTestCase(TestCase):
"""Test with mocked OpenTelemetry to verify actual instrumentation calls"""
def setUp(self):
self.factory = RequestFactory()
@patch("ivatar.telemetry_utils.avatar_metrics")
def test_avatar_generation_metrics(self, mock_metrics):
"""Test that avatar generation records metrics"""
# Test that metrics would be called (we can't easily test the full flow)
mock_metrics.record_avatar_generated.assert_not_called() # Not called yet
# Call the metric recording directly to test the interface
mock_metrics.record_avatar_generated(
size="80", format_type="png", source="test"
)
mock_metrics.record_avatar_generated.assert_called_once_with(
size="80", format_type="png", source="test"
)
@patch("ivatar.telemetry_utils.avatar_metrics")
def test_file_upload_metrics(self, mock_metrics):
"""Test that file uploads record metrics"""
# Test the metric recording interface
mock_metrics.record_file_upload(1024, "image/png", True)
mock_metrics.record_file_upload.assert_called_once_with(1024, "image/png", True)
@patch("ivatar.telemetry_utils.avatar_metrics")
def test_external_request_metrics(self, mock_metrics):
"""Test that external requests record metrics"""
# Test the metric recording interface
mock_metrics.record_external_request("gravatar", 200)
mock_metrics.record_external_request.assert_called_once_with("gravatar", 200)
@patch("ivatar.telemetry_utils.avatar_metrics")
def test_cache_metrics(self, mock_metrics):
"""Test that cache operations record metrics"""
# Test the metric recording interface
mock_metrics.record_cache_hit(size="80", format_type="png")
mock_metrics.record_cache_miss(size="80", format_type="png")
mock_metrics.record_cache_hit.assert_called_once_with(
size="80", format_type="png"
)
mock_metrics.record_cache_miss.assert_called_once_with(
size="80", format_type="png"
)
if __name__ == "__main__":
unittest.main()