CWD is incorrectly set if exceptions are thrown

The call to utils.execute ends up in /opt/stack/nova/nova/utils.py which
ultimately calls processutils.execute() in the oslo_concurrency module.
If there's an error when executing the command which calls an bash script
then an exception ProcessExecutionError will be raised.
This patch adds a finally statement to ensure the dir will be switched back
to its original one.

Closes-Bug: #1414530

Change-Id: Ie4e95999795d349a5897f7a180e34187485bd8f1
This commit is contained in:
jichenjc 2015-02-06 02:59:48 +08:00
parent 2495849456
commit 767c461a7f
2 changed files with 14 additions and 2 deletions

View File

@ -120,8 +120,10 @@ def ensure_ca_filesystem():
start = os.getcwd()
fileutils.ensure_tree(ca_dir)
os.chdir(ca_dir)
utils.execute("sh", genrootca_sh_path)
os.chdir(start)
try:
utils.execute("sh", genrootca_sh_path)
finally:
os.chdir(start)
def _generate_fingerprint(public_key_file):

View File

@ -69,6 +69,16 @@ class X509Test(test.TestCase):
dec = crypto.decrypt_text(project_id, enc)
self.assertEqual(text, dec)
@mock.patch.object(utils, 'execute',
side_effect=processutils.ProcessExecutionError)
def test_ensure_ca_filesystem_chdir(self, *args, **kargs):
with utils.tempdir() as tmpdir:
self.flags(ca_path=tmpdir)
start = os.getcwd()
self.assertRaises(processutils.ProcessExecutionError,
crypto.ensure_ca_filesystem)
self.assertEqual(start, os.getcwd())
class RevokeCertsTest(test.TestCase):