|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package testing |
| 19 | + |
| 20 | +import ( |
| 21 | + "bufio" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "io" |
| 25 | + "os" |
| 26 | + "strings" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | + "github.com/stretchr/testify/require" |
| 32 | +) |
| 33 | + |
| 34 | +// LogFile wraps a *os.File and makes it more suitable for tests. |
| 35 | +// Key feature: |
| 36 | +// - Methods to search and wait for substrings in lines are provided, |
| 37 | +// they keep track of the offset, ensuring ordering when |
| 38 | +// when searching. |
| 39 | +type LogFile struct { |
| 40 | + *os.File |
| 41 | + offset int64 |
| 42 | +} |
| 43 | + |
| 44 | +// NewLogFile returns a new LogFile which wraps a os.File meant to be used |
| 45 | +// for testing. Methods to search and wait for strings to appear are provided. |
| 46 | +// dir and pattern are passed directly to os.CreateTemp. |
| 47 | +// It is the callers responsibility to remove the file. To keep the file in |
| 48 | +// when the test fails, use [TempDir] to create a folder. |
| 49 | +func NewLogFile(t testing.TB, dir, pattern string) *LogFile { |
| 50 | + f, err := os.CreateTemp(dir, pattern) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("cannot create log file: %s", err) |
| 53 | + } |
| 54 | + |
| 55 | + lf := &LogFile{ |
| 56 | + File: f, |
| 57 | + } |
| 58 | + |
| 59 | + t.Cleanup(func() { |
| 60 | + if err := f.Sync(); err != nil { |
| 61 | + t.Logf("cannot sync log file: %s", err) |
| 62 | + } |
| 63 | + |
| 64 | + if err := f.Close(); err != nil { |
| 65 | + t.Logf("cannot close log file: %s", err) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + return lf |
| 70 | +} |
| 71 | + |
| 72 | +// WaitLogsContains waits for the specified string s to be present in the logs within |
| 73 | +// the given timeout duration and fails the test if s is not found. |
| 74 | +// It keeps track of the log file offset, reading only new lines. Each |
| 75 | +// subsequent call to WaitLogsContains will only check logs not yet evaluated. |
| 76 | +// msgAndArgs should be a format string and arguments that will be printed |
| 77 | +// if the logs are not found, providing additional context for debugging. |
| 78 | +func (l *LogFile) WaitLogsContains(t testing.TB, s string, timeout time.Duration, msgAndArgs ...any) { |
| 79 | + t.Helper() |
| 80 | + require.EventuallyWithT( |
| 81 | + t, |
| 82 | + func(c *assert.CollectT) { |
| 83 | + found, err := l.FindInLogs(s) |
| 84 | + if err != nil { |
| 85 | + c.Errorf("cannot check the log file: %s", err) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + if !found { |
| 90 | + c.Errorf("did not find '%s' in the logs", s) |
| 91 | + } |
| 92 | + }, |
| 93 | + timeout, |
| 94 | + 100*time.Millisecond, |
| 95 | + msgAndArgs...) |
| 96 | +} |
| 97 | + |
| 98 | +// LogContains searches for str in the log file keeping track of the |
| 99 | +// offset. If there is any issue reading the log file, then t.Fatalf is called, |
| 100 | +// if str is not present in the logs, t.Errorf is called. |
| 101 | +func (l *LogFile) LogContains(t testing.TB, str string) { |
| 102 | + t.Helper() |
| 103 | + found, err := l.FindInLogs(str) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("cannot read log file: %s", err) |
| 106 | + } |
| 107 | + |
| 108 | + if !found { |
| 109 | + t.Errorf("'%s' not found in logs", str) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// FindInLogs searches for str in the log file keeping track of the offset. |
| 114 | +// It returns true if str is found in the logs. If there are any errors, |
| 115 | +// it returns false and the error |
| 116 | +func (l *LogFile) FindInLogs(str string) (bool, error) { |
| 117 | + // Open the file again so we can seek and not interfere with |
| 118 | + // the logger writing to it. |
| 119 | + f, err := os.Open(l.Name()) |
| 120 | + if err != nil { |
| 121 | + return false, fmt.Errorf("cannot open log file for reading: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + if _, err := f.Seek(l.offset, io.SeekStart); err != nil { |
| 125 | + return false, fmt.Errorf("cannot seek log file: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + r := bufio.NewReader(f) |
| 129 | + for { |
| 130 | + data, err := r.ReadBytes('\n') |
| 131 | + line := string(data) |
| 132 | + l.offset += int64(len(data)) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + if !errors.Is(err, io.EOF) { |
| 136 | + return false, fmt.Errorf("error reading log file '%s': %w", l.Name(), err) |
| 137 | + } |
| 138 | + break |
| 139 | + } |
| 140 | + |
| 141 | + if strings.Contains(line, str) { |
| 142 | + return true, nil |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return false, nil |
| 147 | +} |
| 148 | + |
| 149 | +// ResetOffset resets the log file offset |
| 150 | +func (l *LogFile) ResetOffset() { |
| 151 | + l.offset = 0 |
| 152 | +} |
0 commit comments