Skip to content

Commit 93dc850

Browse files
authored
Rel1.5 (#125)
* prep rel1.5 * update unit tests
1 parent ceec198 commit 93dc850

File tree

1 file changed

+143
-3
lines changed

1 file changed

+143
-3
lines changed

pkg/plugin/cloudlogging/cloudlogging_test.go

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestGetLogEntryMessage(t *testing.T) {
4141
expected *expectedResult
4242
}{
4343
{
44-
name: "Text payload",
44+
name: "Empty payload",
4545
entry: &loggingpb.LogEntry{},
4646
expected: &expectedResult{
4747
err: errors.New("empty payload <nil>"),
@@ -94,7 +94,7 @@ func TestGetLogEntryMessage(t *testing.T) {
9494
},
9595
},
9696
expected: &expectedResult{
97-
message: "{\"database_role\":\"user\",\"severity\":\"INFO\"}",
97+
message: "{\"database_role\":\"user\", \"severity\":\"INFO\"}",
9898
},
9999
},
100100
{
@@ -120,6 +120,50 @@ func TestGetLogEntryMessage(t *testing.T) {
120120
message: "Message body",
121121
},
122122
},
123+
{
124+
name: "JSON payload, with empty string message",
125+
entry: &loggingpb.LogEntry{
126+
Payload: &loggingpb.LogEntry_JsonPayload{
127+
JsonPayload: &structpb.Struct{
128+
Fields: map[string]*structpb.Value{
129+
"message": {
130+
Kind: &structpb.Value_StringValue{StringValue: ""},
131+
},
132+
"data": {
133+
Kind: &structpb.Value_StringValue{StringValue: "some data"},
134+
},
135+
},
136+
},
137+
},
138+
},
139+
expected: &expectedResult{
140+
message: "\"\"",
141+
},
142+
},
143+
{
144+
name: "JSON payload, with complex message field",
145+
entry: &loggingpb.LogEntry{
146+
Payload: &loggingpb.LogEntry_JsonPayload{
147+
JsonPayload: &structpb.Struct{
148+
Fields: map[string]*structpb.Value{
149+
"message": {
150+
Kind: &structpb.Value_StructValue{
151+
StructValue: &structpb.Struct{
152+
Fields: map[string]*structpb.Value{
153+
"text": {Kind: &structpb.Value_StringValue{StringValue: "nested message"}},
154+
"code": {Kind: &structpb.Value_NumberValue{NumberValue: 123}},
155+
},
156+
},
157+
},
158+
},
159+
},
160+
},
161+
},
162+
},
163+
expected: &expectedResult{
164+
message: "{\"code\":123, \"text\":\"nested message\"}",
165+
},
166+
},
123167
}
124168

125169
for _, tc := range testCases {
@@ -128,6 +172,8 @@ func TestGetLogEntryMessage(t *testing.T) {
128172

129173
if tc.expected.err != nil {
130174
require.ErrorContains(t, err, tc.expected.err.Error())
175+
} else {
176+
require.NoError(t, err)
131177
}
132178
require.Equal(t, tc.expected.message, message)
133179
})
@@ -254,7 +300,7 @@ func TestGetLogLabels(t *testing.T) {
254300
},
255301
},
256302
{
257-
name: "JSON payload",
303+
name: "JSON payload with nested fields",
258304
entry: &loggingpb.LogEntry{
259305
InsertId: "insert-id4",
260306
Labels: map[string]string{
@@ -301,6 +347,100 @@ func TestGetLogLabels(t *testing.T) {
301347
"jsonPayload.service_context.version": "v42",
302348
},
303349
},
350+
{
351+
name: "Text payload",
352+
entry: &loggingpb.LogEntry{
353+
InsertId: "insert-id5",
354+
Payload: &loggingpb.LogEntry_TextPayload{
355+
TextPayload: "This is a text log message",
356+
},
357+
},
358+
expected: data.Labels{
359+
"id": "insert-id5",
360+
"level": "info",
361+
"textPayload": "This is a text log message",
362+
},
363+
},
364+
{
365+
name: "Trace and span data",
366+
entry: &loggingpb.LogEntry{
367+
InsertId: "insert-id6",
368+
Trace: "projects/my-project/traces/06796866738c859f2f19b7cfb3214824",
369+
SpanId: "000000000000004a",
370+
},
371+
expected: data.Labels{
372+
"id": "insert-id6",
373+
"level": "info",
374+
"trace": "projects/my-project/traces/06796866738c859f2f19b7cfb3214824",
375+
"traceId": "06796866738c859f2f19b7cfb3214824",
376+
"spanId": "000000000000004a",
377+
},
378+
},
379+
{
380+
name: "JSON payload with various field types",
381+
entry: &loggingpb.LogEntry{
382+
InsertId: "insert-id7",
383+
Payload: &loggingpb.LogEntry_JsonPayload{
384+
JsonPayload: &structpb.Struct{
385+
Fields: map[string]*structpb.Value{
386+
"string_field": {Kind: &structpb.Value_StringValue{StringValue: "test"}},
387+
"number_field": {Kind: &structpb.Value_NumberValue{NumberValue: 42.5}},
388+
"bool_field": {Kind: &structpb.Value_BoolValue{BoolValue: false}},
389+
"null_field": {Kind: &structpb.Value_NullValue{}},
390+
"list_field": {Kind: &structpb.Value_ListValue{
391+
ListValue: &structpb.ListValue{
392+
Values: []*structpb.Value{
393+
{Kind: &structpb.Value_StringValue{StringValue: "item1"}},
394+
{Kind: &structpb.Value_NumberValue{NumberValue: 2}},
395+
},
396+
},
397+
}},
398+
},
399+
},
400+
},
401+
},
402+
expected: data.Labels{
403+
"id": "insert-id7",
404+
"level": "info",
405+
"jsonPayload.string_field": "test",
406+
"jsonPayload.number_field": "42.5",
407+
"jsonPayload.bool_field": "false",
408+
"jsonPayload.null_field": "null_value:NULL_VALUE",
409+
"jsonPayload.list_field": "list_value:{values:{string_value:\"item1\"} values:{number_value:2}}",
410+
},
411+
},
412+
{
413+
name: "Proto payload with AuditLog",
414+
entry: &loggingpb.LogEntry{
415+
InsertId: "insert-id8",
416+
Payload: &loggingpb.LogEntry_ProtoPayload{
417+
ProtoPayload: &anypb.Any{
418+
TypeUrl: "type.googleapis.com/google.cloud.audit.AuditLog",
419+
Value: []byte{}, // Empty for simplicity in test
420+
},
421+
},
422+
},
423+
expected: data.Labels{
424+
"id": "insert-id8",
425+
"level": "info",
426+
},
427+
},
428+
{
429+
name: "Proto payload with RequestLog",
430+
entry: &loggingpb.LogEntry{
431+
InsertId: "insert-id9",
432+
Payload: &loggingpb.LogEntry_ProtoPayload{
433+
ProtoPayload: &anypb.Any{
434+
TypeUrl: "type.googleapis.com/google.appengine.logging.v1.RequestLog",
435+
Value: []byte{}, // Empty for simplicity in test
436+
},
437+
},
438+
},
439+
expected: data.Labels{
440+
"id": "insert-id9",
441+
"level": "info",
442+
},
443+
},
304444
}
305445

306446
for _, tc := range testCases {

0 commit comments

Comments
 (0)