Skip to content

Commit e35609d

Browse files
Create findMatchingLogs and refactor all uses of hasMatchingLog. Refactor QueryLog to use QueryAllLogs.
1 parent fa81069 commit e35609d

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

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

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,10 @@ func AssertMetricMissing(ctx context.Context, logger *log.Logger, vm *VM, metric
604604
return nil
605605
}
606606

607-
// hasMatchingLog looks in the logging backend for a log matching the given query,
607+
// findMatchingLogs looks in the logging backend for a log matching the given query,
608608
// over the trailing time interval specified by the given window.
609-
// Returns a boolean indicating whether the log was present in the backend,
610-
// plus all the matching log entries found, or an error if the lookup failed.
611-
func hasMatchingLog(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string) (bool, []*cloudlogging.Entry, error) {
609+
// Returns all the matching log entries found, or an error if the lookup failed.
610+
func findMatchingLogs(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string) ([]*cloudlogging.Entry, error) {
612611
start := time.Now().Add(-window)
613612

614613
t := start.Format(time.RFC3339)
@@ -620,10 +619,9 @@ func hasMatchingLog(ctx context.Context, logger *log.Logger, vm *VM, logNameRege
620619

621620
logClient, err := logClients.new(vm.Project)
622621
if err != nil {
623-
return false, nil, fmt.Errorf("hasMatchingLog() failed to obtain logClient for project %v: %v", vm.Project, err)
622+
return nil, fmt.Errorf("findMatchingLogs() failed to obtain logClient for project %v: %v", vm.Project, err)
624623
}
625624
it := logClient.Entries(ctx, logadmin.Filter(filter))
626-
found := false
627625

628626
var matchingLogs []*cloudlogging.Entry
629627
// Loop through the iterator printing out each matching log entry.
@@ -634,16 +632,12 @@ func hasMatchingLog(ctx context.Context, logger *log.Logger, vm *VM, logNameRege
634632
break
635633
}
636634
if err != nil {
637-
return false, nil, err
635+
return nil, err
638636
}
639637
logger.Printf("Found matching log entry: %v", entry)
640-
found = true
641638
matchingLogs = append(matchingLogs, entry)
642639
}
643-
if found && len(matchingLogs) == 0 {
644-
return false, nil, fmt.Errorf("hasMatchingLog() internal logic error: found is true but matchingLogs is empty")
645-
}
646-
return found, matchingLogs, nil
640+
return matchingLogs, nil
647641
}
648642

649643
// WaitForLog looks in the logging backend for a log matching the given query,
@@ -666,63 +660,52 @@ func shouldRetryHasMatchingLog(err error) bool {
666660
// Returns the first log entry found, or an error if the log could not be
667661
// found after some retries.
668662
func QueryLog(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string, maxAttempts int) (*cloudlogging.Entry, error) {
669-
for attempt := 1; attempt <= maxAttempts; attempt++ {
670-
found, matchingLogs, err := hasMatchingLog(ctx, logger, vm, logNameRegex, window, query)
671-
if found {
672-
if len(matchingLogs) > 0 {
673-
// Success.
674-
return matchingLogs[0], nil
675-
}
676-
// This should never happen if we found at least one log (found == true).
677-
return nil, fmt.Errorf("QueryLog() internal logic error: found is true but matchingLogs is empty")
678-
}
679-
logger.Printf("Query returned found=%v, err=%v, attempt=%d", found, err, attempt)
680-
if err != nil && !shouldRetryHasMatchingLog(err) {
681-
// A non-retryable error.
682-
return nil, fmt.Errorf("QueryLog() failed: %v", err)
683-
}
684-
// found was false, or we hit a retryable error.
685-
time.Sleep(logQueryBackoffDuration)
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
686669
}
687670
return nil, fmt.Errorf("QueryLog() failed: %s not found, exhausted retries", logNameRegex)
688671
}
689672

690-
// QueryAllLog looks in the logging backend for a log matching the given query,
673+
// QueryAllLog looks in the logging backend for logs matching the given query,
691674
// over the trailing time interval specified by the given window.
692-
// Returns all the log entries found, or an error if the log could not be
675+
// Returns all the log entries found, or an error if no log could not be
693676
// found after some retries.
694677
func QueryAllLogs(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string, maxAttempts int) ([]*cloudlogging.Entry, error) {
695678
for attempt := 1; attempt <= maxAttempts; attempt++ {
696-
found, matchingLogs, err := hasMatchingLog(ctx, logger, vm, logNameRegex, window, query)
697-
if found {
679+
matchingLogs, err := findMatchingLogs(ctx, logger, vm, logNameRegex, window, query)
680+
if len(matchingLogs) > 0 {
698681
// Success.
699682
return matchingLogs, nil
700683
}
701-
logger.Printf("Query returned found=%v, err=%v, attempt=%d", found, err, attempt)
684+
logger.Printf("Query returned matchingLogs=%v, err=%v, attempt=%d", matchingLogs, err, attempt)
702685
if err != nil && !shouldRetryHasMatchingLog(err) {
703686
// A non-retryable error.
704687
return nil, fmt.Errorf("QueryLog() failed: %v", err)
705688
}
706689
// found was false, or we hit a retryable error.
707690
time.Sleep(logQueryBackoffDuration)
708691
}
709-
return nil, fmt.Errorf("QueryLog() failed: %s not found, exhausted retries", logNameRegex)
692+
return nil, fmt.Errorf("QueryAllLogs() failed: %s not found, exhausted retries", logNameRegex)
710693
}
711694

712695
// AssertLogMissing looks in the logging backend for a log matching the given query
713696
// and returns success if no data is found. To consider possible transient errors
714697
// while querying the backend we make queryMaxAttemptsLogMissing query attempts.
715698
func AssertLogMissing(ctx context.Context, logger *log.Logger, vm *VM, logNameRegex string, window time.Duration, query string) error {
716699
for attempt := 1; attempt <= queryMaxAttemptsLogMissing; attempt++ {
717-
found, _, err := hasMatchingLog(ctx, logger, vm, logNameRegex, window, query)
700+
matchingLogs, err := findMatchingLogs(ctx, logger, vm, logNameRegex, window, query)
718701
if err == nil {
719-
if found {
702+
if len(matchingLogs) > 0 {
720703
return fmt.Errorf("AssertLogMissing(log=%q): %v failed: unexpectedly found data for log", query, err)
721704
}
722705
// Success
723706
return nil
724707
}
725-
logger.Printf("Query returned found=%v, err=%v, attempt=%d", found, err, attempt)
708+
logger.Printf("Query returned matchingLogs=%v, err=%v, attempt=%d", matchingLogs, err, attempt)
726709
if err != nil && !shouldRetryHasMatchingLog(err) {
727710
// A non-retryable error.
728711
return fmt.Errorf("AssertLogMissing() failed: %v", err)

0 commit comments

Comments
 (0)