Skip to content

Commit a9bc73d

Browse files
Refactor QueryLog, QueryAllLogs and AssertLogMissing.
1 parent fb6c69a commit a9bc73d

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

integration_test/gce-testing-internal/gce/gce_testing.go

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -660,27 +660,12 @@ func shouldRetryHasMatchingLog(err error) bool {
660660
// Returns the first log entry found, or an error if the log could not be
661661
// found after some retries.
662662
func QueryLog(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string, maxAttempts int) (*cloudlogging.Entry, error) {
663-
matchingLogs, err := QueryAllLogs(ctx, logger, vm, logNameRegex, window, query, maxAttempts)
664-
if err != nil {
665-
return nil, err
666-
}
667-
if len(matchingLogs) > 0 {
668-
return matchingLogs[0], nil
669-
}
670-
return nil, fmt.Errorf("QueryLog() failed: %s not found, exhausted retries", logNameRegex)
671-
}
672-
673-
// QueryAllLog looks in the logging backend for logs matching the given query,
674-
// over the trailing time interval specified by the given window.
675-
// Returns all the log entries found, or an error if no log could not be
676-
// found after some retries.
677-
func QueryAllLogs(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string, maxAttempts int) ([]*cloudlogging.Entry, error) {
678663
for attempt := 1; attempt <= maxAttempts; attempt++ {
679664
matchingLogs, err := findMatchingLogs(ctx, logger, vm, logNameRegex, window, query)
680665
if err == nil {
681666
if len(matchingLogs) > 0 {
682667
// Success.
683-
return matchingLogs, nil
668+
return matchingLogs[0], nil
684669
}
685670
}
686671
logger.Printf("Query returned matchingLogs=%v, err=%v, attempt=%d", matchingLogs, err, attempt)
@@ -694,27 +679,40 @@ func QueryAllLogs(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex
694679
return nil, fmt.Errorf("QueryAllLogs() failed: %s not found, exhausted retries", logNameRegex)
695680
}
696681

697-
// AssertLogMissing looks in the logging backend for a log matching the given query
698-
// and returns success if no data is found. To consider possible transient errors
699-
// while querying the backend we make queryMaxAttemptsLogMissing query attempts.
700-
func AssertLogMissing(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string) error {
701-
for attempt := 1; attempt <= queryMaxAttemptsLogMissing; attempt++ {
682+
// QueryAllLogs looks in the logging backend for logs matching the given query,
683+
// over the trailing time interval specified by the given window.
684+
// Returns all the log entries found, or an error if no successful queries to the
685+
// backend. To consider possible transient errors while querying the backend we
686+
// make LogQueryMaxAttempts query attempts.
687+
func QueryAllLogs(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string, maxAttempts int) ([]*cloudlogging.Entry, error) {
688+
for attempt := 1; attempt <= LogQueryMaxAttempts; attempt++ {
702689
matchingLogs, err := findMatchingLogs(ctx, logger, vm, logNameRegex, window, query)
703690
if err == nil {
704-
if len(matchingLogs) > 0 {
705-
return fmt.Errorf("AssertLogMissing(log=%q): %v failed: unexpectedly found data for log", query, err)
706-
}
707-
// Success
708-
return nil
691+
// Success.
692+
return matchingLogs, nil
709693
}
710694
logger.Printf("Query returned matchingLogs=%v, err=%v, attempt=%d", matchingLogs, err, attempt)
711695
if err != nil && !shouldRetryHasMatchingLog(err) {
712696
// A non-retryable error.
713-
return fmt.Errorf("AssertLogMissing() failed: %v", err)
697+
return nil, fmt.Errorf("QueryLog() failed: %v", err)
714698
}
715699
// found was false, or we hit a retryable error.
716700
time.Sleep(logQueryBackoffDuration)
717701
}
702+
return nil, fmt.Errorf("QueryAllLogs() failed: %s no succesful queries to the backend, exhausted retries", logNameRegex)
703+
}
704+
705+
// AssertLogMissing looks in the logging backend for a log matching the given query
706+
// and returns success if no data is found. To consider possible transient errors
707+
// while querying the backend we make queryMaxAttemptsLogMissing query attempts.
708+
func AssertLogMissing(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string) error {
709+
matchingLogs, err := QueryAllLogs(ctx, logger, vm, logNameRegex, window, query, LogQueryMaxAttempts)
710+
if err != nil {
711+
return fmt.Errorf("AssertLogMissing() failed: %v", err)
712+
}
713+
if len(matchingLogs) > 0 {
714+
return fmt.Errorf("AssertLogMissing(log=%q): %v failed: unexpectedly found data for log", query, err)
715+
}
718716
return fmt.Errorf("AssertLogMissing() failed: no successful queries to the backend for log %s, exhausted retries", logNameRegex)
719717
}
720718

0 commit comments

Comments
 (0)