From 3bef37f881d6b85891a3f3e902678ef88854beb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Fri, 26 Feb 2016 11:02:59 +0100 Subject: [PATCH] tests: PCI passthrough to HVM A simple test which checks if the device is visible there at all. Device set with QUBES_TEST_PCIDEV env variable is used - it should be some unimportant device which can be freely detached from dom0. QubesOS/qubes-issues#1659 --- tests/Makefile | 2 ++ tests/__init__.py | 1 + tests/hardware.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 2f503a15..696ef568 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -31,3 +31,5 @@ endif cp storage.py[co] $(DESTDIR)$(PYTHON_TESTSPATH) cp storage_xen.py $(DESTDIR)$(PYTHON_TESTSPATH) cp storage_xen.py[co] $(DESTDIR)$(PYTHON_TESTSPATH) + cp hardware.py $(DESTDIR)$(PYTHON_TESTSPATH) + cp hardware.py[co] $(DESTDIR)$(PYTHON_TESTSPATH) diff --git a/tests/__init__.py b/tests/__init__.py index 34f9363b..b8b6fdf8 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -696,6 +696,7 @@ def load_tests(loader, tests, pattern): 'qubes.tests.regressions', 'qubes.tests.storage', 'qubes.tests.storage_xen', + 'qubes.tests.hardware', ): tests.addTests(loader.loadTestsFromName(modname)) diff --git a/tests/hardware.py b/tests/hardware.py index e6b28fd8..8ae282e5 100644 --- a/tests/hardware.py +++ b/tests/hardware.py @@ -1 +1,75 @@ -__author__ = 'user' +#!/usr/bin/python2 +# -*- coding: utf-8 -*- +# +# The Qubes OS Project, http://www.qubes-os.org +# +# Copyright (C) 2016 Marek Marczykowski-Górecki +# +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# +import os + +import qubes.tests +import time +import subprocess +from unittest import expectedFailure + + +class TC_00_HVM(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): + def setUp(self): + super(TC_00_HVM, self).setUp() + self.vm = self.qc.add_new_vm("QubesHVm", + name=self.make_vm_name('vm1')) + self.vm.create_on_disk(verbose=False) + + @expectedFailure + def test_000_pci_passthrough_presence(self): + pcidev = os.environ.get('QUBES_TEST_PCIDEV', None) + if pcidev is None: + self.skipTest('Specify PCI device with QUBES_TEST_PCIDEV ' + 'environment variable') + self.vm.pcidevs = [pcidev] + self.vm.pci_strictreset = False + self.qc.save() + self.qc.unlock_db() + + init_script = ( + "#!/bin/sh\n" + "set -e\n" + "lspci -n > /dev/xvdb\n" + "poweroff\n" + ) + + self.prepare_hvm_system_linux(self.vm, init_script, + ['/usr/sbin/lspci']) + self.vm.start() + timeout = 60 + while timeout > 0: + if not self.vm.is_running(): + break + time.sleep(1) + timeout -= 1 + if self.vm.is_running(): + self.fail("Timeout while waiting for VM shutdown") + + with open(self.vm.storage.private_img, 'r') as f: + lspci_vm = f.read(512).strip('\0') + p = subprocess.Popen(['lspci', '-ns', pcidev], stdout=subprocess.PIPE) + (lspci_host, _) = p.communicate() + # strip BDF, as it is different in VM + pcidev_desc = ' '.join(lspci_host.strip().split(' ')[1:]) + self.assertIn(pcidev_desc, lspci_vm)