|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Unit tests for AzureMonitorAgent/agent.py - pure logic functions only. |
| 4 | +""" |
| 5 | + |
| 6 | +import sys |
| 7 | +import os |
| 8 | +import re |
| 9 | +import unittest |
| 10 | +from unittest.mock import patch, MagicMock |
| 11 | + |
| 12 | +# Mock Linux-only modules before importing |
| 13 | +for mod_name in ('grp', 'pwd'): |
| 14 | + if mod_name not in sys.modules: |
| 15 | + sys.modules[mod_name] = MagicMock() |
| 16 | + |
| 17 | +# Mock waagent/HUtil imports that agent.py tries to load at module level |
| 18 | +sys.modules['Utils'] = MagicMock() |
| 19 | +sys.modules['Utils.WAAgentUtil'] = MagicMock() |
| 20 | +sys.modules['Utils.WAAgentUtil'].waagent = MagicMock() |
| 21 | +sys.modules['Utils.HandlerUtil'] = MagicMock() |
| 22 | + |
| 23 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 24 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'LAD-AMA-Common'))) |
| 25 | + |
| 26 | +import agent |
| 27 | + |
| 28 | + |
| 29 | +class TestIsTruthy(unittest.TestCase): |
| 30 | + """Tests for is_truthy function.""" |
| 31 | + |
| 32 | + def test_true_bool(self): |
| 33 | + self.assertTrue(agent.is_truthy(True)) |
| 34 | + |
| 35 | + def test_false_bool(self): |
| 36 | + self.assertFalse(agent.is_truthy(False)) |
| 37 | + |
| 38 | + def test_true_string(self): |
| 39 | + self.assertTrue(agent.is_truthy("true")) |
| 40 | + |
| 41 | + def test_true_string_capitalized(self): |
| 42 | + self.assertTrue(agent.is_truthy("True")) |
| 43 | + |
| 44 | + def test_true_string_uppercase(self): |
| 45 | + self.assertTrue(agent.is_truthy("TRUE")) |
| 46 | + |
| 47 | + def test_true_string_with_whitespace(self): |
| 48 | + self.assertTrue(agent.is_truthy(" true ")) |
| 49 | + |
| 50 | + def test_false_string(self): |
| 51 | + self.assertFalse(agent.is_truthy("false")) |
| 52 | + |
| 53 | + def test_none(self): |
| 54 | + self.assertFalse(agent.is_truthy(None)) |
| 55 | + |
| 56 | + def test_empty_string(self): |
| 57 | + self.assertFalse(agent.is_truthy("")) |
| 58 | + |
| 59 | + def test_zero(self): |
| 60 | + self.assertFalse(agent.is_truthy(0)) |
| 61 | + |
| 62 | + def test_one(self): |
| 63 | + self.assertFalse(agent.is_truthy(1)) |
| 64 | + |
| 65 | + |
| 66 | +class TestIsDpkgOrRpmLocked(unittest.TestCase): |
| 67 | + """Tests for is_dpkg_or_rpm_locked.""" |
| 68 | + |
| 69 | + def test_dpkg_locked(self): |
| 70 | + output = "E: Could not get lock /var/lib/dpkg/lock - open" |
| 71 | + self.assertTrue(agent.is_dpkg_or_rpm_locked(1, output)) |
| 72 | + |
| 73 | + def test_rpm_locked(self): |
| 74 | + output = "error: waiting for transaction rpm lock on /var/lib/rpm/.rpm.lock" |
| 75 | + self.assertTrue(agent.is_dpkg_or_rpm_locked(1, output)) |
| 76 | + |
| 77 | + def test_not_locked_success(self): |
| 78 | + self.assertFalse(agent.is_dpkg_or_rpm_locked(0, "Success")) |
| 79 | + |
| 80 | + def test_not_locked_other_error(self): |
| 81 | + self.assertFalse(agent.is_dpkg_or_rpm_locked(1, "Package not found")) |
| 82 | + |
| 83 | + |
| 84 | +class TestRetryIfDpkgOrRpmLocked(unittest.TestCase): |
| 85 | + """Tests for retry_if_dpkg_or_rpm_locked.""" |
| 86 | + |
| 87 | + def test_locked_returns_retry(self): |
| 88 | + result = agent.retry_if_dpkg_or_rpm_locked( |
| 89 | + 1, "dpkg status database is locked by another process" |
| 90 | + ) |
| 91 | + self.assertTrue(result[0]) |
| 92 | + |
| 93 | + def test_not_locked_no_retry(self): |
| 94 | + result = agent.retry_if_dpkg_or_rpm_locked(0, "ok") |
| 95 | + self.assertFalse(result[0]) |
| 96 | + |
| 97 | + |
| 98 | +class TestFinalCheckIfDpkgOrRpmLocked(unittest.TestCase): |
| 99 | + """Tests for final_check_if_dpkg_or_rpm_locked.""" |
| 100 | + |
| 101 | + def test_locked_returns_special_code(self): |
| 102 | + code = agent.final_check_if_dpkg_or_rpm_locked( |
| 103 | + 1, "dpkg lock held" |
| 104 | + ) |
| 105 | + self.assertEqual(code, agent.DPKGOrRPMLockedErrorCode) |
| 106 | + |
| 107 | + def test_not_locked_returns_original_code(self): |
| 108 | + code = agent.final_check_if_dpkg_or_rpm_locked(0, "ok") |
| 109 | + self.assertEqual(code, 0) |
| 110 | + |
| 111 | + |
| 112 | +class TestValidatePortNumber(unittest.TestCase): |
| 113 | + """Tests for validate_port_number.""" |
| 114 | + |
| 115 | + @patch('agent.hutil_log_error') |
| 116 | + def test_valid_port(self, mock_log): |
| 117 | + self.assertEqual(agent.validate_port_number("8080", "test"), "8080") |
| 118 | + |
| 119 | + @patch('agent.hutil_log_error') |
| 120 | + def test_valid_port_with_whitespace(self, mock_log): |
| 121 | + self.assertEqual(agent.validate_port_number(" 443 ", "test"), "443") |
| 122 | + |
| 123 | + @patch('agent.hutil_log_error') |
| 124 | + def test_port_min(self, mock_log): |
| 125 | + self.assertEqual(agent.validate_port_number("1", "test"), "1") |
| 126 | + |
| 127 | + @patch('agent.hutil_log_error') |
| 128 | + def test_port_max(self, mock_log): |
| 129 | + self.assertEqual(agent.validate_port_number("65535", "test"), "65535") |
| 130 | + |
| 131 | + @patch('agent.hutil_log_error') |
| 132 | + def test_port_zero_invalid(self, mock_log): |
| 133 | + self.assertEqual(agent.validate_port_number("0", "test"), "") |
| 134 | + |
| 135 | + @patch('agent.hutil_log_error') |
| 136 | + def test_port_too_high(self, mock_log): |
| 137 | + self.assertEqual(agent.validate_port_number("65536", "test"), "") |
| 138 | + |
| 139 | + @patch('agent.hutil_log_error') |
| 140 | + def test_port_negative(self, mock_log): |
| 141 | + self.assertEqual(agent.validate_port_number("-1", "test"), "") |
| 142 | + |
| 143 | + @patch('agent.hutil_log_error') |
| 144 | + def test_port_non_numeric(self, mock_log): |
| 145 | + self.assertEqual(agent.validate_port_number("abc", "test"), "") |
| 146 | + |
| 147 | + @patch('agent.hutil_log_error') |
| 148 | + def test_port_empty(self, mock_log): |
| 149 | + self.assertEqual(agent.validate_port_number("", "test"), "") |
| 150 | + |
| 151 | + @patch('agent.hutil_log_error') |
| 152 | + def test_port_none(self, mock_log): |
| 153 | + self.assertEqual(agent.validate_port_number(None, "test"), "") |
| 154 | + |
| 155 | + |
| 156 | +class TestGetProxyMode(unittest.TestCase): |
| 157 | + """Tests for get_proxy_mode.""" |
| 158 | + |
| 159 | + def test_none_settings(self): |
| 160 | + self.assertIsNone(agent.get_proxy_mode(None)) |
| 161 | + |
| 162 | + def test_no_proxy_key(self): |
| 163 | + self.assertIsNone(agent.get_proxy_mode({"other": "value"})) |
| 164 | + |
| 165 | + def test_proxy_no_mode(self): |
| 166 | + self.assertIsNone(agent.get_proxy_mode({"proxy": {}})) |
| 167 | + |
| 168 | + def test_proxy_with_mode(self): |
| 169 | + self.assertEqual( |
| 170 | + agent.get_proxy_mode({"proxy": {"mode": "application"}}), |
| 171 | + "application" |
| 172 | + ) |
| 173 | + |
| 174 | + def test_proxy_none_value(self): |
| 175 | + self.assertIsNone(agent.get_proxy_mode({"proxy": None})) |
| 176 | + |
| 177 | + |
| 178 | +class TestGetServiceCommand(unittest.TestCase): |
| 179 | + """Tests for get_service_command.""" |
| 180 | + |
| 181 | + @patch('agent.is_systemd', return_value=True) |
| 182 | + def test_systemd_single_op(self, _): |
| 183 | + cmd = agent.get_service_command("myservice", "start") |
| 184 | + self.assertEqual(cmd, "systemctl start myservice") |
| 185 | + |
| 186 | + @patch('agent.is_systemd', return_value=True) |
| 187 | + def test_systemd_multiple_ops(self, _): |
| 188 | + cmd = agent.get_service_command("myservice", "daemon-reload", "restart") |
| 189 | + self.assertEqual(cmd, "systemctl daemon-reload myservice && systemctl restart myservice") |
| 190 | + |
| 191 | + @patch('agent.is_systemd', return_value=False) |
| 192 | + @patch('agent.hutil_log_info') |
| 193 | + def test_initd_fallback(self, _, __): |
| 194 | + cmd = agent.get_service_command("myservice", "start") |
| 195 | + self.assertEqual(cmd, "/etc/init.d/myservice start") |
| 196 | + |
| 197 | + |
| 198 | +class TestConstants(unittest.TestCase): |
| 199 | + """Tests for agent constants.""" |
| 200 | + |
| 201 | + def test_error_codes(self): |
| 202 | + self.assertEqual(agent.GenericErrorCode, 1) |
| 203 | + self.assertEqual(agent.UnsupportedOperatingSystem, 51) |
| 204 | + self.assertEqual(agent.MissingorInvalidParameterErrorCode, 53) |
| 205 | + self.assertEqual(agent.DPKGOrRPMLockedErrorCode, 56) |
| 206 | + self.assertEqual(agent.MissingDependency, 52) |
| 207 | + |
| 208 | + def test_supported_arch(self): |
| 209 | + self.assertIn('x86_64', agent.SupportedArch) |
| 210 | + self.assertIn('aarch64', agent.SupportedArch) |
| 211 | + |
| 212 | + def test_config_keys(self): |
| 213 | + self.assertEqual(agent.GenevaConfigKey, "genevaConfiguration") |
| 214 | + self.assertEqual(agent.AzureMonitorConfigKey, "azureMonitorConfiguration") |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == '__main__': |
| 218 | + unittest.main() |
0 commit comments