|
| 1 | +import importlib |
| 2 | +import unittest |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import confuse |
| 6 | + |
| 7 | +from pvecontrol.config import list_clusters, set_config |
| 8 | + |
| 9 | +# pvecontrol/__init__.py does `from pvecontrol.config import config`, which |
| 10 | +# overwrites the `config` attribute on the pvecontrol package with the LazyConfig |
| 11 | +# instance. importlib.import_module resolves via sys.modules directly, bypassing |
| 12 | +# that shadowing, and returns the actual module. |
| 13 | +config_module = importlib.import_module("pvecontrol.config") |
| 14 | + |
| 15 | + |
| 16 | +def _make_cluster(name, node=None, vm=None): |
| 17 | + c = MagicMock() |
| 18 | + c.name = name |
| 19 | + c.node = node if node is not None else {} |
| 20 | + c.vm = vm if vm is not None else {} |
| 21 | + return c |
| 22 | + |
| 23 | + |
| 24 | +def _make_validconfig(cluster_names): |
| 25 | + vc = MagicMock() |
| 26 | + vc.clusters = [_make_cluster(n) for n in cluster_names] |
| 27 | + vc.node = {"cpufactor": 2.5, "memoryminimum": 8589934592} |
| 28 | + vc.vm = {"max_last_backup": 1500} |
| 29 | + return vc |
| 30 | + |
| 31 | + |
| 32 | +class TestLoadConfigErrors(unittest.TestCase): |
| 33 | + |
| 34 | + def test_config_read_error(self): |
| 35 | + with patch.object(config_module, "config") as mock_config: |
| 36 | + mock_config.get.side_effect = confuse.ConfigReadError("config.yaml") |
| 37 | + with self.assertRaises(SystemExit) as cm: |
| 38 | + list_clusters() |
| 39 | + self.assertEqual(cm.exception.code, 1) |
| 40 | + |
| 41 | + def test_not_found_error(self): |
| 42 | + with patch.object(config_module, "config") as mock_config: |
| 43 | + mock_config.get.side_effect = confuse.NotFoundError() |
| 44 | + with self.assertRaises(SystemExit) as cm: |
| 45 | + list_clusters() |
| 46 | + self.assertEqual(cm.exception.code, 1) |
| 47 | + |
| 48 | + def test_config_type_error(self): |
| 49 | + with patch.object(config_module, "config") as mock_config: |
| 50 | + mock_config.get.side_effect = confuse.ConfigTypeError() |
| 51 | + with self.assertRaises(SystemExit) as cm: |
| 52 | + list_clusters() |
| 53 | + self.assertEqual(cm.exception.code, 1) |
| 54 | + |
| 55 | + |
| 56 | +class TestListClusters(unittest.TestCase): |
| 57 | + |
| 58 | + def test_empty(self): |
| 59 | + with patch.object(config_module, "config") as mock_config: |
| 60 | + mock_config.get.return_value = _make_validconfig([]) |
| 61 | + self.assertEqual(list_clusters(), []) |
| 62 | + |
| 63 | + def test_returns_names(self): |
| 64 | + with patch.object(config_module, "config") as mock_config: |
| 65 | + mock_config.get.return_value = _make_validconfig(["prod", "staging"]) |
| 66 | + self.assertEqual(list_clusters(), ["prod", "staging"]) |
| 67 | + |
| 68 | + |
| 69 | +class TestSetConfig(unittest.TestCase): |
| 70 | + |
| 71 | + def test_case_insensitive_lower_input(self): |
| 72 | + with patch.object(config_module, "config") as mock_config: |
| 73 | + mock_config.get.return_value = _make_validconfig(["PROD"]) |
| 74 | + result = set_config("prod") |
| 75 | + self.assertEqual(result.name, "PROD") |
| 76 | + |
| 77 | + def test_case_insensitive_upper_input(self): |
| 78 | + with patch.object(config_module, "config") as mock_config: |
| 79 | + mock_config.get.return_value = _make_validconfig(["prod"]) |
| 80 | + result = set_config("PROD") |
| 81 | + self.assertEqual(result.name, "prod") |
| 82 | + |
| 83 | + def test_exact_match(self): |
| 84 | + with patch.object(config_module, "config") as mock_config: |
| 85 | + mock_config.get.return_value = _make_validconfig(["prod"]) |
| 86 | + result = set_config("prod") |
| 87 | + self.assertEqual(result.name, "prod") |
| 88 | + |
| 89 | + def test_not_found(self): |
| 90 | + with patch.object(config_module, "config") as mock_config: |
| 91 | + mock_config.get.return_value = _make_validconfig(["prod", "staging"]) |
| 92 | + with self.assertRaises(SystemExit) as cm: |
| 93 | + set_config("unknown") |
| 94 | + self.assertEqual(cm.exception.code, 1) |
| 95 | + |
| 96 | + def test_ambiguous_names(self): |
| 97 | + vc = MagicMock() |
| 98 | + vc.clusters = [_make_cluster("prod"), _make_cluster("PROD")] |
| 99 | + vc.node = {} |
| 100 | + vc.vm = {} |
| 101 | + with patch.object(config_module, "config") as mock_config: |
| 102 | + mock_config.get.return_value = vc |
| 103 | + with self.assertRaises(SystemExit) as cm: |
| 104 | + set_config("prod") |
| 105 | + self.assertEqual(cm.exception.code, 1) |
| 106 | + |
| 107 | + def test_ambiguous_names_error_lists_conflicts(self): |
| 108 | + vc = MagicMock() |
| 109 | + vc.clusters = [_make_cluster("prod"), _make_cluster("PROD")] |
| 110 | + vc.node = {} |
| 111 | + vc.vm = {} |
| 112 | + with patch.object(config_module, "config") as mock_config: |
| 113 | + mock_config.get.return_value = vc |
| 114 | + with self.assertLogs("root", level="ERROR") as log: |
| 115 | + with self.assertRaises(SystemExit): |
| 116 | + set_config("prod") |
| 117 | + self.assertTrue(any("prod" in msg and "PROD" in msg for msg in log.output)) |
0 commit comments