|
| 1 | +"""Unit tests for updater.components """ |
| 2 | +import logging |
| 3 | +import pytest |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import patch, mock_open |
| 6 | + |
| 7 | +from updater.components import ( |
| 8 | + ComponentConfig, ComponentStatus, load_components |
| 9 | +) |
| 10 | + |
| 11 | +BUNDLE_YAML = """ |
| 12 | +pool_internal_minutes: 30 |
| 13 | +components: |
| 14 | + - name: klipper |
| 15 | + - type: git |
| 16 | + - path: ~/klipper |
| 17 | + - service: klipper.service |
| 18 | + - reset_mode: hard |
| 19 | + - order: 1 |
| 20 | + - name: BlocksScreen |
| 21 | + - type: git |
| 22 | + - path: ~/BlocksScreen |
| 23 | + - service: BlocksScreen.service |
| 24 | + - reset_mode: hard |
| 25 | + - order: 99 |
| 26 | +""" |
| 27 | + |
| 28 | +OVERRIDE_YAML = """ |
| 29 | +components: |
| 30 | + - name: klipper |
| 31 | + path: ~/costum_klipper |
| 32 | + - name: my-plugin |
| 33 | + type: git |
| 34 | + path: ~/my-plugin |
| 35 | + service: my-plugin.service |
| 36 | + reset_mode: hard |
| 37 | + order: 5 |
| 38 | +""" |
| 39 | + |
| 40 | +def _mock_load(bundled: str, override: str | None = None): |
| 41 | + import updater.components as mod |
| 42 | + bundled_path = Path(mod.__file__).parent / "components.yaml" |
| 43 | + |
| 44 | + def fake_open(path, *a, **kw): |
| 45 | + if Path(path) == bundled_path: |
| 46 | + return mock_open(read_data=bundled)() |
| 47 | + if override is not None: |
| 48 | + return mock_open(read_data=override)() |
| 49 | + raise FileNotFoundError(Path) |
| 50 | + return fake_open |
| 51 | + |
| 52 | + |
| 53 | +class TestLoadComponents: |
| 54 | + def test_load_return_sorted_by_order(self): |
| 55 | + with patch("builtins.open", _mock_load(BUNDLE_YAML)), patch("pathlib.Path.exists", return_value=False), patch("pathlib.Path.is_dir", return_value=True): |
| 56 | + components = load_components() |
| 57 | + names = [c.name for c in components] |
| 58 | + assert names.index("klipper") < names.index("BlocksScreen") |
| 59 | + |
| 60 | + def test_missing_override_file_silently_ignored(self): |
| 61 | + with patch("builtins.open", _mock_load(BUNDLE_YAML)), patch("pathlib.Path.exists", return_value=False), patch("pathlib.Path.is_dir", return_value=True): |
| 62 | + components = load_components() |
| 63 | + assert len(components) >= 2 |
| 64 | + |
| 65 | +class TestYamlMerge: |
| 66 | + def test_ovvrides_path_by_name(self): |
| 67 | + with patch("builtins.open", _mock_load(BUNDLE_YAML)), patch("pathlib.Path.exists", return_value=True), patch("pathlib.Path.is_dir", return_value=True): |
| 68 | + components = load_components() |
| 69 | + klipper = next(c for c in components if c.name == "klipper") |
| 70 | + assert str(klipper.path).endswith("costum_klipper") |
| 71 | + assert klipper.service == "klipper.service" |
| 72 | + |
| 73 | + def test_appends_new_componentself(self): |
| 74 | + with patch("builtins.open", _mock_load(BUNDLE_YAML, OVERRIDE_YAML)), patch("pathlib.Path.exists", return_value=True), patch("pathlib.Path.is_dir", return_value=True): |
| 75 | + components = load_components() |
| 76 | + assert any(c.name == "my-plugin" for c in components) |
| 77 | + def test_keeps_bundled_fields_not_in_override(self): |
| 78 | + with patch("builtins.open", _mock_load(BUNDLE_YAML, OVERRIDE_YAML)), patch("pathlib.Path.exists", return_value=True), patch("pathlib.Path.is_dir", return_value=True): |
| 79 | + components = load_components() |
| 80 | + klipper = next(c for c in components if c.name == "klipper") |
| 81 | + assert klipper.type == "git" |
| 82 | + assert klipper.reset_mode == "hard" |
| 83 | + |
| 84 | + def test_syntax_error_falls_back_to_bundled(self, caplog): |
| 85 | + with patch("builtins.open", _mock_load(BUNDLE_YAML, OVERRIDE_YAML)), patch("pathlib.Path.exists", return_value=True), patch("pathlib.Path.is_dir", return_value=True), \ |
| 86 | + caplog.at_level(logging.ERROR, logger="updater.components"): |
| 87 | + components = load_components() |
| 88 | + names = [c.name for c in components] |
| 89 | + assert "klipper" in names |
| 90 | + assert "my-plugin" not in names |
| 91 | + |
| 92 | + |
| 93 | +class TestValidation: |
| 94 | + def test_invalid_component_skipped_with_warning(self, caplog): |
| 95 | + bad_yaml = """ |
| 96 | +components: |
| 97 | + - name: bad |
| 98 | + type: git |
| 99 | + path: /etc/passwd |
| 100 | + service: bad.service |
| 101 | + order: 10 |
| 102 | +""" |
| 103 | + with patch("builtins.open", _mock_load(bad_yaml)), \ |
| 104 | + patch("pathlib.Path.exists", return_value=True), \ |
| 105 | + caplog.at_level(logging.WARNING, logger="updater.components"): |
| 106 | + components = load_components() |
| 107 | + assert not any(c.name == "bad" for c in components) |
| 108 | + assert any("bad" in r.message for r in caplog.records) |
| 109 | + |
| 110 | + def test_path_outside_home_rejected(self): |
| 111 | + outside_yaml = """ |
| 112 | +components: |
| 113 | + - name: outside |
| 114 | + type: git |
| 115 | + path: /etc/outside |
| 116 | + service: outside.service |
| 117 | + order: 10 |
| 118 | +""" |
| 119 | + with patch("builtins.open", _mock_load(outside_yaml)), \ |
| 120 | + patch("pathlib.Path.exists", return_value=False): |
| 121 | + components = load_components() |
| 122 | + assert not any(c.name == "outside" for c in components) |
| 123 | + |
| 124 | + def test_service_name_with_slash_rejected(self): |
| 125 | + bad_yaml = """ |
| 126 | +components: |
| 127 | + - name: bad |
| 128 | + type: git |
| 129 | + path: ~/bad |
| 130 | + service: ../bad.service |
| 131 | + order: 10 |
| 132 | +""" |
| 133 | + with patch("builtins.open", _mock_load(bad_yaml)), \ |
| 134 | + patch("pathlib.Path.exists", return_value=False): |
| 135 | + components = load_components() |
| 136 | + assert not any(c.name == "evil" for c in components) |
| 137 | + |
| 138 | + def test_service_name_with_shell_metachar_rejected(self): |
| 139 | + for metachar in [";", "&", "|", "$", "`"]: |
| 140 | + bad_yaml = f""" |
| 141 | +components: |
| 142 | +- name: bad |
| 143 | +type: git |
| 144 | +path: ~/bad |
| 145 | +service: bad{metachar}cmd.service |
| 146 | +order: 10 |
| 147 | +""" |
| 148 | + with patch("builtins.open", _mock_load(bad_yaml)), \ |
| 149 | + patch("pathlib.Path.exists", return_value=False): |
| 150 | + components = load_components() |
| 151 | + assert not any(c.name == "bad" for c in components), f"metachar {metachar!r} not rejected" |
| 152 | + |
| 153 | + def test_service_name_valid_pattern_accepted(self): |
| 154 | + good_yaml = """ |
| 155 | +components: |
| 156 | + - name: my-plugin |
| 157 | + type: git |
| 158 | + path: ~/my-plugin |
| 159 | + service: my-plugin.service |
| 160 | + order: 5 |
| 161 | +""" |
| 162 | + with patch("builtins.open", _mock_load(good_yaml)), \ |
| 163 | + patch("pathlib.Path.exists", return_value=False), \ |
| 164 | + patch("pathlib.Path.is_dir", return_value=True): |
| 165 | + components = load_components() |
| 166 | + assert any(c.name == "my-plugin" for c in components) |
| 167 | + |
0 commit comments