Skip to content

Commit

Permalink
feat(tests): extract fingerprint size change as separate test
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Rosiek <[email protected]>
  • Loading branch information
Dominik Rosiek committed Jun 8, 2022
1 parent df84111 commit 251c4f0
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions pkg/stanza/operator/input/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,6 @@ func TestFileReader_FingerprintUpdated(t *testing.T) {
// - Updates as a file is read
// - Stops updating when the max fingerprint size is reached
// - Stops exactly at max fingerprint size, regardless of content
// - Do not change size after fingerprint configuration change
func TestFingerprintGrowsAndStops(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -920,7 +919,73 @@ func TestFingerprintGrowsAndStops(t *testing.T) {
reader.ReadToEnd(context.Background())
require.Equal(t, fileContent[:expectedFP], reader.Fingerprint.FirstBytes)
}

})
}
}

// This is same test like TestFingerprintGrowsAndStops, but with additional check for fingerprint size check
// Test that a fingerprint:
// - Starts empty
// - Updates as a file is read
// - Stops updating when the max fingerprint size is reached
// - Stops exactly at max fingerprint size, regardless of content
// - Do not change size after fingerprint configuration change
func TestFingerprintChangeSize(t *testing.T) {
t.Parallel()

// Use a number with many factors.
// Sometimes fingerprint length will align with
// the end of a line, sometimes not. Test both.
maxFP := 360

// Use prime numbers to ensure variation in
// whether or not they are factors of maxFP
lineLens := []int{3, 5, 7, 11, 13, 17, 19, 23, 27}

for _, lineLen := range lineLens {
t.Run(fmt.Sprintf("%d", lineLen), func(t *testing.T) {
t.Parallel()
operator, _, tempDir := newTestFileOperator(t, func(cfg *Config) {
cfg.FingerprintSize = helper.ByteSize(maxFP)
}, nil)
defer func() {
require.NoError(t, operator.Stop())
}()

temp := openTemp(t, tempDir)
tempCopy := openFile(t, temp.Name())
fp, err := operator.NewFingerprint(temp)
require.NoError(t, err)
require.Equal(t, []byte(""), fp.FirstBytes)

splitter, err := operator.getMultiline()
require.NoError(t, err)

reader, err := operator.NewReader(temp.Name(), tempCopy, fp, splitter)
require.NoError(t, err)
defer reader.Close()

// keep track of what has been written to the file
fileContent := []byte{}

// keep track of expected fingerprint size
expectedFP := 0

// Write lines until file is much larger than the length of the fingerprint
for len(fileContent) < 2*maxFP {
expectedFP += lineLen
if expectedFP > maxFP {
expectedFP = maxFP
}

line := stringWithLength(lineLen-1) + "\n"
fileContent = append(fileContent, []byte(line)...)

writeString(t, temp, line)
reader.ReadToEnd(context.Background())
require.Equal(t, fileContent[:expectedFP], reader.Fingerprint.FirstBytes)
}

// Test fingerprint change
// Change fingerprint and try to read file again
// We do not expect fingerprint change
Expand Down

0 comments on commit 251c4f0

Please sign in to comment.