|
| 1 | +import unittest |
| 2 | + |
| 3 | +try: |
| 4 | + from unittest.mock import Mock, patch, call |
| 5 | +except ImportError: |
| 6 | + from mock import Mock, patch, call |
| 7 | + |
| 8 | +import tuned.consts as consts |
| 9 | + |
| 10 | + |
| 11 | +class BootloaderPluginSourceTestCase(unittest.TestCase): |
| 12 | + """Tests for the rpm-ostree --source=tuned integration in the bootloader plugin. |
| 13 | +
|
| 14 | + These tests exercise the --source detection, source-based kargs apply, |
| 15 | + source-based kargs removal, and the dispatch logic that falls back to |
| 16 | + the legacy code path when --source is not available. |
| 17 | +
|
| 18 | + The BootloaderPlugin constructor requires /etc/grub.d/00_tuned to exist, |
| 19 | + so we test the methods directly on a minimal mock rather than |
| 20 | + instantiating the full plugin. |
| 21 | + """ |
| 22 | + |
| 23 | + def _make_plugin_mock(self, has_source=True, rpm_ostree_idle=True): |
| 24 | + """Create a mock that has the real methods under test patched onto it. |
| 25 | +
|
| 26 | + This avoids instantiating BootloaderPlugin (which requires GRUB2 |
| 27 | + template files on disk) while still testing the actual method logic. |
| 28 | + """ |
| 29 | + from tuned.plugins.plugin_bootloader import BootloaderPlugin |
| 30 | + |
| 31 | + mock_plugin = Mock(spec=BootloaderPlugin) |
| 32 | + mock_plugin._rpm_ostree_has_source = has_source |
| 33 | + mock_plugin._cmdline_val = "" |
| 34 | + mock_plugin._cmd = Mock() |
| 35 | + mock_plugin._cmd.add_modify_option_in_file = Mock(return_value=True) |
| 36 | + |
| 37 | + # Make _wait_till_rpm_ostree_idle return the desired value |
| 38 | + mock_plugin._wait_till_rpm_ostree_idle = Mock(return_value=rpm_ostree_idle) |
| 39 | + |
| 40 | + # Bind real methods to the mock so we test actual logic |
| 41 | + mock_plugin._rpm_ostree_has_source_flag = \ |
| 42 | + lambda: BootloaderPlugin._rpm_ostree_has_source_flag(mock_plugin) |
| 43 | + mock_plugin._rpm_ostree_source_update = \ |
| 44 | + lambda: BootloaderPlugin._rpm_ostree_source_update(mock_plugin) |
| 45 | + mock_plugin._remove_rpm_ostree_source_tuning = \ |
| 46 | + lambda: BootloaderPlugin._remove_rpm_ostree_source_tuning(mock_plugin) |
| 47 | + mock_plugin._rpm_ostree_update = \ |
| 48 | + lambda: BootloaderPlugin._rpm_ostree_update(mock_plugin) |
| 49 | + mock_plugin._remove_rpm_ostree_tuning = \ |
| 50 | + lambda: BootloaderPlugin._remove_rpm_ostree_tuning(mock_plugin) |
| 51 | + mock_plugin._patch_bootcmdline = \ |
| 52 | + lambda d: BootloaderPlugin._patch_bootcmdline(mock_plugin, d) |
| 53 | + |
| 54 | + return mock_plugin |
| 55 | + |
| 56 | + # ------------------------------------------------------------------ |
| 57 | + # _rpm_ostree_has_source_flag() detection |
| 58 | + # ------------------------------------------------------------------ |
| 59 | + |
| 60 | + def test_has_source_flag_present(self): |
| 61 | + """--source flag is detected when present in rpm-ostree kargs --help.""" |
| 62 | + plugin = self._make_plugin_mock() |
| 63 | + help_text = ( |
| 64 | + "Usage:\n" |
| 65 | + " rpm-ostree kargs [OPTION...]\n\n" |
| 66 | + " --append=KEY=VALUE Append kernel argument\n" |
| 67 | + " --source=NAME Track kargs ownership by source name\n" |
| 68 | + " --delete=KEY=VALUE Delete a kernel argument\n" |
| 69 | + ) |
| 70 | + plugin._cmd.execute = Mock(return_value=(0, help_text, "")) |
| 71 | + self.assertTrue(plugin._rpm_ostree_has_source_flag()) |
| 72 | + plugin._cmd.execute.assert_called_once_with( |
| 73 | + ['rpm-ostree', 'kargs', '--help'], return_err=True) |
| 74 | + |
| 75 | + def test_has_source_flag_absent(self): |
| 76 | + """--source flag is not detected when absent from rpm-ostree kargs --help.""" |
| 77 | + plugin = self._make_plugin_mock() |
| 78 | + help_text = ( |
| 79 | + "Usage:\n" |
| 80 | + " rpm-ostree kargs [OPTION...]\n\n" |
| 81 | + " --append=KEY=VALUE Append kernel argument\n" |
| 82 | + " --delete=KEY=VALUE Delete a kernel argument\n" |
| 83 | + ) |
| 84 | + plugin._cmd.execute = Mock(return_value=(0, help_text, "")) |
| 85 | + self.assertFalse(plugin._rpm_ostree_has_source_flag()) |
| 86 | + |
| 87 | + def test_has_source_flag_in_stderr(self): |
| 88 | + """--source flag is detected even if it appears in stderr.""" |
| 89 | + plugin = self._make_plugin_mock() |
| 90 | + plugin._cmd.execute = Mock(return_value=(0, "", "--source=NAME Track kargs")) |
| 91 | + self.assertTrue(plugin._rpm_ostree_has_source_flag()) |
| 92 | + |
| 93 | + def test_has_source_flag_help_fails(self): |
| 94 | + """If rpm-ostree kargs --help fails, --source is not detected.""" |
| 95 | + plugin = self._make_plugin_mock() |
| 96 | + plugin._cmd.execute = Mock(return_value=(1, "", "command not found")) |
| 97 | + self.assertFalse(plugin._rpm_ostree_has_source_flag()) |
| 98 | + |
| 99 | + # ------------------------------------------------------------------ |
| 100 | + # _rpm_ostree_source_update() |
| 101 | + # ------------------------------------------------------------------ |
| 102 | + |
| 103 | + def test_source_update_basic(self): |
| 104 | + """--source=tuned is called with correct --append flags.""" |
| 105 | + plugin = self._make_plugin_mock() |
| 106 | + plugin._cmdline_val = "nohz=full isolcpus=1-3" |
| 107 | + plugin._cmd.execute = Mock(return_value=(0, "", "")) |
| 108 | + |
| 109 | + plugin._rpm_ostree_source_update() |
| 110 | + |
| 111 | + plugin._cmd.execute.assert_called_once_with( |
| 112 | + ["rpm-ostree", "kargs", "--source=tuned", |
| 113 | + "--append=nohz=full", "--append=isolcpus=1-3"], |
| 114 | + return_err=True) |
| 115 | + # Verify bootcmdline state file is updated |
| 116 | + plugin._cmd.add_modify_option_in_file.assert_called_once_with( |
| 117 | + consts.BOOT_CMDLINE_FILE, |
| 118 | + {consts.BOOT_CMDLINE_TUNED_VAR: "nohz=full isolcpus=1-3"}) |
| 119 | + |
| 120 | + def test_source_update_empty_cmdline(self): |
| 121 | + """When cmdline is empty, only --source=tuned is passed (clears kargs).""" |
| 122 | + plugin = self._make_plugin_mock() |
| 123 | + plugin._cmdline_val = "" |
| 124 | + plugin._cmd.execute = Mock(return_value=(0, "", "")) |
| 125 | + |
| 126 | + plugin._rpm_ostree_source_update() |
| 127 | + |
| 128 | + plugin._cmd.execute.assert_called_once_with( |
| 129 | + ["rpm-ostree", "kargs", "--source=tuned"], |
| 130 | + return_err=True) |
| 131 | + plugin._cmd.add_modify_option_in_file.assert_called_once_with( |
| 132 | + consts.BOOT_CMDLINE_FILE, |
| 133 | + {consts.BOOT_CMDLINE_TUNED_VAR: ""}) |
| 134 | + |
| 135 | + def test_source_update_rpm_ostree_busy(self): |
| 136 | + """When rpm-ostree is busy, source update does not execute.""" |
| 137 | + plugin = self._make_plugin_mock(rpm_ostree_idle=False) |
| 138 | + plugin._cmdline_val = "nohz=full" |
| 139 | + plugin._cmd.execute = Mock() |
| 140 | + |
| 141 | + plugin._rpm_ostree_source_update() |
| 142 | + |
| 143 | + plugin._cmd.execute.assert_not_called() |
| 144 | + plugin._cmd.add_modify_option_in_file.assert_not_called() |
| 145 | + |
| 146 | + def test_source_update_command_fails(self): |
| 147 | + """When rpm-ostree kargs --source fails, bootcmdline is not updated.""" |
| 148 | + plugin = self._make_plugin_mock() |
| 149 | + plugin._cmdline_val = "nohz=full" |
| 150 | + plugin._cmd.execute = Mock(return_value=(1, "", "error: unknown option")) |
| 151 | + |
| 152 | + plugin._rpm_ostree_source_update() |
| 153 | + |
| 154 | + plugin._cmd.execute.assert_called_once() |
| 155 | + plugin._cmd.add_modify_option_in_file.assert_not_called() |
| 156 | + |
| 157 | + # ------------------------------------------------------------------ |
| 158 | + # _remove_rpm_ostree_source_tuning() |
| 159 | + # ------------------------------------------------------------------ |
| 160 | + |
| 161 | + def test_remove_source_tuning(self): |
| 162 | + """Removal calls --source=tuned with no --append (clears all).""" |
| 163 | + plugin = self._make_plugin_mock() |
| 164 | + plugin._cmd.execute = Mock(return_value=(0, "", "")) |
| 165 | + |
| 166 | + plugin._remove_rpm_ostree_source_tuning() |
| 167 | + |
| 168 | + plugin._cmd.execute.assert_called_once_with( |
| 169 | + ["rpm-ostree", "kargs", "--source=tuned"], |
| 170 | + return_err=True) |
| 171 | + plugin._cmd.add_modify_option_in_file.assert_called_once_with( |
| 172 | + consts.BOOT_CMDLINE_FILE, |
| 173 | + {consts.BOOT_CMDLINE_TUNED_VAR: ""}) |
| 174 | + |
| 175 | + def test_remove_source_tuning_rpm_ostree_busy(self): |
| 176 | + """When rpm-ostree is busy, source removal does not execute.""" |
| 177 | + plugin = self._make_plugin_mock(rpm_ostree_idle=False) |
| 178 | + plugin._cmd.execute = Mock() |
| 179 | + |
| 180 | + plugin._remove_rpm_ostree_source_tuning() |
| 181 | + |
| 182 | + plugin._cmd.execute.assert_not_called() |
| 183 | + # bootcmdline should not be touched either |
| 184 | + plugin._cmd.add_modify_option_in_file.assert_not_called() |
| 185 | + |
| 186 | + def test_remove_source_tuning_command_fails(self): |
| 187 | + """When --source removal fails, bootcmdline is still cleared (best-effort).""" |
| 188 | + plugin = self._make_plugin_mock() |
| 189 | + plugin._cmd.execute = Mock(return_value=(1, "", "error")) |
| 190 | + |
| 191 | + plugin._remove_rpm_ostree_source_tuning() |
| 192 | + |
| 193 | + # bootcmdline should still be cleared even on failure |
| 194 | + plugin._cmd.add_modify_option_in_file.assert_called_once_with( |
| 195 | + consts.BOOT_CMDLINE_FILE, |
| 196 | + {consts.BOOT_CMDLINE_TUNED_VAR: ""}) |
| 197 | + |
| 198 | + # ------------------------------------------------------------------ |
| 199 | + # _rpm_ostree_update() dispatch |
| 200 | + # ------------------------------------------------------------------ |
| 201 | + |
| 202 | + def test_rpm_ostree_update_dispatches_to_source(self): |
| 203 | + """When --source is available, _rpm_ostree_update uses the source path.""" |
| 204 | + plugin = self._make_plugin_mock(has_source=True) |
| 205 | + plugin._cmdline_val = "nohz=full" |
| 206 | + plugin._cmd.execute = Mock(return_value=(0, "", "")) |
| 207 | + |
| 208 | + plugin._rpm_ostree_update() |
| 209 | + |
| 210 | + # Should have called --source=tuned |
| 211 | + plugin._cmd.execute.assert_called_once_with( |
| 212 | + ["rpm-ostree", "kargs", "--source=tuned", "--append=nohz=full"], |
| 213 | + return_err=True) |
| 214 | + |
| 215 | + def test_rpm_ostree_update_fallback_when_no_source(self): |
| 216 | + """When --source is not available, _rpm_ostree_update uses the legacy path.""" |
| 217 | + from tuned.plugins.plugin_bootloader import BootloaderPlugin |
| 218 | + |
| 219 | + plugin = self._make_plugin_mock(has_source=False) |
| 220 | + plugin._cmdline_val = "nohz=full" |
| 221 | + |
| 222 | + # Set up legacy path dependencies |
| 223 | + plugin._get_appended_rpm_ostree_kargs = Mock(return_value=["old_karg=1"]) |
| 224 | + plugin._get_rpm_ostree_kargs = Mock(return_value="root=UUID=xxx old_karg=1") |
| 225 | + plugin._modify_rpm_ostree_kargs = Mock(return_value=True) |
| 226 | + |
| 227 | + # Bind _rpm_ostree_update with the real method |
| 228 | + # but we need to also bind _patch_bootcmdline for the legacy path |
| 229 | + plugin._rpm_ostree_update = \ |
| 230 | + lambda: BootloaderPlugin._rpm_ostree_update(plugin) |
| 231 | + |
| 232 | + plugin._rpm_ostree_update() |
| 233 | + |
| 234 | + # Should NOT have called --source, should use legacy delete/append |
| 235 | + plugin._modify_rpm_ostree_kargs.assert_called_once_with( |
| 236 | + delete_kargs=["old_karg=1"], append_kargs=["nohz=full"]) |
| 237 | + |
| 238 | + # ------------------------------------------------------------------ |
| 239 | + # _remove_rpm_ostree_tuning() dispatch |
| 240 | + # ------------------------------------------------------------------ |
| 241 | + |
| 242 | + def test_remove_rpm_ostree_tuning_dispatches_to_source(self): |
| 243 | + """When --source is available, removal uses the source path.""" |
| 244 | + plugin = self._make_plugin_mock(has_source=True) |
| 245 | + plugin._cmd.execute = Mock(return_value=(0, "", "")) |
| 246 | + |
| 247 | + plugin._remove_rpm_ostree_tuning() |
| 248 | + |
| 249 | + plugin._cmd.execute.assert_called_once_with( |
| 250 | + ["rpm-ostree", "kargs", "--source=tuned"], |
| 251 | + return_err=True) |
| 252 | + |
| 253 | + def test_remove_rpm_ostree_tuning_fallback_when_no_source(self): |
| 254 | + """When --source is not available, removal uses the legacy path.""" |
| 255 | + from tuned.plugins.plugin_bootloader import BootloaderPlugin |
| 256 | + |
| 257 | + plugin = self._make_plugin_mock(has_source=False) |
| 258 | + plugin._get_appended_rpm_ostree_kargs = Mock(return_value=["old=1", "old=2"]) |
| 259 | + plugin._modify_rpm_ostree_kargs = Mock(return_value=True) |
| 260 | + |
| 261 | + plugin._remove_rpm_ostree_tuning = \ |
| 262 | + lambda: BootloaderPlugin._remove_rpm_ostree_tuning(plugin) |
| 263 | + |
| 264 | + plugin._remove_rpm_ostree_tuning() |
| 265 | + |
| 266 | + plugin._modify_rpm_ostree_kargs.assert_called_once_with( |
| 267 | + delete_kargs=["old=1", "old=2"]) |
| 268 | + plugin._cmd.add_modify_option_in_file.assert_called_once_with( |
| 269 | + consts.BOOT_CMDLINE_FILE, |
| 270 | + {consts.BOOT_CMDLINE_TUNED_VAR: ""}) |
| 271 | + |
| 272 | + |
| 273 | +if __name__ == '__main__': |
| 274 | + unittest.main() |
0 commit comments