Skip to content

Commit e73d56d

Browse files
committed
q-dev: fix tests and make linter happy
1 parent c7d244a commit e73d56d

File tree

9 files changed

+46
-23
lines changed

9 files changed

+46
-23
lines changed

qubes/api/admin.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import string
2828
import subprocess
2929
import pathlib
30-
import sys
3130

3231
import libvirt
3332
import lxml.etree
@@ -46,8 +45,8 @@
4645
import qubes.vm
4746
import qubes.vm.adminvm
4847
import qubes.vm.qubesvm
49-
from qubes.device_protocol import (Port, VirtualDevice, UnknownDevice,
50-
DeviceAssignment)
48+
from qubes.device_protocol import (
49+
VirtualDevice, UnknownDevice, DeviceAssignment)
5150

5251

5352
class QubesMgmtEventsDispatcher:

qubes/device_protocol.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,7 @@ def device(self) -> DeviceInfo:
11911191
return devices[0]
11921192
if len(devices) > 1:
11931193
raise ProtocolError("Too many devices matches to assignment")
1194-
if len(devices) == 0:
1195-
raise ProtocolError("Any devices matches to assignment")
1194+
raise ProtocolError("Any devices matches to assignment")
11961195

11971196
@property
11981197
def port(self) -> Port:

qubes/ext/admin.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# You should have received a copy of the GNU Lesser General Public
1818
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
1919
import importlib
20-
import sys
2120

2221
import qubes.api
2322
import qubes.api.internal
@@ -171,22 +170,23 @@ def on_tag_add(self, vm, event, tag, **kwargs):
171170
def on_device_attach(
172171
self, vm, event, dest, arg, device, mode, options, **kwargs
173172
):
173+
# pylint: disable=unused-argument
174174
# ignore auto-attachment
175175
if mode != 'manual':
176176
return
177177

178178
# load device deny list
179179
deny = {}
180180
try:
181-
with open(DEVICE_DENY_LIST, 'r') as file:
181+
with open(DEVICE_DENY_LIST, 'r', encoding="utf-8") as file:
182182
for line in file:
183183
line = line.strip()
184184

185185
if line:
186186
name, *values = line.split()
187187

188188
values = ' '.join(values).replace(',', ' ').split()
189-
values = set([v for v in values if len(v) > 0])
189+
values = {v for v in values if len(v) > 0}
190190

191191
deny[name] = deny.get(name, set()).union(set(values))
192192
except IOError:
@@ -198,4 +198,3 @@ def on_device_attach(
198198
for devint in device.interfaces:
199199
if pattern.matches(devint):
200200
raise qubes.exc.PermissionDenied()
201-

qubes/ext/pci.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ def interfaces(self) -> List[qubes.device_protocol.DeviceInterface]:
223223
Every device should have at least one interface.
224224
"""
225225
if self._interfaces is None:
226+
if self.backend_domain.app.vmm.offline_mode:
227+
# don't cache this value
228+
return [qubes.device_protocol.DeviceInterface(
229+
'******', devclass='pci')]
226230
hostdev_details = \
227231
self.backend_domain.app.vmm.libvirt_conn.nodeDeviceLookupByName(
228232
self.libvirt_name
@@ -287,7 +291,9 @@ def _load_desc(self) -> Dict[str, str]:
287291
"manufacturer": unknown,
288292
"name": unknown,
289293
"serial": unknown}
290-
if not self.backend_domain.is_running():
294+
if (not self.backend_domain.is_running()
295+
or self.backend_domain.app.vmm.offline_mode
296+
):
291297
# don't cache these values
292298
return result
293299
hostdev_details = \

qubes/ext/utils.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,16 @@ def compare_device_cache(vm, devices_cache, current_devices):
141141

142142

143143
def confirm_device_attachment(device, frontends) -> str:
144-
guivm = 'dom0' # TODO
145-
146144
try:
145+
# pylint: disable=consider-using-with
147146
proc = subprocess.Popen(
148-
["attach-confirm", guivm,
149-
device.backend_domain.name, device.port_id,
150-
device.description,
147+
["attach-confirm", device.backend_domain.name,
148+
device.port_id, device.description,
151149
*[f.name for f in frontends.keys()]],
152-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
150+
stdout=subprocess.PIPE, stderr=subprocess.PIPE
151+
)
153152
(target_name, _) = proc.communicate()
154153
return target_name.decode()
155154
except Exception as exc:
156-
print(exc, file=sys.stderr)
155+
print("attach-confirm", exc, file=sys.stderr)
157156
return ""

qubes/tests/devices_pci.py

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def setUp(self):
126126
def test_000_unsupported_device(self):
127127
vm = TestVM()
128128
vm.app.configure_mock(**{
129+
'vmm.offline_mode': False,
129130
'vmm.libvirt_conn.nodeDeviceLookupByName.return_value':
130131
mock.Mock(**{"XMLDesc.return_value":
131132
PCI_XML.format(*["0000"] * 3)

qubes/tests/vm/init.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable=protected-access
2+
import sys
23

34
#
45
# The Qubes OS Project, https://www.qubes-os.org/
@@ -48,6 +49,10 @@ class TestVM(qubes.vm.BaseVM):
4849
testlabel = qubes.property('testlabel')
4950
defaultprop = qubes.property('defaultprop', default='defaultvalue')
5051

52+
def is_running(self):
53+
return False
54+
55+
5156
class TC_10_BaseVM(qubes.tests.QubesTestCase):
5257
def setUp(self):
5358
super().setUp()
@@ -110,8 +115,10 @@ def test_000_load(self):
110115
})
111116

112117
self.assertCountEqual(vm.devices.keys(), ('pci',))
113-
self.assertCountEqual(list(vm.devices['pci'].get_assigned_devices()),
114-
[qubes.ext.pci.PCIDevice(vm, '00_11.22')])
118+
119+
self.assertTrue(
120+
list(vm.devices['pci'].get_assigned_devices())[0].matches(
121+
qubes.ext.pci.PCIDevice(vm, '00_11.22', )))
115122

116123
assignments = list(vm.devices['pci'].get_assigned_devices())
117124
self.assertEqual(len(assignments), 1)

qubes/tests/vm/qubesvm.py

+14
Original file line numberDiff line numberDiff line change
@@ -1893,23 +1893,37 @@ def test_615_libvirt_xml_block_devices(self):
18931893
'options': {'frontend-dev': 'xvdl'},
18941894
'device.device_node': '/dev/sdb',
18951895
'device.backend_domain.name': 'dom0',
1896+
'devices': [unittest.mock.Mock(**{
1897+
'device_node': '/dev/sdb',
1898+
'backend_domain.name': 'dom0',})]
18961899
}),
18971900
unittest.mock.Mock(**{
18981901
'options': {'devtype': 'cdrom'},
18991902
'device.device_node': '/dev/sda',
19001903
'device.backend_domain.name': 'dom0',
1904+
'devices': [unittest.mock.Mock(**{
1905+
'device_node': '/dev/sda',
1906+
'backend_domain.name': 'dom0', })]
19011907
}),
19021908
unittest.mock.Mock(**{
19031909
'options': {'read-only': True},
19041910
'device.device_node': '/dev/loop0',
19051911
'device.backend_domain.name': 'backend0',
19061912
'device.backend_domain.features.check_with_template.return_value': '4.2',
1913+
'devices': [unittest.mock.Mock(**{
1914+
'device_node': '/dev/loop0',
1915+
'backend_domain.name': 'backend0',
1916+
'backend_domain.features.check_with_template.return_value': '4.2'})]
19071917
}),
19081918
unittest.mock.Mock(**{
19091919
'options': {},
19101920
'device.device_node': '/dev/loop0',
19111921
'device.backend_domain.name': 'backend1',
19121922
'device.backend_domain.features.check_with_template.return_value': '4.2',
1923+
'devices': [unittest.mock.Mock(**{
1924+
'device_node': '/dev/loop0',
1925+
'backend_domain.name': 'backend1',
1926+
'backend_domain.features.check_with_template.return_value': '4.2'})]
19131927
}),
19141928
]
19151929
vm.devices['block'].get_assigned_devices = \

qubes/vm/qubesvm.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1182,10 +1182,9 @@ async def start(self, start_guid=True, notify_function=None,
11821182
if not ass.required:
11831183
continue
11841184
for device in ass.devices:
1185-
if isinstance(device,
1186-
qubes.device_protocol.UnknownDevice):
1187-
continue
1188-
else:
1185+
if not isinstance(
1186+
device, qubes.device_protocol.UnknownDevice
1187+
):
11891188
break
11901189
else:
11911190
raise qubes.exc.QubesException(

0 commit comments

Comments
 (0)