def setUp(self): super(RPCAPITestCase, self).setUp() self.context = context.get_admin_context() self.dbapi = dbapi.get_instance() self.fake_node = dbutils.get_test_node(driver='fake-driver') self.fake_node_obj = objects.Node._from_db_object( objects.Node(), self.fake_node)
def post(self, node): """Create a new node. :param node: a node within the request body. """ if self.from_chassis: raise exception.OperationNotPermitted # NOTE(deva): get_topic_for checks if node.driver is in the hash ring # and raises NoValidHost if it is not. # We need to ensure that node has a UUID before it can # be mapped onto the hash ring. if not node.uuid: node.uuid = utils.generate_uuid() try: pecan.request.rpcapi.get_topic_for(node) except exception.NoValidHost as e: # NOTE(deva): convert from 404 to 400 because client can see # list of available drivers and shouldn't request # one that doesn't exist. e.code = 400 raise e new_node = objects.Node(context=pecan.request.context, **node.as_dict()) new_node.create() # Set the HTTP Location Header pecan.response.location = link.build_url('nodes', new_node.uuid) return Node.convert_with_links(new_node)
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. """ 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'] node = objects.Node(ctxt) for key in db_node: if key == 'traits': # convert list of strings to object raw_traits = db_node['traits'] trait_list = [] for raw_trait in raw_traits: trait = objects.Trait(ctxt, trait=raw_trait) trait_list.append(trait) node.traits = objects.TraitList(ctxt, objects=trait_list) node.traits.obj_reset_changes() else: setattr(node, key, db_node[key]) return node
def post(self, node): """Create a new node. :param node: a node within the request body. """ if self.from_chassis: raise exception.OperationNotPermitted # NOTE(deva): get_topic_for checks if node.driver is in the hash ring # and raises NoValidHost if it is not. # We need to ensure that node has a UUID before it can # be mapped onto the hash ring. if not node.uuid: node.uuid = uuidutils.generate_uuid() try: pecan.request.rpcapi.get_topic_for(node) except exception.NoValidHost as e: # NOTE(deva): convert from 404 to 400 because client can see # list of available drivers and shouldn't request # one that doesn't exist. e.code = http_client.BAD_REQUEST raise e error_msg = _("Cannot create node with invalid name " "%(name)s") % {'name': node.name} self._check_name_acceptable(node.name, error_msg) node.provision_state = api_utils.initial_node_provision_state() new_node = objects.Node(pecan.request.context, **node.as_dict()) new_node.create() # Set the HTTP Location Header pecan.response.location = link.build_url('nodes', new_node.uuid) return Node.convert_with_links(new_node)
def test_rescue_supported_set(self): # rescue_interface set, no change required. node = objects.Node(self.context, **self.fake_node) node.rescue_interface = 'fake' node.obj_reset_changes() node._convert_to_version("1.22") self.assertEqual('fake', node.rescue_interface) self.assertEqual({}, node.obj_get_changes())
def test_rescue_unsupported_set_no_remove_default(self): # rescue_interface set, no change required. node = objects.Node(self.context, **self.fake_node) node.rescue_interface = None node.obj_reset_changes() node._convert_to_version("1.21", False) self.assertIsNone(node.rescue_interface) self.assertEqual({}, node.obj_get_changes())
def test_rescue_unsupported_set_no_remove_non_default(self): # rescue_interface set, should be set to default. node = objects.Node(self.context, **self.fake_node) node.rescue_interface = 'fake' node.obj_reset_changes() node._convert_to_version("1.21", False) self.assertIsNone(node.rescue_interface) self.assertEqual({'rescue_interface': None}, node.obj_get_changes())
def test_rescue_unsupported_set_remove(self): # rescue_interface set, should be removed. node = objects.Node(self.context, **self.fake_node) node.rescue_interface = 'fake' node.obj_reset_changes() node._convert_to_version("1.21") self.assertNotIn('rescue_interface', node) self.assertEqual({}, node.obj_get_changes())
def test_rescue_unsupported_missing(self): # rescue_interface not set, no change required. node = objects.Node(self.context, **self.fake_node) delattr(node, 'rescue_interface') node.obj_reset_changes() node._convert_to_version("1.21") self.assertNotIn('rescue_interface', node) self.assertEqual({}, node.obj_get_changes())
def test_create(self): node = objects.Node(self.context, **self.fake_node) with mock.patch.object(self.dbapi, 'create_node', autospec=True) as mock_create_node: mock_create_node.return_value = db_utils.get_test_node() node.create() args, _kwargs = mock_create_node.call_args self.assertEqual(objects.Node.VERSION, args[0]['version'])
def test_rescue_supported_missing(self): # rescue_interface not set, should be set to default. node = objects.Node(self.context, **self.fake_node) delattr(node, 'rescue_interface') node.obj_reset_changes() node._convert_to_version("1.22") self.assertIsNone(node.rescue_interface) self.assertEqual({'rescue_interface': None}, node.obj_get_changes())
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) node = objects.Node(ctxt) for key in db_node: setattr(node, key, db_node[key]) return node
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) # Let DB generate ID if it isn't specified explicitly if 'id' not in kw: del db_node['id'] node = objects.Node(ctxt) for key in db_node: setattr(node, key, db_node[key]) return node
def post(self, node): """Create a new node. :param node: a node within the request body. """ if self.from_chassis: raise exception.OperationNotPermitted # NOTE(deva): get_topic_for checks if node.driver is in the hash ring # and raises NoValidHost if it is not. # We need to ensure that node has a UUID before it can # be mapped onto the hash ring. if not node.uuid: node.uuid = uuidutils.generate_uuid() try: pecan.request.rpcapi.get_topic_for(node) except exception.NoValidHost as e: # NOTE(deva): convert from 404 to 400 because client can see # list of available drivers and shouldn't request # one that doesn't exist. e.code = 400 raise e # Verify that if we're creating a new node with a 'name' set # that it is a valid name if node.name: if not api_utils.allow_node_logical_names(): raise exception.NotAcceptable() if not api_utils.is_valid_node_name(node.name): msg = _("Cannot create node with invalid name %(name)s") raise wsme.exc.ClientSideError(msg % {'name': node.name}, status_code=400) node.provision_state = api_utils.initial_node_provision_state() new_node = objects.Node(pecan.request.context, **node.as_dict()) new_node.create() # Set the HTTP Location Header pecan.response.location = link.build_url('nodes', new_node.uuid) return Node.convert_with_links(new_node)
def setUp(self): super(RPCAPITestCase, self).setUp() self.fake_node = dbutils.get_test_node(driver='fake-driver') self.fake_node_obj = objects.Node._from_db_object( objects.Node(self.context), self.fake_node)
def test_create_with_invalid_properties(self): node = objects.Node(self.context, **self.fake_node) node.properties = {"local_gb": "5G"} self.assertRaises(exception.InvalidParameterValue, node.create)
def test_create(self): node = objects.Node(self.context, **self.fake_node) node.create()