Skip to content

Commit

Permalink
move logic for checking for duplicates to existing function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewqian2001datadog committed Oct 4, 2024
1 parent 0644d5a commit 1bb1a6a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,4 @@ func assertLogsReceived(

t.Logf("stdout:\n\n%s\n\nstderr:\n\n%s", stdout, stderr)
utils.CheckLogsExpected(t, fakeIntake, "test-app", "bob", []string{sourceHostTag})
utils.CheckNoDuplicateTags(t, fakeIntake, "test-app", "bob")
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func FetchAndFilterLogs(fakeIntake *components.FakeIntake, service, content stri
return logs, nil
}

// CheckLogsExpected verifies the presence of expected logs.
// CheckLogsExpected verifies the presence of expected logs, and verifies that there are no duplicate tags.
func CheckLogsExpected(t *testing.T, fakeIntake *components.FakeIntake, service, content string, expectedTags ddtags) {
t.Helper()

Expand All @@ -160,27 +160,6 @@ func CheckLogsExpected(t *testing.T, fakeIntake *components.FakeIntake, service,
if assert.NotEmpty(c, logs, "Expected logs with content: '%s' not found. Instead, found: %s", content, intakeLog) {
t.Logf("Logs from service: '%s' with content: '%s' collected", service, content)
log := logs[0]
for _, expectedTag := range expectedTags {
assert.Contains(t, log.Tags, expectedTag)
}
}
}
}, 2*time.Minute, 10*time.Second)
}

// CheckNoDuplicateTags verifies that there is no duplicate tags
func CheckNoDuplicateTags(t *testing.T, fakeIntake *components.FakeIntake, service, content string) {
t.Helper()

assert.EventuallyWithT(t, func(c *assert.CollectT) {
logs, err := FetchAndFilterLogs(fakeIntake, service, content)

if assert.NoErrorf(c, err, "Error fetching logs: %s", err) {
intakeLog := logsToString(logs)
if assert.NotEmpty(c, logs, "Expected logs with content: '%s' not found. Instead, found: %s", content, intakeLog) {
t.Logf("Logs from service: '%s' with content: '%s' collected", service, content)
log := logs[0]

// Use a map to check for duplicate tags
seenTags := make(map[string]struct{})
for _, tag := range log.Tags {
Expand All @@ -189,6 +168,9 @@ func CheckNoDuplicateTags(t *testing.T, fakeIntake *components.FakeIntake, servi
}
seenTags[tag] = struct{}{} // Mark the tag as seen
}
for _, expectedTag := range expectedTags {
assert.Contains(t, log.Tags, expectedTag)
}
}
}
}, 2*time.Minute, 10*time.Second)
Expand Down

0 comments on commit 1bb1a6a

Please sign in to comment.