def test_unget_dev_simple(self): # This test is just checking we don't get an exception when we unget # something we don't have tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) self.useFixture(fixtures.MonkeyPatch('patron.utils.execute', _fake_noop)) n.unget_dev()
def test_nbd_allocation(self): tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) self.useFixture(fixtures.MonkeyPatch('os.path.exists', _fake_exists_no_users)) self.useFixture(fixtures.MonkeyPatch('random.shuffle', _fake_noop)) # Allocate a nbd device self.assertEqual('/dev/nbd0', n._allocate_nbd())
def test_inner_get_dev_qemu_fails(self): tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) self.useFixture(fixtures.MonkeyPatch('os.path.exists', _fake_exists_no_users)) # We have a trycmd that always fails def fake_trycmd(*args, **kwargs): return '', 'broken' self.useFixture(fixtures.MonkeyPatch('patron.utils.trycmd', fake_trycmd)) # Error logged, no device consumed self.assertFalse(n._inner_get_dev()) self.assertTrue(n.error.startswith('qemu-nbd error'))
def test_inner_get_dev_qemu_timeout(self): tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) self.useFixture(fixtures.MonkeyPatch('os.path.exists', _fake_exists_no_users)) # We have a trycmd that always passed def fake_trycmd(*args, **kwargs): return '', '' self.useFixture(fixtures.MonkeyPatch('patron.utils.trycmd', fake_trycmd)) self.useFixture(fixtures.MonkeyPatch('time.sleep', _fake_noop)) # Error logged, no device consumed self.assertFalse(n._inner_get_dev()) self.assertTrue(n.error.endswith('did not show up'))
def test_nbd_not_loaded(self): tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) # Fake out os.path.exists def fake_exists(path): if path.startswith('/sys/block/nbd'): return False return ORIG_EXISTS(path) self.useFixture(fixtures.MonkeyPatch('os.path.exists', fake_exists)) # This should fail, as we don't have the module "loaded" # TODO(mikal): work out how to force english as the gettext language # so that the error check always passes self.assertIsNone(n._allocate_nbd()) self.assertEqual('nbd unavailable: module not loaded', n.error)
def setup(self, mount=True): self.imgdir = tempfile.mkdtemp(prefix="openstack-vfs-localfs") try: if self.imgfmt == "raw": LOG.debug("Using LoopMount") mnt = loop.LoopMount(self.imgfile, self.imgdir, self.partition) else: LOG.debug("Using NbdMount") mnt = nbd.NbdMount(self.imgfile, self.imgdir, self.partition) if mount: if not mnt.do_mount(): raise exception.PatronException(mnt.error) self.mount = mnt except Exception as e: with excutils.save_and_reraise_exception(): LOG.debug("Failed to mount image: %(ex)s", {'ex': e}) self.teardown()
def test_do_mount_need_to_specify_fs_type(self): # NOTE(mikal): Bug 1094373 saw a regression where we failed to # communicate a failed mount properly. def fake_trycmd(*args, **kwargs): return '', 'broken' self.useFixture(fixtures.MonkeyPatch('patron.utils.trycmd', fake_trycmd)) imgfile = tempfile.NamedTemporaryFile() self.addCleanup(imgfile.close) tempdir = self.useFixture(fixtures.TempDir()).path mount = nbd.NbdMount(imgfile.name, tempdir) def fake_returns_true(*args, **kwargs): return True mount.get_dev = fake_returns_true mount.map_dev = fake_returns_true self.assertFalse(mount.do_mount())
def test_get_dev_timeout(self): # Always fail to get a device def fake_get_dev_fails(self): return False self.stubs.Set(nbd.NbdMount, '_inner_get_dev', fake_get_dev_fails) tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) self.useFixture(fixtures.MonkeyPatch('random.shuffle', _fake_noop)) self.useFixture(fixtures.MonkeyPatch('time.sleep', _fake_noop)) self.useFixture(fixtures.MonkeyPatch('patron.utils.execute', _fake_noop)) self.useFixture(fixtures.MonkeyPatch('os.path.exists', self.fake_exists_one)) self.useFixture(fixtures.MonkeyPatch('patron.utils.trycmd', self.fake_trycmd_creates_pid)) self.useFixture(fixtures.MonkeyPatch(('patron.virt.disk.mount.api.' 'MAX_DEVICE_WAIT'), -10)) # No error logged, device consumed self.assertFalse(n.get_dev())
def test_get_dev(self): tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) self.useFixture(fixtures.MonkeyPatch('random.shuffle', _fake_noop)) self.useFixture(fixtures.MonkeyPatch('patron.utils.execute', _fake_noop)) self.useFixture(fixtures.MonkeyPatch('os.path.exists', self.fake_exists_one)) self.useFixture(fixtures.MonkeyPatch('patron.utils.trycmd', self.fake_trycmd_creates_pid)) # No error logged, device consumed self.assertTrue(n.get_dev()) self.assertTrue(n.linked) self.assertEqual('', n.error) self.assertEqual('/dev/nbd0', n.device) # Free n.unget_dev() self.assertFalse(n.linked) self.assertEqual('', n.error) self.assertIsNone(n.device)
def test_nbd_allocation_one_in_use(self): tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) self.useFixture(fixtures.MonkeyPatch('random.shuffle', _fake_noop)) # Fake out os.path.exists def fake_exists(path): if path.startswith('/sys/block/nbd'): if path == '/sys/block/nbd0/pid': return True if path.endswith('pid'): return False return True return ORIG_EXISTS(path) self.useFixture(fixtures.MonkeyPatch('os.path.exists', fake_exists)) # Allocate a nbd device, should not be the in use one # TODO(mikal): Note that there is a leak here, as the in use nbd device # is removed from the list, but not returned so it will never be # re-added. I will fix this in a later patch. self.assertEqual('/dev/nbd1', n._allocate_nbd())
def test_nbd_no_free_devices(self): tempdir = self.useFixture(fixtures.TempDir()).path n = nbd.NbdMount(None, tempdir) self.useFixture(fixtures.MonkeyPatch('os.path.exists', _fake_exists_all_used)) self.assertIsNone(n._allocate_nbd())
def test_nbd_no_devices(self): tempdir = self.useFixture(fixtures.TempDir()).path self.stubs.Set(nbd.NbdMount, '_detect_nbd_devices', _fake_detect_nbd_devices_none) n = nbd.NbdMount(None, tempdir) self.assertIsNone(n._allocate_nbd())
def get_a_device(): n = nbd.NbdMount(None, tempdir) n.get_dev() chosen_devices.append(n.device)
def test_inner_get_dev_no_devices(self): tempdir = self.useFixture(fixtures.TempDir()).path self.stubs.Set(nbd.NbdMount, '_detect_nbd_devices', _fake_detect_nbd_devices_none) n = nbd.NbdMount(None, tempdir) self.assertFalse(n._inner_get_dev())