|
| 1 | +# -*- encoding: utf-8 -*- |
| 2 | +# |
| 3 | +# The Qubes OS Project, http://www.qubes-os.org |
| 4 | +# |
| 5 | +# Copyright (C) 2025 Marek Marczykowski-Górecki |
| 6 | + |
| 7 | +# |
| 8 | +# This program is free software; you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU Lesser General Public License as published by |
| 10 | +# the Free Software Foundation; either version 2.1 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU Lesser General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU Lesser General Public License along |
| 19 | +# with this program; if not, see <http://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +# pylint: disable=missing-docstring |
| 22 | + |
| 23 | +import asyncio |
| 24 | +import unittest.mock |
| 25 | + |
| 26 | +import qubesadmin.tests |
| 27 | +import qubesadmin.tests.tools |
| 28 | +import qubesadmin.tools.qvm_restart |
| 29 | + |
| 30 | + |
| 31 | +class TC_00_qvm_restart(qubesadmin.tests.QubesTestCase): |
| 32 | + @unittest.skipUnless( |
| 33 | + qubesadmin.tools.qvm_restart.have_events, "Events not present" |
| 34 | + ) |
| 35 | + def test_000_restart_running(self): |
| 36 | + """Restarting just one already running qube""" |
| 37 | + loop = asyncio.new_event_loop() |
| 38 | + asyncio.set_event_loop(loop) |
| 39 | + |
| 40 | + mock_events = unittest.mock.AsyncMock() |
| 41 | + patch = unittest.mock.patch( |
| 42 | + "qubesadmin.events.EventsDispatcher._get_events_reader", mock_events |
| 43 | + ) |
| 44 | + patch.start() |
| 45 | + self.addCleanup(patch.stop) |
| 46 | + mock_events.side_effect = qubesadmin.tests.tools.MockEventsReader( |
| 47 | + [ |
| 48 | + b"1\0\0connection-established\0\0", |
| 49 | + b"1\0some-vm\0domain-shutdown\0\0", |
| 50 | + ] |
| 51 | + ) |
| 52 | + |
| 53 | + self.app.expected_calls[("dom0", "admin.vm.List", None, None)] = ( |
| 54 | + b"0\x00some-vm class=AppVM state=Running\n" |
| 55 | + ) |
| 56 | + self.app.expected_calls[ |
| 57 | + ("some-vm", "admin.vm.Shutdown", "force", None) |
| 58 | + ] = b"0\x00" |
| 59 | + self.app.expected_calls[ |
| 60 | + ("some-vm", "admin.vm.CurrentState", None, None) |
| 61 | + ] = ( |
| 62 | + [b"0\x00power_state=Running"] |
| 63 | + + [b"0\x00power_state=Halted"] |
| 64 | + + [b"0\x00power_state=Halted"] |
| 65 | + ) |
| 66 | + self.app.expected_calls[("some-vm", "admin.vm.Start", None, None)] = ( |
| 67 | + b"0\x00" |
| 68 | + ) |
| 69 | + qubesadmin.tools.qvm_restart.main(["some-vm"], app=self.app) |
| 70 | + self.assertAllCalled() |
| 71 | + |
| 72 | + @unittest.skipUnless( |
| 73 | + qubesadmin.tools.qvm_restart.have_events, "Events not present" |
| 74 | + ) |
| 75 | + def test_001_restart_halted(self): |
| 76 | + """Trying restart on a already halted qube""" |
| 77 | + loop = asyncio.new_event_loop() |
| 78 | + asyncio.set_event_loop(loop) |
| 79 | + |
| 80 | + mock_events = unittest.mock.AsyncMock() |
| 81 | + patch = unittest.mock.patch( |
| 82 | + "qubesadmin.events.EventsDispatcher._get_events_reader", mock_events |
| 83 | + ) |
| 84 | + patch.start() |
| 85 | + self.addCleanup(patch.stop) |
| 86 | + mock_events.side_effect = qubesadmin.tests.tools.MockEventsReader( |
| 87 | + [ |
| 88 | + b"1\0\0connection-established\0\0", |
| 89 | + b"1\0some-vm\0domain-shutdown\0\0", |
| 90 | + ] |
| 91 | + ) |
| 92 | + |
| 93 | + self.app.expected_calls[("dom0", "admin.vm.List", None, None)] = ( |
| 94 | + b"0\x00some-vm class=AppVM state=Halted\n" |
| 95 | + ) |
| 96 | + self.app.expected_calls[ |
| 97 | + ("some-vm", "admin.vm.Shutdown", "force", None) |
| 98 | + ] = b"0\x00" |
| 99 | + self.app.expected_calls[ |
| 100 | + ("some-vm", "admin.vm.CurrentState", None, None) |
| 101 | + ] = ( |
| 102 | + [b"0\x00power_state=Halted"] |
| 103 | + + [b"0\x00power_state=Halted"] |
| 104 | + + [b"0\x00power_state=Halted"] |
| 105 | + ) |
| 106 | + self.app.expected_calls[("some-vm", "admin.vm.Start", None, None)] = ( |
| 107 | + b"0\x00" |
| 108 | + ) |
| 109 | + qubesadmin.tools.qvm_restart.main(["some-vm"], app=self.app) |
| 110 | + self.assertAllCalled() |
| 111 | + |
| 112 | + @unittest.skipUnless( |
| 113 | + qubesadmin.tools.qvm_restart.have_events, "Events not present" |
| 114 | + ) |
| 115 | + def test_002_restart_all(self): |
| 116 | + """Restarting all running qubes (and skipping true DispVMs)""" |
| 117 | + loop = asyncio.new_event_loop() |
| 118 | + asyncio.set_event_loop(loop) |
| 119 | + |
| 120 | + mock_events = unittest.mock.AsyncMock() |
| 121 | + patch = unittest.mock.patch( |
| 122 | + "qubesadmin.events.EventsDispatcher._get_events_reader", mock_events |
| 123 | + ) |
| 124 | + patch.start() |
| 125 | + self.addCleanup(patch.stop) |
| 126 | + mock_events.side_effect = qubesadmin.tests.tools.MockEventsReader( |
| 127 | + [ |
| 128 | + b"1\0\0connection-established\0\0", |
| 129 | + b"1\0some-vm\0domain-shutdown\0\0", |
| 130 | + b"1\0sys-usb\0domain-shutdown\0\0", |
| 131 | + ] |
| 132 | + ) |
| 133 | + |
| 134 | + self.app.expected_calls[("dom0", "admin.vm.List", None, None)] = ( |
| 135 | + b"0\x00some-vm class=AppVM state=Running\n" |
| 136 | + b"dom0 class=AdminVM state=Running\n" |
| 137 | + b"sys-usb class=DispVM state=Running\n" |
| 138 | + b"disp007 class=DispVM state=Running\n" |
| 139 | + b"dormant-vm class=DispVM state=Halted\n" |
| 140 | + ) |
| 141 | + self.app.expected_calls[ |
| 142 | + ("sys-usb", "admin.vm.CurrentState", None, None) |
| 143 | + ] = [b"0\x00power_state=Running"] |
| 144 | + self.app.expected_calls[ |
| 145 | + ("sys-usb", "admin.vm.property.Get", "auto_cleanup", None) |
| 146 | + ] = b"0\x00default=True type=bool False" |
| 147 | + self.app.expected_calls[ |
| 148 | + ("disp007", "admin.vm.CurrentState", None, None) |
| 149 | + ] = [b"0\x00power_state=Running"] |
| 150 | + self.app.expected_calls[ |
| 151 | + ("disp007", "admin.vm.property.Get", "auto_cleanup", None) |
| 152 | + ] = b"0\x00default=True type=bool True" |
| 153 | + self.app.expected_calls[ |
| 154 | + ("dormant-vm", "admin.vm.CurrentState", None, None) |
| 155 | + ] = [b"0\x00power_state=Halted"] |
| 156 | + for vm in ["some-vm", "sys-usb"]: |
| 157 | + self.app.expected_calls[ |
| 158 | + (vm, "admin.vm.Shutdown", "force", None) |
| 159 | + ] = b"0\x00" |
| 160 | + self.app.expected_calls[ |
| 161 | + (vm, "admin.vm.CurrentState", None, None) |
| 162 | + ] = ( |
| 163 | + [b"0\x00power_state=Running"] |
| 164 | + + [b"0\x00power_state=Running"] |
| 165 | + + [b"0\x00power_state=Halted"] |
| 166 | + + [b"0\x00power_state=Halted"] |
| 167 | + ) |
| 168 | + self.app.expected_calls[(vm, "admin.vm.Start", None, None)] = ( |
| 169 | + b"0\x00" |
| 170 | + ) |
| 171 | + qubesadmin.tools.qvm_restart.main(["--all"], app=self.app) |
| 172 | + self.assertAllCalled() |
| 173 | + |
| 174 | + def test_003_restart_dispvm(self): |
| 175 | + """Trying to restart a true DispVM""" |
| 176 | + self.app.expected_calls[("dom0", "admin.vm.List", None, None)] = ( |
| 177 | + b"0\x00some-vm class=AppVM state=Running\n" |
| 178 | + b"dom0 class=AdminVM state=Running\n" |
| 179 | + b"sys-usb class=DispVM state=Running\n" |
| 180 | + b"disp007 class=DispVM state=Running\n" |
| 181 | + b"dormant-vm class=DispVM state=Halted\n" |
| 182 | + ) |
| 183 | + self.app.expected_calls[ |
| 184 | + ("disp007", "admin.vm.property.Get", "auto_cleanup", None) |
| 185 | + ] = b"0\x00default=True type=bool True" |
| 186 | + with self.assertRaises(SystemExit): |
| 187 | + qubesadmin.tools.qvm_restart.main(["disp007"], app=self.app) |
| 188 | + self.assertAllCalled() |
0 commit comments