Example #1
0
def _log_entry_mapping_to_pb(mapping):
    """Helper for :meth:`write_entries`, et aliae

    Performs "impedance matching" between the protobuf attrs and
    the keys expected in the JSON API.
    """
    entry_pb = LogEntryPB.pb(LogEntryPB())
    # NOTE: We assume ``mapping`` was created in ``Batch.commit``
    #       or ``Logger._make_entry_resource``. In either case, if
    #       the ``protoPayload`` key is present, we assume that the
    #       type URL is registered with ``google.protobuf`` and will
    #       not cause any issues in the JSON->protobuf conversion
    #       of the corresponding ``proto_payload`` in the log entry
    #       (it is an ``Any`` field).
    ParseDict(mapping, entry_pb)
    return LogEntryPB(entry_pb)
    def test_registered_type(self):
        from google.protobuf import any_pb2
        from google.protobuf import descriptor_pool
        from google.protobuf.struct_pb2 import Struct
        from google.protobuf.struct_pb2 import Value

        pool = descriptor_pool.Default()
        type_name = "google.protobuf.Struct"
        # Make sure the descriptor is known in the registry.
        descriptor = pool.FindMessageTypeByName(type_name)
        self.assertEqual(descriptor.name, "Struct")

        type_url = "type.googleapis.com/" + type_name
        field_name = "foo"
        field_value = "Bar"
        struct_pb = Struct(
            fields={field_name: Value(string_value=field_value)})
        any_pb = any_pb2.Any(type_url=type_url,
                             value=struct_pb.SerializeToString())

        entry_pb = LogEntryPB(proto_payload=any_pb, log_name="all-good")
        result = self._call_fut(LogEntryPB.pb(entry_pb))
        expected_proto = {
            "logName": entry_pb.log_name,
            "protoPayload": {
                "@type": type_url,
                "value": {
                    field_name: field_value
                }
            },
        }
        self.assertEqual(result, expected_proto)
    def test_registered_type(self):
        from google.protobuf import any_pb2
        from google.protobuf import descriptor_pool

        pool = descriptor_pool.Default()
        type_name = "google.protobuf.Struct"
        # Make sure the descriptor is known in the registry.
        descriptor = pool.FindMessageTypeByName(type_name)
        self.assertEqual(descriptor.name, "Struct")

        type_url = "type.googleapis.com/" + type_name
        field_name = "foo"
        field_value = "Bar"
        json_mapping = {
            "logName": "hi-everybody",
            "protoPayload": {
                "@type": type_url,
                "value": {
                    field_name: field_value
                }
            },
        }
        # Convert to a valid LogEntry.
        result = self._call_fut(json_mapping)
        entry_pb = LogEntryPB(
            log_name=json_mapping["logName"],
            proto_payload=any_pb2.Any(
                type_url=type_url,
                value=b"\n\014\n\003foo\022\005\032\003Bar"),
        )
        self.assertEqual(result, entry_pb)
    def test_list_entries(self):
        client = self.make_logging_api()

        log_entry_msg = LogEntryPB(log_name=self.LOG_PATH, text_payload="text")

        with mock.patch.object(
                type(client._gapic_api.transport.list_log_entries),
                "__call__") as call:
            call.return_value = logging_v2.types.ListLogEntriesResponse(
                entries=[log_entry_msg])
            result = client.list_entries([PROJECT_PATH],
                                         filter_=FILTER,
                                         order_by=logging_v2.DESCENDING)

        entries = list(result)

        # Check the response
        assert len(entries) == 1
        entry = entries[0]

        assert isinstance(entry, logging_v2.entries.TextEntry)
        assert entry.payload == "text"

        # Check the request
        call.assert_called_once()
        request = call.call_args.args[0]
        assert request.resource_names == [PROJECT_PATH]
        assert request.filter == FILTER
        assert request.order_by == logging_v2.DESCENDING
 def test_simple(self):
     entry_pb = LogEntryPB(log_name="lol-jk", text_payload="bah humbug")
     result = self._call_fut(LogEntryPB.pb(entry_pb))
     expected = {
         "logName": entry_pb.log_name,
         "textPayload": entry_pb.text_payload
     }
     self.assertEqual(result, expected)
Example #6
0
    def test_list_logs_with_max_results(self):
        client = self.make_logging_api()
        log_entry_msg = LogEntryPB(log_name=self.LOG_PATH, text_payload="text")

        with mock.patch.object(
                type(client._gapic_api.transport.list_log_entries),
                "__call__") as call:
            call.return_value = logging_v2.types.ListLogEntriesResponse(
                entries=[log_entry_msg, log_entry_msg])
            result = client.list_entries(
                [PROJECT_PATH],
                filter_=FILTER,
                order_by=google.cloud.logging.ASCENDING,
                page_size=42,
                page_token="token",
                max_results=1,
            )

        # Check the request
        call.assert_called_once()
        assert len(list(result)) == 1
    def test_unregistered_type(self):
        from google.protobuf import any_pb2
        from google.protobuf import descriptor_pool
        from google.protobuf.timestamp_pb2 import Timestamp

        pool = descriptor_pool.Default()
        type_name = "google.bigtable.admin.v2.UpdateClusterMetadata"
        # Make sure the descriptor is not known in the registry.
        with self.assertRaises(KeyError):
            pool.FindMessageTypeByName(type_name)

        type_url = "type.googleapis.com/" + type_name
        metadata_bytes = b"\n\n\n\x03foo\x12\x03bar\x12\x06\x08\xbd\xb6\xfb\xc6\x05"
        any_pb = any_pb2.Any(type_url=type_url, value=metadata_bytes)
        timestamp = Timestamp(seconds=61, nanos=1234000)

        entry_pb = LogEntryPB(proto_payload=any_pb, timestamp=timestamp)
        result = self._call_fut(LogEntryPB.pb(entry_pb))
        self.assertEqual(len(result), 2)
        self.assertEqual(result["timestamp"], "1970-01-01T00:01:01.001234Z")
        # NOTE: This "hack" is needed on Windows, where the equality check
        #       for an ``Any`` instance fails on unregistered types.
        self.assertEqual(result["protoPayload"].type_url, type_url)
        self.assertEqual(result["protoPayload"].value, metadata_bytes)
 def test_simple(self):
     result = self._call_fut({})
     self.assertEqual(result, LogEntryPB())