Esempio n. 1
0
    def test_get_iam_policy(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock response
        version = 351608024
        etag = b'21'
        expected_response = policy_pb2.Policy(version=version, etag=etag)
        grpc_stub.GetIamPolicy.return_value = expected_response

        response = client.get_iam_policy(resource)
        self.assertEqual(expected_response, response)

        grpc_stub.GetIamPolicy.assert_called_once()
        args, kwargs = grpc_stub.GetIamPolicy.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = iam_policy_pb2.GetIamPolicyRequest(
            resource=resource)
        self.assertEqual(expected_request, actual_request)
Esempio n. 2
0
    def __init__(self, batch_settings=(), batch_class=thread.Batch, **kwargs):
        # Sanity check: Is our goal to use the emulator?
        # If so, create a grpc insecure channel with the emulator host
        # as the target.
        if os.environ.get('PUBSUB_EMULATOR_HOST'):
            kwargs['channel'] = grpc.insecure_channel(
                target=os.environ.get('PUBSUB_EMULATOR_HOST'),
            )

        # Use a custom channel.
        # We need this in order to set appropriate default message size and
        # keepalive options.
        if 'channel' not in kwargs:
            kwargs['channel'] = grpc_helpers.create_channel(
                credentials=kwargs.get('credentials', None),
                target=self.target,
                scopes=publisher_client.PublisherClient._ALL_SCOPES,
                options={
                    'grpc.max_send_message_length': -1,
                    'grpc.max_receive_message_length': -1,
                }.items(),
            )

        # Add the metrics headers, and instantiate the underlying GAPIC
        # client.
        kwargs['lib_name'] = 'gccl'
        kwargs['lib_version'] = __VERSION__
        self.api = publisher_client.PublisherClient(**kwargs)
        self.batch_settings = types.BatchSettings(*batch_settings)

        # The batches on the publisher client are responsible for holding
        # messages. One batch exists for each topic.
        self._batch_class = batch_class
        self._batch_lock = threading.Lock()
        self._batches = {}
Esempio n. 3
0
    def test_list_topics(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        project = client.project_path('[PROJECT]')

        # Mock response
        next_page_token = ''
        topics_element = pubsub_pb2.Topic()
        topics = [topics_element]
        expected_response = pubsub_pb2.ListTopicsResponse(
            next_page_token=next_page_token, topics=topics)
        grpc_stub.ListTopics.return_value = expected_response

        paged_list_response = client.list_topics(project)
        resources = list(paged_list_response)
        self.assertEqual(1, len(resources))
        self.assertEqual(expected_response.topics[0], resources[0])

        grpc_stub.ListTopics.assert_called_once()
        args, kwargs = grpc_stub.ListTopics.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = pubsub_pb2.ListTopicsRequest(project=project)
        self.assertEqual(expected_request, actual_request)
Esempio n. 4
0
    def test_test_iam_permissions(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')
        permissions = []

        # Mock response
        expected_response = iam_policy_pb2.TestIamPermissionsResponse()
        grpc_stub.TestIamPermissions.return_value = expected_response

        response = client.test_iam_permissions(resource, permissions)
        self.assertEqual(expected_response, response)

        grpc_stub.TestIamPermissions.assert_called_once()
        args, kwargs = grpc_stub.TestIamPermissions.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = iam_policy_pb2.TestIamPermissionsRequest(
            resource=resource, permissions=permissions)
        self.assertEqual(expected_request, actual_request)
Esempio n. 5
0
    def test_list_topic_subscriptions(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock response
        next_page_token = ''
        subscriptions_element = 'subscriptionsElement1698708147'
        subscriptions = [subscriptions_element]
        expected_response = pubsub_pb2.ListTopicSubscriptionsResponse(
            next_page_token=next_page_token, subscriptions=subscriptions)
        grpc_stub.ListTopicSubscriptions.return_value = expected_response

        paged_list_response = client.list_topic_subscriptions(topic)
        resources = list(paged_list_response)
        self.assertEqual(1, len(resources))
        self.assertEqual(expected_response.subscriptions[0], resources[0])

        grpc_stub.ListTopicSubscriptions.assert_called_once()
        args, kwargs = grpc_stub.ListTopicSubscriptions.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = pubsub_pb2.ListTopicSubscriptionsRequest(
            topic=topic)
        self.assertEqual(expected_request, actual_request)
Esempio n. 6
0
    def test_publish(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')
        data = b'-86'
        messages_element = pubsub_pb2.PubsubMessage(data=data)
        messages = [messages_element]

        # Mock response
        message_ids_element = 'messageIdsElement-744837059'
        message_ids = [message_ids_element]
        expected_response = pubsub_pb2.PublishResponse(message_ids=message_ids)
        grpc_stub.Publish.return_value = expected_response

        response = client.publish(topic, messages)
        self.assertEqual(expected_response, response)

        grpc_stub.Publish.assert_called_once()
        args, kwargs = grpc_stub.Publish.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = pubsub_pb2.PublishRequest(topic=topic,
                                                     messages=messages)
        self.assertEqual(expected_request, actual_request)
Esempio n. 7
0
    def test_publish(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.PublisherStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')
        data = b'-86'
        messages_element = pubsub_pb2.PubsubMessage(data)
        messages = [messages_element]

        # Mock response
        message_ids_element = 'messageIdsElement-744837059'
        message_ids = [message_ids_element]
        expected_response = pubsub_pb2.PublishResponse(message_ids)
        grpc_stub.Publish.return_value = expected_response

        response = client.publish(topic, messages)
        self.assertEqual(expected_response, response)

        grpc_stub.Publish.assert_called_once()
        request = grpc_stub.Publish.call_args[0]

        self.assertEqual(topic, request.topic)
        self.assertEqual(messages, request.messages)
Esempio n. 8
0
    def test_set_iam_policy(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=iam_policy_pb2.IAMPolicyStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')
        policy = policy_pb2.Policy()

        # Mock response
        version = 351608024
        etag = b'21'
        expected_response = policy_pb2.Policy(version, etag)
        grpc_stub.SetIamPolicy.return_value = expected_response

        response = client.set_iam_policy(resource, policy)
        self.assertEqual(expected_response, response)

        grpc_stub.SetIamPolicy.assert_called_once()
        request = grpc_stub.SetIamPolicy.call_args[0]

        self.assertEqual(resource, request.resource)
        self.assertEqual(policy, request.policy)
Esempio n. 9
0
    def test_create_topic(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        name = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock response
        name_2 = 'name2-1052831874'
        expected_response = pubsub_pb2.Topic(name=name_2)
        grpc_stub.CreateTopic.return_value = expected_response

        response = client.create_topic(name)
        self.assertEqual(expected_response, response)

        grpc_stub.CreateTopic.assert_called_once()
        args, kwargs = grpc_stub.CreateTopic.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = pubsub_pb2.Topic(name=name)
        self.assertEqual(expected_request, actual_request)
    def __init__(self, batch_settings=(), batch_class=thread.Batch, **kwargs):
        # Add the metrics headers, and instantiate the underlying GAPIC
        # client.
        kwargs['lib_name'] = 'gccl'
        kwargs['lib_version'] = __VERSION__
        self.api = publisher_client.PublisherClient(**kwargs)
        self.batch_settings = types.BatchSettings(*batch_settings)

        # The batches on the publisher client are responsible for holding
        # messages. One batch exists for each topic.
        self._batch_class = batch_class
        self._batch_lock = threading.Lock()
        self._batches = {}
Esempio n. 11
0
    def test_get_topic_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.PublisherStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock exception response
        grpc_stub.GetTopic.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.get_topic, topic)
Esempio n. 12
0
    def test_get_iam_policy_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock exception response
        grpc_stub.GetIamPolicy.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.get_iam_policy, resource)
Esempio n. 13
0
    def test_create_topic_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        name = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock exception response
        grpc_stub.CreateTopic.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.create_topic, name)
Esempio n. 14
0
    def test_list_topics_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.PublisherStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        project = client.project_path('[PROJECT]')

        # Mock exception response
        grpc_stub.ListTopics.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.list_topics, project)
Esempio n. 15
0
    def test_list_topic_subscriptions_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock exception response
        grpc_stub.ListTopicSubscriptions.side_effect = CustomException()

        paged_list_response = client.list_topic_subscriptions(topic)
        self.assertRaises(errors.GaxError, list, paged_list_response)
Esempio n. 16
0
    def test_delete_topic(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.PublisherStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        client.delete_topic(topic)

        grpc_stub.DeleteTopic.assert_called_once()
        request = grpc_stub.DeleteTopic.call_args[0]

        self.assertEqual(topic, request.topic)
Esempio n. 17
0
    def test_test_iam_permissions_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=iam_policy_pb2.IAMPolicyStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')
        permissions = []

        # Mock exception response
        grpc_stub.TestIamPermissions.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.test_iam_permissions,
                          resource, permissions)
def test_grpc_request_with_on_demand_jwt_credentials():
    credentials, project_id = google.auth.default()
    credentials = google.auth.jwt.OnDemandCredentials.from_signing_credentials(
        credentials)

    channel = google.auth.transport.grpc.secure_authorized_channel(
        credentials, None, publisher_client.PublisherClient.SERVICE_ADDRESS)

    # Create a pub/sub client.
    client = publisher_client.PublisherClient(channel=channel)

    # list the topics and drain the iterator to test that an authorized API
    # call works.
    list_topics_iter = client.list_topics(
        project='projects/{}'.format(project_id))
    list(list_topics_iter)
def test_grpc_request_with_regular_credentials(http_request):
    credentials, project_id = google.auth.default()
    credentials = google.auth.credentials.with_scopes_if_required(
        credentials, ['https://www.googleapis.com/auth/pubsub'])

    channel = google.auth.transport.grpc.secure_authorized_channel(
        credentials, http_request,
        publisher_client.PublisherClient.SERVICE_ADDRESS)

    # Create a pub/sub client.
    client = publisher_client.PublisherClient(channel=channel)

    # list the topics and drain the iterator to test that an authorized API
    # call works.
    list_topics_iter = client.list_topics(
        project='projects/{}'.format(project_id))
    list(list_topics_iter)
Esempio n. 20
0
    def test_publish_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')
        data = b'-86'
        messages_element = pubsub_pb2.PubsubMessage(data=data)
        messages = [messages_element]

        # Mock exception response
        grpc_stub.Publish.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.publish, topic, messages)
Esempio n. 21
0
def test_grpc_request_with_jwt_credentials(http_request):
    credentials, project_id = google.auth.default()
    audience = 'https://{}/google.pubsub.v1.Publisher'.format(
        publisher_client.PublisherClient.SERVICE_ADDRESS)
    credentials = google.auth.jwt.Credentials.from_signing_credentials(
        credentials,
        audience=audience)

    channel = google.auth.transport.grpc.secure_authorized_channel(
        credentials,
        http_request,
        publisher_client.PublisherClient.SERVICE_ADDRESS)

    # Create a pub/sub client.
    client = publisher_client.PublisherClient(channel=channel)

    # list the topics and drain the iterator to test that an authorized API
    # call works.
    list_topics_iter = client.list_topics(
        project='projects/{}'.format(project_id))
    list(list_topics_iter)
Esempio n. 22
0
    def test_delete_topic(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        client.delete_topic(topic)

        grpc_stub.DeleteTopic.assert_called_once()
        args, kwargs = grpc_stub.DeleteTopic.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = pubsub_pb2.DeleteTopicRequest(topic=topic)
        self.assertEqual(expected_request, actual_request)
Esempio n. 23
0
    def test_get_topic(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.PublisherStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock response
        name = 'name3373707'
        expected_response = pubsub_pb2.Topic(name)
        grpc_stub.GetTopic.return_value = expected_response

        response = client.get_topic(topic)
        self.assertEqual(expected_response, response)

        grpc_stub.GetTopic.assert_called_once()
        request = grpc_stub.GetTopic.call_args[0]

        self.assertEqual(topic, request.topic)
Esempio n. 24
0
    def test_create_topic(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.PublisherStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        name = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock response
        name_2 = 'name2-1052831874'
        expected_response = pubsub_pb2.Topic(name_2)
        grpc_stub.CreateTopic.return_value = expected_response

        response = client.create_topic(name)
        self.assertEqual(expected_response, response)

        grpc_stub.CreateTopic.assert_called_once()
        request = grpc_stub.CreateTopic.call_args[0]

        self.assertEqual(name, request.name)
Esempio n. 25
0
    def test_test_iam_permissions(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=iam_policy_pb2.IAMPolicyStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')
        permissions = []

        # Mock response
        expected_response = iam_policy_pb2.TestIamPermissionsResponse()
        grpc_stub.TestIamPermissions.return_value = expected_response

        response = client.test_iam_permissions(resource, permissions)
        self.assertEqual(expected_response, response)

        grpc_stub.TestIamPermissions.assert_called_once()
        request = grpc_stub.TestIamPermissions.call_args[0]

        self.assertEqual(resource, request.resource)
        self.assertEqual(permissions, request.permissions)