From a246c5576d726c7bc385b49bc7b626eb6edcd137 Mon Sep 17 00:00:00 2001 From: Michael Still Date: Sat, 15 Dec 2012 18:32:20 +1100 Subject: Make NBD retry logic more generic, add retry to loop. Other device implementations require retry logic as well. This change pushes the retry logic up one layer so that its easy to implement for the other drivers that will benefit from it, and then adds retry to the loop driver. This change also adds some unit test coverage for the loop driver. Change-Id: Iab0d42d5075e9d50b4e7eb8c7fcef12cae281b40 --- nova/tests/virt/disk/test_loop.py | 105 ++++++++++++++++++++++++++++++++++++++ nova/tests/virt/disk/test_nbd.py | 4 +- 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 nova/tests/virt/disk/test_loop.py (limited to 'nova/tests') diff --git a/nova/tests/virt/disk/test_loop.py b/nova/tests/virt/disk/test_loop.py new file mode 100644 index 000000000..47f45d565 --- /dev/null +++ b/nova/tests/virt/disk/test_loop.py @@ -0,0 +1,105 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 Michael Still +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import os + +import fixtures +from testtools.matchers import Equals +from testtools.matchers import MatchesListwise + +from nova import test +from nova import utils +from nova.virt.disk.mount import loop + + +def _fake_noop(*args, **kwargs): + return + + +def _fake_trycmd_losetup_works(*args, **kwargs): + return '/dev/loop0', '' + + +def _fake_trycmd_losetup_fails(*args, **kwards): + return '', 'doh' + + +class LoopTestCase(test.TestCase): + def test_get_dev(self): + tempdir = self.useFixture(fixtures.TempDir()).path + l = loop.LoopMount(None, tempdir) + self.useFixture(fixtures.MonkeyPatch('nova.utils.trycmd', + _fake_trycmd_losetup_works)) + self.useFixture(fixtures.MonkeyPatch('nova.utils.execute', + _fake_noop)) + + # No error logged, device consumed + self.assertTrue(l.get_dev()) + self.assertTrue(l.linked) + self.assertEquals('', l.error) + self.assertEquals('/dev/loop0', l.device) + + # Free + l.unget_dev() + self.assertFalse(l.linked) + self.assertEquals('', l.error) + self.assertEquals(None, l.device) + + def test_inner_get_dev_fails(self): + tempdir = self.useFixture(fixtures.TempDir()).path + l = loop.LoopMount(None, tempdir) + self.useFixture(fixtures.MonkeyPatch('nova.utils.trycmd', + _fake_trycmd_losetup_fails)) + + # No error logged, device consumed + self.assertFalse(l._inner_get_dev()) + self.assertFalse(l.linked) + self.assertNotEquals('', l.error) + self.assertEquals(None, l.device) + + # Free + l.unget_dev() + self.assertFalse(l.linked) + self.assertEquals(None, l.device) + + def test_get_dev_timeout(self): + tempdir = self.useFixture(fixtures.TempDir()).path + l = loop.LoopMount(None, tempdir) + self.useFixture(fixtures.MonkeyPatch('time.sleep', _fake_noop)) + self.useFixture(fixtures.MonkeyPatch('nova.utils.trycmd', + _fake_trycmd_losetup_fails)) + self.useFixture(fixtures.MonkeyPatch(('nova.virt.disk.mount.api.' + 'MAX_DEVICE_WAIT'), -10)) + + # Always fail to get a device + def fake_get_dev_fails(): + return False + l._inner_get_dev = fake_get_dev_fails + + # Fail to get a device + self.assertFalse(l.get_dev()) + + def test_unget_dev(self): + tempdir = self.useFixture(fixtures.TempDir()).path + l = loop.LoopMount(None, tempdir) + self.useFixture(fixtures.MonkeyPatch('nova.utils.execute', + _fake_noop)) + + # This just checks that a free of something we don't have doesn't + # throw an exception + l.unget_dev() diff --git a/nova/tests/virt/disk/test_nbd.py b/nova/tests/virt/disk/test_nbd.py index 4b067d405..a73df8a84 100644 --- a/nova/tests/virt/disk/test_nbd.py +++ b/nova/tests/virt/disk/test_nbd.py @@ -268,8 +268,8 @@ class NbdTestCase(test.TestCase): self.fake_exists_one)) self.useFixture(fixtures.MonkeyPatch('nova.utils.trycmd', self.fake_trycmd_creates_pid)) - self.useFixture(fixtures.MonkeyPatch(('nova.virt.disk.mount.nbd.' - 'MAX_NBD_WAIT'), -10)) + self.useFixture(fixtures.MonkeyPatch(('nova.virt.disk.mount.api.' + 'MAX_DEVICE_WAIT'), -10)) # No error logged, device consumed self.assertFalse(n.get_dev()) -- cgit