Merge "Check whether specified FS is supported"

This commit is contained in:
Jenkins 2014-09-18 10:35:44 +00:00 committed by Gerrit Code Review
commit 99771d1d25
3 changed files with 71 additions and 25 deletions

View File

@ -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.")

View File

@ -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):

View File

@ -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):