|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +package logfile_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strconv" |
| 11 | + "sync" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/runtime/internal/logfile" |
| 17 | +) |
| 18 | + |
| 19 | +func TestWrite(t *testing.T) { |
| 20 | + dir := t.TempDir() |
| 21 | + path := filepath.Join(dir, "test.log") |
| 22 | + |
| 23 | + lf := logfile.NewLogFile(path, 1024) |
| 24 | + defer require.NoError(t, lf.Close()) |
| 25 | + |
| 26 | + err := lf.Write([]byte("hello world")) |
| 27 | + require.NoError(t, err) |
| 28 | + |
| 29 | + // Expect write to retain data in the buffer |
| 30 | + st, err := os.Stat(path) |
| 31 | + require.NoError(t, err) |
| 32 | + require.Equal(t, int64(0), st.Size(), "file should be empty before flush") |
| 33 | + require.NoError(t, lf.Flush()) |
| 34 | + |
| 35 | + // After flush, check the data got written to the file |
| 36 | + content, err := os.ReadFile(path) |
| 37 | + require.NoError(t, err) |
| 38 | + require.Equal(t, "hello world\n", string(content)) |
| 39 | +} |
| 40 | + |
| 41 | +func TestWriteMultipleLines(t *testing.T) { |
| 42 | + dir := t.TempDir() |
| 43 | + path := filepath.Join(dir, "test.log") |
| 44 | + |
| 45 | + lf := logfile.NewLogFile(path, 1024) |
| 46 | + defer require.NoError(t, lf.Close()) |
| 47 | + |
| 48 | + lines := []string{"line1", "line2", "line3"} |
| 49 | + for _, line := range lines { |
| 50 | + require.NoError(t, lf.Write([]byte(line))) |
| 51 | + } |
| 52 | + |
| 53 | + require.NoError(t, lf.Flush()) |
| 54 | + |
| 55 | + content, err := os.ReadFile(path) |
| 56 | + require.NoError(t, err) |
| 57 | + require.Equal(t, "line1\nline2\nline3\n", string(content)) |
| 58 | +} |
| 59 | + |
| 60 | +func TestLogRotation(t *testing.T) { |
| 61 | + dir := t.TempDir() |
| 62 | + path := filepath.Join(dir, "test.log") |
| 63 | + expectedRotatedPath := path + ".1" |
| 64 | + |
| 65 | + lf := logfile.NewLogFile(path, 50) |
| 66 | + defer require.NoError(t, lf.Close()) |
| 67 | + |
| 68 | + // We write 4 lines (indices 0-3) |
| 69 | + // expecting 0-2 to be written before rotation and 3 after rotation |
| 70 | + for i := range 4 { |
| 71 | + line := []byte("_20_character_line_" + strconv.Itoa(i)) |
| 72 | + require.NoError(t, lf.Write(line)) |
| 73 | + } |
| 74 | + |
| 75 | + _, err := os.Stat(expectedRotatedPath) |
| 76 | + require.NoError(t, err) |
| 77 | + |
| 78 | + // Verify the rotated file contains the written data |
| 79 | + rotatedContent, err := os.ReadFile(expectedRotatedPath) |
| 80 | + require.NoError(t, err) |
| 81 | + require.Len(t, rotatedContent, 63) |
| 82 | + require.Contains(t, string(rotatedContent), "_20_character_line_2") |
| 83 | + require.NoError(t, lf.Flush()) |
| 84 | + |
| 85 | + currentContent, err := os.ReadFile(path) |
| 86 | + require.NoError(t, err) |
| 87 | + require.Len(t, currentContent, 21) |
| 88 | + require.Contains(t, string(currentContent), "_20_character_line_3") |
| 89 | +} |
| 90 | + |
| 91 | +func TestLogRotationMultipleTimes(t *testing.T) { |
| 92 | + dir := t.TempDir() |
| 93 | + path := filepath.Join(dir, "test.log") |
| 94 | + rotatedPath := path + ".1" |
| 95 | + |
| 96 | + lf := logfile.NewLogFile(path, 40) |
| 97 | + defer require.NoError(t, lf.Close()) |
| 98 | + |
| 99 | + for i := range 10 { |
| 100 | + line := []byte("_20_character_line_" + strconv.Itoa(i)) |
| 101 | + require.NoError(t, lf.Write(line)) |
| 102 | + } |
| 103 | + |
| 104 | + // Rotated file should exist and contain most recent events before the current |
| 105 | + rotatedContent, err := os.ReadFile(rotatedPath) |
| 106 | + require.NoError(t, err) |
| 107 | + require.Len(t, rotatedContent, 42) |
| 108 | + require.Contains(t, string(rotatedContent), "_20_character_line_8") |
| 109 | + require.Contains(t, string(rotatedContent), "_20_character_line_9") |
| 110 | +} |
| 111 | + |
| 112 | +func TestFlushWithoutFile(t *testing.T) { |
| 113 | + dir := t.TempDir() |
| 114 | + path := filepath.Join(dir, "test.log") |
| 115 | + |
| 116 | + lf := logfile.NewLogFile(path, 1024) |
| 117 | + defer require.NoError(t, lf.Close()) |
| 118 | + |
| 119 | + require.NoError(t, lf.Flush()) |
| 120 | +} |
| 121 | + |
| 122 | +func TestClose(t *testing.T) { |
| 123 | + dir := t.TempDir() |
| 124 | + path := filepath.Join(dir, "test.log") |
| 125 | + |
| 126 | + lf := logfile.NewLogFile(path, 1024) |
| 127 | + |
| 128 | + require.NoError(t, lf.Write([]byte("data"))) |
| 129 | + |
| 130 | + err := lf.Close() |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + // Expect Close to have flushed the buffer |
| 134 | + content, err := os.ReadFile(path) |
| 135 | + require.NoError(t, err) |
| 136 | + require.Contains(t, string(content), "data") |
| 137 | +} |
| 138 | + |
| 139 | +func TestCloseWithoutWrite(t *testing.T) { |
| 140 | + dir := t.TempDir() |
| 141 | + path := filepath.Join(dir, "test.log") |
| 142 | + |
| 143 | + lf := logfile.NewLogFile(path, 1024) |
| 144 | + require.NoError(t, lf.Close()) |
| 145 | +} |
| 146 | + |
| 147 | +func TestConcurrentWrites(t *testing.T) { |
| 148 | + dir := t.TempDir() |
| 149 | + path := filepath.Join(dir, "test.log") |
| 150 | + |
| 151 | + // Do not rotate while the test runs |
| 152 | + lf := logfile.NewLogFile(path, 100000) |
| 153 | + defer require.NoError(t, lf.Close()) |
| 154 | + |
| 155 | + var wg sync.WaitGroup |
| 156 | + |
| 157 | + numGoroutines := 10 |
| 158 | + writesPerGoroutine := 100 |
| 159 | + |
| 160 | + for range numGoroutines { |
| 161 | + wg.Go(func() { |
| 162 | + for range writesPerGoroutine { |
| 163 | + require.NoError(t, lf.Write([]byte("goroutine write"))) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | + |
| 168 | + wg.Wait() |
| 169 | + |
| 170 | + require.NoError(t, lf.Flush()) |
| 171 | + |
| 172 | + content, err := os.ReadFile(path) |
| 173 | + require.NoError(t, err) |
| 174 | + |
| 175 | + // Count lines to verify all writes succeeded |
| 176 | + lineCount := bytes.Count(content, []byte("\n")) |
| 177 | + expectedLines := numGoroutines * writesPerGoroutine |
| 178 | + require.Equal(t, expectedLines, lineCount) |
| 179 | +} |
| 180 | + |
| 181 | +func TestConcurrentWriteAndFlush(t *testing.T) { |
| 182 | + dir := t.TempDir() |
| 183 | + path := filepath.Join(dir, "test.log") |
| 184 | + |
| 185 | + lf := logfile.NewLogFile(path, 10000) |
| 186 | + defer require.NoError(t, lf.Close()) |
| 187 | + |
| 188 | + var wg sync.WaitGroup |
| 189 | + |
| 190 | + // Writer goroutines |
| 191 | + for range 5 { |
| 192 | + wg.Go(func() { |
| 193 | + for range 50 { |
| 194 | + require.NoError(t, lf.Write([]byte("concurrent data"))) |
| 195 | + } |
| 196 | + }) |
| 197 | + } |
| 198 | + |
| 199 | + // Flusher goroutines |
| 200 | + for range 3 { |
| 201 | + wg.Go(func() { |
| 202 | + for range 10 { |
| 203 | + require.NoError(t, lf.Flush()) |
| 204 | + } |
| 205 | + }) |
| 206 | + } |
| 207 | + |
| 208 | + wg.Wait() |
| 209 | + |
| 210 | + require.NoError(t, lf.Flush()) |
| 211 | +} |
| 212 | + |
| 213 | +func TestConcurrentWritesWithRotation(t *testing.T) { |
| 214 | + dir := t.TempDir() |
| 215 | + path := filepath.Join(dir, "test.log") |
| 216 | + |
| 217 | + // Small threshold to trigger rotation during concurrent writes |
| 218 | + lf := logfile.NewLogFile(path, 100) |
| 219 | + defer require.NoError(t, lf.Close()) |
| 220 | + |
| 221 | + var wg sync.WaitGroup |
| 222 | + |
| 223 | + numGoroutines := 5 |
| 224 | + writesPerGoroutine := 50 |
| 225 | + |
| 226 | + for range numGoroutines { |
| 227 | + wg.Go(func() { |
| 228 | + for range writesPerGoroutine { |
| 229 | + require.NoError(t, lf.Write([]byte("rotation test line"))) |
| 230 | + } |
| 231 | + }) |
| 232 | + } |
| 233 | + |
| 234 | + wg.Wait() |
| 235 | + |
| 236 | + _, err := os.Stat(path + ".1") |
| 237 | + require.NoError(t, err) |
| 238 | +} |
0 commit comments