diff options
-rw-r--r-- | nova/tests/baremetal/test_utils.py | 36 | ||||
-rw-r--r-- | nova/virt/baremetal/utils.py | 17 |
2 files changed, 48 insertions, 5 deletions
diff --git a/nova/tests/baremetal/test_utils.py b/nova/tests/baremetal/test_utils.py index 827b1fcaf..2615a26cb 100644 --- a/nova/tests/baremetal/test_utils.py +++ b/nova/tests/baremetal/test_utils.py @@ -20,6 +20,9 @@ import mox +import errno +import os + from nova import exception from nova import test from nova.virt.baremetal import utils @@ -32,3 +35,36 @@ class BareMetalUtilsTestCase(test.TestCase): self.assertEqual(len(s), 10) s = utils.random_alnum(100) self.assertEqual(len(s), 100) + + def test_unlink(self): + self.mox.StubOutWithMock(os, "unlink") + os.unlink("/fake/path") + + self.mox.ReplayAll() + utils.unlink_without_raise("/fake/path") + self.mox.VerifyAll() + + def test_unlink_ENOENT(self): + self.mox.StubOutWithMock(os, "unlink") + os.unlink("/fake/path").AndRaise(OSError(errno.ENOENT)) + + self.mox.ReplayAll() + utils.unlink_without_raise("/fake/path") + self.mox.VerifyAll() + + def test_create_link(self): + self.mox.StubOutWithMock(os, "symlink") + os.symlink("/fake/source", "/fake/link") + + self.mox.ReplayAll() + utils.create_link_without_raise("/fake/source", "/fake/link") + self.mox.VerifyAll() + + def test_create_link_EEXIST(self): + self.mox.StubOutWithMock(os, "symlink") + os.symlink("/fake/source", "/fake/link").AndRaise( + OSError(errno.EEXIST)) + + self.mox.ReplayAll() + utils.create_link_without_raise("/fake/source", "/fake/link") + self.mox.VerifyAll() diff --git a/nova/virt/baremetal/utils.py b/nova/virt/baremetal/utils.py index 0842ae201..6ce42eb6e 100644 --- a/nova/virt/baremetal/utils.py +++ b/nova/virt/baremetal/utils.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import errno import os from nova.openstack.common import log as logging @@ -43,8 +44,11 @@ def inject_into_image(image, key, net, metadata, admin_password, def unlink_without_raise(path): try: os.unlink(path) - except OSError: - LOG.exception(_("Failed to unlink %s") % path) + except OSError, e: + if e.errno == errno.ENOENT: + return + else: + LOG.warn(_("Failed to unlink %(path)s, error: %(e)s") % locals()) def write_to_file(path, contents): @@ -55,9 +59,12 @@ def write_to_file(path, contents): def create_link_without_raise(source, link): try: os.symlink(source, link) - except OSError: - LOG.exception(_("Failed to create symlink from %(source)s to %(link)s") - % locals()) + except OSError, e: + if e.errno == errno.EEXIST: + return + else: + LOG.warn(_("Failed to create symlink from %(source)s to %(link)s" + ", error: %(e)s") % locals()) def random_alnum(count): |