def test_destroy(self): uuid = self.fake_server['uuid'] with mock.patch.object(self.dbapi, 'server_destroy', autospec=True) as mock_server_destroy: server = objects.Server(self.context, **self.fake_server) server.destroy(self.context) mock_server_destroy.assert_called_once_with(self.context, uuid)
def setUp(self): super(RPCAPITestCase, self).setUp() self.fake_server = dbutils.get_test_server() self.fake_server_obj = objects.Server._from_db_object( objects.Server(self.context), self.fake_server) self.fake_type = dbutils.get_test_flavor() self.fake_type['extra_specs'] = {} self.fake_type_obj = objects.Flavor._from_db_object( self.context, objects.Flavor(self.context), self.fake_type)
def test_create(self): with mock.patch.object(self.dbapi, 'server_create', autospec=True) as mock_server_create: mock_server_create.return_value = self.fake_server server = objects.Server(self.context, **self.fake_server) server.create(self.context) expected_called = copy.deepcopy(self.fake_server) expected_called['nics'][0].update( server_uuid=self.fake_server['uuid']) expected_called.pop('extra', None) mock_server_create.assert_called_once_with(self.context, expected_called) self.assertEqual(self.fake_server['uuid'], server['uuid'])
def get_test_server(ctxt, **kw): """Return a Server 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'] = 'server' get_db_server_checked = check_keyword_arguments( db_utils.get_test_server) db_server = get_db_server_checked(**kw) # Let DB generate ID if it isn't specified explicitly if 'id' not in kw: del db_server['id'] server = objects.Server(ctxt, **db_server) return server
def test_save(self): uuid = self.fake_server['uuid'] with mock.patch.object(self.dbapi, 'server_update', autospec=True) as mock_server_update: with mock.patch.object(self.dbapi, 'server_nic_update_or_create', autospec=True) as mock_server_nic_update: mock_server_update.return_value = self.fake_server server_nics = self.fake_server['nics'] port_id = server_nics[0]['port_id'] server = objects.Server(self.context, **self.fake_server) updates = server.obj_get_changes() updates.pop('nics', None) server.save(self.context) mock_server_update.assert_called_once_with( self.context, uuid, updates) expected_called_nic = copy.deepcopy(server_nics[0]) expected_called_nic.update(server_uuid=uuid) mock_server_nic_update.assert_called_once_with( self.context, port_id, expected_called_nic)
def _provision_servers(self, context, base_options, min_count, max_count, server_group): # Return num_servers according quota num_servers = self._check_num_servers_quota( context, min_count, max_count) # Create the servers reservations reserve_opts = {'servers': num_servers} reservations = self.quota.reserve(context, **reserve_opts) LOG.debug("Going to run %s servers...", num_servers) servers = [] try: for num in range(num_servers): server = objects.Server(context=context) server.update(base_options) server.uuid = uuidutils.generate_uuid() # Refactor name of the server. self._populate_server_names(server, num_servers, num) server.create() servers.append(server) if server_group: objects.ServerGroup.add_members( context, server_group.uuid, [server.uuid]) except Exception: with excutils.save_and_reraise_exception(): try: for server in servers: try: server.destroy() except exception.ObjectActionError: pass finally: self.quota.rollback(context, reservations) # Commit servers reservations if reservations: self.quota.commit(context, reservations) return servers
def manage(self, context, node_uuid, name, description, metadata): """Create a new server by managing an existing bare metal node Sending manageable server information to the engine and will handle creating the DB entries. Returns a server object """ self._check_num_servers_quota(context, 1, 1) # Create the servers reservations reserve_opts = {'servers': 1} reservations = self.quota.reserve(context, **reserve_opts) if reservations: self.quota.commit(context, reservations) # TODO(litao) we will support to specify user and project in # managing bare metal node later. base_options = { 'image_uuid': None, 'status': None, 'user_id': context.user, 'project_id': context.tenant, 'power_state': states.NOSTATE, 'name': name, 'description': description, 'metadata': metadata or {}, 'availability_zone': None} server = objects.Server(context=context) server.update(base_options) server.uuid = uuidutils.generate_uuid() server = self.engine_rpcapi.manage_server(context, server, node_uuid) return server
def _create_fake_server_obj(self, fake_server): fake_server_obj = objects.Server(self.context, **fake_server) fake_server_obj.create(self.context) return fake_server_obj