|
| 1 | +import logging |
| 2 | +import unittest |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +from codecarbon.output_methods.emissions_data import EmissionsData |
| 6 | +from codecarbon.output_methods.logger import GoogleCloudLoggerOutput, LoggerOutput |
| 7 | + |
| 8 | + |
| 9 | +class TestLoggerOutput(unittest.TestCase): |
| 10 | + def setUp(self): |
| 11 | + self.emissions_data = EmissionsData( |
| 12 | + timestamp="2023-01-01T00:00:00", |
| 13 | + project_name="test_project", |
| 14 | + run_id="test_run_id", |
| 15 | + experiment_id="test_experiment_id", |
| 16 | + duration=10, |
| 17 | + emissions=0.5, |
| 18 | + emissions_rate=0.05, |
| 19 | + cpu_power=20, |
| 20 | + gpu_power=30, |
| 21 | + ram_power=5, |
| 22 | + cpu_energy=200, |
| 23 | + gpu_energy=300, |
| 24 | + ram_energy=50, |
| 25 | + energy_consumed=550, |
| 26 | + water_consumed=0.1, |
| 27 | + country_name="Testland", |
| 28 | + country_iso_code="TS", |
| 29 | + region="Test Region", |
| 30 | + cloud_provider="Test Cloud", |
| 31 | + cloud_region="test-cloud-1", |
| 32 | + os="TestOS", |
| 33 | + python_version="3.8", |
| 34 | + codecarbon_version="2.0", |
| 35 | + cpu_count=4, |
| 36 | + cpu_model="Test CPU", |
| 37 | + gpu_count=1, |
| 38 | + gpu_model="Test GPU", |
| 39 | + longitude=0, |
| 40 | + latitude=0, |
| 41 | + ram_total_size=16, |
| 42 | + tracking_mode="machine", |
| 43 | + on_cloud="true", |
| 44 | + pue=1.5, |
| 45 | + wue=0.5, |
| 46 | + ) |
| 47 | + self.mock_logger = MagicMock() |
| 48 | + |
| 49 | + def test_logger_output_success(self): |
| 50 | + logger_output = LoggerOutput(self.mock_logger, logging.WARNING) |
| 51 | + logger_output.out(self.emissions_data, None) |
| 52 | + |
| 53 | + self.mock_logger.log.assert_called_once() |
| 54 | + |
| 55 | + @patch("codecarbon.output_methods.logger.logger.error") |
| 56 | + def test_logger_output_exception(self, mock_error_logger): |
| 57 | + self.mock_logger.log.side_effect = Exception("Test exception") |
| 58 | + logger_output = LoggerOutput(self.mock_logger) |
| 59 | + logger_output.out(self.emissions_data, None) |
| 60 | + |
| 61 | + mock_error_logger.assert_called_once() |
| 62 | + |
| 63 | + def test_logger_live_out_calls_out(self): |
| 64 | + logger_output = LoggerOutput(self.mock_logger) |
| 65 | + logger_output.live_out(self.emissions_data, None) |
| 66 | + |
| 67 | + self.mock_logger.log.assert_called_once() |
| 68 | + |
| 69 | + |
| 70 | +class TestGoogleCloudLoggerOutput(unittest.TestCase): |
| 71 | + def setUp(self): |
| 72 | + self.emissions_data = EmissionsData( |
| 73 | + timestamp="2023-01-01T00:00:00", |
| 74 | + project_name="test_project", |
| 75 | + run_id="test_run_id", |
| 76 | + experiment_id="test_experiment_id", |
| 77 | + duration=10, |
| 78 | + emissions=0.5, |
| 79 | + emissions_rate=0.05, |
| 80 | + cpu_power=20, |
| 81 | + gpu_power=30, |
| 82 | + ram_power=5, |
| 83 | + cpu_energy=200, |
| 84 | + gpu_energy=300, |
| 85 | + ram_energy=50, |
| 86 | + energy_consumed=550, |
| 87 | + water_consumed=0.1, |
| 88 | + country_name="Testland", |
| 89 | + country_iso_code="TS", |
| 90 | + region="Test Region", |
| 91 | + cloud_provider="Test Cloud", |
| 92 | + cloud_region="test-cloud-1", |
| 93 | + os="TestOS", |
| 94 | + python_version="3.8", |
| 95 | + codecarbon_version="2.0", |
| 96 | + cpu_count=4, |
| 97 | + cpu_model="Test CPU", |
| 98 | + gpu_count=1, |
| 99 | + gpu_model="Test GPU", |
| 100 | + longitude=0, |
| 101 | + latitude=0, |
| 102 | + ram_total_size=16, |
| 103 | + tracking_mode="machine", |
| 104 | + on_cloud="true", |
| 105 | + pue=1.5, |
| 106 | + wue=0.5, |
| 107 | + ) |
| 108 | + self.mock_logger = MagicMock() |
| 109 | + |
| 110 | + def test_google_cloud_logger_output_success(self): |
| 111 | + logger_output = GoogleCloudLoggerOutput(self.mock_logger, logging.WARNING) |
| 112 | + logger_output.out(self.emissions_data, None) |
| 113 | + |
| 114 | + self.mock_logger.log_struct.assert_called_once() |
| 115 | + |
| 116 | + @patch("codecarbon.output_methods.logger.logger.error") |
| 117 | + def test_google_cloud_logger_output_exception(self, mock_error_logger): |
| 118 | + self.mock_logger.log_struct.side_effect = Exception("Test exception") |
| 119 | + logger_output = GoogleCloudLoggerOutput(self.mock_logger) |
| 120 | + logger_output.out(self.emissions_data, None) |
| 121 | + |
| 122 | + mock_error_logger.assert_called_once() |
| 123 | + |
| 124 | + def test_google_cloud_logger_live_out_calls_out(self): |
| 125 | + logger_output = GoogleCloudLoggerOutput(self.mock_logger) |
| 126 | + logger_output.live_out(self.emissions_data, None) |
| 127 | + |
| 128 | + self.mock_logger.log_struct.assert_called_once() |
0 commit comments