Skip to content

Commit

Permalink
[pkg/stanza] Enable errcheck linter and fix issues (open-telemetry#10271
Browse files Browse the repository at this point in the history
)

* [pkg/stanza] Enable errcheck linter and fix issues

* Linux only lint
  • Loading branch information
djaglowski authored and kentquirk committed Jun 13, 2022
1 parent 91a4d9a commit f701fca
Show file tree
Hide file tree
Showing 27 changed files with 185 additions and 90 deletions.
1 change: 0 additions & 1 deletion pkg/stanza/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,5 @@ issues:
linters:
- revive
- gosec
- errcheck
- misspell
- govet
2 changes: 1 addition & 1 deletion pkg/stanza/operator/helper/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,6 @@ func writerWithFakeOut(t *testing.T) (*WriterOperator, *testutil.FakeOutput) {
},
OutputIDs: []string{fakeOut.ID()},
}
writer.SetOutputs([]operator.Operator{fakeOut})
require.NoError(t, writer.SetOutputs([]operator.Operator{fakeOut}))
return writer, fakeOut
}
13 changes: 9 additions & 4 deletions pkg/stanza/operator/input/file/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ type benchFile struct {
log func(int)
}

func simpleTextFile(file *os.File) *benchFile {
func simpleTextFile(b *testing.B, file *os.File) *benchFile {
line := stringWithLength(49) + "\n"
return &benchFile{
File: file,
log: func(_ int) { file.WriteString(line) },
log: func(_ int) {
_, err := file.WriteString(line)
require.NoError(b, err)
},
}
}

Expand Down Expand Up @@ -146,7 +149,7 @@ func BenchmarkFileInput(b *testing.B) {
files := []*benchFile{}
for _, path := range bench.paths {
file := openFile(b, filepath.Join(rootDir, path))
files = append(files, simpleTextFile(file))
files = append(files, simpleTextFile(b, file))
}

cfg := bench.config()
Expand All @@ -173,7 +176,9 @@ func BenchmarkFileInput(b *testing.B) {

b.ResetTimer()
err = op.Start(testutil.NewMockPersister("test"))
defer op.Stop()
defer func() {
require.NoError(b, op.Stop())
}()
require.NoError(b, err)

// write the remainder of lines while running
Expand Down
117 changes: 87 additions & 30 deletions pkg/stanza/operator/input/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ See this issue for details: https://github.com/census-instrumentation/opencensus
_ = openTemp(t, tempDir)
err := operator.Start(testutil.NewMockPersister("test"))
require.NoError(t, err)
operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()
}

// AddFields tests that the `log.file.name` and `log.file.path` fields are included
Expand All @@ -59,7 +61,9 @@ func TestAddFileFields(t *testing.T) {
writeString(t, temp, "testlog\n")

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

e := waitForOne(t, logReceived)
require.Equal(t, filepath.Base(temp.Name()), e.Attributes["log.file.name"])
Expand Down Expand Up @@ -99,7 +103,9 @@ func TestAddFileResolvedFields(t *testing.T) {
require.NoError(t, err)

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

e := waitForOne(t, logReceived)
require.Equal(t, filepath.Base(symLinkPath), e.Attributes["log.file.name"])
Expand Down Expand Up @@ -160,7 +166,9 @@ func TestAddFileResolvedFieldsWithChangeOfSymlinkTarget(t *testing.T) {
writeString(t, file1, "testlog\n")

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

e := waitForOne(t, logReceived)
require.Equal(t, filepath.Base(symLinkPath), e.Attributes["log.file.name"])
Expand Down Expand Up @@ -199,7 +207,9 @@ func TestReadExistingLogs(t *testing.T) {
writeString(t, temp, "testlog1\ntestlog2\n")

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

waitForMessage(t, logReceived, "testlog1")
waitForMessage(t, logReceived, "testlog2")
Expand Down Expand Up @@ -262,7 +272,9 @@ func TestReadUsingNopEncoding(t *testing.T) {
require.Greater(t, bytesWritten, 0)
require.NoError(t, err)
require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

tc.test(t, logReceived)
})
Expand Down Expand Up @@ -340,7 +352,9 @@ func TestNopEncodingDifferentLogSizes(t *testing.T) {
require.Greater(t, bytesWritten, 0)
require.NoError(t, err)
require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

tc.test(t, logReceived)
})
Expand All @@ -356,7 +370,9 @@ func TestReadNewLogs(t *testing.T) {

// Poll once so we know this isn't a new file
operator.poll(context.Background())
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

// Create a new file
temp := openTemp(t, tempDir)
Expand All @@ -376,7 +392,9 @@ func TestReadExistingAndNewLogs(t *testing.T) {
t.Parallel()
operator, logReceived, tempDir := newTestFileOperator(t, nil, nil)
operator.persister = testutil.NewMockPersister("test")
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

// Start with a file with an entry in it, and expect that entry
// to come through when we poll for the first time
Expand All @@ -400,7 +418,9 @@ func TestStartAtEnd(t *testing.T) {
cfg.StartAt = "end"
}, nil)
operator.persister = testutil.NewMockPersister("test")
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

temp := openTemp(t, tempDir)
writeString(t, temp, "testlog1\n")
Expand All @@ -423,7 +443,9 @@ func TestStartAtEndNewFile(t *testing.T) {
operator, logReceived, tempDir := newTestFileOperator(t, nil, nil)
operator.persister = testutil.NewMockPersister("test")
operator.startAtBeginning = false
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

operator.poll(context.Background())
temp := openTemp(t, tempDir)
Expand All @@ -447,7 +469,9 @@ func TestNoNewline(t *testing.T) {
writeString(t, temp, "testlog1\ntestlog2")

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

waitForMessage(t, logReceived, "testlog1")
waitForMessage(t, logReceived, "testlog2")
Expand All @@ -462,7 +486,9 @@ func TestSkipEmpty(t *testing.T) {
writeString(t, temp, "testlog1\n\ntestlog2\n")

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

waitForMessage(t, logReceived, "testlog1")
waitForMessage(t, logReceived, "testlog2")
Expand All @@ -474,7 +500,9 @@ func TestSplitWrite(t *testing.T) {
t.Parallel()
operator, logReceived, tempDir := newTestFileOperator(t, nil, nil)
operator.persister = testutil.NewMockPersister("test")
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

temp := openTemp(t, tempDir)
writeString(t, temp, "testlog1")
Expand All @@ -491,7 +519,9 @@ func TestIgnoreEmptyFiles(t *testing.T) {
t.Parallel()
operator, logReceived, tempDir := newTestFileOperator(t, nil, nil)
operator.persister = testutil.NewMockPersister("test")
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

temp := openTemp(t, tempDir)
temp2 := openTemp(t, tempDir)
Expand All @@ -516,7 +546,9 @@ func TestDecodeBufferIsResized(t *testing.T) {
operator, logReceived, tempDir := newTestFileOperator(t, nil, nil)

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

temp := openTemp(t, tempDir)
expected := stringWithLength(1<<12 + 1)
Expand All @@ -536,7 +568,9 @@ func TestMultiFileSimple(t *testing.T) {
writeString(t, temp2, "testlog2\n")

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

waitForMessages(t, logReceived, []string{"testlog1", "testlog2"})
}
Expand Down Expand Up @@ -571,7 +605,9 @@ func TestMultiFileParallel_PreloadedFiles(t *testing.T) {
}

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

waitForMessages(t, logReceived, expected)
wg.Wait()
Expand All @@ -595,7 +631,9 @@ func TestMultiFileParallel_LiveFiles(t *testing.T) {
}

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

temps := make([]*os.File, 0, numFiles)
for i := 0; i < numFiles; i++ {
Expand Down Expand Up @@ -629,7 +667,9 @@ func TestOffsetsAfterRestart(t *testing.T) {

// Start the operator and expect a message
require.NoError(t, operator.Start(persister))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()
waitForMessage(t, logReceived, "testlog1")

// Restart the operator. Stop and build a new
Expand All @@ -655,7 +695,9 @@ func TestOffsetsAfterRestart_BigFiles(t *testing.T) {

// Start the operator
require.NoError(t, operator.Start(persister))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()
waitForMessage(t, logReceived, log1)

// Restart the operator
Expand All @@ -679,7 +721,9 @@ func TestOffsetsAfterRestart_BigFilesWrittenWhileOff(t *testing.T) {

// Start the operator and expect the first message
require.NoError(t, operator.Start(persister))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()
waitForMessage(t, logReceived, log1)

// Stop the operator and write a new message
Expand All @@ -703,12 +747,15 @@ func TestManyLogsDelivered(t *testing.T) {

// Start the operator
require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

// Write lots of logs
temp := openTemp(t, tempDir)
for _, message := range expectedMessages {
temp.WriteString(message + "\n")
_, err := temp.WriteString(message + "\n")
require.NoError(t, err)
}

// Expect each of them to come through once
Expand Down Expand Up @@ -741,7 +788,9 @@ func TestFileBatching(t *testing.T) {
},
)
operator.persister = testutil.NewMockPersister("test")
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

temps := make([]*os.File, 0, files)
for i := 0; i < files; i++ {
Expand All @@ -752,7 +801,8 @@ func TestFileBatching(t *testing.T) {
for i, temp := range temps {
for j := 0; j < linesPerFile; j++ {
message := fmt.Sprintf("%s %d %d", stringWithLength(100), i, j)
temp.WriteString(message + "\n")
_, err := temp.WriteString(message + "\n")
require.NoError(t, err)
expectedMessages = append(expectedMessages, message)
}
}
Expand All @@ -769,7 +819,8 @@ func TestFileBatching(t *testing.T) {
for i, temp := range temps {
for j := 0; j < linesPerFile; j++ {
message := fmt.Sprintf("%s %d %d", stringWithLength(20), i, j)
temp.WriteString(message + "\n")
_, err := temp.WriteString(message + "\n")
require.NoError(t, err)
expectedMessages = append(expectedMessages, message)
}
}
Expand All @@ -787,7 +838,9 @@ func TestFileReader_FingerprintUpdated(t *testing.T) {
t.Parallel()

operator, logReceived, tempDir := newTestFileOperator(t, nil, nil)
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

temp := openTemp(t, tempDir)
tempCopy := openFile(t, temp.Name())
Expand Down Expand Up @@ -830,7 +883,9 @@ func TestFingerprintGrowsAndStops(t *testing.T) {
operator, _, tempDir := newTestFileOperator(t, func(cfg *InputConfig) {
cfg.FingerprintSize = helper.ByteSize(maxFP)
}, nil)
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

temp := openTemp(t, tempDir)
tempCopy := openFile(t, temp.Name())
Expand Down Expand Up @@ -940,7 +995,9 @@ func TestEncodings(t *testing.T) {
require.NoError(t, err)

require.NoError(t, operator.Start(testutil.NewMockPersister("test")))
defer operator.Stop()
defer func() {
require.NoError(t, operator.Stop())
}()

for _, expected := range tc.expected {
select {
Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/operator/input/file/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func TestFinder(t *testing.T) {
expected := absPath(tempDir, tc.expected)

for _, f := range files {
os.MkdirAll(filepath.Dir(f), 0755)
ioutil.WriteFile(f, []byte(filepath.Base(f)), 0755)
require.NoError(t, os.MkdirAll(filepath.Dir(f), 0755))
require.NoError(t, ioutil.WriteFile(f, []byte(filepath.Base(f)), 0755))
}

finder := Finder{include, exclude}
Expand Down
3 changes: 2 additions & 1 deletion pkg/stanza/operator/input/file/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func TestNewFingerprintDoesNotModifyOffset(t *testing.T) {
require.Equal(t, len(fileContents), int(info.Size()))

// Set the file descriptors pointer to the beginning of the file
temp.Seek(0, 0)
_, err = temp.Seek(0, 0)
require.NoError(t, err)

fp, err := f.NewFingerprint(temp)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit f701fca

Please sign in to comment.