From 62900989493fbcb46cc425f0670e23a5924d39ed Mon Sep 17 00:00:00 2001 From: Haiwei Xu Date: Wed, 6 Jan 2016 14:56:24 +0900 Subject: [PATCH] Add test case for common/utils Change-Id: Iffe0bfaac928c415de331c70be64c2c7961c255d --- senlinclient/tests/unit/test_utils.py | 66 ++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/senlinclient/tests/unit/test_utils.py b/senlinclient/tests/unit/test_utils.py index f4607e4c..008e0558 100644 --- a/senlinclient/tests/unit/test_utils.py +++ b/senlinclient/tests/unit/test_utils.py @@ -10,12 +10,74 @@ # License for the specific language governing permissions and limitations # under the License. -from senlinclient.common import utils - +import mock +import six import testtools +from heatclient.common import template_utils +from senlinclient.common import exc +from senlinclient.common.i18n import _ +from senlinclient.common import utils + class shellTest(testtools.TestCase): + def test_format_parameter(self): + params = ['status=ACTIVE;name=cluster1'] + format_params = {'status': 'ACTIVE', 'name': 'cluster1'} + self.assertEqual(format_params, + utils.format_parameters(params)) + + def test_format_parameter_split(self): + params = ['status=ACTIVE', 'name=cluster1'] + format_params = {'status': 'ACTIVE', 'name': 'cluster1'} + self.assertEqual(format_params, + utils.format_parameters(params)) + def test_format_parameter_none(self): self.assertEqual({}, utils.format_parameters(None)) + + def test_format_parameter_bad_format(self): + params = ['status:ACTIVE;name:cluster1'] + ex = self.assertRaises(exc.CommandError, + utils.format_parameters, + params) + msg = _('Malformed parameter(status:ACTIVE). ' + 'Use the key=value format.') + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(template_utils, + 'process_multiple_environments_and_files') + @mock.patch.object(template_utils, 'get_template_contents') + def test_process_stack_spec(self, mock_get_temp, mock_process): + spec = { + 'template': 'temp.yaml', + 'disable_rollback': True, + 'context': { + 'region_name': 'RegionOne' + }, + } + tpl_files = {'fake_key1': 'fake_value1'} + template = mock.Mock() + mock_get_temp.return_value = tpl_files, template + env_files = {'fake_key2': 'fake_value2'} + env = mock.Mock() + mock_process.return_value = env_files, env + new_spec = utils.process_stack_spec(spec) + stack_spec = { + 'disable_rollback': True, + 'context': { + 'region_name': 'RegionOne', + }, + 'parameters': {}, + 'timeout': 60, + 'template': template, + 'files': { + 'fake_key1': 'fake_value1', + 'fake_key2': 'fake_value2', + }, + 'environment': env + } + self.assertEqual(stack_spec, new_spec) + mock_get_temp.assert_called_once_with(template_file='temp.yaml') + mock_process.assert_called_once_with(env_paths=None)