From 885bfbda969f2cceeb6f5ac2c05461c86c6b5dbf Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Mon, 4 Mar 2019 14:52:14 +0100 Subject: Allocation API: optimize check on candidate nodes Currently we fetch all nodes one by one when validating the provided candidate_nodes. This is not overly efficient, so this change introduces a new database API to specifically validate a list of names/uuids and convert it to uuids. As a nice side effect, no database query is done for identities that cannot be a valid node name or uuid. Change-Id: I7adfe897200b1b05d9e632c68d0d3d046c06a921 Story: #2004341 --- .../unit/api/controllers/v1/test_allocation.py | 9 ++++++ ironic/tests/unit/db/test_nodes.py | 35 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) (limited to 'ironic/tests') diff --git a/ironic/tests/unit/api/controllers/v1/test_allocation.py b/ironic/tests/unit/api/controllers/v1/test_allocation.py index 0637903ae..91124965f 100644 --- a/ironic/tests/unit/api/controllers/v1/test_allocation.py +++ b/ironic/tests/unit/api/controllers/v1/test_allocation.py @@ -566,6 +566,15 @@ class TestPost(test_api_base.BaseApiTest): self.assertEqual(http_client.BAD_REQUEST, response.status_int) 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_name_ok(self): name = 'foo' adict = apiutils.allocation_post_data(name=name) diff --git a/ironic/tests/unit/db/test_nodes.py b/ironic/tests/unit/db/test_nodes.py index 79002dabc..11568b963 100644 --- a/ironic/tests/unit/db/test_nodes.py +++ b/ironic/tests/unit/db/test_nodes.py @@ -853,3 +853,38 @@ class DbNodeTestCase(base.DbTestCase): 'Multiple nodes', self.dbapi.get_node_by_port_addresses, addresses) + + def test_check_node_list(self): + node1 = utils.create_test_node(uuid=uuidutils.generate_uuid()) + node2 = utils.create_test_node(uuid=uuidutils.generate_uuid(), + name='node_2') + node3 = utils.create_test_node(uuid=uuidutils.generate_uuid(), + name='node_3') + + mapping = self.dbapi.check_node_list([node1.uuid, node2.name, + node3.uuid]) + self.assertEqual({node1.uuid: node1.uuid, + node2.name: node2.uuid, + node3.uuid: node3.uuid}, + mapping) + + def test_check_node_list_non_existing(self): + node1 = utils.create_test_node(uuid=uuidutils.generate_uuid()) + node2 = utils.create_test_node(uuid=uuidutils.generate_uuid(), + name='node_2') + uuid = uuidutils.generate_uuid() + + exc = self.assertRaises(exception.NodeNotFound, + self.dbapi.check_node_list, + [node1.uuid, uuid, 'could-be-a-name', + node2.name]) + self.assertIn(uuid, str(exc)) + self.assertIn('could-be-a-name', str(exc)) + + def test_check_node_list_impossible(self): + node1 = utils.create_test_node(uuid=uuidutils.generate_uuid()) + + exc = self.assertRaises(exception.NodeNotFound, + self.dbapi.check_node_list, + [node1.uuid, 'this/cannot/be/a/name']) + self.assertIn('this/cannot/be/a/name', str(exc)) -- cgit v1.2.1