|
| 1 | +"""Unit tests for CPUNormalization.getCPUTime()""" |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from DIRAC import S_OK, S_ERROR |
| 5 | + |
| 6 | + |
| 7 | +@patch("DIRAC.WorkloadManagementSystem.Client.CPUNormalization.TimeLeft") |
| 8 | +@patch("DIRAC.WorkloadManagementSystem.Client.CPUNormalization.gConfig") |
| 9 | +class TestGetCPUTime: |
| 10 | + """Tests for getCPUTime() fallback chain.""" |
| 11 | + |
| 12 | + def _import_getCPUTime(self): |
| 13 | + from DIRAC.WorkloadManagementSystem.Client.CPUNormalization import getCPUTime |
| 14 | + |
| 15 | + return getCPUTime |
| 16 | + |
| 17 | + def test_from_config(self, mock_gConfig, mock_TimeLeft): |
| 18 | + """Primary path: CPUTimeLeft is in config (written by JobAgent).""" |
| 19 | + mock_gConfig.getValue.side_effect = lambda key, default=0: { |
| 20 | + "/LocalSite/CPUTimeLeft": 50000, # HS06*s |
| 21 | + "/LocalSite/CPUNormalizationFactor": 10.0, |
| 22 | + }.get(key, default) |
| 23 | + |
| 24 | + result = self._import_getCPUTime()(cpuNormalizationFactor=10.0) |
| 25 | + |
| 26 | + # 50000 / 10.0 = 5000 seconds |
| 27 | + assert result == 5000 |
| 28 | + # TimeLeft should NOT be instantiated since config had a value |
| 29 | + mock_TimeLeft.assert_not_called() |
| 30 | + |
| 31 | + def test_from_batch_system(self, mock_gConfig, mock_TimeLeft): |
| 32 | + """Bootstrap path: no CPUTimeLeft in config, falls back to batch system.""" |
| 33 | + mock_gConfig.getValue.side_effect = lambda key, default=0: { |
| 34 | + "/LocalSite/CPUTimeLeft": 0, # not set yet |
| 35 | + "/LocalSite/CPUNormalizationFactor": 10.0, |
| 36 | + }.get(key, default) |
| 37 | + mock_TimeLeft.return_value.getTimeLeft.return_value = S_OK(30000) # HS06*s |
| 38 | + |
| 39 | + result = self._import_getCPUTime()(cpuNormalizationFactor=10.0) |
| 40 | + |
| 41 | + # 30000 / 10.0 = 3000 seconds |
| 42 | + assert result == 3000 |
| 43 | + mock_TimeLeft.return_value.getTimeLeft.assert_called_once() |
| 44 | + |
| 45 | + def test_from_queue_cs(self, mock_gConfig, mock_TimeLeft): |
| 46 | + """Fallback: batch system fails, uses queue maxCPUTime from CS.""" |
| 47 | + mock_TimeLeft.return_value.getTimeLeft.return_value = S_ERROR("No batch info") |
| 48 | + |
| 49 | + config_values = { |
| 50 | + "/LocalSite/CPUTimeLeft": 0, |
| 51 | + "/LocalSite/GridCE": "ce.example.com", |
| 52 | + "/LocalSite/CEQueue": "default", |
| 53 | + "/LocalSite/Site": "LCG.Example.com", |
| 54 | + } |
| 55 | + |
| 56 | + def mock_getValue(key, default=0): |
| 57 | + if key in config_values: |
| 58 | + return config_values[key] |
| 59 | + # maxCPUTime in minutes |
| 60 | + if "maxCPUTime" in key: |
| 61 | + return 120.0 # 120 minutes |
| 62 | + return default |
| 63 | + |
| 64 | + mock_gConfig.getValue.side_effect = mock_getValue |
| 65 | + |
| 66 | + with patch( |
| 67 | + "DIRAC.WorkloadManagementSystem.Client.CPUNormalization.getQueueInfo", |
| 68 | + return_value=S_OK( |
| 69 | + {"QueueCSSection": "/Resources/Sites/LCG/LCG.Example.com/CEs/ce.example.com/Queues/default"} |
| 70 | + ), |
| 71 | + ): |
| 72 | + result = self._import_getCPUTime()(cpuNormalizationFactor=10.0) |
| 73 | + |
| 74 | + # 120 minutes * 60 = 7200 seconds |
| 75 | + assert result == 7200 |
| 76 | + |
| 77 | + def test_fallback_max_cpu_time(self, mock_gConfig, mock_TimeLeft): |
| 78 | + """Last resort: everything fails, uses /Resources/Computing/CEDefaults/MaxCPUTime.""" |
| 79 | + mock_TimeLeft.return_value.getTimeLeft.return_value = S_ERROR("No batch info") |
| 80 | + |
| 81 | + config_values = { |
| 82 | + "/LocalSite/CPUTimeLeft": 0, |
| 83 | + "/LocalSite/GridCE": "ce.example.com", |
| 84 | + "/LocalSite/CEQueue": "default", |
| 85 | + "/LocalSite/Site": "LCG.Example.com", |
| 86 | + "/Resources/Computing/CEDefaults/MaxCPUTime": 86400, |
| 87 | + } |
| 88 | + |
| 89 | + def mock_getValue(key, default=0): |
| 90 | + if key in config_values: |
| 91 | + return config_values[key] |
| 92 | + return default |
| 93 | + |
| 94 | + mock_gConfig.getValue.side_effect = mock_getValue |
| 95 | + |
| 96 | + with patch( |
| 97 | + "DIRAC.WorkloadManagementSystem.Client.CPUNormalization.getQueueInfo", |
| 98 | + return_value=S_OK( |
| 99 | + {"QueueCSSection": "/Resources/Sites/LCG/LCG.Example.com/CEs/ce.example.com/Queues/default"} |
| 100 | + ), |
| 101 | + ): |
| 102 | + result = self._import_getCPUTime()(cpuNormalizationFactor=10.0) |
| 103 | + |
| 104 | + # maxCPUTime from queue returned 0, so falls through to CEDefaults/MaxCPUTime |
| 105 | + assert result == 86400 |
| 106 | + |
| 107 | + def test_hardcoded_fallback(self, mock_gConfig, mock_TimeLeft): |
| 108 | + """Absolute last resort: no CS default either, returns 9999999.""" |
| 109 | + mock_TimeLeft.return_value.getTimeLeft.return_value = S_ERROR("No batch info") |
| 110 | + |
| 111 | + config_values = { |
| 112 | + "/LocalSite/CPUTimeLeft": 0, |
| 113 | + "/LocalSite/GridCE": "ce.example.com", |
| 114 | + "/LocalSite/CEQueue": "default", |
| 115 | + "/LocalSite/Site": "LCG.Example.com", |
| 116 | + } |
| 117 | + |
| 118 | + def mock_getValue(key, default=0): |
| 119 | + if key in config_values: |
| 120 | + return config_values[key] |
| 121 | + return default |
| 122 | + |
| 123 | + mock_gConfig.getValue.side_effect = mock_getValue |
| 124 | + |
| 125 | + with patch( |
| 126 | + "DIRAC.WorkloadManagementSystem.Client.CPUNormalization.getQueueInfo", |
| 127 | + return_value=S_OK( |
| 128 | + {"QueueCSSection": "/Resources/Sites/LCG/LCG.Example.com/CEs/ce.example.com/Queues/default"} |
| 129 | + ), |
| 130 | + ): |
| 131 | + result = self._import_getCPUTime()(cpuNormalizationFactor=10.0) |
| 132 | + |
| 133 | + assert result == 9999999 |
| 134 | + |
| 135 | + def test_cpu_normalization_factor_from_config(self, mock_gConfig, mock_TimeLeft): |
| 136 | + """When cpuNormalizationFactor=0, it should be read from config.""" |
| 137 | + mock_gConfig.getValue.side_effect = lambda key, default=0: { |
| 138 | + "/LocalSite/CPUTimeLeft": 50000, |
| 139 | + "/LocalSite/CPUNormalizationFactor": 5.0, |
| 140 | + }.get(key, default) |
| 141 | + |
| 142 | + result = self._import_getCPUTime()(cpuNormalizationFactor=0) |
| 143 | + |
| 144 | + # 50000 / 5.0 = 10000 seconds |
| 145 | + assert result == 10000 |
0 commit comments