Skip to content

Commit

Permalink
chore: Fix errcheck CI warnings outside of plugins directory
Browse files Browse the repository at this point in the history
  • Loading branch information
DStrand1 committed May 21, 2024
1 parent 8004548 commit 3bd1a91
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 45 deletions.
1 change: 1 addition & 0 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ func (t *Telegraf) runAgent(ctx context.Context, c *config.Config, reloadConfig
// SdNotify() only tries to notify if the NOTIFY_SOCKET environment is set, so it's safe to call when systemd isn't present.
// Ignore the return values here because they're not valid for platforms that don't use systemd.
// For platforms that use systemd, telegraf doesn't log if the notification failed.
//nolint:errcheck // see above
_, _ = daemon.SdNotify(false, daemon.SdNotifyReady)

if t.once {
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,8 @@ func (c *Config) probeParser(parentcategory string, parentname string, table *as
}

// Try to parse the options to detect if any of them is misspelled
// We don't actually use the parser, so no need to check the error.
parser := creator("")
//nolint:errcheck // We don't actually use the parser, so no need to check the error.
_ = c.toml.UnmarshalTable(table, parser)

return true
Expand Down
11 changes: 8 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@ func TestConfig_AzureMonitorNamespacePrefix(t *testing.T) {
func TestGetDefaultConfigPathFromEnvURL(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("[agent]\ndebug = true"))
_, err := w.Write([]byte("[agent]\ndebug = true"))
require.NoError(t, err)
}))
defer ts.Close()

Expand Down Expand Up @@ -1193,7 +1194,8 @@ func TestPersisterInputStoreLoad(t *testing.T) {
p.state.Version++
p.state.Offset += uint64(i + 1)
p.state.Bits = append(p.state.Bits, len(p.state.Bits))
p.state.Modified, _ = time.Parse(time.RFC3339, "2022-11-03T16:49:00+02:00")
p.state.Modified, err = time.Parse(time.RFC3339, "2022-11-03T16:49:00+02:00")
require.NoError(t, err)

// Store the state for later comparison
expected[plugin.ID()] = p.GetState()
Expand Down Expand Up @@ -1542,7 +1544,10 @@ type MockupStatePlugin struct {
}

func (m *MockupStatePlugin) Init() error {
t0, _ := time.Parse(time.RFC3339, "2021-04-24T23:42:00+02:00")
t0, err := time.Parse(time.RFC3339, "2021-04-24T23:42:00+02:00")
if err != nil {
return err
}
m.state = MockupState{
Name: "mockup",
Bits: []int{},
Expand Down
18 changes: 12 additions & 6 deletions filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func TestIncludeExclude(t *testing.T) {
var benchbool bool

func BenchmarkFilterSingleNoGlobFalse(b *testing.B) {
f, _ := Compile([]string{"cpu"})
f, err := Compile([]string{"cpu"})
require.NoError(b, err)
var tmp bool
for n := 0; n < b.N; n++ {
tmp = f.Match("network")
Expand All @@ -79,7 +80,8 @@ func BenchmarkFilterSingleNoGlobFalse(b *testing.B) {
}

func BenchmarkFilterSingleNoGlobTrue(b *testing.B) {
f, _ := Compile([]string{"cpu"})
f, err := Compile([]string{"cpu"})
require.NoError(b, err)
var tmp bool
for n := 0; n < b.N; n++ {
tmp = f.Match("cpu")
Expand All @@ -88,7 +90,8 @@ func BenchmarkFilterSingleNoGlobTrue(b *testing.B) {
}

func BenchmarkFilter(b *testing.B) {
f, _ := Compile([]string{"cpu", "mem", "net*"})
f, err := Compile([]string{"cpu", "mem", "net*"})
require.NoError(b, err)
var tmp bool
for n := 0; n < b.N; n++ {
tmp = f.Match("network")
Expand All @@ -97,7 +100,8 @@ func BenchmarkFilter(b *testing.B) {
}

func BenchmarkFilterNoGlob(b *testing.B) {
f, _ := Compile([]string{"cpu", "mem", "net"})
f, err := Compile([]string{"cpu", "mem", "net"})
require.NoError(b, err)
var tmp bool
for n := 0; n < b.N; n++ {
tmp = f.Match("net")
Expand All @@ -106,8 +110,9 @@ func BenchmarkFilterNoGlob(b *testing.B) {
}

func BenchmarkFilter2(b *testing.B) {
f, _ := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq",
f, err := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq",
"aw", "az", "axxx", "ab", "cpu", "mem", "net*"})
require.NoError(b, err)
var tmp bool
for n := 0; n < b.N; n++ {
tmp = f.Match("network")
Expand All @@ -116,8 +121,9 @@ func BenchmarkFilter2(b *testing.B) {
}

func BenchmarkFilter2NoGlob(b *testing.B) {
f, _ := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq",
f, err := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq",
"aw", "az", "axxx", "ab", "cpu", "mem", "net"})
require.NoError(b, err)
var tmp bool
for n := 0; n < b.N; n++ {
tmp = f.Match("net")
Expand Down
3 changes: 2 additions & 1 deletion internal/globpath/globpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func TestRootGlob(t *testing.T) {
}

for _, test := range tests {
actual, _ := Compile(test.input)
actual, err := Compile(test.input)
require.NoError(t, err)
require.Equal(t, actual.rootGlob, test.output)
}
}
Expand Down
38 changes: 20 additions & 18 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,17 @@ func TestSnakeCase(t *testing.T) {
}
}

var (
sleepbin, _ = exec.LookPath("sleep")
echobin, _ = exec.LookPath("echo")
shell, _ = exec.LookPath("sh")
)

func TestRunTimeout(t *testing.T) {
t.Skip("Skipping test due to random failures & a data race when running test-all.")

if sleepbin == "" {
sleepbin, err := exec.LookPath("sleep")
if err != nil || sleepbin == "" {
t.Skip("'sleep' binary not available on OS, skipping.")
}

cmd := exec.Command(sleepbin, "10")
start := time.Now()
err := RunTimeout(cmd, time.Millisecond*20)
err = RunTimeout(cmd, time.Millisecond*20)
elapsed := time.Since(start)

require.Equal(t, ErrTimeout, err)
Expand All @@ -71,12 +67,13 @@ func TestRunTimeoutFastExit(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test due to random failures.")
}
if echobin == "" {
echobin, err := exec.LookPath("echo")
if err != nil || echobin == "" {
t.Skip("'echo' binary not available on OS, skipping.")
}
cmd := exec.Command(echobin)
start := time.Now()
err := RunTimeout(cmd, time.Millisecond*20)
err = RunTimeout(cmd, time.Millisecond*20)
buf := &bytes.Buffer{}
log.SetOutput(buf)
elapsed := time.Since(start)
Expand All @@ -93,12 +90,13 @@ func TestRunTimeoutFastExit(t *testing.T) {
func TestCombinedOutputTimeout(t *testing.T) {
// TODO: Fix this test
t.Skip("Test failing too often, skip for now and revisit later.")
if sleepbin == "" {
sleepbin, err := exec.LookPath("sleep")
if err != nil || sleepbin == "" {
t.Skip("'sleep' binary not available on OS, skipping.")
}
cmd := exec.Command(sleepbin, "10")
start := time.Now()
_, err := CombinedOutputTimeout(cmd, time.Millisecond*20)
_, err = CombinedOutputTimeout(cmd, time.Millisecond*20)
elapsed := time.Since(start)

require.Equal(t, ErrTimeout, err)
Expand All @@ -107,7 +105,8 @@ func TestCombinedOutputTimeout(t *testing.T) {
}

func TestCombinedOutput(t *testing.T) {
if echobin == "" {
echobin, err := exec.LookPath("echo")
if err != nil || echobin == "" {
t.Skip("'echo' binary not available on OS, skipping.")
}
cmd := exec.Command(echobin, "foo")
Expand All @@ -120,7 +119,8 @@ func TestCombinedOutput(t *testing.T) {
// test that CombinedOutputTimeout and exec.Cmd.CombinedOutput return
// the same output from a failed command.
func TestCombinedOutputError(t *testing.T) {
if shell == "" {
shell, err := exec.LookPath("sh")
if err != nil || shell == "" {
t.Skip("'sh' binary not available on OS, skipping.")
}
cmd := exec.Command(shell, "-c", "false")
Expand All @@ -135,11 +135,12 @@ func TestCombinedOutputError(t *testing.T) {
}

func TestRunError(t *testing.T) {
if shell == "" {
shell, err := exec.LookPath("sh")
if err != nil || shell == "" {
t.Skip("'sh' binary not available on OS, skipping.")
}
cmd := exec.Command(shell, "-c", "false")
err := RunTimeout(cmd, time.Second)
err = RunTimeout(cmd, time.Second)

require.Error(t, err)
}
Expand Down Expand Up @@ -306,8 +307,9 @@ func TestAlignDuration(t *testing.T) {

func TestAlignTime(t *testing.T) {
rfc3339 := func(value string) time.Time {
t, _ := time.Parse(time.RFC3339, value)
return t
tt, err := time.Parse(time.RFC3339, value)
require.NoError(t, err)
return tt
}

tests := []struct {
Expand Down
29 changes: 19 additions & 10 deletions internal/rotate/file_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ func TestFileWriter_NoRotation(t *testing.T) {
require.NoError(t, err)
_, err = writer.Write([]byte("Hello World 2"))
require.NoError(t, err)
files, _ := os.ReadDir(tempDir)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Len(t, files, 1)
}

func TestFileWriter_TimeRotation(t *testing.T) {
tempDir := t.TempDir()
interval, _ := time.ParseDuration("10ms")
interval, err := time.ParseDuration("10ms")
require.NoError(t, err)
writer, err := NewFileWriter(filepath.Join(tempDir, "test"), interval, 0, -1)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, writer.Close()) })
Expand All @@ -35,22 +37,25 @@ func TestFileWriter_TimeRotation(t *testing.T) {
time.Sleep(interval)
_, err = writer.Write([]byte("Hello World 2"))
require.NoError(t, err)
files, _ := os.ReadDir(tempDir)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Len(t, files, 2)
}

func TestFileWriter_ReopenTimeRotation(t *testing.T) {
tempDir := t.TempDir()
interval, _ := time.ParseDuration("10ms")
interval, err := time.ParseDuration("10ms")
require.NoError(t, err)
filePath := filepath.Join(tempDir, "test.log")
err := os.WriteFile(filePath, []byte("Hello World"), 0640)
err = os.WriteFile(filePath, []byte("Hello World"), 0640)
time.Sleep(interval)
require.NoError(t, err)
writer, err := NewFileWriter(filepath.Join(tempDir, "test.log"), interval, 0, -1)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, writer.Close()) })

files, _ := os.ReadDir(tempDir)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Len(t, files, 2)
}

Expand All @@ -65,7 +70,8 @@ func TestFileWriter_SizeRotation(t *testing.T) {
require.NoError(t, err)
_, err = writer.Write([]byte("World 2"))
require.NoError(t, err)
files, _ := os.ReadDir(tempDir)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Len(t, files, 2)
}

Expand All @@ -81,7 +87,8 @@ func TestFileWriter_ReopenSizeRotation(t *testing.T) {

_, err = writer.Write([]byte("Hello World Again"))
require.NoError(t, err)
files, _ := os.ReadDir(tempDir)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Len(t, files, 2)
}

Expand All @@ -108,7 +115,8 @@ func TestFileWriter_DeleteArchives(t *testing.T) {
_, err = writer.Write([]byte("Third file"))
require.NoError(t, err)

files, _ := os.ReadDir(tempDir)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Len(t, files, 3)

for _, tempFile := range files {
Expand All @@ -135,7 +143,8 @@ func TestFileWriter_CloseDoesNotRotate(t *testing.T) {
require.NoError(t, err)
require.NoError(t, writer.Close())

files, _ := os.ReadDir(tempDir)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Len(t, files, 1)
require.Regexp(t, "^test.log$", files[0].Name())
}
10 changes: 8 additions & 2 deletions internal/snmp/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,16 @@ func (f *Field) Convert(ent gosnmp.SnmpPDU) (v interface{}, err error) {
case uint64:
v = float64(vt) / math.Pow10(d)
case []byte:
vf, _ := strconv.ParseFloat(string(vt), 64)
vf, err := strconv.ParseFloat(string(vt), 64)
if err != nil {
return nil, fmt.Errorf("failed to convert field to float with value %s: %w", vt, err)
}
v = vf / math.Pow10(d)
case string:
vf, _ := strconv.ParseFloat(vt, 64)
vf, err := strconv.ParseFloat(vt, 64)
if err != nil {
return nil, fmt.Errorf("failed to convert field to float with value %s: %w", vt, err)
}
v = vf / math.Pow10(d)
}
return v, nil
Expand Down
5 changes: 4 additions & 1 deletion internal/snmp/translator_gosmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ func snmpTranslateCall(oid string) (mibName string, oidNum string, oidText strin
}
}
oidNum = strings.Join(s, ".")
out, _ = gosmi.GetNodeByOID(types.OidMustFromString(oidNum))
out, err = gosmi.GetNodeByOID(types.OidMustFromString(oidNum))
if err != nil {
return oid, oid, oid, "", out, err
}
} else {
out, err = gosmi.GetNodeByOID(types.OidMustFromString(oid))
oidNum = oid
Expand Down
3 changes: 2 additions & 1 deletion internal/templating/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

func TestEngineAlternateSeparator(t *testing.T) {
defaultTemplate, _ := NewDefaultTemplateWithPattern("topic*")
defaultTemplate, err := NewDefaultTemplateWithPattern("topic*")
require.NoError(t, err)
engine, err := NewEngine("_", defaultTemplate, []string{
"/ /*/*/* /measurement/origin/measurement*",
})
Expand Down
3 changes: 2 additions & 1 deletion logger/default_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func TestWriteToFileInRotation(t *testing.T) {

log.Printf("I! TEST 1") // Writes 31 bytes, will rotate
log.Printf("I! TEST") // Writes 29 byes, no rotation expected
files, _ := os.ReadDir(tempDir)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Len(t, files, 2)
}

Expand Down
3 changes: 2 additions & 1 deletion migrations/inputs_httpjson/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func TestParsing(t *testing.T) {
// Start the test-server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/stats" {
_, _ = w.Write(input)
_, err = w.Write(input)
require.NoError(t, err)
} else {
w.WriteHeader(http.StatusNotFound)
}
Expand Down

0 comments on commit 3bd1a91

Please sign in to comment.