From 4115068f8b50b694094adf8e68cd0da7a9ca5eb3 Mon Sep 17 00:00:00 2001 From: Devananda van der Veen Date: Thu, 17 Jan 2013 13:30:58 -0800 Subject: [PATCH] Baremetal/utils should not log certain exceptions unlink_without_raise was logging exceptions when it failed to unlink a file, which is confusing since it is often called on non-existing files. create_link_without_raise was also logging exceptions when it failed to create a symlink. This patch corrects this behaviour; both functions now explicitly check for the type of error they expect, and then suppress that. If another type of OSError is encountered, a warning is logged. This patch also adds unit tests for both functions. fixes bug 1097931. Change-Id: Ie36f59dce34a6c67765770c5f968c003003acc88 --- nova/tests/baremetal/test_utils.py | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/nova/tests/baremetal/test_utils.py b/nova/tests/baremetal/test_utils.py index 827b1fca..2615a26c 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()