diff options
-rw-r--r-- | ceilometerclient/tests/unit/v2/test_shell.py | 10 | ||||
-rw-r--r-- | ceilometerclient/v2/shell.py | 7 |
2 files changed, 16 insertions, 1 deletions
diff --git a/ceilometerclient/tests/unit/v2/test_shell.py b/ceilometerclient/tests/unit/v2/test_shell.py index 663cf24..c4a61a8 100644 --- a/ceilometerclient/tests/unit/v2/test_shell.py +++ b/ceilometerclient/tests/unit/v2/test_shell.py @@ -576,6 +576,16 @@ class ShellSampleCreateCommandTest(utils.BaseTestCase): +-------------------+---------------------------------------------+ ''', sys.stdout.getvalue()) + def test_sample_create_with_invalid_resource_metadata(self): + self.args.resource_metadata = 'foo=bar' + with mock.patch('ceilometerclient.exc.CommandError') as e: + e.return_value = exc.BaseException() + self.assertRaises(exc.BaseException, + ceilometer_shell.do_sample_create, + self.cc, self.args) + e.assert_called_with('Invalid resource metadata, it should be a' + ' json string, like: \'{"foo":"bar"}\'') + class ShellSampleCreateListCommandTest(utils.BaseTestCase): diff --git a/ceilometerclient/v2/shell.py b/ceilometerclient/v2/shell.py index b67d765..7b0e89e 100644 --- a/ceilometerclient/v2/shell.py +++ b/ceilometerclient/v2/shell.py @@ -231,7 +231,12 @@ def do_sample_create(cc, args={}): k, v = var[0], var[1] if v is not None: if k == 'resource_metadata': - fields[k] = json.loads(v) + try: + fields[k] = json.loads(v) + except ValueError: + msg = ('Invalid resource metadata, it should be a json' + ' string, like: \'{"foo":"bar"}\'') + raise exc.CommandError(msg) else: fields[arg_to_field_mapping.get(k, k)] = v sample = cc.samples.create(**fields) |