def test_create_allocation(self, mock_utcnow, mock_notify): adict = apiutils.allocation_post_data() test_time = datetime.datetime(2000, 1, 1, 0, 0) mock_utcnow.return_value = test_time response = self.post_json('/allocations', adict, headers=self.headers) self.assertEqual(http_client.CREATED, response.status_int) self.assertEqual(adict['uuid'], response.json['uuid']) self.assertEqual('allocating', response.json['state']) self.assertIsNone(response.json['node_uuid']) self.assertEqual([], response.json['candidate_nodes']) self.assertEqual([], response.json['traits']) result = self.get_json('/allocations/%s' % adict['uuid'], headers=self.headers) self.assertEqual(adict['uuid'], result['uuid']) self.assertFalse(result['updated_at']) self.assertIsNone(result['node_uuid']) self.assertEqual([], result['candidate_nodes']) self.assertEqual([], result['traits']) 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/allocations/%s' % adict['uuid'] self.assertEqual( urlparse.urlparse(response.location).path, expected_location) 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_allocation(self, mock_utcnow, mock_notify): adict = apiutils.allocation_post_data() test_time = datetime.datetime(2000, 1, 1, 0, 0) mock_utcnow.return_value = test_time response = self.post_json('/allocations', adict, headers=self.headers) self.assertEqual(http_client.CREATED, response.status_int) self.assertEqual(adict['uuid'], response.json['uuid']) self.assertEqual('allocating', response.json['state']) self.assertIsNone(response.json['node_uuid']) self.assertEqual([], response.json['candidate_nodes']) self.assertEqual([], response.json['traits']) result = self.get_json('/allocations/%s' % adict['uuid'], headers=self.headers) self.assertEqual(adict['uuid'], result['uuid']) self.assertFalse(result['updated_at']) self.assertIsNone(result['node_uuid']) self.assertEqual([], result['candidate_nodes']) self.assertEqual([], result['traits']) 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/allocations/%s' % adict['uuid'] self.assertEqual(urlparse.urlparse(response.location).path, expected_location) 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_allocation_name_ok(self): name = 'foo' adict = apiutils.allocation_post_data(name=name) self.post_json('/allocations', adict, headers=self.headers) result = self.get_json('/allocations/%s' % adict['uuid'], headers=self.headers) self.assertEqual(name, result['name'])
def test_create_allocation_invalid_api_version(self): adict = apiutils.allocation_post_data() response = self.post_json('/allocations', adict, headers={api_base.Version.string: '1.50'}, expect_errors=True) self.assertEqual(http_client.NOT_FOUND, response.status_int)
def test_create_with_node_uuid_not_allowed(self): adict = apiutils.allocation_post_data() adict['node_uuid'] = uuidutils.generate_uuid() response = self.post_json('/allocations', adict, expect_errors=True, headers=self.headers) self.assertEqual(http_client.BAD_REQUEST, response.status_int) self.assertEqual('application/json', response.content_type) self.assertTrue(response.json['error_message'])
def test_create_allocation_candidate_node_invalid(self): adict = apiutils.allocation_post_data( candidate_nodes=['this/is/not a/node/name']) response = self.post_json('/allocations', adict, expect_errors=True, headers=self.headers) self.assertEqual('application/json', response.content_type) self.assertEqual(http_client.BAD_REQUEST, response.status_int) self.assertTrue(response.json['error_message'])
def test_create_allocation_invalid_trait(self): adict = apiutils.allocation_post_data() adict['traits'] = ['CUSTOM_GPU', 'FOO_BAR'] response = self.post_json('/allocations', adict, expect_errors=True, headers=self.headers) self.assertEqual(http_client.BAD_REQUEST, response.status_int) self.assertEqual('application/json', response.content_type) self.assertTrue(response.json['error_message'])
def test_create_allocation_resource_class_too_long(self): adict = apiutils.allocation_post_data() adict['resource_class'] = 'f' * 81 response = self.post_json('/allocations', adict, expect_errors=True, headers=self.headers) self.assertEqual(http_client.BAD_REQUEST, response.status_int) self.assertEqual('application/json', response.content_type) self.assertTrue(response.json['error_message'])
def test_create_allocation_valid_extra(self): adict = apiutils.allocation_post_data( extra={'str': 'foo', 'int': 123, 'float': 0.1, 'bool': True, 'list': [1, 2], 'none': None, 'dict': {'cat': 'meow'}}) self.post_json('/allocations', adict, headers=self.headers) result = self.get_json('/allocations/%s' % adict['uuid'], headers=self.headers) self.assertEqual(adict['extra'], result['extra'])
def test_create_by_node_not_allowed(self): node = obj_utils.create_test_node(self.context) adict = apiutils.allocation_post_data() response = self.post_json('/nodes/%s/allocation' % node.uuid, adict, headers=self.headers, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(http_client.METHOD_NOT_ALLOWED, response.status_int)
def test_create_allocation_name_invalid(self): name = 'aa:bb_cc' adict = apiutils.allocation_post_data(name=name) response = self.post_json('/allocations', adict, headers=self.headers, expect_errors=True) self.assertEqual(http_client.BAD_REQUEST, response.status_int)
def test_create_allocation_generate_uuid(self, mock_warn, mock_except): adict = apiutils.allocation_post_data() del adict['uuid'] response = self.post_json('/allocations', adict, headers=self.headers) result = self.get_json('/allocations/%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_allocation_candidate_node_not_found(self): adict = apiutils.allocation_post_data( candidate_nodes=['1a1a1a1a-2b2b-3c3c-4d4d-5e5e5e5e5e5e']) response = self.post_json('/allocations', adict, expect_errors=True, headers=self.headers) self.assertEqual('application/json', response.content_type) self.assertEqual(http_client.BAD_REQUEST, response.status_int) self.assertTrue(response.json['error_message'])
def test_create_allocation_doesnt_contain_id(self): with mock.patch.object(self.dbapi, 'create_allocation', wraps=self.dbapi.create_allocation) as cp_mock: adict = apiutils.allocation_post_data(extra={'foo': 123}) self.post_json('/allocations', adict, headers=self.headers) result = self.get_json('/allocations/%s' % adict['uuid'], headers=self.headers) self.assertEqual(adict['extra'], result['extra']) cp_mock.assert_called_once_with(mock.ANY) # Check that 'id' is not in first arg of positional args self.assertNotIn('id', cp_mock.call_args[0][0])
def test_create_allocation_with_traits(self): adict = apiutils.allocation_post_data() adict['traits'] = ['CUSTOM_GPU', 'CUSTOM_FOO_BAR'] response = self.post_json('/allocations', adict, headers=self.headers) self.assertEqual('application/json', response.content_type) self.assertEqual(http_client.CREATED, response.status_int) self.assertEqual(['CUSTOM_GPU', 'CUSTOM_FOO_BAR'], response.json['traits']) result = self.get_json('/allocations/%s' % adict['uuid'], headers=self.headers) self.assertEqual(adict['uuid'], result['uuid']) self.assertEqual(['CUSTOM_GPU', 'CUSTOM_FOO_BAR'], result['traits'])
def test_create_allocation_with_candidate_nodes(self): node1 = obj_utils.create_test_node(self.context, name='node-1') node2 = obj_utils.create_test_node(self.context, uuid=uuidutils.generate_uuid()) adict = apiutils.allocation_post_data( candidate_nodes=[node1.name, node2.uuid]) response = self.post_json('/allocations', adict, headers=self.headers) self.assertEqual(http_client.CREATED, response.status_int) result = self.get_json('/allocations/%s' % adict['uuid'], headers=self.headers) self.assertEqual(adict['uuid'], result['uuid']) self.assertEqual([node1.uuid, node2.uuid], result['candidate_nodes'])
def test_create_allocation_error(self, mock_create, mock_notify): mock_create.side_effect = Exception() adict = apiutils.allocation_post_data() self.post_json('/allocations', adict, 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_allocation_valid_extra(self): adict = apiutils.allocation_post_data( extra={ 'str': 'foo', 'int': 123, 'float': 0.1, 'bool': True, 'list': [1, 2], 'none': None, 'dict': { 'cat': 'meow' } }) self.post_json('/allocations', adict, headers=self.headers) result = self.get_json('/allocations/%s' % adict['uuid'], headers=self.headers) self.assertEqual(adict['extra'], result['extra'])
def test_allocation_init(self): allocation_dict = apiutils.allocation_post_data(node_id=None) del allocation_dict['extra'] allocation = api_allocation.Allocation(**allocation_dict) self.assertEqual(wtypes.Unset, allocation.extra)
def test_create_allocation_with_no_extra(self): adict = apiutils.allocation_post_data() del adict['extra'] response = self.post_json('/allocations', adict, headers=self.headers) self.assertEqual('application/json', response.content_type) self.assertEqual(http_client.CREATED, response.status_int)
def test_create_allocation_invalid_api_version(self): adict = apiutils.allocation_post_data() response = self.post_json( '/allocations', adict, headers={api_base.Version.string: '1.50'}, expect_errors=True) self.assertEqual(http_client.METHOD_NOT_ALLOWED, response.status_int)