diff options
author | Angus Salkeld <asalkeld@redhat.com> | 2013-07-16 12:24:35 +1000 |
---|---|---|
committer | Angus Salkeld <asalkeld@redhat.com> | 2013-07-16 12:24:35 +1000 |
commit | 710f7512bbb9074cc31219f8d02153d7e747476d (patch) | |
tree | b48ab602f8557014194d471961023cb91f877c4e | |
parent | ff2033a8c1e0b7cac722890424d3d8ac32952415 (diff) | |
download | python-heatclient-710f7512bbb9074cc31219f8d02153d7e747476d.tar.gz |
Make the parameter checking consistent.
Make format_parameters() raise CommandError with
the same message. Rather than raise MalformedRequestBody
and then catch that and raise CommandError. This just
simplifies things.
Change-Id: Ic05af32dd916e953e6b162daccc4525cb4734a46
-rw-r--r-- | heatclient/common/utils.py | 4 | ||||
-rw-r--r-- | heatclient/exc.py | 5 | ||||
-rw-r--r-- | heatclient/tests/test_shell.py | 47 | ||||
-rw-r--r-- | heatclient/tests/test_utils.py | 2 | ||||
-rw-r--r-- | heatclient/v1/shell.py | 8 |
5 files changed, 52 insertions, 14 deletions
diff --git a/heatclient/common/utils.py b/heatclient/common/utils.py index f22fba3..7a94598 100644 --- a/heatclient/common/utils.py +++ b/heatclient/common/utils.py @@ -145,7 +145,9 @@ def format_parameters(params): try: (n, v) = p.split(('='), 1) except ValueError: - raise exc.MalformedRequestBody() + msg = '%s(%s). %s.' % ('Malformed parameter', p, + 'Use the key=value format') + raise exc.CommandError(msg) parameters[n] = v return parameters diff --git a/heatclient/exc.py b/heatclient/exc.py index ddadfb7..c90b0fc 100644 --- a/heatclient/exc.py +++ b/heatclient/exc.py @@ -39,11 +39,6 @@ class HTTPException(BaseException): code = 'N/A' -class MalformedRequestBody(BaseException): - """Malformed parameter in request.""" - pass - - class HTTPMultipleChoices(HTTPException): code = 300 diff --git a/heatclient/tests/test_shell.py b/heatclient/tests/test_shell.py index 4b891ad..61321fb 100644 --- a/heatclient/tests/test_shell.py +++ b/heatclient/tests/test_shell.py @@ -93,6 +93,53 @@ class EnvVarTest(TestCase): self.shell_error('list', self.err) +class ShellParamValidationTest(TestCase): + + scenarios = [ + ('create', dict( + command='create ts -P "a!b"', + err='Malformed parameter')), + ('stack-create', dict( + command='stack-create ts -P "ab"', + err='Malformed parameter')), + ('update', dict( + command='update ts -P "a~b"', + err='Malformed parameter')), + ('stack-update', dict( + command='stack-update ts -P "a-b"', + err='Malformed parameter')), + ('validate', dict( + command='validate -P "a=b;c"', + err='Malformed parameter')), + ('template-validate', dict( + command='template-validate -P "a$b"', + err='Malformed parameter')), + ] + + def setUp(self): + super(ShellParamValidationTest, self).setUp() + self.m = mox.Mox() + self.addCleanup(self.m.VerifyAll) + self.addCleanup(self.m.UnsetStubs) + + def test_bad_parameters(self): + self.m.StubOutWithMock(ksclient, 'Client') + self.m.StubOutWithMock(v1client.Client, 'json_request') + fakes.script_keystone_client() + + self.m.ReplayAll() + fake_env = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': 'password', + 'OS_TENANT_NAME': 'tenant_name', + 'OS_AUTH_URL': 'http://no.where', + } + self.set_fake_env(fake_env) + template_file = os.path.join(TEST_VAR_DIR, 'minimal.template') + cmd = '%s --template-file=%s ' % (self.command, template_file) + self.shell_error(cmd, self.err) + + class ShellValidationTest(TestCase): def setUp(self): diff --git a/heatclient/tests/test_utils.py b/heatclient/tests/test_utils.py index 3de7aab..05e97fa 100644 --- a/heatclient/tests/test_utils.py +++ b/heatclient/tests/test_utils.py @@ -47,7 +47,7 @@ class shellTest(testtools.TestCase): def test_format_parameter_bad_parameter(self): params = 'KeyName=heat_key;UpstreamDNS8.8.8.8' - self.assertRaises(exc.MalformedRequestBody, + self.assertRaises(exc.CommandError, utils.format_parameters, params) def test_link_formatter(self): diff --git a/heatclient/v1/shell.py b/heatclient/v1/shell.py index 07092da..6b80e99 100644 --- a/heatclient/v1/shell.py +++ b/heatclient/v1/shell.py @@ -130,16 +130,10 @@ def do_create(hc, args): help='Name of the stack to create.') def do_stack_create(hc, args): '''Create the stack.''' - try: - parameters = utils.format_parameters(args.parameters) - except exc.MalformedRequestBody: - msg = "Malformed parameters. Parameters should have key=value format" - raise exc.CommandError(msg=msg) - fields = {'stack_name': args.name, 'timeout_mins': args.create_timeout, 'disable_rollback': not(args.enable_rollback), - 'parameters': parameters} + 'parameters': utils.format_parameters(args.parameters)} _set_template_fields(hc, args, fields) _process_environment_and_files(hc, args, fields) |