def test_delete_node_not_found(self): self.mox.StubOutWithMock(db, 'bm_node_destroy') db.bm_node_destroy(self.context, 1).\ AndRaise(exception.NodeNotFound(node_id=1)) self.mox.ReplayAll() self.assertRaises(exc.HTTPNotFound, self.controller.delete, self.request, 1)
def bm_pxe_ip_associate(context, bm_node_id): session = db_session.get_session() with session.begin(): # Check if the node really exists node_ref = model_query(context, models.BareMetalNode, read_deleted="no", session=session).\ filter_by(id=bm_node_id).\ first() if not node_ref: raise exception.NodeNotFound(node_id=bm_node_id) # Check if the node already has a pxe_ip ip_ref = model_query(context, models.BareMetalPxeIp, read_deleted="no", session=session).\ filter_by(bm_node_id=bm_node_id).\ first() if ip_ref: return ip_ref.id # with_lockmode('update') and filter_by(bm_node_id=None) will lock all # records. It may cause a performance problem in high-concurrency # environment. ip_ref = model_query(context, models.BareMetalPxeIp, read_deleted="no", session=session).\ filter_by(bm_node_id=None).\ with_lockmode('update').\ first() # this exception is not caught in nova/compute/manager if not ip_ref: raise exception.NovaException(_("No more PXE IPs available")) ip_ref.bm_node_id = bm_node_id session.add(ip_ref) return ip_ref.id
def _test_index(self, ext_status=False): nodes = [ { 'id': 1 }, { 'id': 2 }, ] interfaces = [ { 'id': 1, 'address': '11:11:11:11:11:11' }, { 'id': 2, 'address': '22:22:22:22:22:22' }, ] self.mox.StubOutWithMock(db, 'bm_node_get_all') self.mox.StubOutWithMock(db, 'bm_interface_get_all_by_bm_node_id') db.bm_node_get_all(self.context).AndReturn(nodes) db.bm_interface_get_all_by_bm_node_id(self.context, 1).\ AndRaise(exception.NodeNotFound(node_id=1)) for n in nodes: self.ext_mgr.is_loaded('os-baremetal-ext-status').\ AndReturn(ext_status) db.bm_interface_get_all_by_bm_node_id(self.context, 2).\ AndReturn(interfaces) self.mox.ReplayAll() res_dict = self.controller.index(self.request) self.assertEqual(2, len(res_dict['nodes'])) self.assertEqual([], res_dict['nodes'][0]['interfaces']) self.assertEqual(2, len(res_dict['nodes'][1]['interfaces']))
def bm_node_update(context, bm_node_id, values): rows = model_query(context, models.BareMetalNode, read_deleted="no").\ filter_by(id=bm_node_id).\ update(values) if not rows: raise exception.NodeNotFound(node_id=bm_node_id)
def test_index(self): nodes = [ { 'id': 1 }, { 'id': 2 }, ] interfaces = [ { 'id': 1, 'address': '11:11:11:11:11:11' }, { 'id': 2, 'address': '22:22:22:22:22:22' }, ] self.mox.StubOutWithMock(db, 'bm_node_get_all') self.mox.StubOutWithMock(db, 'bm_interface_get_all_by_bm_node_id') db.bm_node_get_all(self.context).AndReturn(nodes) db.bm_interface_get_all_by_bm_node_id(self.context, 1).\ AndRaise(exception.NodeNotFound(node_id=1)) db.bm_interface_get_all_by_bm_node_id(self.context, 2).\ AndReturn(interfaces) self.mox.ReplayAll() res_dict = self.controller.index(self.request) self.assertEqual(2, len(res_dict['nodes'])) self.assertEqual([], res_dict['nodes'][0]['interfaces']) self.assertEqual(2, len(res_dict['nodes'][1]['interfaces']))
def test_remove_interface_node_not_found(self): node_id = 1 self.mox.StubOutWithMock(db, 'bm_node_get') db.bm_node_get(self.context, node_id).\ AndRaise(exception.NodeNotFound(node_id=node_id)) self.mox.ReplayAll() body = {'remove_interface': {'address': '11:11:11:11:11:11'}} self.assertRaises(exc.HTTPNotFound, self.controller._remove_interface, self.request, node_id, body)
def bm_pxe_ip_get_by_bm_node_id(context, bm_node_id): result = model_query(context, models.BareMetalPxeIp, read_deleted="no").\ filter_by(bm_node_id=bm_node_id).\ first() if not result: raise exception.NodeNotFound(node_id=bm_node_id) return result
def bm_interface_get_all_by_bm_node_id(context, bm_node_id): result = model_query(context, models.BareMetalInterface, read_deleted="no").\ filter_by(bm_node_id=bm_node_id).\ all() if not result: raise exception.NodeNotFound(node_id=bm_node_id) return result
def bm_node_get(context, bm_node_id): # bm_node_id may be passed as a string. Convert to INT to improve DB perf. bm_node_id = int(bm_node_id) result = model_query(context, models.BareMetalNode, read_deleted="no").\ filter_by(id=bm_node_id).\ first() if not result: raise exception.NodeNotFound(node_id=bm_node_id) return result
def test_show_no_interfaces(self): node_id = 1 node = {'id': node_id} self.mox.StubOutWithMock(db, 'bm_node_get') self.mox.StubOutWithMock(db, 'bm_interface_get_all_by_bm_node_id') db.bm_node_get(self.context, node_id).AndReturn(node) db.bm_interface_get_all_by_bm_node_id(self.context, node_id).\ AndRaise(exception.NodeNotFound(node_id=node_id)) self.mox.ReplayAll() res_dict = self.controller.show(self.request, node_id) self.assertEqual(node_id, res_dict['node']['id']) self.assertEqual(0, len(res_dict['node']['interfaces']))
def _test_show_no_interfaces(self, ext_status=False): node_id = 1 node = {'id': node_id} self.mox.StubOutWithMock(db, 'bm_node_get') self.mox.StubOutWithMock(db, 'bm_interface_get_all_by_bm_node_id') db.bm_node_get(self.context, node_id).AndReturn(node) db.bm_interface_get_all_by_bm_node_id(self.context, node_id).\ AndRaise(exception.NodeNotFound(node_id=node_id)) self.ext_mgr.is_loaded('os-baremetal-ext-status').AndReturn(ext_status) self.mox.ReplayAll() res_dict = self.controller.show(self.request, node_id) self.assertEqual(node_id, res_dict['node']['id']) self.assertEqual(0, len(res_dict['node']['interfaces']))
def bm_node_destroy(context, bm_node_id): # First, delete all interfaces belonging to the node. # Delete physically since these have unique columns. session = db_session.get_session() with session.begin(): model_query(context, models.BareMetalInterface, read_deleted="no").\ filter_by(bm_node_id=bm_node_id).\ delete() rows = model_query(context, models.BareMetalNode, read_deleted="no").\ filter_by(id=bm_node_id).\ update({'deleted': True, 'deleted_at': timeutils.utcnow(), 'updated_at': literal_column('updated_at')}) if not rows: raise exception.NodeNotFound(node_id=bm_node_id)
def is_power_on(self): LOG.debug(_("Checking if %s is running"), self._node_name) if not self._check_for_node(): err_msg = _('Node "%(name)s" with MAC address %(mac)s not found.') LOG.error(err_msg, {'name': self._node_name, 'mac': self._mac_addresses}) # in our case the _node_name is the the node_id raise exception.NodeNotFound(node_id=self._node_name) cmd = self._vp_cmd.list_running_cmd running_node_list = self._run_command(cmd) for node in running_node_list: if self._matched_name in node: return True return False