From 054eeb65280749fd1a8be2c83e81a4a02cf7ad45 Mon Sep 17 00:00:00 2001 From: Gleb Stepanov Date: Fri, 5 Aug 2016 14:53:59 +0300 Subject: [PATCH] Check keyword arguments Check that all keyword arguments were properly used in ironic.tests.unit.db.utils. Change-Id: If04a162accedb13e63a9de4b3fa2f2961347353a --- ironic/tests/unit/conductor/test_manager.py | 3 +- ironic/tests/unit/dhcp/test_neutron.py | 18 +++----- .../unit/drivers/modules/test_deploy_utils.py | 8 ++-- ironic/tests/unit/objects/utils.py | 45 +++++++++++++++++-- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/ironic/tests/unit/conductor/test_manager.py b/ironic/tests/unit/conductor/test_manager.py index 75c194751d..9284874acb 100644 --- a/ironic/tests/unit/conductor/test_manager.py +++ b/ironic/tests/unit/conductor/test_manager.py @@ -271,8 +271,7 @@ class UpdateNodeTestCase(mgr_utils.ServiceSetUpMixin, node = obj_utils.create_test_node(self.context, driver=existing_driver, extra={'test': 'one'}, - instance_uuid=None, - task_state=states.POWER_ON) + instance_uuid=None) # check that it fails because driver not found node.driver = wrong_driver node.driver_info = {} diff --git a/ironic/tests/unit/dhcp/test_neutron.py b/ironic/tests/unit/dhcp/test_neutron.py index ca93feb66e..a9329143d6 100644 --- a/ironic/tests/unit/dhcp/test_neutron.py +++ b/ironic/tests/unit/dhcp/test_neutron.py @@ -363,8 +363,7 @@ class TestNeutron(db_base.DbTestCase): address='aa:bb:cc:dd:ee:ff', uuid=uuidutils.generate_uuid(), extra={'vif_port_id': - 'test-vif-A'}, - driver='fake') + 'test-vif-A'}) mock_gfia.return_value = expected with task_manager.acquire(self.context, self.node.uuid) as task: @@ -380,8 +379,7 @@ class TestNeutron(db_base.DbTestCase): port = object_utils.create_test_port(self.context, node_id=self.node.id, address='aa:bb:cc:dd:ee:ff', - uuid=uuidutils.generate_uuid(), - driver='fake') + uuid=uuidutils.generate_uuid()) mock_gfia.return_value = expected with task_manager.acquire(self.context, self.node.uuid) as task: @@ -397,8 +395,7 @@ class TestNeutron(db_base.DbTestCase): pg = object_utils.create_test_portgroup(self.context, node_id=self.node.id, address='aa:bb:cc:dd:ee:ff', - uuid=uuidutils.generate_uuid(), - driver='fake') + uuid=uuidutils.generate_uuid()) mock_gfia.return_value = expected with task_manager.acquire(self.context, self.node.uuid) as task: @@ -416,8 +413,7 @@ class TestNeutron(db_base.DbTestCase): address='aa:bb:cc:dd:ee:ff', uuid=uuidutils.generate_uuid(), extra={'vif_port_id': - 'test-vif-A'}, - driver='fake') + 'test-vif-A'}) mock_gfia.return_value = ip_address with task_manager.acquire(self.context, self.node.uuid) as task: api = dhcp_factory.DHCPFactory().provider @@ -434,8 +430,7 @@ class TestNeutron(db_base.DbTestCase): address='aa:bb:cc:dd:ee:ff', uuid=uuidutils.generate_uuid(), extra={'vif_port_id': - 'test-vif-A'}, - driver='fake') + 'test-vif-A'}) mock_gfia.return_value = ip_address with task_manager.acquire(self.context, self.node.uuid) as task: api = dhcp_factory.DHCPFactory().provider @@ -463,8 +458,7 @@ class TestNeutron(db_base.DbTestCase): address='aa:bb:cc:dd:ee:ff', uuid=uuidutils.generate_uuid(), extra={'vif_port_id': - 'test-vif-A'}, - driver='fake') + 'test-vif-A'}) with task_manager.acquire(self.context, self.node.uuid) as task: api = dhcp_factory.DHCPFactory().provider api.get_ip_addresses(task) diff --git a/ironic/tests/unit/drivers/modules/test_deploy_utils.py b/ironic/tests/unit/drivers/modules/test_deploy_utils.py index 9a26c3680e..200961a277 100644 --- a/ironic/tests/unit/drivers/modules/test_deploy_utils.py +++ b/ironic/tests/unit/drivers/modules/test_deploy_utils.py @@ -1430,7 +1430,7 @@ class VirtualMediaDeployUtilsTestCase(db_base.DbTestCase): obj_utils.create_test_port( self.context, node_id=self.node.id, address='aa:bb:cc:dd:ee:ff', uuid=uuidutils.generate_uuid(), - extra={'vif_port_id': 'test-vif-A'}, driver='iscsi_ilo') + extra={'vif_port_id': 'test-vif-A'}) with task_manager.acquire(self.context, self.node.uuid, shared=False) as task: address = utils.get_single_nic_with_vif_port_id(task) @@ -1440,8 +1440,7 @@ class VirtualMediaDeployUtilsTestCase(db_base.DbTestCase): obj_utils.create_test_port( self.context, node_id=self.node.id, address='aa:bb:cc:dd:ee:ff', uuid=uuidutils.generate_uuid(), - internal_info={'cleaning_vif_port_id': 'test-vif-A'}, - driver='iscsi_ilo') + internal_info={'cleaning_vif_port_id': 'test-vif-A'}) with task_manager.acquire(self.context, self.node.uuid, shared=False) as task: address = utils.get_single_nic_with_vif_port_id(task) @@ -1451,8 +1450,7 @@ class VirtualMediaDeployUtilsTestCase(db_base.DbTestCase): obj_utils.create_test_port( self.context, node_id=self.node.id, address='aa:bb:cc:dd:ee:ff', uuid=uuidutils.generate_uuid(), - internal_info={'provisioning_vif_port_id': 'test-vif-A'}, - driver='iscsi_ilo') + internal_info={'provisioning_vif_port_id': 'test-vif-A'}) with task_manager.acquire(self.context, self.node.uuid, shared=False) as task: address = utils.get_single_nic_with_vif_port_id(task) diff --git a/ironic/tests/unit/objects/utils.py b/ironic/tests/unit/objects/utils.py index f371818225..11bccbb97d 100644 --- a/ironic/tests/unit/objects/utils.py +++ b/ironic/tests/unit/objects/utils.py @@ -13,18 +13,43 @@ # License for the specific language governing permissions and limitations # under the License. """Ironic object test utilities.""" +import six +from ironic.common import exception +from ironic.common.i18n import _ from ironic import objects from ironic.tests.unit.db import utils as db_utils +def check_keyword_arguments(func): + @six.wraps(func) + def wrapper(**kw): + obj_type = kw.pop('object_type') + result = func(**kw) + + extra_args = set(kw) - set(result) + if extra_args: + raise exception.InvalidParameterValue( + _("Unknown keyword arguments (%(extra)s) were passed " + "while creating a test %(object_type)s object.") % + {"extra": ", ".join(extra_args), + "object_type": obj_type}) + + return result + + return wrapper + + def get_test_node(ctxt, **kw): """Return a Node object with appropriate attributes. NOTE: The object leaves the attributes marked as changed, such that a create() could be used to commit it to the DB. """ - db_node = db_utils.get_test_node(**kw) + kw['object_type'] = 'node' + get_db_node_checked = check_keyword_arguments(db_utils.get_test_node) + db_node = get_db_node_checked(**kw) + # Let DB generate ID if it isn't specified explicitly if 'id' not in kw: del db_node['id'] @@ -51,7 +76,11 @@ def get_test_port(ctxt, **kw): NOTE: The object leaves the attributes marked as changed, such that a create() could be used to commit it to the DB. """ - db_port = db_utils.get_test_port(**kw) + kw['object_type'] = 'port' + get_db_port_checked = check_keyword_arguments( + db_utils.get_test_port) + db_port = get_db_port_checked(**kw) + # Let DB generate ID if it isn't specified explicitly if 'id' not in kw: del db_port['id'] @@ -78,7 +107,11 @@ def get_test_chassis(ctxt, **kw): NOTE: The object leaves the attributes marked as changed, such that a create() could be used to commit it to the DB. """ - db_chassis = db_utils.get_test_chassis(**kw) + kw['object_type'] = 'chassis' + get_db_chassis_checked = check_keyword_arguments( + db_utils.get_test_chassis) + db_chassis = get_db_chassis_checked(**kw) + # Let DB generate ID if it isn't specified explicitly if 'id' not in kw: del db_chassis['id'] @@ -105,7 +138,11 @@ def get_test_portgroup(ctxt, **kw): NOTE: The object leaves the attributes marked as changed, such that a create() could be used to commit it to the DB. """ - db_portgroup = db_utils.get_test_portgroup(**kw) + kw['object_type'] = 'portgroup' + get_db_port_group_checked = check_keyword_arguments( + db_utils.get_test_portgroup) + db_portgroup = get_db_port_group_checked(**kw) + # Let DB generate ID if it isn't specified explicitly if 'id' not in kw: del db_portgroup['id']