Skip to content

Commit f9b4977

Browse files
authored
Refactor test (#1779)
1 parent 077143a commit f9b4977

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

e2e/testcases/postsync_logging_test.go

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func TestStatusEventLogRootSync(t *testing.T) {
7373
nt.T.Fatal(err)
7474
}
7575

76-
logMessages := []string{"KNV1017: The system/ directory must declare a Repo Resource."}
77-
nt.Must(assertLogEntryHasCount(logs, 1, commit, rootSyncID.Name, rootSyncID.Namespace, logMessages))
76+
message := "KNV1017: The system/ directory must declare a Repo Resource."
77+
nt.Must(assertOneLogLineWithMessage(logs, commit, rootSyncID.Name, rootSyncID.Namespace, message))
7878

7979
nt.T.Log("Reset test repo")
8080
nt.Must(rootSyncGitRepo.Git("reset", "--hard", "HEAD^"))
@@ -117,7 +117,8 @@ func TestCloudLoggingRootSync(t *testing.T) {
117117
nt.ClusterName,
118118
startTime.Format(time.RFC3339),
119119
)
120-
nt.Must(waitForLogEntryInCloudLogs(nt, filter, 1, commit, rootSyncID.Name, rootSyncID.Namespace, []string{"KNV1017: The system/ directory must declare a Repo Resource."}, 120*time.Second))
120+
message := "KNV1017: The system/ directory must declare a Repo Resource."
121+
nt.Must(waitForLogEntryInCloudLogs(nt, filter, commit, rootSyncID.Name, rootSyncID.Namespace, message, 120*time.Second))
121122

122123
nt.T.Log("Reset test repo")
123124
nt.Must(rootSyncGitRepo.Git("reset", "--hard", "HEAD^"))
@@ -176,8 +177,7 @@ func TestConditionEventLogRootSync(t *testing.T) {
176177
nt.T.Fatal(err)
177178
}
178179

179-
logMessages := strings.Split(msg, "\n")
180-
nt.Must(assertLogEntryHasCount(logs, 1, "", rootSync.Name, rootSync.Namespace, logMessages))
180+
nt.Must(assertOneLogLineWithMessage(logs, "", rootSync.Name, rootSync.Namespace, strings.Split(msg, "\n")[0]))
181181
}
182182

183183
// TestCloudLoggingConditionEventRootSync tests that condition-based errors from RootSync
@@ -233,7 +233,7 @@ func TestCloudLoggingConditionEventRootSync(t *testing.T) {
233233
nt.ClusterName,
234234
startTime.Format(time.RFC3339),
235235
)
236-
nt.Must(waitForLogEntryInCloudLogs(nt, filter, 1, "", rootSync.Name, rootSync.Namespace, strings.Split(msg, "\n"), 120*time.Second))
236+
nt.Must(waitForLogEntryInCloudLogs(nt, filter, "", rootSync.Name, rootSync.Namespace, strings.Split(msg, "\n")[0], 120*time.Second))
237237
}
238238

239239
// TestStatusEventLogRepoSync tests that error events from RepoSync are properly logged
@@ -275,8 +275,8 @@ func TestStatusEventLogRepoSync(t *testing.T) {
275275
nt.T.Fatal(err)
276276
}
277277

278-
logMessages := []string{msg, "\"generation\":1"}
279-
nt.Must(assertLogEntryHasCount(logs, 1, commit, repoSyncID.Name, repoSyncID.Namespace, logMessages))
278+
nt.Must(assertOneLogLineWithMessage(logs, commit, repoSyncID.Name, repoSyncID.Namespace, msg))
279+
nt.Must(assertOneLogLineWithMessage(logs, commit, repoSyncID.Name, repoSyncID.Namespace, "\"generation\":1"))
280280
}
281281

282282
// TestCloudLoggingStatusEventRepoSync tests that error events from RepoSync are properly sent
@@ -320,37 +320,29 @@ func TestCloudLoggingStatusEventRepoSync(t *testing.T) {
320320
nt.ClusterName,
321321
startTime.Format(time.RFC3339),
322322
)
323-
nt.Must(waitForLogEntryInCloudLogs(nt, filter, 1, commit, repoSyncID.Name, repoSyncID.Namespace, []string{msg, "\"generation\":1"}, 120*time.Second))
323+
nt.Must(waitForLogEntryInCloudLogs(nt, filter, commit, repoSyncID.Name, repoSyncID.Namespace, msg, 120*time.Second))
324324
}
325325

326-
// assertLogEntryHasCount checks if the specified messages appear in the logs
327-
// exactly the expected number of times, matching the given commit, name, and namespace.
328-
func assertLogEntryHasCount(logs []string, expectedCount int, commit, rname, rnamespace string, messages []string) error {
326+
// assertOneLogLineWithMessage checks if the specified message appears in exactly one line
327+
// among the log entries which match the given commit, resource name, and namespace.
328+
func assertOneLogLineWithMessage(logs []string, commit, resourceName, resourceNamespace, message string) error {
329329
count := 0
330-
331330
for _, line := range logs {
332-
if strings.Contains(line, commit) && strings.Contains(line, rname) && strings.Contains(line, rnamespace) {
333-
allFound := false
334-
for _, msg := range messages {
335-
if !strings.Contains(line, msg) {
336-
break
337-
}
338-
allFound = true
339-
}
340-
if allFound {
341-
count++
342-
}
331+
if !(strings.Contains(line, commit) && strings.Contains(line, resourceName) && strings.Contains(line, resourceNamespace)) {
332+
continue
333+
}
334+
if strings.Contains(line, message) {
335+
count++
343336
}
344337
}
345-
if count != expectedCount {
346-
return fmt.Errorf("Expected %d occurrences of error log entry %s, but found %d",
347-
expectedCount, messages, count)
338+
if count != 1 {
339+
return fmt.Errorf("failed to find single occurrence of message in logs, counted %d", count)
348340
}
349341
return nil
350342
}
351343

352344
// waitForLogEntryInCloudLogs queries Cloud Logging and asserts the log entry appears within the timeout.
353-
func waitForLogEntryInCloudLogs(nt *nomostest.NT, filter string, expectedCount int, commit, rname, rnamespace string, messages []string, timeout time.Duration) error {
345+
func waitForLogEntryInCloudLogs(nt *nomostest.NT, filter string, commit, rname, rnamespace, message string, timeout time.Duration) error {
354346
ctx := nt.Context
355347
client, err := logadmin.NewClient(ctx, *e2e.GCPProject)
356348
if err != nil {
@@ -379,7 +371,7 @@ func waitForLogEntryInCloudLogs(nt *nomostest.NT, filter string, expectedCount i
379371
lastLogs = append(lastLogs, fmt.Sprintf("%s: %v", entry.Timestamp.Format(time.RFC3339), entry.Payload))
380372
}
381373

382-
return assertLogEntryHasCount(lastLogs, expectedCount, commit, rname, rnamespace, messages)
374+
return assertOneLogLineWithMessage(lastLogs, commit, rname, rnamespace, message)
383375
})
384376

385377
if err != nil {

0 commit comments

Comments
 (0)