From 0d0c31af7a992fbb46e41076240a000923e81a83 Mon Sep 17 00:00:00 2001 From: Mikhail Durnosvistov Date: Thu, 5 Jun 2014 15:50:17 +0300 Subject: [PATCH] Check whether specified FS is supported Added to `utils.mkfs()` checking that not supported file systems. Change-Id: If3cff21b8b4ffb969e7bcfc0d9769a36f23061dd Closes-Bug: #1286244 --- ironic/common/exception.py | 5 +++ ironic/common/utils.py | 17 ++++++++- ironic/tests/test_utils.py | 74 +++++++++++++++++++++++++------------- 3 files changed, 71 insertions(+), 25 deletions(-) diff --git a/ironic/common/exception.py b/ironic/common/exception.py index 337ea76ca1..dd20761b8d 100644 --- a/ironic/common/exception.py +++ b/ironic/common/exception.py @@ -472,3 +472,8 @@ class SwiftOperationError(IronicException): class SNMPFailure(IronicException): message = _("SNMP operation '%(operation)s' failed: %(error)s") + + +class FileSystemNotSupported(IronicException): + message = _("Failed to create a file system. " + "File system %(fs)s is not supported.") diff --git a/ironic/common/utils.py b/ironic/common/utils.py index 6fd7d822f6..f0ff70973e 100644 --- a/ironic/common/utils.py +++ b/ironic/common/utils.py @@ -30,6 +30,7 @@ import uuid import netaddr from oslo.config import cfg +from oslo.utils import excutils import paramiko import six @@ -436,7 +437,21 @@ def mkfs(fs, path, label=None): label_opt = '-L' args.extend([label_opt, label]) args.append(path) - execute(*args, run_as_root=True) + try: + env = os.environ.copy() + env['LC_ALL'] = 'C' + execute(*args, run_as_root=True, env_variables=env) + except processutils.ProcessExecutionError as e: + with excutils.save_and_reraise_exception() as ctx: + if os.strerror(errno.ENOENT) in e.stderr: + ctx.reraise = False + LOG.exception(_LE('Failed to make file system. ' + 'File system %s is not supported.'), fs) + raise exception.FileSystemNotSupported(fs=fs) + else: + LOG.exception(_LE('Failed to create a file system ' + 'in %(path)s. Error: %(error)s'), + {'path': path, 'error': e}) def unlink_without_raise(path): diff --git a/ironic/tests/test_utils.py b/ironic/tests/test_utils.py index 23d38478b2..5beae71977 100644 --- a/ironic/tests/test_utils.py +++ b/ironic/tests/test_utils.py @@ -349,33 +349,59 @@ class GenericUtilsTestCase(base.TestCase): class MkfsTestCase(base.TestCase): - def test_mkfs(self): - with mock.patch.object(utils, 'execute') as execute_mock: - utils.mkfs('ext4', '/my/block/dev') - utils.mkfs('msdos', '/my/msdos/block/dev') - utils.mkfs('swap', '/my/swap/block/dev') + @mock.patch.object(os.environ, 'copy') + @mock.patch.object(utils, 'execute') + def test_mkfs(self, execute_mock, mock_env): + lang_env_variable = {'LC_ALL': 'C'} + mock_env.return_value = lang_env_variable + utils.mkfs('ext4', '/my/block/dev') + utils.mkfs('msdos', '/my/msdos/block/dev') + utils.mkfs('swap', '/my/swap/block/dev') - expected = [mock.call('mkfs', '-t', 'ext4', '-F', '/my/block/dev', - run_as_root=True), - mock.call('mkfs', '-t', 'msdos', '/my/msdos/block/dev', - run_as_root=True), - mock.call('mkswap', '/my/swap/block/dev', - run_as_root=True)] - self.assertEqual(expected, execute_mock.call_args_list) + expected = [mock.call('mkfs', '-t', 'ext4', '-F', '/my/block/dev', + run_as_root=True, + env_variables=lang_env_variable), + mock.call('mkfs', '-t', 'msdos', '/my/msdos/block/dev', + run_as_root=True, + env_variables=lang_env_variable), + mock.call('mkswap', '/my/swap/block/dev', + run_as_root=True, + env_variables=lang_env_variable)] + self.assertEqual(expected, execute_mock.call_args_list) - def test_mkfs_with_label(self): - with mock.patch.object(utils, 'execute') as execute_mock: - utils.mkfs('ext4', '/my/block/dev', 'ext4-vol') - utils.mkfs('msdos', '/my/msdos/block/dev', 'msdos-vol') - utils.mkfs('swap', '/my/swap/block/dev', 'swap-vol') + @mock.patch.object(os.environ, 'copy') + @mock.patch.object(utils, 'execute') + def test_mkfs_with_label(self, execute_mock, mock_env): + lang_env_variable = {'LC_ALL': 'C'} + mock_env.return_value = lang_env_variable + utils.mkfs('ext4', '/my/block/dev', 'ext4-vol') + utils.mkfs('msdos', '/my/msdos/block/dev', 'msdos-vol') + utils.mkfs('swap', '/my/swap/block/dev', 'swap-vol') - expected = [mock.call('mkfs', '-t', 'ext4', '-F', '-L', 'ext4-vol', - '/my/block/dev', run_as_root=True), - mock.call('mkfs', '-t', 'msdos', '-n', 'msdos-vol', - '/my/msdos/block/dev', run_as_root=True), - mock.call('mkswap', '-L', 'swap-vol', - '/my/swap/block/dev', run_as_root=True)] - self.assertEqual(expected, execute_mock.call_args_list) + expected = [mock.call('mkfs', '-t', 'ext4', '-F', '-L', 'ext4-vol', + '/my/block/dev', run_as_root=True, + env_variables=lang_env_variable), + mock.call('mkfs', '-t', 'msdos', '-n', 'msdos-vol', + '/my/msdos/block/dev', run_as_root=True, + env_variables=lang_env_variable), + mock.call('mkswap', '-L', 'swap-vol', + '/my/swap/block/dev', run_as_root=True, + env_variables=lang_env_variable)] + self.assertEqual(expected, execute_mock.call_args_list) + + @mock.patch.object(utils, 'execute', + side_effect=processutils.ProcessExecutionError( + stderr=os.strerror(errno.ENOENT))) + def test_mkfs_with_unsupported_fs(self, execute_mock): + self.assertRaises(exception.FileSystemNotSupported, + utils.mkfs, 'foo', '/my/block/dev') + + @mock.patch.object(utils, 'execute', + side_effect=processutils.ProcessExecutionError( + stderr='fake')) + def test_mkfs_with_unexpected_error(self, execute_mock): + self.assertRaises(processutils.ProcessExecutionError, utils.mkfs, + 'ext4', '/my/block/dev', 'ext4-vol') class IntLikeTestCase(base.TestCase):