Merge "lxc: make use of filter python3 compatible" into stable/stein

This commit is contained in:
Zuul 2019-08-27 15:37:40 +00:00 committed by Gerrit Code Review
commit a1157700dc
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) 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()
@ -272,7 +294,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):

View File

@ -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: