@@ -73,8 +73,8 @@ func TestStatusEventLogRootSync(t *testing.T) {
73
73
nt .T .Fatal (err )
74
74
}
75
75
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 ))
78
78
79
79
nt .T .Log ("Reset test repo" )
80
80
nt .Must (rootSyncGitRepo .Git ("reset" , "--hard" , "HEAD^" ))
@@ -117,7 +117,8 @@ func TestCloudLoggingRootSync(t *testing.T) {
117
117
nt .ClusterName ,
118
118
startTime .Format (time .RFC3339 ),
119
119
)
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 ))
121
122
122
123
nt .T .Log ("Reset test repo" )
123
124
nt .Must (rootSyncGitRepo .Git ("reset" , "--hard" , "HEAD^" ))
@@ -176,8 +177,7 @@ func TestConditionEventLogRootSync(t *testing.T) {
176
177
nt .T .Fatal (err )
177
178
}
178
179
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 ]))
181
181
}
182
182
183
183
// TestCloudLoggingConditionEventRootSync tests that condition-based errors from RootSync
@@ -233,7 +233,7 @@ func TestCloudLoggingConditionEventRootSync(t *testing.T) {
233
233
nt .ClusterName ,
234
234
startTime .Format (time .RFC3339 ),
235
235
)
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 ))
237
237
}
238
238
239
239
// TestStatusEventLogRepoSync tests that error events from RepoSync are properly logged
@@ -275,8 +275,8 @@ func TestStatusEventLogRepoSync(t *testing.T) {
275
275
nt .T .Fatal (err )
276
276
}
277
277
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" ))
280
280
}
281
281
282
282
// TestCloudLoggingStatusEventRepoSync tests that error events from RepoSync are properly sent
@@ -320,37 +320,29 @@ func TestCloudLoggingStatusEventRepoSync(t *testing.T) {
320
320
nt .ClusterName ,
321
321
startTime .Format (time .RFC3339 ),
322
322
)
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 ))
324
324
}
325
325
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 {
329
329
count := 0
330
-
331
330
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 ++
343
336
}
344
337
}
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 )
348
340
}
349
341
return nil
350
342
}
351
343
352
344
// 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 {
354
346
ctx := nt .Context
355
347
client , err := logadmin .NewClient (ctx , * e2e .GCPProject )
356
348
if err != nil {
@@ -379,7 +371,7 @@ func waitForLogEntryInCloudLogs(nt *nomostest.NT, filter string, expectedCount i
379
371
lastLogs = append (lastLogs , fmt .Sprintf ("%s: %v" , entry .Timestamp .Format (time .RFC3339 ), entry .Payload ))
380
372
}
381
373
382
- return assertLogEntryHasCount (lastLogs , expectedCount , commit , rname , rnamespace , messages )
374
+ return assertOneLogLineWithMessage (lastLogs , commit , rname , rnamespace , message )
383
375
})
384
376
385
377
if err != nil {
0 commit comments