-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][pkg/stanza] Add tests for reader.Validate (#30623)
Depends on #30449
- Loading branch information
1 parent
c1188d9
commit 9b080fc
Showing
1 changed file
with
142 additions
and
0 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
pkg/stanza/fileconsumer/internal/reader/validate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package reader | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/filetest" | ||
) | ||
|
||
// When a file it moved, we should detect that our old handle is still valid. | ||
func TestValidateMoved(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("Moving files while open is unsupported on Windows") | ||
} | ||
t.Parallel() | ||
|
||
tempDir := t.TempDir() | ||
temp := filetest.OpenTemp(t, tempDir) | ||
_, err := temp.WriteString("testlog1\n") | ||
require.NoError(t, err) | ||
|
||
f, sink := testFactory(t) | ||
fp, err := f.NewFingerprint(temp) | ||
require.NoError(t, err) | ||
|
||
reader, err := f.NewReader(temp, fp) | ||
require.NoError(t, err) | ||
|
||
reader.ReadToEnd(context.Background()) | ||
sink.ExpectToken(t, []byte("testlog1")) | ||
|
||
// Validate before moving | ||
assert.True(t, reader.Validate()) | ||
|
||
// Move the file | ||
require.NoError(t, os.Rename(temp.Name(), temp.Name()+".old")) | ||
|
||
// Validate after moving | ||
assert.True(t, reader.Validate()) | ||
|
||
_, err = temp.WriteString("testlog2\n") | ||
require.NoError(t, err) | ||
|
||
reader.ReadToEnd(context.Background()) | ||
sink.ExpectToken(t, []byte("testlog2")) | ||
|
||
// Validate after writing to the moved file | ||
assert.True(t, reader.Validate()) | ||
} | ||
|
||
func TestInvalidateTruncated(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempDir := t.TempDir() | ||
temp := filetest.OpenTemp(t, tempDir) | ||
_, err := temp.WriteString("testlog1\n") | ||
require.NoError(t, err) | ||
|
||
f, sink := testFactory(t) | ||
fp, err := f.NewFingerprint(temp) | ||
require.NoError(t, err) | ||
|
||
reader, err := f.NewReader(temp, fp) | ||
require.NoError(t, err) | ||
|
||
reader.ReadToEnd(context.Background()) | ||
sink.ExpectToken(t, []byte("testlog1")) | ||
|
||
// Validate before truncating | ||
assert.True(t, reader.Validate()) | ||
|
||
// Truncate the file | ||
require.NoError(t, temp.Truncate(0)) | ||
|
||
// Invalidate after truncating | ||
assert.False(t, reader.Validate()) | ||
|
||
// Write different content to the file | ||
_, err = temp.WriteString("testlog2\n") | ||
require.NoError(t, err) | ||
|
||
// Still invalid | ||
assert.False(t, reader.Validate()) | ||
} | ||
|
||
func TestInvalidateClosed(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempDir := t.TempDir() | ||
temp := filetest.OpenTemp(t, tempDir) | ||
_, err := temp.WriteString("testlog1\n") | ||
require.NoError(t, err) | ||
|
||
f, _ := testFactory(t) | ||
fp, err := f.NewFingerprint(temp) | ||
require.NoError(t, err) | ||
|
||
reader, err := f.NewReader(temp, fp) | ||
require.NoError(t, err) | ||
|
||
// Validate before closing | ||
assert.True(t, reader.Validate()) | ||
|
||
// Close the file using the reader to drop the handle. | ||
reader.Close() | ||
|
||
// Invalidate after closing | ||
assert.False(t, reader.Validate()) | ||
} | ||
|
||
func TestInvalidateUnreadable(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempDir := t.TempDir() | ||
temp := filetest.OpenTemp(t, tempDir) | ||
_, err := temp.WriteString("testlog1\n") | ||
require.NoError(t, err) | ||
|
||
f, _ := testFactory(t) | ||
fp, err := f.NewFingerprint(temp) | ||
require.NoError(t, err) | ||
|
||
reader, err := f.NewReader(temp, fp) | ||
require.NoError(t, err) | ||
|
||
// Validate before closing | ||
assert.True(t, reader.Validate()) | ||
|
||
// Close the file using our direct handle. The reader still has a handle but cannot be read. | ||
require.NoError(t, temp.Close()) | ||
|
||
// Invalidate unreadable file | ||
assert.False(t, reader.Validate()) | ||
} |