def test_create_invalid_api_version(self): tdict = test_api_utils.post_get_test_deploy_template() response = self.post_json('/deploy_templates', tdict, headers=self.invalid_version_headers, expect_errors=True) self.assertEqual(http_client.METHOD_NOT_ALLOWED, response.status_int)
def test_create(self, mock_utcnow, mock_notify): tdict = test_api_utils.post_get_test_deploy_template() test_time = datetime.datetime(2000, 1, 1, 0, 0) mock_utcnow.return_value = test_time response = self.post_json('/deploy_templates', tdict, headers=self.headers) self.assertEqual(http_client.CREATED, response.status_int) result = self.get_json('/deploy_templates/%s' % tdict['uuid'], headers=self.headers) self.assertEqual(tdict['uuid'], result['uuid']) self.assertFalse(result['updated_at']) return_created_at = timeutils.parse_isotime( result['created_at']).replace(tzinfo=None) self.assertEqual(test_time, return_created_at) # Check location header self.assertIsNotNone(response.location) expected_location = '/v1/deploy_templates/%s' % tdict['uuid'] self.assertEqual(expected_location, urlparse.urlparse(response.location).path) mock_notify.assert_has_calls([mock.call(mock.ANY, mock.ANY, 'create', obj_fields.NotificationLevel.INFO, obj_fields.NotificationStatus.START), mock.call(mock.ANY, mock.ANY, 'create', obj_fields.NotificationLevel.INFO, obj_fields.NotificationStatus.END)])
def test_create(self, mock_utcnow, mock_notify): tdict = test_api_utils.post_get_test_deploy_template() test_time = datetime.datetime(2000, 1, 1, 0, 0) mock_utcnow.return_value = test_time response = self.post_json('/deploy_templates', tdict, headers=self.headers) self.assertEqual(http_client.CREATED, response.status_int) result = self.get_json('/deploy_templates/%s' % tdict['uuid'], headers=self.headers) self.assertEqual(tdict['uuid'], result['uuid']) self.assertFalse(result['updated_at']) return_created_at = timeutils.parse_isotime( result['created_at']).replace(tzinfo=None) self.assertEqual(test_time, return_created_at) # Check location header self.assertIsNotNone(response.location) expected_location = '/v1/deploy_templates/%s' % tdict['uuid'] self.assertEqual(expected_location, urlparse.urlparse(response.location).path) mock_notify.assert_has_calls([mock.call(mock.ANY, mock.ANY, 'create', obj_fields.NotificationLevel.INFO, obj_fields.NotificationStatus.START), mock.call(mock.ANY, mock.ANY, 'create', obj_fields.NotificationLevel.INFO, obj_fields.NotificationStatus.END)])
def test_create_generate_uuid(self, mock_warn, mock_except): tdict = test_api_utils.post_get_test_deploy_template() del tdict['uuid'] response = self.post_json('/deploy_templates', tdict, headers=self.headers) result = self.get_json('/deploy_templates/%s' % response.json['uuid'], headers=self.headers) self.assertTrue(uuidutils.is_uuid_like(result['uuid'])) self.assertFalse(mock_warn.called) self.assertFalse(mock_except.called)
def test_create_generate_uuid(self, mock_warn, mock_except): tdict = test_api_utils.post_get_test_deploy_template() del tdict['uuid'] response = self.post_json('/deploy_templates', tdict, headers=self.headers) result = self.get_json('/deploy_templates/%s' % response.json['uuid'], headers=self.headers) self.assertTrue(uuidutils.is_uuid_like(result['uuid'])) self.assertFalse(mock_warn.called) self.assertFalse(mock_except.called)
def test_create_doesnt_contain_id(self): with mock.patch.object( self.dbapi, 'create_deploy_template', wraps=self.dbapi.create_deploy_template) as mock_create: tdict = test_api_utils.post_get_test_deploy_template() self.post_json('/deploy_templates', tdict, headers=self.headers) self.get_json('/deploy_templates/%s' % tdict['uuid'], headers=self.headers) mock_create.assert_called_once_with(mock.ANY) # Check that 'id' is not in first arg of positional args self.assertNotIn('id', mock_create.call_args[0][0])
def test_create_error(self, mock_create, mock_notify): mock_create.side_effect = Exception() tdict = test_api_utils.post_get_test_deploy_template() self.post_json('/deploy_templates', tdict, headers=self.headers, expect_errors=True) mock_notify.assert_has_calls([mock.call(mock.ANY, mock.ANY, 'create', obj_fields.NotificationLevel.INFO, obj_fields.NotificationStatus.START), mock.call(mock.ANY, mock.ANY, 'create', obj_fields.NotificationLevel.ERROR, obj_fields.NotificationStatus.ERROR)])
def test_create_steps_invalid_duplicate(self): steps = [{ 'interface': 'raid', 'step': 'create_configuration', 'args': { 'foo': '%d' % i }, 'priority': i, } for i in range(2)] tdict = test_api_utils.post_get_test_deploy_template(steps=steps) self._test_create_bad_request(tdict, "Duplicate deploy steps")
def test_create_doesnt_contain_id(self): with mock.patch.object( self.dbapi, 'create_deploy_template', wraps=self.dbapi.create_deploy_template) as mock_create: tdict = test_api_utils.post_get_test_deploy_template() self.post_json('/deploy_templates', tdict, headers=self.headers) self.get_json('/deploy_templates/%s' % tdict['uuid'], headers=self.headers) mock_create.assert_called_once_with(mock.ANY) # Check that 'id' is not in first arg of positional args self.assertNotIn('id', mock_create.call_args[0][0])
def test_create_error(self, mock_create, mock_notify): mock_create.side_effect = Exception() tdict = test_api_utils.post_get_test_deploy_template() self.post_json('/deploy_templates', tdict, headers=self.headers, expect_errors=True) mock_notify.assert_has_calls([mock.call(mock.ANY, mock.ANY, 'create', obj_fields.NotificationLevel.INFO, obj_fields.NotificationStatus.START), mock.call(mock.ANY, mock.ANY, 'create', obj_fields.NotificationLevel.ERROR, obj_fields.NotificationStatus.ERROR)])
def test_create_steps_invalid_duplicate(self): steps = [ { 'interface': 'raid', 'step': 'create_configuration', 'args': {'foo': '%d' % i}, 'priority': i, } for i in range(2) ] tdict = test_api_utils.post_get_test_deploy_template(steps=steps) self._test_create_bad_request(tdict, "Duplicate deploy steps")
def _test_create_no_mandatory_step_field(self, field): tdict = test_api_utils.post_get_test_deploy_template() del tdict['steps'][0][field] self._test_create_bad_request(tdict, "is a required property")
def test_create_invalid_api_version(self): tdict = test_api_utils.post_get_test_deploy_template() response = self.post_json( '/deploy_templates', tdict, headers=self.invalid_version_headers, expect_errors=True) self.assertEqual(http_client.METHOD_NOT_ALLOWED, response.status_int)
def test_create_step_string_priority(self): tdict = test_api_utils.post_get_test_deploy_template() tdict['steps'][0]['priority'] = '42' self._test_create_ok(tdict)
def test_create_standard_trait_name(self): name = 'HW_CPU_X86_VMX' tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_ok(tdict)
def test_create_name_invalid_not_a_trait(self): name = 'not-a-trait' tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_bad_request( tdict, 'Deploy template name must be a valid trait')
def test_create_standard_trait_name(self): name = 'HW_CPU_X86_VMX' tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_ok(tdict)
def _test_create_no_mandatory_step_field(self, field): tdict = test_api_utils.post_get_test_deploy_template() del tdict['steps'][0][field] self._test_create_bad_request(tdict, "Mandatory field missing")
def test_create_name_invalid_not_a_trait(self): name = 'not-a-trait' tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_bad_request( tdict, 'Deploy template name must be a valid trait')
def test_create_name_invalid_too_long(self): name = 'CUSTOM_' + 'X' * 249 tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_bad_request( tdict, 'Deploy template name must be a valid trait')
def _test_create_invalid_field(self, field, value, error_msg): tdict = test_api_utils.post_get_test_deploy_template() tdict[field] = value self._test_create_bad_request(tdict, error_msg)
def _test_create_invalid_field(self, field, value, error_msg): tdict = test_api_utils.post_get_test_deploy_template() tdict[field] = value self._test_create_bad_request(tdict, error_msg)
def test_create_long_name(self): name = 'CUSTOM_' + 'X' * 248 tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_ok(tdict)
def test_create_long_name(self): name = 'CUSTOM_' + 'X' * 248 tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_ok(tdict)
def _test_create_invalid_step_field(self, field, value, error_msg=None): tdict = test_api_utils.post_get_test_deploy_template() tdict['steps'][0][field] = value if error_msg is None: error_msg = "Invalid input for field/attribute" self._test_create_bad_request(tdict, error_msg)
def test_create_name_invalid_too_long(self): name = 'CUSTOM_' + 'X' * 249 tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_bad_request( tdict, 'Deploy template name must be a valid trait')
def test_create_step_string_priority(self): tdict = test_api_utils.post_get_test_deploy_template() tdict['steps'][0]['priority'] = '42' self._test_create_ok(tdict)
def _test_create_no_mandatory_step_field(self, field): tdict = test_api_utils.post_get_test_deploy_template() del tdict['steps'][0][field] self._test_create_bad_request(tdict, "Mandatory field missing")
def test_create_complex_step_args(self): tdict = test_api_utils.post_get_test_deploy_template() tdict['steps'][0]['args'] = {'foo': [{'bar': 'baz'}]} self._test_create_ok(tdict)
def _test_create_invalid_step_field(self, field, value, error_msg=None): tdict = test_api_utils.post_get_test_deploy_template() tdict['steps'][0][field] = value if error_msg is None: error_msg = "Invalid input for field/attribute" self._test_create_bad_request(tdict, error_msg)
def test_create_name_invalid_too_long(self): name = 'CUSTOM_' + 'X' * 249 tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_bad_request( tdict, "'%s' is too long" % name)
def test_create_complex_step_args(self): tdict = test_api_utils.post_get_test_deploy_template() tdict['steps'][0]['args'] = {'foo': [{'bar': 'baz'}]} self._test_create_ok(tdict)
def test_create_name_invalid_not_a_trait(self): name = 'not-a-trait' tdict = test_api_utils.post_get_test_deploy_template(name=name) self._test_create_bad_request( tdict, "'not-a-trait' does not match '^CUSTOM_[A-Z0-9_]+$'")