def test_topic_list_subscriptions_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic local_sub_path = '%s/subscriptions/%s' % ( self.PROJECT_PATH, self.SUB_NAME) response = _GAXPageIterator([local_sub_path]) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) client = _Client(self.PROJECT) api = self._make_one(gax_api, client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic) subscriptions = list(iterator) next_token = iterator.next_page_token self.assertIsNone(next_token) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertEqual(subscription.topic, topic) self.assertIs(subscription._client, client) topic_path, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(topic_path, self.TOPIC_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_topics_with_paging(self): from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.topic import Topic SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' response = _GAXPageIterator([_TopicPB(self.TOPIC_PATH)], page_token=NEW_TOKEN) gax_api = _GAXPublisherAPI(_list_topics_response=response) client = _Client(self.PROJECT) api = self._make_one(gax_api, client) iterator = api.list_topics(self.PROJECT, page_size=SIZE, page_token=TOKEN) topics = list(iterator) next_token = iterator.next_page_token self.assertEqual(len(topics), 1) topic = topics[0] self.assertIsInstance(topic, Topic) self.assertEqual(topic.name, self.TOPIC_NAME) self.assertEqual(topic.full_name, self.TOPIC_PATH) self.assertEqual(next_token, NEW_TOKEN) name, page_size, options = gax_api._list_topics_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_sinks_w_paging(self): from google.cloud._testing import _GAXPageIterator from google.logging.v2.logging_config_pb2 import LogSink TOKEN = 'TOKEN' PAGE_SIZE = 42 SINKS = [{ 'name': self.SINK_PATH, 'filter': self.FILTER, 'destination': self.DESTINATION_URI, }] sink_pb = LogSink(name=self.SINK_PATH, destination=self.DESTINATION_URI, filter=self.FILTER) response = _GAXPageIterator([sink_pb]) gax_api = _GAXSinksAPI(_list_sinks_response=response) api = self._makeOne(gax_api) sinks, token = api.list_sinks( self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) self.assertEqual(sinks, SINKS) self.assertIsNone(token) project, page_size, options = gax_api._list_sinks_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, PAGE_SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_subscriptions_with_paging(self): from google.cloud._testing import _GAXPageIterator SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' response = _GAXPageIterator([_SubscriptionPB( self.SUB_PATH, self.TOPIC_PATH, self.PUSH_ENDPOINT, 0)], NEW_TOKEN) gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response) api = self._makeOne(gax_api) subscriptions, next_token = api.list_subscriptions( self.PROJECT, page_size=SIZE, page_token=TOKEN) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, dict) self.assertEqual(subscription['name'], self.SUB_PATH) self.assertEqual(subscription['topic'], self.TOPIC_PATH) self.assertEqual(subscription['pushConfig'], {'pushEndpoint': self.PUSH_ENDPOINT}) self.assertEqual(subscription['ackDeadlineSeconds'], 0) self.assertEqual(next_token, NEW_TOKEN) name, page_size, options = gax_api._list_subscriptions_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, 23) self.assertEqual(options.page_token, TOKEN)
def test_list_metrics_w_paging(self): from google.cloud._testing import _GAXPageIterator from google.logging.v2.logging_metrics_pb2 import LogMetric TOKEN = 'TOKEN' PAGE_SIZE = 42 METRICS = [{ 'name': self.METRIC_PATH, 'filter': self.FILTER, 'description': self.DESCRIPTION, }] metric_pb = LogMetric(name=self.METRIC_PATH, description=self.DESCRIPTION, filter=self.FILTER) response = _GAXPageIterator([metric_pb]) gax_api = _GAXMetricsAPI(_list_log_metrics_response=response) api = self._makeOne(gax_api) metrics, token = api.list_metrics( self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) self.assertEqual(metrics, METRICS) self.assertIsNone(token) project, page_size, options = gax_api._list_log_metrics_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, PAGE_SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_subscriptions_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator sub_pb = _SubscriptionPB( self.SUB_PATH, self.TOPIC_PATH, self.PUSH_ENDPOINT, 0) response = _GAXPageIterator([sub_pb]) gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response) api = self._makeOne(gax_api) subscriptions, next_token = api.list_subscriptions(self.PROJECT) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, dict) self.assertEqual(subscription['name'], self.SUB_PATH) self.assertEqual(subscription['topic'], self.TOPIC_PATH) self.assertEqual(subscription['pushConfig'], {'pushEndpoint': self.PUSH_ENDPOINT}) self.assertEqual(subscription['ackDeadlineSeconds'], 0) self.assertIsNone(next_token) name, page_size, options = gax_api._list_subscriptions_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_topic_list_subscriptions_with_paging(self): from google.cloud._testing import _GAXPageIterator SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' response = _GAXPageIterator([{ 'name': self.SUB_PATH, 'topic': self.TOPIC_PATH }], NEW_TOKEN) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) api = self._makeOne(gax_api) subscriptions, next_token = api.topic_list_subscriptions( self.TOPIC_PATH, page_size=SIZE, page_token=TOKEN) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, dict) self.assertEqual(subscription['name'], self.SUB_PATH) self.assertEqual(subscription['topic'], self.TOPIC_PATH) self.assertEqual(next_token, NEW_TOKEN) name, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(name, self.TOPIC_PATH) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_sinks_w_paging(self): from google.cloud.grpc.logging.v2.logging_config_pb2 import LogSink from google.cloud._testing import _GAXPageIterator from google.cloud.logging.sink import Sink TOKEN = 'TOKEN' PAGE_SIZE = 42 sink_pb = LogSink(name=self.SINK_PATH, destination=self.DESTINATION_URI, filter=self.FILTER) response = _GAXPageIterator([sink_pb]) gax_api = _GAXSinksAPI(_list_sinks_response=response) client = object() api = self._make_one(gax_api, client) iterator = api.list_sinks( self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) sinks = list(iterator) token = iterator.next_page_token # First check the token. self.assertIsNone(token) # Then check the sinks returned. self.assertEqual(len(sinks), 1) sink = sinks[0] self.assertIsInstance(sink, Sink) self.assertEqual(sink.name, self.SINK_PATH) self.assertEqual(sink.filter_, self.FILTER) self.assertEqual(sink.destination, self.DESTINATION_URI) self.assertIs(sink.client, client) project, page_size, options = gax_api._list_sinks_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, PAGE_SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_databases_w_paging(self): from google.cloud._testing import _GAXPageIterator from google.cloud.spanner_v1.database import Database SIZE = 15 TOKEN = 'TOKEN' database_pb = _DatabasePB(name=self.DATABASE_NAME) response = _GAXPageIterator([database_pb]) client = _Client(self.PROJECT) api = client.database_admin_api = _FauxDatabaseAdminAPI() api._list_databases_response = response instance = self._make_one(self.INSTANCE_ID, client) iterator = instance.list_databases(page_size=SIZE, page_token=TOKEN) next_token = iterator.next_page_token databases = list(iterator) self.assertEqual(len(databases), 1) database = databases[0] self.assertTrue(isinstance(database, Database)) self.assertEqual(database.name, self.DATABASE_NAME) self.assertEqual(next_token, None) instance_name, page_size, options = api._listed_databases self.assertEqual(instance_name, self.INSTANCE_NAME) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN) self.assertEqual(options.kwargs['metadata'], [('google-cloud-resource-prefix', instance.name)])
def test_topic_list_subscriptions_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator response = _GAXPageIterator([{ 'name': self.SUB_PATH, 'topic': self.TOPIC_PATH }], None) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) api = self._makeOne(gax_api) subscriptions, next_token = api.topic_list_subscriptions( self.TOPIC_PATH) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, dict) self.assertEqual(subscription['name'], self.SUB_PATH) self.assertEqual(subscription['topic'], self.TOPIC_PATH) self.assertIsNone(next_token) topic_path, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(topic_path, self.TOPIC_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_topic_list_subscriptions_with_paging(self): from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' local_sub_path = '%s/subscriptions/%s' % (self.PROJECT_PATH, self.SUB_NAME) response = _GAXPageIterator([local_sub_path], page_token=NEW_TOKEN) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) client = _Client(self.PROJECT) api = self._make_one(gax_api, client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic, page_size=SIZE, page_token=TOKEN) subscriptions = list(iterator) next_token = iterator.next_page_token self.assertEqual(next_token, NEW_TOKEN) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertEqual(subscription.topic, topic) self.assertIs(subscription._client, client) name, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(name, self.TOPIC_PATH) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_instance_configs_wo_paging(self): from google.cloud._testing import _GAXPageIterator from google.gax import INITIAL_PAGE from google.cloud.spanner.client import InstanceConfig credentials = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=credentials) client.connection = object() api = client._instance_admin_api = _FauxInstanceAdminAPI() config = _InstanceConfigPB(name=self.CONFIGURATION_NAME, display_name=self.DISPLAY_NAME) response = _GAXPageIterator([config]) api._list_instance_configs_response = response iterator = client.list_instance_configs() configs = list(iterator) self.assertEqual(len(configs), 1) config = configs[0] self.assertTrue(isinstance(config, InstanceConfig)) self.assertEqual(config.name, self.CONFIGURATION_NAME) self.assertEqual(config.display_name, self.DISPLAY_NAME) project, page_size, options = api._listed_instance_configs self.assertEqual(project, self.PATH) self.assertEqual(page_size, None) self.assertIs(options.page_token, INITIAL_PAGE) self.assertEqual( options.kwargs['metadata'], [('google-cloud-resource-prefix', client.project_name)])
def test_list_metrics_w_paging(self): from google.cloud._testing import _GAXPageIterator from google.logging.v2.logging_metrics_pb2 import LogMetric TOKEN = 'TOKEN' PAGE_SIZE = 42 METRICS = [{ 'name': self.METRIC_PATH, 'filter': self.FILTER, 'description': self.DESCRIPTION, }] metric_pb = LogMetric(name=self.METRIC_PATH, description=self.DESCRIPTION, filter=self.FILTER) response = _GAXPageIterator([metric_pb], None) gax_api = _GAXMetricsAPI(_list_log_metrics_response=response) api = self._makeOne(gax_api) metrics, token = api.list_metrics(self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) self.assertEqual(metrics, METRICS) self.assertIsNone(token) project, page_size, options = gax_api._list_log_metrics_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, PAGE_SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_entries_with_paging(self): from google.protobuf.struct_pb2 import Value from google.cloud._testing import _GAXPageIterator SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' PAYLOAD = {'message': 'MESSAGE', 'weather': 'sunny'} struct_pb = _StructPB( {key: Value(string_value=value) for key, value in PAYLOAD.items()}) response = _GAXPageIterator( [_LogEntryPB(self.LOG_NAME, json_payload=struct_pb)], NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) api = self._makeOne(gax_api) entries, next_token = api.list_entries([self.PROJECT], page_size=SIZE, page_token=TOKEN) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, dict) self.assertEqual(entry['logName'], self.LOG_NAME) self.assertEqual(entry['resource'], {'type': 'global'}) self.assertEqual(entry['jsonPayload'], PAYLOAD) self.assertEqual(next_token, NEW_TOKEN) projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, '') self.assertEqual(order_by, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_topic_list_subscriptions_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic local_sub_path = '%s/subscriptions/%s' % (self.PROJECT_PATH, self.SUB_NAME) response = _GAXPageIterator([local_sub_path]) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) client = _Client(self.PROJECT) api = self._makeOne(gax_api, client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic) subscriptions = list(iterator) next_token = iterator.next_page_token self.assertIsNone(next_token) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertEqual(subscription.topic, topic) self.assertIs(subscription._client, client) topic_path, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(topic_path, self.TOPIC_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_entries_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud.logging import DESCENDING from google.cloud._testing import _GAXPageIterator TOKEN = 'TOKEN' TEXT = 'TEXT' response = _GAXPageIterator( [_LogEntryPB(self.LOG_NAME, text_payload=TEXT)], TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) api = self._makeOne(gax_api) entries, next_token = api.list_entries([self.PROJECT], self.FILTER, DESCENDING) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, dict) self.assertEqual(entry['logName'], self.LOG_NAME) self.assertEqual(entry['resource'], {'type': 'global'}) self.assertEqual(entry['textPayload'], TEXT) self.assertEqual(next_token, TOKEN) projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, self.FILTER) self.assertEqual(order_by, DESCENDING) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_instance_configs_w_paging(self): from google.cloud._testing import _GAXPageIterator from google.cloud.spanner.client import InstanceConfig SIZE = 15 TOKEN_RETURNED = 'TOKEN_RETURNED' TOKEN_PASSED = 'TOKEN_PASSED' credentials = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=credentials) client.connection = object() api = client._instance_admin_api = _FauxInstanceAdminAPI() config = _InstanceConfigPB(name=self.CONFIGURATION_NAME, display_name=self.DISPLAY_NAME) response = _GAXPageIterator([config], page_token=TOKEN_RETURNED) api._list_instance_configs_response = response iterator = client.list_instance_configs(SIZE, TOKEN_PASSED) page = six.next(iterator.pages) next_token = iterator.next_page_token configs = list(page) self.assertEqual(len(configs), 1) config = configs[0] self.assertTrue(isinstance(config, InstanceConfig)) self.assertEqual(config.name, self.CONFIGURATION_NAME) self.assertEqual(config.display_name, self.DISPLAY_NAME) self.assertEqual(next_token, TOKEN_RETURNED) project, page_size, options = api._listed_instance_configs self.assertEqual(project, self.PATH) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN_PASSED) self.assertEqual( options.kwargs['metadata'], [('google-cloud-resource-prefix', client.project_name)])
def test_list_instances_wo_paging(self): from google.cloud._testing import _GAXPageIterator from google.gax import INITIAL_PAGE from google.cloud.spanner.instance import Instance credentials = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=credentials) client.connection = object() api = client._instance_admin_api = _FauxInstanceAdminAPI() instance = _InstancePB(name=self.INSTANCE_NAME, config=self.CONFIGURATION_NAME, display_name=self.DISPLAY_NAME, node_count=self.NODE_COUNT) response = _GAXPageIterator([instance]) api._list_instances_response = response iterator = client.list_instances(filter_='name:TEST') instances = list(iterator) self.assertEqual(len(instances), 1) instance = instances[0] self.assertTrue(isinstance(instance, Instance)) self.assertEqual(instance.name, self.INSTANCE_NAME) self.assertEqual(instance.configuration_name, self.CONFIGURATION_NAME) self.assertEqual(instance.display_name, self.DISPLAY_NAME) self.assertEqual(instance.node_count, self.NODE_COUNT) project, filter_, page_size, options = api._listed_instances self.assertEqual(project, self.PATH) self.assertEqual(filter_, 'name:TEST') self.assertEqual(page_size, None) self.assertIs(options.page_token, INITIAL_PAGE) self.assertEqual( options.kwargs['metadata'], [('google-cloud-resource-prefix', client.project_name)])
def test_list_subscriptions_with_paging(self): from google.cloud._testing import _GAXPageIterator SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' response = _GAXPageIterator([ _SubscriptionPB(self.SUB_PATH, self.TOPIC_PATH, self.PUSH_ENDPOINT, 0) ], NEW_TOKEN) gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response) api = self._makeOne(gax_api) subscriptions, next_token = api.list_subscriptions(self.PROJECT, page_size=SIZE, page_token=TOKEN) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, dict) self.assertEqual(subscription['name'], self.SUB_PATH) self.assertEqual(subscription['topic'], self.TOPIC_PATH) self.assertEqual(subscription['pushConfig'], {'pushEndpoint': self.PUSH_ENDPOINT}) self.assertEqual(subscription['ackDeadlineSeconds'], 0) self.assertEqual(next_token, NEW_TOKEN) name, page_size, options = gax_api._list_subscriptions_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, 23) self.assertEqual(options.page_token, TOKEN)
def test_list_metrics_w_paging(self): from google.cloud.grpc.logging.v2.logging_metrics_pb2 import LogMetric from google.cloud._testing import _GAXPageIterator from google.cloud.logging.metric import Metric TOKEN = 'TOKEN' PAGE_SIZE = 42 metric_pb = LogMetric(name=self.METRIC_PATH, description=self.DESCRIPTION, filter=self.FILTER) response = _GAXPageIterator([metric_pb]) gax_api = _GAXMetricsAPI(_list_log_metrics_response=response) client = object() api = self._make_one(gax_api, client) iterator = api.list_metrics( self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) metrics = list(iterator) token = iterator.next_page_token # First check the token. self.assertIsNone(token) # Then check the metrics returned. self.assertEqual(len(metrics), 1) metric = metrics[0] self.assertIsInstance(metric, Metric) self.assertEqual(metric.name, self.METRIC_PATH) self.assertEqual(metric.filter_, self.FILTER) self.assertEqual(metric.description, self.DESCRIPTION) self.assertIs(metric.client, client) project, page_size, options = gax_api._list_log_metrics_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, PAGE_SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_topics_with_paging(self): from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.topic import Topic SIZE = 23 TOKEN = "TOKEN" NEW_TOKEN = "NEW_TOKEN" response = _GAXPageIterator([_TopicPB(self.TOPIC_PATH)], page_token=NEW_TOKEN) gax_api = _GAXPublisherAPI(_list_topics_response=response) client = _Client(self.PROJECT) api = self._make_one(gax_api, client) iterator = api.list_topics(self.PROJECT, page_size=SIZE, page_token=TOKEN) topics = list(iterator) next_token = iterator.next_page_token self.assertEqual(len(topics), 1) topic = topics[0] self.assertIsInstance(topic, Topic) self.assertEqual(topic.name, self.TOPIC_NAME) self.assertEqual(topic.full_name, self.TOPIC_PATH) self.assertEqual(next_token, NEW_TOKEN) name, page_size, options = gax_api._list_topics_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_topic_list_subscriptions_with_paging(self): from google.cloud._testing import _GAXPageIterator SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' response = _GAXPageIterator([ {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH}], NEW_TOKEN) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) client = _Client(self.PROJECT) api = self._makeOne(gax_api, client) subscriptions, next_token = api.topic_list_subscriptions( self.TOPIC_PATH, page_size=SIZE, page_token=TOKEN) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, dict) self.assertEqual(subscription['name'], self.SUB_PATH) self.assertEqual(subscription['topic'], self.TOPIC_PATH) self.assertEqual(next_token, NEW_TOKEN) name, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(name, self.TOPIC_PATH) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_topics_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.topic import Topic TOKEN = 'TOKEN' response = _GAXPageIterator([_TopicPB(self.TOPIC_PATH)], TOKEN) gax_api = _GAXPublisherAPI(_list_topics_response=response) client = _Client(self.PROJECT) api = self._makeOne(gax_api, client) iterator = api.list_topics(self.PROJECT) topics = list(iterator) next_token = iterator.next_page_token self.assertEqual(len(topics), 1) topic = topics[0] self.assertIsInstance(topic, Topic) self.assertEqual(topic.name, self.TOPIC_NAME) self.assertEqual(topic.full_name, self.TOPIC_PATH) self.assertEqual(next_token, TOKEN) name, page_size, options = gax_api._list_topics_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_sinks_w_paging(self): from google.cloud._testing import _GAXPageIterator from google.logging.v2.logging_config_pb2 import LogSink TOKEN = 'TOKEN' PAGE_SIZE = 42 SINKS = [{ 'name': self.SINK_PATH, 'filter': self.FILTER, 'destination': self.DESTINATION_URI, }] sink_pb = LogSink(name=self.SINK_PATH, destination=self.DESTINATION_URI, filter=self.FILTER) response = _GAXPageIterator([sink_pb], None) gax_api = _GAXSinksAPI(_list_sinks_response=response) api = self._makeOne(gax_api) sinks, token = api.list_sinks(self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) self.assertEqual(sinks, SINKS) self.assertIsNone(token) project, page_size, options = gax_api._list_sinks_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, PAGE_SIZE) self.assertEqual(options.page_token, TOKEN)
def test_topic_list_subscriptions_with_paging(self): from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic SIZE = 23 TOKEN = "TOKEN" NEW_TOKEN = "NEW_TOKEN" local_sub_path = "%s/subscriptions/%s" % (self.PROJECT_PATH, self.SUB_NAME) response = _GAXPageIterator([local_sub_path], page_token=NEW_TOKEN) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) client = _Client(self.PROJECT) api = self._make_one(gax_api, client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic, page_size=SIZE, page_token=TOKEN) subscriptions = list(iterator) next_token = iterator.next_page_token self.assertEqual(next_token, NEW_TOKEN) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertEqual(subscription.topic, topic) self.assertIs(subscription._client, client) name, page_size, options = gax_api._list_topic_subscriptions_called_with self.assertEqual(name, self.TOPIC_PATH) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_entries_no_paging(self): import datetime from google.api.monitored_resource_pb2 import MonitoredResource from google.gax import INITIAL_PAGE from google.cloud.grpc.logging.v2.log_entry_pb2 import LogEntry from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud._helpers import UTC from google.cloud._testing import _GAXPageIterator from google.cloud.logging import DESCENDING from google.cloud.logging.client import Client from google.cloud.logging.entries import TextEntry from google.cloud.logging.logger import Logger TOKEN = 'TOKEN' TEXT = 'TEXT' resource_pb = MonitoredResource(type='global') timestamp = datetime.datetime.utcnow().replace(tzinfo=UTC) timestamp_pb = _datetime_to_pb_timestamp(timestamp) entry_pb = LogEntry(log_name=self.LOG_PATH, resource=resource_pb, timestamp=timestamp_pb, text_payload=TEXT) response = _GAXPageIterator([entry_pb], page_token=TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) client = Client(project=self.PROJECT, credentials=object(), use_gax=True) api = self._make_one(gax_api, client) iterator = api.list_entries( [self.PROJECT], self.FILTER, DESCENDING) entries = list(iterator) next_token = iterator.next_page_token # First check the token. self.assertEqual(next_token, TOKEN) # Then check the entries returned. self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, TextEntry) self.assertEqual(entry.payload, TEXT) self.assertIsInstance(entry.logger, Logger) self.assertEqual(entry.logger.name, self.LOG_NAME) self.assertIsNone(entry.insert_id) self.assertEqual(entry.timestamp, timestamp) self.assertIsNone(entry.labels) self.assertIsNone(entry.severity) self.assertIsNone(entry.http_request) resource_names, projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(resource_names, []) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, self.FILTER) self.assertEqual(order_by, DESCENDING) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_entries_no_paging(self): import datetime from google.api.monitored_resource_pb2 import MonitoredResource from google.gax import INITIAL_PAGE from google.cloud.grpc.logging.v2.log_entry_pb2 import LogEntry from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud._helpers import UTC from google.cloud._testing import _GAXPageIterator from google.cloud.logging import DESCENDING from google.cloud.logging.client import Client from google.cloud.logging.entries import TextEntry from google.cloud.logging.logger import Logger TOKEN = 'TOKEN' TEXT = 'TEXT' resource_pb = MonitoredResource(type='global') timestamp = datetime.datetime.utcnow().replace(tzinfo=UTC) timestamp_pb = _datetime_to_pb_timestamp(timestamp) entry_pb = LogEntry(log_name=self.LOG_PATH, resource=resource_pb, timestamp=timestamp_pb, text_payload=TEXT) response = _GAXPageIterator([entry_pb], page_token=TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=True) api = self._make_one(gax_api, client) iterator = api.list_entries( [self.PROJECT], self.FILTER, DESCENDING) entries = list(iterator) next_token = iterator.next_page_token # First check the token. self.assertEqual(next_token, TOKEN) # Then check the entries returned. self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, TextEntry) self.assertEqual(entry.payload, TEXT) self.assertIsInstance(entry.logger, Logger) self.assertEqual(entry.logger.name, self.LOG_NAME) self.assertIsNone(entry.insert_id) self.assertEqual(entry.timestamp, timestamp) self.assertIsNone(entry.labels) self.assertIsNone(entry.severity) self.assertIsNone(entry.http_request) resource_names, projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(resource_names, []) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, self.FILTER) self.assertEqual(order_by, DESCENDING) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def _list_entries_with_paging_helper(self, payload, struct_pb): import datetime from google.api.monitored_resource_pb2 import MonitoredResource from google.cloud.grpc.logging.v2.log_entry_pb2 import LogEntry from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud._helpers import UTC from google.cloud._testing import _GAXPageIterator from google.cloud.logging.client import Client from google.cloud.logging.entries import StructEntry from google.cloud.logging.logger import Logger SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' resource_pb = MonitoredResource(type='global') timestamp = datetime.datetime.utcnow().replace(tzinfo=UTC) timestamp_pb = _datetime_to_pb_timestamp(timestamp) entry_pb = LogEntry(log_name=self.LOG_PATH, resource=resource_pb, timestamp=timestamp_pb, json_payload=struct_pb) response = _GAXPageIterator([entry_pb], page_token=NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=True) api = self._make_one(gax_api, client) iterator = api.list_entries([self.PROJECT], page_size=SIZE, page_token=TOKEN) entries = list(iterator) next_token = iterator.next_page_token # First check the token. self.assertEqual(next_token, NEW_TOKEN) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, StructEntry) self.assertEqual(entry.payload, payload) self.assertIsInstance(entry.logger, Logger) self.assertEqual(entry.logger.name, self.LOG_NAME) self.assertIsNone(entry.insert_id) self.assertEqual(entry.timestamp, timestamp) self.assertIsNone(entry.labels) self.assertIsNone(entry.severity) self.assertIsNone(entry.http_request) resource_names, projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(resource_names, []) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, '') self.assertEqual(order_by, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def _list_entries_with_paging_helper(self, payload, struct_pb): import datetime from google.api.monitored_resource_pb2 import MonitoredResource from google.cloud.grpc.logging.v2.log_entry_pb2 import LogEntry from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud._helpers import UTC from google.cloud._testing import _GAXPageIterator from google.cloud.logging.client import Client from google.cloud.logging.entries import StructEntry from google.cloud.logging.logger import Logger SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' resource_pb = MonitoredResource(type='global') timestamp = datetime.datetime.utcnow().replace(tzinfo=UTC) timestamp_pb = _datetime_to_pb_timestamp(timestamp) entry_pb = LogEntry(log_name=self.LOG_PATH, resource=resource_pb, timestamp=timestamp_pb, json_payload=struct_pb) response = _GAXPageIterator([entry_pb], page_token=NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=True) api = self._make_one(gax_api, client) iterator = api.list_entries( [self.PROJECT], page_size=SIZE, page_token=TOKEN) entries = list(iterator) next_token = iterator.next_page_token # First check the token. self.assertEqual(next_token, NEW_TOKEN) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, StructEntry) self.assertEqual(entry.payload, payload) self.assertIsInstance(entry.logger, Logger) self.assertEqual(entry.logger.name, self.LOG_NAME) self.assertIsNone(entry.insert_id) self.assertEqual(entry.timestamp, timestamp) self.assertIsNone(entry.labels) self.assertIsNone(entry.severity) self.assertIsNone(entry.http_request) resource_names, projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(resource_names, []) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, '') self.assertEqual(order_by, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test__next_page_empty(self): from google.cloud._testing import _GAXPageIterator # Make a mock ``google.gax.PageIterator`` page_iter = _GAXPageIterator() # Wrap the GAX iterator. iterator = self._make_one(None, page_iter, self._do_nothing) page = iterator._next_page() self.assertIsNone(page) self.assertIsNone(iterator.next_page_token)
def test__next_page_empty(self): from google.cloud._testing import _GAXPageIterator # Make a mock ``google.gax.PageIterator`` page_iter = _GAXPageIterator() # Wrap the GAX iterator. iterator = self._makeOne(None, page_iter, self._do_nothing) page = iterator._next_page() self.assertIsNone(page) self.assertIsNone(iterator.next_page_token)
def test_list_subscriptions_with_paging(self): from google.cloud.grpc.pubsub.v1.pubsub_pb2 import PushConfig from google.cloud.grpc.pubsub.v1.pubsub_pb2 import (Subscription as SubscriptionPB) from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' push_cfg_pb = PushConfig(push_endpoint=self.PUSH_ENDPOINT) local_sub_path = '%s/subscriptions/%s' % (self.PROJECT_PATH, self.SUB_NAME) sub_pb = SubscriptionPB(name=local_sub_path, topic=self.TOPIC_PATH, push_config=push_cfg_pb) response = _GAXPageIterator([sub_pb], page_token=NEW_TOKEN) gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response) client = _Client(self.PROJECT) creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) api = self._make_one(gax_api, client) iterator = api.list_subscriptions(self.PROJECT, page_size=SIZE, page_token=TOKEN) subscriptions = list(iterator) next_token = iterator.next_page_token # Check the token returned. self.assertEqual(next_token, NEW_TOKEN) # Check the subscription object returned. self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertIsInstance(subscription.topic, Topic) self.assertEqual(subscription.topic.name, self.TOPIC_NAME) self.assertIs(subscription._client, client) self.assertEqual(subscription._project, self.PROJECT) self.assertIsNone(subscription.ack_deadline) self.assertEqual(subscription.push_endpoint, self.PUSH_ENDPOINT) name, page_size, options = gax_api._list_subscriptions_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, 23) self.assertEqual(options.page_token, TOKEN)
def test_list_subscriptions_with_paging(self): from google.cloud.grpc.pubsub.v1.pubsub_pb2 import PushConfig from google.cloud.grpc.pubsub.v1.pubsub_pb2 import ( Subscription as SubscriptionPB) from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' push_cfg_pb = PushConfig(push_endpoint=self.PUSH_ENDPOINT) local_sub_path = '%s/subscriptions/%s' % ( self.PROJECT_PATH, self.SUB_NAME) sub_pb = SubscriptionPB(name=local_sub_path, topic=self.TOPIC_PATH, push_config=push_cfg_pb) response = _GAXPageIterator([sub_pb], page_token=NEW_TOKEN) gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response) client = _Client(self.PROJECT) creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) api = self._make_one(gax_api, client) iterator = api.list_subscriptions( self.PROJECT, page_size=SIZE, page_token=TOKEN) subscriptions = list(iterator) next_token = iterator.next_page_token # Check the token returned. self.assertEqual(next_token, NEW_TOKEN) # Check the subscription object returned. self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertIsInstance(subscription.topic, Topic) self.assertEqual(subscription.topic.name, self.TOPIC_NAME) self.assertIs(subscription._client, client) self.assertEqual(subscription._project, self.PROJECT) self.assertIsNone(subscription.ack_deadline) self.assertEqual(subscription.push_endpoint, self.PUSH_ENDPOINT) name, page_size, options = gax_api._list_subscriptions_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, 23) self.assertEqual(options.page_token, TOKEN)
def test__next_page(self): from google.cloud._testing import _GAXPageIterator from google.cloud.iterator import Page # Make a mock ``google.gax.PageIterator`` page_items = (29, 31) # Items for just one page. page_token = '2sde98ds2s0hh' page_iter = _GAXPageIterator(page_items, page_token=page_token) # Wrap the GAX iterator. iterator = self._make_one(None, page_iter, self._do_nothing) page = iterator._next_page() # First check the page token. self.assertEqual(iterator.next_page_token, page_token) # Then check the page. self.assertIsInstance(page, Page) # _do_nothing will throw the iterator in front. expected = zip((iterator, iterator), page_items) self.assertEqual(list(page), list(expected))
def test_iterate(self): import six from google.cloud._testing import _GAXPageIterator item1 = object() item2 = object() item3 = object() token1 = 'smkdme30e2e32r' token2 = '39cm9csl123dck' # Make a mock ``google.gax.PageIterator`` page1 = (item1, ) page2 = (item2, item3) page_iter = _GAXPageIterator(page1, page2, page_token=token1) iterator = self._make_one(None, page_iter, self._do_nothing) self.assertEqual(iterator.num_results, 0) items_iter = iter(iterator) val1 = six.next(items_iter) self.assertEqual(val1, (iterator, item1)) self.assertEqual(iterator.num_results, 1) self.assertEqual(iterator.next_page_token, token1) # Before grabbing the next page, hot-swap the token # on the ``page_iter``. page_iter.page_token = token2 # Grab the next item (which will cause the next page). val2 = six.next(items_iter) self.assertEqual(val2, (iterator, item2)) self.assertEqual(iterator.num_results, 2) self.assertEqual(iterator.next_page_token, token2) # Grab the final item from the final / current page. val3 = six.next(items_iter) self.assertEqual(val3, (iterator, item3)) self.assertEqual(iterator.num_results, 3) # Make sure the token did not change. self.assertEqual(iterator.next_page_token, token2) with self.assertRaises(StopIteration): six.next(items_iter)
def test__next_page(self): from google.cloud._testing import _GAXPageIterator from google.cloud.iterator import Page # Make a mock ``google.gax.PageIterator`` page_items = (29, 31) # Items for just one page. page_token = '2sde98ds2s0hh' page_iter = _GAXPageIterator(page_items, page_token=page_token) # Wrap the GAX iterator. iterator = self._makeOne(None, page_iter, self._do_nothing) page = iterator._next_page() # First check the page token. self.assertEqual(iterator.next_page_token, page_token) # Then check the page. self.assertIsInstance(page, Page) # _do_nothing will throw the iterator in front. expected = zip((iterator, iterator), page_items) self.assertEqual(list(page), list(expected))
def test_iterate(self): import six from google.cloud._testing import _GAXPageIterator item1 = object() item2 = object() item3 = object() token1 = 'smkdme30e2e32r' token2 = '39cm9csl123dck' # Make a mock ``google.gax.PageIterator`` page1 = (item1,) page2 = (item2, item3) page_iter = _GAXPageIterator(page1, page2, page_token=token1) iterator = self._makeOne(None, page_iter, self._do_nothing) self.assertEqual(iterator.num_results, 0) items_iter = iter(iterator) val1 = six.next(items_iter) self.assertEqual(val1, (iterator, item1)) self.assertEqual(iterator.num_results, 1) self.assertEqual(iterator.next_page_token, token1) # Before grabbing the next page, hot-swap the token # on the ``page_iter``. page_iter.page_token = token2 # Grab the next item (which will cause the next page). val2 = six.next(items_iter) self.assertEqual(val2, (iterator, item2)) self.assertEqual(iterator.num_results, 2) self.assertEqual(iterator.next_page_token, token2) # Grab the final item from the final / current page. val3 = six.next(items_iter) self.assertEqual(val3, (iterator, item3)) self.assertEqual(iterator.num_results, 3) # Make sure the token did not change. self.assertEqual(iterator.next_page_token, token2) with self.assertRaises(StopIteration): six.next(items_iter)
def test_list_entries_no_paging(self): import datetime from google.api.monitored_resource_pb2 import MonitoredResource from google.gax import INITIAL_PAGE from google.logging.v2.log_entry_pb2 import LogEntry from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud._testing import _GAXPageIterator from google.cloud.logging import DESCENDING TOKEN = 'TOKEN' TEXT = 'TEXT' resource_pb = MonitoredResource(type='global') timestamp_pb = _datetime_to_pb_timestamp( datetime.datetime.utcnow()) entry_pb = LogEntry(log_name=self.LOG_NAME, resource=resource_pb, timestamp=timestamp_pb, text_payload=TEXT) response = _GAXPageIterator([entry_pb], page_token=TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) api = self._makeOne(gax_api) entries, next_token = api.list_entries( [self.PROJECT], self.FILTER, DESCENDING) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, dict) self.assertEqual(entry['logName'], self.LOG_NAME) self.assertEqual(entry['resource'], {'type': 'global'}) self.assertEqual(entry['textPayload'], TEXT) self.assertEqual(next_token, TOKEN) projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, self.FILTER) self.assertEqual(order_by, DESCENDING) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_instances_w_paging(self): import six from google.cloud._testing import _GAXPageIterator from google.cloud.spanner.instance import Instance SIZE = 15 TOKEN_RETURNED = 'TOKEN_RETURNED' TOKEN_PASSED = 'TOKEN_PASSED' credentials = _Credentials() client = self._make_one(project=self.PROJECT, credentials=credentials) client.connection = object() api = client._instance_admin_api = _FauxInstanceAdminAPI() instance = _InstancePB(name=self.INSTANCE_NAME, config=self.CONFIGURATION_NAME, display_name=self.DISPLAY_NAME, node_count=self.NODE_COUNT) response = _GAXPageIterator([instance], page_token=TOKEN_RETURNED) api._list_instances_response = response iterator = client.list_instances(page_size=SIZE, page_token=TOKEN_PASSED) page = six.next(iterator.pages) next_token = iterator.next_page_token instances = list(page) self.assertEqual(len(instances), 1) instance = instances[0] self.assertTrue(isinstance(instance, Instance)) self.assertEqual(instance.name, self.INSTANCE_NAME) self.assertEqual(instance.configuration_name, self.CONFIGURATION_NAME) self.assertEqual(instance.display_name, self.DISPLAY_NAME) self.assertEqual(instance.node_count, self.NODE_COUNT) self.assertEqual(next_token, TOKEN_RETURNED) project, filter_, page_size, options = api._listed_instances self.assertEqual(project, self.PATH) self.assertEqual(filter_, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN_PASSED) self.assertEqual( options.kwargs['metadata'], [('google-cloud-resource-prefix', client.project_name)])
def test_list_topics_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator TOKEN = 'TOKEN' response = _GAXPageIterator([_TopicPB(self.TOPIC_PATH)], TOKEN) gax_api = _GAXPublisherAPI(_list_topics_response=response) api = self._makeOne(gax_api) topics, next_token = api.list_topics(self.PROJECT) self.assertEqual(len(topics), 1) topic = topics[0] self.assertIsInstance(topic, dict) self.assertEqual(topic['name'], self.TOPIC_PATH) self.assertEqual(next_token, TOKEN) name, page_size, options = gax_api._list_topics_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_entries_no_paging(self): import datetime from google.api.monitored_resource_pb2 import MonitoredResource from google.gax import INITIAL_PAGE from google.logging.v2.log_entry_pb2 import LogEntry from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud._testing import _GAXPageIterator from google.cloud.logging import DESCENDING TOKEN = 'TOKEN' TEXT = 'TEXT' resource_pb = MonitoredResource(type='global') timestamp_pb = _datetime_to_pb_timestamp(datetime.datetime.utcnow()) entry_pb = LogEntry(log_name=self.LOG_NAME, resource=resource_pb, timestamp=timestamp_pb, text_payload=TEXT) response = _GAXPageIterator([entry_pb], TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) api = self._makeOne(gax_api) entries, next_token = api.list_entries([self.PROJECT], self.FILTER, DESCENDING) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, dict) self.assertEqual(entry['logName'], self.LOG_NAME) self.assertEqual(entry['resource'], {'type': 'global'}) self.assertEqual(entry['textPayload'], TEXT) self.assertEqual(next_token, TOKEN) projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, self.FILTER) self.assertEqual(order_by, DESCENDING) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def _list_entries_with_paging_helper(self, payload, struct_pb): import datetime from google.api.monitored_resource_pb2 import MonitoredResource from google.logging.v2.log_entry_pb2 import LogEntry from google.cloud._testing import _GAXPageIterator from google.cloud._helpers import _datetime_to_pb_timestamp SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' resource_pb = MonitoredResource(type='global') timestamp_pb = _datetime_to_pb_timestamp(datetime.datetime.utcnow()) entry_pb = LogEntry(log_name=self.LOG_NAME, resource=resource_pb, timestamp=timestamp_pb, json_payload=struct_pb) response = _GAXPageIterator([entry_pb], NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) api = self._makeOne(gax_api) entries, next_token = api.list_entries([self.PROJECT], page_size=SIZE, page_token=TOKEN) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, dict) self.assertEqual(entry['logName'], self.LOG_NAME) self.assertEqual(entry['resource'], {'type': 'global'}) self.assertEqual(entry['jsonPayload'], payload) self.assertEqual(next_token, NEW_TOKEN) projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, '') self.assertEqual(order_by, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def _list_entries_with_paging_helper(self, payload, struct_pb): import datetime from google.api.monitored_resource_pb2 import MonitoredResource from google.logging.v2.log_entry_pb2 import LogEntry from google.cloud._testing import _GAXPageIterator from google.cloud._helpers import _datetime_to_pb_timestamp SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' resource_pb = MonitoredResource(type='global') timestamp_pb = _datetime_to_pb_timestamp( datetime.datetime.utcnow()) entry_pb = LogEntry(log_name=self.LOG_NAME, resource=resource_pb, timestamp=timestamp_pb, json_payload=struct_pb) response = _GAXPageIterator([entry_pb], page_token=NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) api = self._makeOne(gax_api) entries, next_token = api.list_entries( [self.PROJECT], page_size=SIZE, page_token=TOKEN) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, dict) self.assertEqual(entry['logName'], self.LOG_NAME) self.assertEqual(entry['resource'], {'type': 'global'}) self.assertEqual(entry['jsonPayload'], payload) self.assertEqual(next_token, NEW_TOKEN) projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, '') self.assertEqual(order_by, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_topic_list_subscriptions_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator response = _GAXPageIterator([ {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH}], None) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) client = _Client(self.PROJECT) api = self._makeOne(gax_api, client) subscriptions, next_token = api.topic_list_subscriptions( self.TOPIC_PATH) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, dict) self.assertEqual(subscription['name'], self.SUB_PATH) self.assertEqual(subscription['topic'], self.TOPIC_PATH) self.assertIsNone(next_token) topic_path, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(topic_path, self.TOPIC_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_sinks_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator TOKEN = 'TOKEN' SINKS = [{ 'name': self.SINK_PATH, 'filter': self.FILTER, 'destination': self.DESTINATION_URI, }] response = _GAXPageIterator( [_LogSinkPB(self.SINK_PATH, self.DESTINATION_URI, self.FILTER)], TOKEN) gax_api = _GAXSinksAPI(_list_sinks_response=response) api = self._makeOne(gax_api) sinks, token = api.list_sinks(self.PROJECT) self.assertEqual(sinks, SINKS) self.assertEqual(token, TOKEN) project, page_size, options = gax_api._list_sinks_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, 0) self.assertEqual(options.page_token, INITIAL_PAGE)
def test_list_metrics_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator TOKEN = 'TOKEN' METRICS = [{ 'name': self.METRIC_PATH, 'filter': self.FILTER, 'description': self.DESCRIPTION, }] response = _GAXPageIterator( [_LogMetricPB(self.METRIC_PATH, self.DESCRIPTION, self.FILTER)], TOKEN) gax_api = _GAXMetricsAPI(_list_log_metrics_response=response) api = self._makeOne(gax_api) metrics, token = api.list_metrics(self.PROJECT) self.assertEqual(metrics, METRICS) self.assertEqual(token, TOKEN) project, page_size, options = gax_api._list_log_metrics_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, 0) self.assertEqual(options.page_token, INITIAL_PAGE)
def test_list_entries_with_extra_properties(self): from datetime import datetime # Import the wrappers to register the type URL for BoolValue # pylint: disable=unused-variable from google.protobuf import wrappers_pb2 # pylint: enable=unused-variable from google.cloud._helpers import UTC from google.cloud._helpers import _datetime_to_rfc3339 from google.cloud._testing import _GAXPageIterator NOW = datetime.utcnow().replace(tzinfo=UTC) SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' SEVERITY = 'WARNING' LABELS = { 'foo': 'bar', } IID = 'IID' bool_type_url = 'type.googleapis.com/google.protobuf.BoolValue' entry_pb = self._make_log_entry_with_extras(LABELS, IID, bool_type_url, NOW) response = _GAXPageIterator([entry_pb], NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) api = self._makeOne(gax_api) entries, next_token = api.list_entries([self.PROJECT], page_size=SIZE, page_token=TOKEN) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, dict) self.assertEqual(entry['logName'], self.LOG_NAME) self.assertEqual(entry['resource'], { 'type': 'global', 'labels': { 'foo': 'bar' } }) self.assertEqual(entry['protoPayload'], { '@type': bool_type_url, 'value': False, }) self.assertEqual(entry['severity'], SEVERITY) self.assertEqual(entry['labels'], LABELS) self.assertEqual(entry['insertId'], IID) self.assertEqual(entry['timestamp'], _datetime_to_rfc3339(NOW)) request = entry_pb.http_request EXPECTED_REQUEST = { 'requestMethod': request.request_method, 'requestUrl': request.request_url, 'status': request.status, 'requestSize': str(request.request_size), 'responseSize': str(request.response_size), 'referer': request.referer, 'userAgent': request.user_agent, 'remoteIp': request.remote_ip, 'cacheHit': request.cache_hit, } self.assertEqual(entry['httpRequest'], EXPECTED_REQUEST) operation = entry_pb.operation EXPECTED_OPERATION = { 'producer': operation.producer, 'id': operation.id, 'first': operation.first, 'last': operation.last, } self.assertEqual(entry['operation'], EXPECTED_OPERATION) self.assertEqual(next_token, NEW_TOKEN) projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, '') self.assertEqual(order_by, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_entries_with_extra_properties(self): import datetime # Import the wrappers to register the type URL for BoolValue # pylint: disable=unused-variable from google.protobuf import wrappers_pb2 # pylint: enable=unused-variable from google.cloud._helpers import UTC from google.cloud._testing import _GAXPageIterator from google.cloud.logging.client import Client from google.cloud.logging.entries import ProtobufEntry from google.cloud.logging.logger import Logger NOW = datetime.datetime.utcnow().replace(tzinfo=UTC) SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' SEVERITY = 'WARNING' LABELS = { 'foo': 'bar', } IID = 'IID' bool_type_url = 'type.googleapis.com/google.protobuf.BoolValue' entry_pb = self._make_log_entry_with_extras( LABELS, IID, bool_type_url, NOW) response = _GAXPageIterator([entry_pb], page_token=NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=True) api = self._make_one(gax_api, client) iterator = api.list_entries( [self.PROJECT], page_size=SIZE, page_token=TOKEN) entries = list(iterator) next_token = iterator.next_page_token # First check the token. self.assertEqual(next_token, NEW_TOKEN) # Then check the entries returned. self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, ProtobufEntry) self.assertEqual(entry.payload, { '@type': bool_type_url, 'value': False, }) self.assertIsInstance(entry.logger, Logger) self.assertEqual(entry.logger.name, self.LOG_NAME) self.assertEqual(entry.insert_id, IID) self.assertEqual(entry.timestamp, NOW) self.assertEqual(entry.labels, {'foo': 'bar'}) self.assertEqual(entry.severity, SEVERITY) self.assertEqual(entry.http_request, { 'requestMethod': entry_pb.http_request.request_method, 'requestUrl': entry_pb.http_request.request_url, 'status': entry_pb.http_request.status, 'requestSize': str(entry_pb.http_request.request_size), 'responseSize': str(entry_pb.http_request.response_size), 'referer': entry_pb.http_request.referer, 'userAgent': entry_pb.http_request.user_agent, 'remoteIp': entry_pb.http_request.remote_ip, 'cacheHit': entry_pb.http_request.cache_hit, }) resource_names, projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(resource_names, []) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, '') self.assertEqual(order_by, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_entries_with_extra_properties(self): from datetime import datetime # Import the wrappers to register the type URL for BoolValue # pylint: disable=unused-variable from google.protobuf import wrappers_pb2 # pylint: enable=unused-variable from google.cloud._helpers import UTC from google.cloud._helpers import _datetime_to_rfc3339 from google.cloud._testing import _GAXPageIterator NOW = datetime.utcnow().replace(tzinfo=UTC) SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' SEVERITY = 'WARNING' LABELS = { 'foo': 'bar', } IID = 'IID' bool_type_url = 'type.googleapis.com/google.protobuf.BoolValue' entry_pb = self._make_log_entry_with_extras( LABELS, IID, bool_type_url, NOW) response = _GAXPageIterator([entry_pb], page_token=NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) api = self._makeOne(gax_api) entries, next_token = api.list_entries( [self.PROJECT], page_size=SIZE, page_token=TOKEN) self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, dict) self.assertEqual(entry['logName'], self.LOG_NAME) self.assertEqual(entry['resource'], {'type': 'global', 'labels': {'foo': 'bar'}}) self.assertEqual(entry['protoPayload'], { '@type': bool_type_url, 'value': False, }) self.assertEqual(entry['severity'], SEVERITY) self.assertEqual(entry['labels'], LABELS) self.assertEqual(entry['insertId'], IID) self.assertEqual(entry['timestamp'], _datetime_to_rfc3339(NOW)) request = entry_pb.http_request EXPECTED_REQUEST = { 'requestMethod': request.request_method, 'requestUrl': request.request_url, 'status': request.status, 'requestSize': str(request.request_size), 'responseSize': str(request.response_size), 'referer': request.referer, 'userAgent': request.user_agent, 'remoteIp': request.remote_ip, 'cacheHit': request.cache_hit, } self.assertEqual(entry['httpRequest'], EXPECTED_REQUEST) operation = entry_pb.operation EXPECTED_OPERATION = { 'producer': operation.producer, 'id': operation.id, 'first': operation.first, 'last': operation.last, } self.assertEqual(entry['operation'], EXPECTED_OPERATION) self.assertEqual(next_token, NEW_TOKEN) projects, filter_, order_by, page_size, options = ( gax_api._list_log_entries_called_with) self.assertEqual(projects, [self.PROJECT]) self.assertEqual(filter_, '') self.assertEqual(order_by, '') self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def test_list_traces_with_paging(self): from google.cloud._testing import _GAXPageIterator from google.cloud.gapic.trace.v1 import trace_service_client from google.cloud.gapic.trace.v1.enums import ListTracesRequest as Enum from datetime import datetime trace_id = 'test_trace_id' span_id = 1234 span_name = 'test_span_name' span_kind = 'RPC_CLIENT' parent_span_id = 123 start_time = datetime.utcnow().isoformat() + 'Z' end_time = datetime.utcnow().isoformat() + 'Z' labels = { '/http/status_code': '200', '/component': 'HTTP load balancer', } size = 10 view_type = Enum.ViewType.COMPLETE token = 'TOKEN' trace_pb = self._make_trace_pb(self.project, trace_id, span_id, span_name, start_time, end_time, parent_span_id, labels) response = _GAXPageIterator(trace_pb) gax_api = mock.Mock(spec=trace_service_client.TraceServiceClient) gax_api.list_traces.return_value = response api = self._make_one(gax_api, None) iterator = api.list_traces(project_id=self.project, view=view_type, page_size=size, page_token=token) traces = list(iterator) self.assertEqual(len(traces), 1) trace = traces[0] self.assertEqual(len(trace['spans']), 1) span = trace['spans'][0] self.assertEqual(trace['projectId'], self.project) self.assertEqual(trace['traceId'], trace_id) self.assertEqual(span['spanId'], str(span_id)) self.assertEqual(span['name'], span_name) self.assertEqual(span['startTime'], start_time) self.assertEqual(span['endTime'], end_time) self.assertEqual(span['kind'], span_kind) self.assertEqual(span['parentSpanId'], str(parent_span_id)) self.assertEqual(span['labels'], labels) call_args = gax_api.list_traces.call_args[1] self.assertEqual(call_args['project_id'], self.project) self.assertEqual(call_args['view'], view_type) self.assertEqual(call_args['page_size'], size) self.assertIsNone(call_args['start_time']) self.assertIsNone(call_args['end_time']) self.assertIsNone(call_args['filter_']) self.assertIsNone(call_args['order_by']) self.assertEqual(call_args['options'].page_token, token)