summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/api/controllers/v1/test_port.py
diff options
context:
space:
mode:
Diffstat (limited to 'ironic/tests/unit/api/controllers/v1/test_port.py')
-rw-r--r--ironic/tests/unit/api/controllers/v1/test_port.py88
1 files changed, 39 insertions, 49 deletions
diff --git a/ironic/tests/unit/api/controllers/v1/test_port.py b/ironic/tests/unit/api/controllers/v1/test_port.py
index 6823c3b51..31885f45f 100644
--- a/ironic/tests/unit/api/controllers/v1/test_port.py
+++ b/ironic/tests/unit/api/controllers/v1/test_port.py
@@ -15,7 +15,6 @@ Tests for the API /ports/ methods.
import datetime
from http import client as http_client
-import types
from unittest import mock
from urllib import parse as urlparse
@@ -194,7 +193,7 @@ class TestPortsController__GetPortsCollection(base.TestCase):
mock_request.context = 'fake-context'
mock_list.return_value = []
self.controller._get_ports_collection(None, None, None, None, None,
- None, 'asc',
+ None, None, 'asc',
resource_url='ports')
mock_list.assert_called_once_with('fake-context', 1000, None,
project=None, sort_dir='asc',
@@ -236,53 +235,6 @@ class TestListPorts(test_api_base.BaseApiTest):
# never expose the node_id
self.assertNotIn('node_id', data['ports'][0])
- # NOTE(jlvillal): autospec=True doesn't work on staticmethods:
- # https://bugs.python.org/issue23078
- @mock.patch.object(objects.Node, 'get_by_id', spec_set=types.FunctionType)
- def test_list_with_deleted_node(self, mock_get_node):
- # check that we don't end up with HTTP 400 when node deletion races
- # with listing ports - see https://launchpad.net/bugs/1748893
- obj_utils.create_test_port(self.context, node_id=self.node.id)
- mock_get_node.side_effect = exception.NodeNotFound('boom')
- data = self.get_json('/ports')
- self.assertEqual([], data['ports'])
-
- # NOTE(jlvillal): autospec=True doesn't work on staticmethods:
- # https://bugs.python.org/issue23078
- @mock.patch.object(objects.Node, 'get_by_id',
- spec_set=types.FunctionType)
- def test_list_detailed_with_deleted_node(self, mock_get_node):
- # check that we don't end up with HTTP 400 when node deletion races
- # with listing ports - see https://launchpad.net/bugs/1748893
- port = obj_utils.create_test_port(self.context, node_id=self.node.id)
- port2 = obj_utils.create_test_port(self.context, node_id=self.node.id,
- uuid=uuidutils.generate_uuid(),
- address='66:44:55:33:11:22')
- mock_get_node.side_effect = [exception.NodeNotFound('boom'), self.node]
- data = self.get_json('/ports/detail')
- # The "correct" port is still returned
- self.assertEqual(1, len(data['ports']))
- self.assertIn(data['ports'][0]['uuid'], {port.uuid, port2.uuid})
- self.assertEqual(self.node.uuid, data['ports'][0]['node_uuid'])
-
- # NOTE(jlvillal): autospec=True doesn't work on staticmethods:
- # https://bugs.python.org/issue23078
- @mock.patch.object(objects.Portgroup, 'get', spec_set=types.FunctionType)
- def test_list_with_deleted_port_group(self, mock_get_pg):
- # check that we don't end up with HTTP 400 when port group deletion
- # races with listing ports - see https://launchpad.net/bugs/1748893
- portgroup = obj_utils.create_test_portgroup(self.context,
- node_id=self.node.id)
- port = obj_utils.create_test_port(self.context, node_id=self.node.id,
- portgroup_id=portgroup.id)
- mock_get_pg.side_effect = exception.PortgroupNotFound('boom')
- data = self.get_json(
- '/ports/detail',
- headers={api_base.Version.string: str(api_v1.max_version())}
- )
- self.assertEqual(port.uuid, data['ports'][0]["uuid"])
- self.assertIsNone(data['ports'][0]["portgroup_uuid"])
-
@mock.patch.object(policy, 'authorize', spec=True)
def test_list_non_admin_forbidden(self, mock_authorize):
def mock_authorize_function(rule, target, creds):
@@ -1129,6 +1081,44 @@ class TestListPorts(test_api_base.BaseApiTest):
response.json['error_message'])
+class TestListPortsByShard(test_api_base.BaseApiTest):
+ def setUp(self):
+ super(TestListPortsByShard, self).setUp()
+ self.headers = {
+ api_base.Version.string: '1.%s' % versions.MINOR_82_NODE_SHARD
+ }
+
+ def _create_port_with_shard(self, shard, address):
+ node = obj_utils.create_test_node(self.context, owner='12345',
+ shard=shard,
+ uuid=uuidutils.generate_uuid())
+ return obj_utils.create_test_port(self.context, name='port_%s' % shard,
+ node_id=node.id, address=address,
+ uuid=uuidutils.generate_uuid())
+
+ def test_get_by_shard_single_fail_api_version(self):
+ self._create_port_with_shard('test_shard', 'aa:bb:cc:dd:ee:ff')
+ data = self.get_json('/ports?shard=test_shard', expect_errors=True)
+ self.assertEqual(406, data.status_int)
+
+ def test_get_by_shard_single(self):
+ port = self._create_port_with_shard('test_shard', 'aa:bb:cc:dd:ee:ff')
+ data = self.get_json('/ports?shard=test_shard', headers=self.headers)
+ self.assertEqual(port.uuid, data['ports'][0]["uuid"])
+
+ def test_get_by_shard_multi(self):
+ bad_shard_address = 'ee:ee:ee:ee:ee:ee'
+ self._create_port_with_shard('shard1', 'aa:bb:cc:dd:ee:ff')
+ self._create_port_with_shard('shard2', 'ab:bb:cc:dd:ee:ff')
+ self._create_port_with_shard('shard3', bad_shard_address)
+
+ res = self.get_json('/ports?shard=shard1,shard2', headers=self.headers)
+ self.assertEqual(2, len(res['ports']))
+ print(res['ports'][0])
+ self.assertNotEqual(res['ports'][0]['address'], bad_shard_address)
+ self.assertNotEqual(res['ports'][1]['address'], bad_shard_address)
+
+
@mock.patch.object(rpcapi.ConductorAPI, 'update_port', autospec=True,
side_effect=_rpcapi_update_port)
class TestPatch(test_api_base.BaseApiTest):