def test_get_by_user_limit_and_marker_invalid_marker( self, mock_api_get, mock_kp_get): kp_name = 'unknown_kp' mock_api_get.side_effect = exception.MarkerNotFound(marker=kp_name) mock_kp_get.side_effect = exception.MarkerNotFound(marker=kp_name) self.assertRaises(exception.MarkerNotFound, keypair.KeyPairList.get_by_user, self.context, 'fake-user', limit=5, marker=kp_name)
def fake_get_all(context, inactive=False, filters=None, sort_key='flavorid', sort_dir='asc', limit=None, marker=None): if marker in ['99999']: raise exc.MarkerNotFound(marker) def reject_min(db_attr, filter_attr): return (filter_attr in filters and getattr(flavor, db_attr) < int(filters[filter_attr])) filters = filters or {} res = [] for flavor in FLAVORS.values(): if reject_min('memory_mb', 'min_memory_mb'): continue elif reject_min('root_gb', 'min_root_gb'): continue res.append(flavor) res = sorted(res, key=lambda item: getattr(item, sort_key)) output = [] marker_found = True if marker is None else False for flavor in res: if not marker_found and marker == flavor.flavorid: marker_found = True elif marker_found: if limit is None or len(output) < int(limit): output.append(flavor) return objects.FlavorList(objects=output)
def fake_get_all_flavors_sorted_list(context=None, inactive=False, filters=None, sort_key='flavorid', sort_dir='asc', limit=None, marker=None): if marker in ['99999']: raise exception.MarkerNotFound(marker) def reject_min(db_attr, filter_attr): return (filter_attr in filters and int(flavor[db_attr]) < int(filters[filter_attr])) filters = filters or {} res = [] for (flavor_name, flavor) in FAKE_FLAVORS.items(): if reject_min('memory_mb', 'min_memory_mb'): continue elif reject_min('root_gb', 'min_root_gb'): continue res.append(flavor) res = sorted(res, key=lambda item: item[sort_key]) output = [] marker_found = True if marker is None else False for flavor in res: if not marker_found and marker == flavor['flavorid']: marker_found = True elif marker_found: if limit is None or len(output) < int(limit): output.append(flavor) return output
def _get_instances_all_cells(self, context, period_start, period_stop, tenant_id, limit, marker): all_instances = [] cells = objects.CellMappingList.get_all(context) for cell in cells: with nova_context.target_cell(context, cell) as cctxt: try: instances = ( objects.InstanceList.get_active_by_window_joined( cctxt, period_start, period_stop, tenant_id, expected_attrs=['flavor'], limit=limit, marker=marker)) except exception.MarkerNotFound: # NOTE(danms): We need to keep looking through the later # cells to find the marker continue all_instances.extend(instances) # NOTE(danms): We must have found a marker if we had one, # so make sure we don't require a marker in the next cell marker = None if limit: limit -= len(instances) if limit <= 0: break if marker is not None and len(all_instances) == 0: # NOTE(danms): If we did not find the marker in any cell, # mimic the db_api behavior here raise exception.MarkerNotFound(marker=marker) return all_instances
def _return_servers(context, *args, **kwargs): servers_list = [] marker = None limit = None found_marker = False if "marker" in kwargs: marker = kwargs["marker"] if "limit" in kwargs: limit = kwargs["limit"] if 'columns_to_join' in kwargs: kwargs.pop('columns_to_join') if 'use_slave' in kwargs: kwargs.pop('use_slave') if 'sort_keys' in kwargs: kwargs.pop('sort_keys') if 'sort_dirs' in kwargs: kwargs.pop('sort_dirs') for i in range(num_servers): uuid = get_fake_uuid(i) server = stub_instance(id=i + 1, uuid=uuid, **kwargs) servers_list.append(server) if marker is not None and uuid == marker: found_marker = True servers_list = [] if marker is not None and not found_marker: raise exc.MarkerNotFound(marker=marker) if limit is not None: servers_list = servers_list[:limit] return servers_list
def test_get_action_with_invalid_marker(self, mock_actions_get): """Tests detail paging with an invalid marker (not found).""" mock_actions_get.side_effect = exception.MarkerNotFound( marker=FAKE_REQUEST_NOTFOUND_ID) req = self._get_http_req('os-instance-actions?' 'marker=%s' % FAKE_REQUEST_NOTFOUND_ID) self.assertRaises(exc.HTTPBadRequest, self.controller.index, req, FAKE_UUID)
def test_keypair_list_limit_and_marker_invalid_marker(self, mock_kp_get): mock_kp_get.side_effect = exception.MarkerNotFound(marker='unknown_kp') req = fakes.HTTPRequest.blank( self.base_url + '/os-keypairs?limit=3&marker=unknown_kp', version=self.wsgi_api_version, use_admin_context=True) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.index, req)
def get_marker_record(self, ctx, marker): try: im = objects.InstanceMapping.get_by_instance_uuid(ctx, marker) except exception.InstanceMappingNotFound: raise exception.MarkerNotFound(marker=marker) elevated = ctx.elevated(read_deleted='yes') with context.target_cell(elevated, im.cell_mapping) as cctx: try: # NOTE(danms): We query this with no columns_to_join() # as we're just getting values for the sort keys from # it and none of the valid sort keys are on joined # columns. db_inst = db.instance_get_by_uuid(cctx, marker, columns_to_join=[]) except exception.InstanceNotFound: raise exception.MarkerNotFound(marker=marker) return db_inst
def _get_marker_instance(ctx, marker): """Get the marker instance from its cell. This returns the marker instance from the cell in which it lives """ try: im = objects.InstanceMapping.get_by_instance_uuid(ctx, marker) except exception.InstanceMappingNotFound: raise exception.MarkerNotFound(marker=marker) elevated = ctx.elevated(read_deleted='yes') with context.target_cell(elevated, im.cell_mapping) as cctx: try: db_inst = db.instance_get_by_uuid(cctx, marker, columns_to_join=[]) except exception.InstanceNotFound: raise exception.MarkerNotFound(marker=marker) return db_inst
def test_index_with_invalid_marker(self, mock_migrations_get): """Tests detail paging with an invalid marker (not found).""" mock_migrations_get.side_effect = exception.MarkerNotFound( marker=uuids.invalid_marker) req = fakes.HTTPRequest.blank('/os-migrations?marker=%s' % uuids.invalid_marker, version=self.wsgi_api_version, use_admin_context=True) e = self.assertRaises(exc.HTTPBadRequest, self.controller.index, req) self.assertEqual( "Marker %s could not be found." % uuids.invalid_marker, str(e))
def fake_compute_node_get_all(context, limit=None, marker=None): if marker in ['99999']: raise exception.MarkerNotFound(marker) marker_found = True if marker is None else False output = [] for hyper in TEST_HYPERS_OBJ: if not marker_found and marker == str(hyper.id): marker_found = True elif marker_found: if limit is None or len(output) < int(limit): output.append(hyper) return output
def get_marker_record(self, ctx, marker): """Get the marker migration from its cell. This returns the marker migration from the cell in which it lives """ results = context.scatter_gather_skip_cell0(ctx, db.migration_get_by_uuid, marker) db_migration = None for result_cell_uuid, result in results.items(): if not context.is_cell_failure_sentinel(result): db_migration = result cell_uuid = result_cell_uuid break if not db_migration: raise exception.MarkerNotFound(marker=marker) return cell_uuid, db_migration
def get_marker_record(self, ctx, marker): """Get the marker migration from its cell. This returns the marker migration from the cell in which it lives """ results = context.scatter_gather_skip_cell0(ctx, db.migration_get_by_uuid, marker) db_migration = None for result in results.values(): if result not in (context.did_not_respond_sentinel, context.raised_exception_sentinel): db_migration = result break if not db_migration: raise exception.MarkerNotFound(marker=marker) return db_migration
def test_get_by_user_marker(self, mock_api_get, mock_kp_get): api_kp_name = 'api_kp' mock_api_get.side_effect = exception.MarkerNotFound(marker=api_kp_name) mock_kp_get.return_value = [fake_keypair] keypairs = keypair.KeyPairList.get_by_user(self.context, 'fake-user', marker=api_kp_name) self.assertEqual(1, len(keypairs)) self.compare_obj(keypairs[0], fake_keypair) mock_api_get.assert_called_once_with(self.context, 'fake-user', limit=None, marker=api_kp_name) mock_kp_get.assert_called_once_with(self.context, 'fake-user', limit=None, marker=api_kp_name)
def _flavor_get_all_from_db(context, inactive, filters, sort_key, sort_dir, limit, marker): """Returns all flavors. """ filters = filters or {} query = Flavor._flavor_get_query_from_db(context) if 'min_memory_mb' in filters: query = query.filter( api_models.Flavors.memory_mb >= filters['min_memory_mb']) if 'min_root_gb' in filters: query = query.filter( api_models.Flavors.root_gb >= filters['min_root_gb']) if 'disabled' in filters: query = query.filter( api_models.Flavors.disabled == filters['disabled']) if 'is_public' in filters and filters['is_public'] is not None: the_filter = [api_models.Flavors.is_public == filters['is_public']] if filters['is_public'] and context.project_id is not None: the_filter.extend([ api_models.Flavors.projects.any(project_id=context.project_id) ]) if len(the_filter) > 1: query = query.filter(or_(*the_filter)) else: query = query.filter(the_filter[0]) marker_row = None if marker is not None: marker_row = Flavor._flavor_get_query_from_db(context).\ filter_by(flavorid=marker).\ first() if not marker_row: raise exception.MarkerNotFound(marker=marker) query = sqlalchemyutils.paginate_query(query, api_models.Flavors, limit, [sort_key, 'id'], marker=marker_row, sort_dir=sort_dir) return [_dict_with_extra_specs(i) for i in query.all()]
def _get_from_db(context, user_id, name=None, limit=None, marker=None): query = context.session.query(api_models.KeyPair).\ filter(api_models.KeyPair.user_id == user_id) if name is not None: db_keypair = query.filter(api_models.KeyPair.name == name).\ first() if not db_keypair: raise exception.KeypairNotFound(user_id=user_id, name=name) return db_keypair marker_row = None if marker is not None: marker_row = context.session.query(api_models.KeyPair).\ filter(api_models.KeyPair.name == marker).\ filter(api_models.KeyPair.user_id == user_id).first() if not marker_row: raise exception.MarkerNotFound(marker=marker) query = sqlalchemyutils.paginate_query( query, api_models.KeyPair, limit, ['name'], marker=marker_row) return query.all()
def _return_servers(context, *args, **kwargs): servers_list = [] marker = None limit = None found_marker = False if "marker" in kwargs: marker = kwargs["marker"] if "limit" in kwargs: limit = kwargs["limit"] for i in xrange(num_servers): uuid = get_fake_uuid(i) server = stub_instance(id=i + 1, uuid=uuid, **kwargs) servers_list.append(server) if marker is not None and uuid == marker: found_marker = True servers_list = [] if marker is not None and not found_marker: raise exc.MarkerNotFound(marker=marker) if limit is not None: servers_list = servers_list[:limit] return servers_list
def get_by_filters(cls, context, filters, limit=None, marker=None, sort_keys=None, sort_dirs=None): if limit == 0: return cls(context, objects=[]) # 'deleted' records can not be returned from here since build_requests # are not soft deleted. if filters.get('deleted', False): return cls(context, objects=[]) # 'cleaned' records won't exist as they would need to be deleted. if filters.get('cleaned', False): return cls(context, objects=[]) # Because the build_requests table stores an instance as a serialized # versioned object it is not feasible to do the filtering and sorting # in the database. Just get all potentially relevant records and # process them here. It should be noted that build requests are short # lived so there should not be a lot of results to deal with. build_requests = cls.get_all(context) # Fortunately some filters do not apply here. # 'changes-since' works off of the updated_at field which has not yet # been set at the point in the boot process where build_request still # exists. So it can be ignored. # 'deleted' and 'cleaned' are handled above. sort_keys, sort_dirs = db.process_sort_params(sort_keys, sort_dirs, default_dir='desc') # For other filters that don't match this, we will do regexp matching # Taken from db/sqlalchemy/api.py exact_match_filter_names = [ 'project_id', 'user_id', 'image_ref', 'vm_state', 'instance_type_id', 'uuid', 'metadata', 'host', 'task_state', 'system_metadata', 'tags', 'tags-any', 'not-tags', 'not-tags-any' ] exact_filters = {} regex_filters = {} for key, value in filters.items(): if key in exact_match_filter_names: exact_filters[key] = value else: regex_filters[key] = value # As much as possible this copies the logic from db/sqlalchemy/api.py # instance_get_all_by_filters_sort. The main difference is that method # builds a sql query and this filters in python. filtered_build_reqs = [] for build_req in build_requests: instance = build_req.instance filter_result = cls._pass_exact_filters(instance, exact_filters) if filter_result is None: # The filter condition is such that nothing will match. # Bail early. return cls(context, objects=[]) if filter_result is False: continue if not cls._pass_regex_filters(instance, regex_filters): continue filtered_build_reqs.append(build_req) if (((len(filtered_build_reqs) < 2) or (not sort_keys)) and not marker): # No need to sort return cls(context, objects=filtered_build_reqs) sorted_build_reqs = cls._sort_build_requests(filtered_build_reqs, sort_keys, sort_dirs) marker_index = 0 if marker: for i, build_req in enumerate(sorted_build_reqs): if build_req.instance.uuid == marker: # The marker is the last seen item in the last page, so # we increment the index to the next item immediately # after the marker so the marker is not returned. marker_index = i + 1 break else: raise exception.MarkerNotFound(marker=marker) len_build_reqs = len(sorted_build_reqs) limit_index = len_build_reqs if limit: limit_index = marker_index + limit if limit_index > len_build_reqs: limit_index = len_build_reqs return cls(context, objects=sorted_build_reqs[marker_index:limit_index])
def test_marker_not_found_index(self, mock_get): mock_get.side_effect = exception.MarkerNotFound(marker='some-marker') req = self._get_request('?start=%s&end=%s&limit=3&marker=some-marker') self.assertRaises(webob.exc.HTTPBadRequest, self.controller.index, req)