Merge "Do not verify error messages from jsonpatch in unit tests"

This commit is contained in:
Zuul
2020-07-08 09:06:30 +00:00
committed by Gerrit Code Review
2 changed files with 14 additions and 25 deletions

View File

@@ -353,13 +353,14 @@ class TestPatch(BaseDeployTemplatesAPITest):
mock_save.assert_called_once_with(mock.ANY) mock_save.assert_called_once_with(mock.ANY)
return response return response
def _test_update_bad_request(self, mock_save, patch, error_msg): def _test_update_bad_request(self, mock_save, patch, error_msg=None):
response = self.patch_json('/deploy_templates/%s' % self.template.uuid, response = self.patch_json('/deploy_templates/%s' % self.template.uuid,
patch, expect_errors=True, patch, expect_errors=True,
headers=self.headers) headers=self.headers)
self.assertEqual('application/json', response.content_type) self.assertEqual('application/json', response.content_type)
self.assertEqual(http_client.BAD_REQUEST, response.status_code) self.assertEqual(http_client.BAD_REQUEST, response.status_code)
self.assertTrue(response.json['error_message']) self.assertTrue(response.json['error_message'])
if error_msg:
self.assertRegex(response.json['error_message'], error_msg) self.assertRegex(response.json['error_message'], error_msg)
self.assertFalse(mock_save.called) self.assertFalse(mock_save.called)
return response return response
@@ -537,16 +538,14 @@ class TestPatch(BaseDeployTemplatesAPITest):
'priority': 42 'priority': 42
} }
patch = [{'path': '/steps/1', 'op': 'replace', 'value': step}] patch = [{'path': '/steps/1', 'op': 'replace', 'value': step}]
self._test_update_bad_request( self._test_update_bad_request(mock_save, patch)
mock_save, patch, "list assignment index out of range|"
"can't replace outside of list")
def test_replace_empty_step_list_fail(self, mock_save): def test_replace_empty_step_list_fail(self, mock_save):
patch = [{'path': '/steps', 'op': 'replace', 'value': []}] patch = [{'path': '/steps', 'op': 'replace', 'value': []}]
self._test_update_bad_request( self._test_update_bad_request(
mock_save, patch, 'No deploy steps specified') mock_save, patch, 'No deploy steps specified')
def _test_remove_not_allowed(self, mock_save, field, error_msg): def _test_remove_not_allowed(self, mock_save, field, error_msg=None):
patch = [{'path': '/%s' % field, 'op': 'remove'}] patch = [{'path': '/%s' % field, 'op': 'remove'}]
self._test_update_bad_request(mock_save, patch, error_msg) self._test_update_bad_request(mock_save, patch, error_msg)
@@ -566,8 +565,7 @@ class TestPatch(BaseDeployTemplatesAPITest):
"'/steps' is a mandatory attribute and can not be removed") "'/steps' is a mandatory attribute and can not be removed")
def test_remove_foo(self, mock_save): def test_remove_foo(self, mock_save):
self._test_remove_not_allowed( self._test_remove_not_allowed(mock_save, 'foo')
mock_save, 'foo', "can't remove non-existent object 'foo'")
def test_replace_step_invalid_interface(self, mock_save): def test_replace_step_invalid_interface(self, mock_save):
patch = [{'path': '/steps/0/interface', 'op': 'replace', patch = [{'path': '/steps/0/interface', 'op': 'replace',
@@ -632,14 +630,11 @@ class TestPatch(BaseDeployTemplatesAPITest):
def test_remove_non_existent_property_fail(self, mock_save): def test_remove_non_existent_property_fail(self, mock_save):
patch = [{'path': '/non-existent', 'op': 'remove'}] patch = [{'path': '/non-existent', 'op': 'remove'}]
self._test_update_bad_request( self._test_update_bad_request(mock_save, patch)
mock_save, patch,
"can't remove non-existent object 'non-existent'")
def test_remove_non_existent_step_fail(self, mock_save): def test_remove_non_existent_step_fail(self, mock_save):
patch = [{'path': '/steps/1', 'op': 'remove'}] patch = [{'path': '/steps/1', 'op': 'remove'}]
self._test_update_bad_request( self._test_update_bad_request(mock_save, patch)
mock_save, patch, "can't remove non-existent object '1'")
def test_remove_only_step_fail(self, mock_save): def test_remove_only_step_fail(self, mock_save):
patch = [{'path': '/steps/0', 'op': 'remove'}] patch = [{'path': '/steps/0', 'op': 'remove'}]
@@ -648,9 +643,7 @@ class TestPatch(BaseDeployTemplatesAPITest):
def test_remove_non_existent_step_property_fail(self, mock_save): def test_remove_non_existent_step_property_fail(self, mock_save):
patch = [{'path': '/steps/0/non-existent', 'op': 'remove'}] patch = [{'path': '/steps/0/non-existent', 'op': 'remove'}]
self._test_update_bad_request( self._test_update_bad_request(mock_save, patch)
mock_save, patch,
"can't remove non-existent object 'non-existent'")
def test_add_root_non_existent(self, mock_save): def test_add_root_non_existent(self, mock_save):
patch = [{'path': '/foo', 'value': 'bar', 'op': 'add'}] patch = [{'path': '/foo', 'value': 'bar', 'op': 'add'}]
@@ -665,8 +658,7 @@ class TestPatch(BaseDeployTemplatesAPITest):
'priority': 42 'priority': 42
} }
patch = [{'path': '/steps/2', 'op': 'add', 'value': step}] patch = [{'path': '/steps/2', 'op': 'add', 'value': step}]
self._test_update_bad_request( self._test_update_bad_request(mock_save, patch)
mock_save, patch, "can't insert outside of list")
def test_add_multi(self, mock_save): def test_add_multi(self, mock_save):
steps = [ steps = [

View File

@@ -105,17 +105,14 @@ class TestApiUtils(base.TestCase):
# Raises a KeyError. # Raises a KeyError.
doc = {} doc = {}
patch = [{"op": "remove", "path": "/foo"}] patch = [{"op": "remove", "path": "/foo"}]
self.assertRaisesRegex(exception.PatchError, self.assertRaises(exception.PatchError,
"can't remove non-existent object 'foo'",
utils.apply_jsonpatch, doc, patch) utils.apply_jsonpatch, doc, patch)
def test_apply_jsonpatch_replace_non_existent_list_item(self): def test_apply_jsonpatch_replace_non_existent_list_item(self):
# Raises an IndexError. # Raises an IndexError.
doc = [] doc = []
patch = [{"op": "replace", "path": "/0", "value": 42}] patch = [{"op": "replace", "path": "/0", "value": 42}]
self.assertRaisesRegex(exception.PatchError, self.assertRaises(exception.PatchError,
"can't replace outside of list|"
"list assignment index out of range",
utils.apply_jsonpatch, doc, patch) utils.apply_jsonpatch, doc, patch)
def test_get_patch_values_no_path(self): def test_get_patch_values_no_path(self):