Skip to content

Commit

Permalink
Fix TestHTTPRequestLog: write both logs at file upload (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonk authored Jun 9, 2022
1 parent 0ab8924 commit dd16b52
Showing 1 changed file with 59 additions and 41 deletions.
100 changes: 59 additions & 41 deletions integration_test/ops_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,74 +465,92 @@ func TestHTTPRequestLog(t *testing.T) {
"status": 200,
}

// Write a log with an http request in the message to cloud logging and immediately query it.
writeAndQueryLog := func(httpRequestKey string, logId string) *cloudlogging.Entry {
logBody := map[string]interface{}{
"logId": logId,
httpRequestKey: httpRequestBody,
}
logBytes, err := json.Marshal(logBody)
if err != nil {
t.Fatalf("could not marshal test log: %v", err)
}
err = gce.UploadContent(
ctx,
logger,
vm,
strings.NewReader(string(logBytes)+"\n"),
logPath)
if err != nil {
t.Fatalf("error writing log line: %v", err)
}
// Log with HTTP request data nested under "logging.googleapis.com/httpRequest".
const newHTTPRequestKey = confgenerator.HttpRequestKey
const newHTTPRequestLogId = "new_request_log"
newLogBody := map[string]interface{}{
"logId": newHTTPRequestLogId,
newHTTPRequestKey: httpRequestBody,
}
newLogBytes, err := json.Marshal(newLogBody)
if err != nil {
t.Fatalf("could not marshal new test log: %v", err)
}

// Log with HTTP request data nested under "logging.googleapis.com/http_request".
const oldHTTPRequestKey = "logging.googleapis.com/http_request"
const oldHTTPRequestLogId = "old_request_log"
oldLogBody := map[string]interface{}{
"logId": oldHTTPRequestLogId,
oldHTTPRequestKey: httpRequestBody,
}
oldLogBytes, err := json.Marshal(oldLogBody)
if err != nil {
t.Fatalf("could not marshal old test log: %v", err)
}

// Write both logs to log source file at the same time.
err = gce.UploadContent(
ctx,
logger,
vm,
strings.NewReader(fmt.Sprintf("%s\n%s\n", string(newLogBytes), string(oldLogBytes))),
logPath)
if err != nil {
t.Fatalf("error writing log line: %v", err)
}

entry, err := gce.QueryLog(
queryLogById := func(logId string) (*cloudlogging.Entry, error) {
return gce.QueryLog(
ctx,
logger.ToMainLog(),
vm,
"mylog_source",
time.Hour,
fmt.Sprintf("jsonPayload.logId=%q", logId),
gce.QueryMaxAttempts)
if err != nil {
t.Fatalf("could not find written log with id %s: %v", logId, err)
}

isKeyInPayload := func(httpRequestKey string, entry *cloudlogging.Entry) bool {
payload := entry.Payload.(*structpb.Struct)
for k := range payload.GetFields() {
if k == httpRequestKey {
return true
}
}
return entry
return false
}

// Test that the new documented field, "logging.googleapis.com/httpRequest", will be
// parsed as expected by Fluent Bit.
t.Run("parse new HTTPRequest key", func(t *testing.T) {
httpRequestKey := confgenerator.HttpRequestKey
entry := writeAndQueryLog(httpRequestKey, "341231")
payload := entry.Payload.(*structpb.Struct)
for key := range payload.GetFields() {
if key == httpRequestKey {
t.Fatal("expected request key to be stripped out of message")
}
t.Parallel()
entry, err := queryLogById(newHTTPRequestLogId)
if err != nil {
t.Fatalf("could not find written log with id %s: %v", newHTTPRequestLogId, err)
}
if entry.HTTPRequest == nil {
t.Fatal("expected log entry to have HTTPRequest field")
}
if isKeyInPayload(newHTTPRequestKey, entry) {
t.Fatalf("expected %s key to be stripped out of the payload", newHTTPRequestKey)
}
})

// Test that the old field, "logging.googleapis.com/http_request", is no longer
// parsed by Fluent Bit.
t.Run("don't parse old HTTPRequest key", func(t *testing.T) {
oldHTTPRequestKey := "logging.googleapis.com/http_request"
entry := writeAndQueryLog(oldHTTPRequestKey, "34203948")
payload := entry.Payload.(*structpb.Struct)
foundKey := false
for key := range payload.GetFields() {
if key == oldHTTPRequestKey {
foundKey = true
}
}
if !foundKey {
t.Fatalf("expected %s key to be present in the payload", oldHTTPRequestKey)
t.Parallel()
entry, err := queryLogById(oldHTTPRequestLogId)
if err != nil {
t.Fatalf("could not find written log with id %s: %v", oldHTTPRequestLogId, err)
}
if entry.HTTPRequest != nil {
t.Fatal("expected log entry not to have HTTPRequest field")
}
if !isKeyInPayload(oldHTTPRequestKey, entry) {
t.Fatalf("expected %s key to be present in payload", oldHTTPRequestKey)
}
})
})
}
Expand Down

0 comments on commit dd16b52

Please sign in to comment.