Merge "lxc: make use of filter python3 compatible"

This commit is contained in:
Zuul 2019-08-14 18:25:56 +00:00 committed by Gerrit Code Review
commit 7bcf8c5a7e
2 changed files with 28 additions and 5 deletions

View File

@ -51,11 +51,11 @@ def _fake_exists_all_used(path):
return ORIG_EXISTS(path)
def _fake_detect_nbd_devices_none(self):
def _fake_detect_nbd_devices_none():
return []
def _fake_detect_nbd_devices(self):
def _fake_detect_nbd_devices():
return ['nbd0', 'nbd1']
@ -63,6 +63,28 @@ def _fake_noop(*args, **kwargs):
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):
def setUp(self):
super(NbdTestCase, self).setUp()
@ -274,7 +296,7 @@ class NbdTestCase(test.NoDBTestCase):
# they cannot choose the same nbd number (see bug 1207422)
tempdir = self.useFixture(fixtures.TempDir()).path
free_devices = _fake_detect_nbd_devices(None)[:]
free_devices = _fake_detect_nbd_devices()[:]
chosen_devices = []
def fake_find_unused(self):

View File

@ -39,9 +39,10 @@ class NbdMount(api.Mount):
"""qemu-nbd support disk images."""
mode = 'nbd'
def _detect_nbd_devices(self):
@staticmethod
def _detect_nbd_devices():
"""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):
for device in devices: