|
| 1 | +# |
| 2 | +# The Qubes OS Project, https://www.qubes-os.org/ |
| 3 | +# |
| 4 | +# Copyright (C) 2025 Marek Marczykowski-Górecki |
| 5 | + |
| 6 | +# |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 2.1 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 19 | +import asyncio |
| 20 | +import contextlib |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +import unittest |
| 24 | +from distutils import spawn |
| 25 | + |
| 26 | +import qubes |
| 27 | + |
| 28 | + |
| 29 | +class TC_06_AppVMMixin(object): |
| 30 | + template = None |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + super(TC_06_AppVMMixin, self).setUp() |
| 34 | + self.init_default_template(self.template) |
| 35 | + |
| 36 | + def test_010_os_metadata(self): |
| 37 | + tpl = self.app.default_template |
| 38 | + if self.template.startswith("fedora-"): |
| 39 | + self.assertEqual(tpl.features.get("os-distribution"), "fedora") |
| 40 | + version = self.template.split("-")[1] |
| 41 | + self.assertEqual(tpl.features.get("os-version"), version) |
| 42 | + self.assertIsNotNone(tpl.features.get("os-eol")) |
| 43 | + elif self.template.startswith("debian-"): |
| 44 | + self.assertEqual(tpl.features.get("os-distribution"), "debian") |
| 45 | + version = self.template.split("-")[1] |
| 46 | + self.assertEqual(tpl.features.get("os-version"), version) |
| 47 | + self.assertIsNotNone(tpl.features.get("os-eol")) |
| 48 | + elif self.template.startswith("whonix-"): |
| 49 | + self.assertEqual(tpl.features.get("os-distribution"), "whonix") |
| 50 | + self.assertEqual(tpl.features.get("os-distribution-like"), "debian") |
| 51 | + version = self.template.split("-")[2] |
| 52 | + self.assertEqual(tpl.features.get("os-version"), version) |
| 53 | + elif self.template.startswith("kali-core"): |
| 54 | + self.assertEqual(tpl.features.get("os-distribution"), "kali") |
| 55 | + self.assertEqual(tpl.features.get("os-distribution-like"), "debian") |
| 56 | + |
| 57 | + @unittest.skipUnless( |
| 58 | + spawn.find_executable("xdotool"), "xdotool not installed" |
| 59 | + ) |
| 60 | + def test_121_start_standalone_with_cdrom_vm(self): |
| 61 | + cdrom_vmname = self.make_vm_name("cdrom") |
| 62 | + self.cdrom_vm = self.app.add_new_vm( |
| 63 | + "AppVM", label="red", name=cdrom_vmname |
| 64 | + ) |
| 65 | + self.loop.run_until_complete(self.cdrom_vm.create_on_disk()) |
| 66 | + self.loop.run_until_complete(self.cdrom_vm.start()) |
| 67 | + iso_path = self.create_bootable_iso() |
| 68 | + with open(iso_path, "rb") as iso_f: |
| 69 | + self.loop.run_until_complete( |
| 70 | + self.cdrom_vm.run_for_stdio( |
| 71 | + "cat > /home/user/boot.iso", stdin=iso_f |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + vmname = self.make_vm_name("appvm") |
| 76 | + self.vm = self.app.add_new_vm("StandaloneVM", label="red", name=vmname) |
| 77 | + self.loop.run_until_complete(self.vm.create_on_disk()) |
| 78 | + self.vm.kernel = None |
| 79 | + self.vm.virt_mode = "hvm" |
| 80 | + |
| 81 | + # start the VM using qvm-start tool, to test --cdrom option there |
| 82 | + p = self.loop.run_until_complete( |
| 83 | + asyncio.create_subprocess_exec( |
| 84 | + "qvm-start", |
| 85 | + "--cdrom=" + cdrom_vmname + ":/home/user/boot.iso", |
| 86 | + self.vm.name, |
| 87 | + stdout=subprocess.PIPE, |
| 88 | + stderr=subprocess.STDOUT, |
| 89 | + ) |
| 90 | + ) |
| 91 | + (stdout, _) = self.loop.run_until_complete(p.communicate()) |
| 92 | + self.assertEqual(p.returncode, 0, stdout) |
| 93 | + # check if VM do not crash instantly |
| 94 | + self.loop.run_until_complete(asyncio.sleep(50)) |
| 95 | + self.assertTrue(self.vm.is_running()) |
| 96 | + # Type 'halt' |
| 97 | + subprocess.check_call( |
| 98 | + [ |
| 99 | + "xdotool", |
| 100 | + "search", |
| 101 | + "--name", |
| 102 | + self.vm.name, |
| 103 | + "type", |
| 104 | + "--window", |
| 105 | + "%1", |
| 106 | + "halt\r", |
| 107 | + ] |
| 108 | + ) |
| 109 | + for _ in range(10): |
| 110 | + if not self.vm.is_running(): |
| 111 | + break |
| 112 | + self.loop.run_until_complete(asyncio.sleep(1)) |
| 113 | + self.assertFalse(self.vm.is_running()) |
| 114 | + |
| 115 | + |
| 116 | +def create_testcases_for_templates(): |
| 117 | + yield from qubes.tests.create_testcases_for_templates( |
| 118 | + "TC_06_AppVM", |
| 119 | + TC_06_AppVMMixin, |
| 120 | + qubes.tests.SystemTestCase, |
| 121 | + module=sys.modules[__name__], |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def load_tests(loader, tests, pattern): |
| 126 | + tests.addTests(loader.loadTestsFromNames(create_testcases_for_templates())) |
| 127 | + |
| 128 | + return tests |
| 129 | + |
| 130 | + |
| 131 | +qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates) |
| 132 | + |
| 133 | +# vim: ts=4 sw=4 et |
0 commit comments