lxc: make use of filter python3 compatible
_detect_nbd_devices uses the filter builtin internally to filter valid devices. In python 2, filter returns a list. In python 3, filter returns an iterable or generator function. This change eagerly converts the result of calling filter to a list to preserve the python 2 behaviour under python 3. Closes-Bug: #1840068 Change-Id: I25616c5761ea625a15d725777ae58175651558f8
This commit is contained in:
parent
e3a55493ba
commit
fc9fb383c1
|
@ -51,11 +51,11 @@ def _fake_exists_all_used(path):
|
||||||
return ORIG_EXISTS(path)
|
return ORIG_EXISTS(path)
|
||||||
|
|
||||||
|
|
||||||
def _fake_detect_nbd_devices_none(self):
|
def _fake_detect_nbd_devices_none():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def _fake_detect_nbd_devices(self):
|
def _fake_detect_nbd_devices():
|
||||||
return ['nbd0', 'nbd1']
|
return ['nbd0', 'nbd1']
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,6 +63,28 @@ def _fake_noop(*args, **kwargs):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
class NbdTestCaseNoStub(test.NoDBTestCase):
|
||||||
|
|
||||||
|
@mock.patch('os.listdir')
|
||||||
|
def test_detect_nbd_devices(self, list_dir_mock):
|
||||||
|
list_dir_mock.return_value = _fake_detect_nbd_devices()
|
||||||
|
result = nbd.NbdMount._detect_nbd_devices()
|
||||||
|
self.assertIsNotNone(result)
|
||||||
|
self.assertIsInstance(result, list)
|
||||||
|
self.assertEqual(len(list_dir_mock.return_value), len(result))
|
||||||
|
for path in list_dir_mock.return_value:
|
||||||
|
self.assertIn(path, result)
|
||||||
|
|
||||||
|
@mock.patch('os.listdir')
|
||||||
|
def test_detect_nbd_devices_empty(self, list_dir_mock):
|
||||||
|
list_dir_mock.return_value = [
|
||||||
|
"nbdz", "fake0", "not-nbd1"]
|
||||||
|
result = nbd.NbdMount._detect_nbd_devices()
|
||||||
|
self.assertIsNotNone(result)
|
||||||
|
self.assertIsInstance(result, list)
|
||||||
|
self.assertEqual(0, len(result))
|
||||||
|
|
||||||
|
|
||||||
class NbdTestCase(test.NoDBTestCase):
|
class NbdTestCase(test.NoDBTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(NbdTestCase, self).setUp()
|
super(NbdTestCase, self).setUp()
|
||||||
|
@ -274,7 +296,7 @@ class NbdTestCase(test.NoDBTestCase):
|
||||||
# they cannot choose the same nbd number (see bug 1207422)
|
# they cannot choose the same nbd number (see bug 1207422)
|
||||||
|
|
||||||
tempdir = self.useFixture(fixtures.TempDir()).path
|
tempdir = self.useFixture(fixtures.TempDir()).path
|
||||||
free_devices = _fake_detect_nbd_devices(None)[:]
|
free_devices = _fake_detect_nbd_devices()[:]
|
||||||
chosen_devices = []
|
chosen_devices = []
|
||||||
|
|
||||||
def fake_find_unused(self):
|
def fake_find_unused(self):
|
||||||
|
|
|
@ -39,9 +39,10 @@ class NbdMount(api.Mount):
|
||||||
"""qemu-nbd support disk images."""
|
"""qemu-nbd support disk images."""
|
||||||
mode = 'nbd'
|
mode = 'nbd'
|
||||||
|
|
||||||
def _detect_nbd_devices(self):
|
@staticmethod
|
||||||
|
def _detect_nbd_devices():
|
||||||
"""Detect nbd device files."""
|
"""Detect nbd device files."""
|
||||||
return filter(NBD_DEVICE_RE.match, os.listdir('/sys/block/'))
|
return list(filter(NBD_DEVICE_RE.match, os.listdir('/sys/block/')))
|
||||||
|
|
||||||
def _find_unused(self, devices):
|
def _find_unused(self, devices):
|
||||||
for device in devices:
|
for device in devices:
|
||||||
|
|
Loading…
Reference in New Issue