Skip to content

Commit

Permalink
Linter fixes (unhandled errors) -- Part 1 (influxdata#8992)
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored Apr 8, 2021
1 parent 2b41a1e commit 8e7da35
Show file tree
Hide file tree
Showing 152 changed files with 2,267 additions and 2,425 deletions.
22 changes: 13 additions & 9 deletions plugins/inputs/burrow/burrow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func getHTTPServer() *httptest.Server {
body, code := getResponseJSON(r.RequestURI)
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
// Ignore the returned error as the test will fail anyway
//nolint:errcheck,revive
w.Write(body)
}))
}
Expand All @@ -61,6 +63,8 @@ func getHTTPServerBasicAuth() *httptest.Server {
body, code := getResponseJSON(r.RequestURI)
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
// Ignore the returned error as the test will fail anyway
//nolint:errcheck,revive
w.Write(body)
}))
}
Expand All @@ -72,7 +76,7 @@ func TestBurrowTopic(t *testing.T) {

plugin := &burrow{Servers: []string{s.URL}}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

fields := []map[string]interface{}{
// topicA
Expand Down Expand Up @@ -103,7 +107,7 @@ func TestBurrowPartition(t *testing.T) {
Servers: []string{s.URL},
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

fields := []map[string]interface{}{
{
Expand Down Expand Up @@ -151,7 +155,7 @@ func TestBurrowGroup(t *testing.T) {
Servers: []string{s.URL},
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

fields := []map[string]interface{}{
{
Expand Down Expand Up @@ -189,7 +193,7 @@ func TestMultipleServers(t *testing.T) {
Servers: []string{s1.URL, s2.URL},
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

require.Exactly(t, 14, len(acc.Metrics))
require.Empty(t, acc.Errors)
Expand All @@ -205,7 +209,7 @@ func TestMultipleRuns(t *testing.T) {
}
for i := 0; i < 4; i++ {
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

require.Exactly(t, 7, len(acc.Metrics))
require.Empty(t, acc.Errors)
Expand All @@ -224,7 +228,7 @@ func TestBasicAuthConfig(t *testing.T) {
}

acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

require.Exactly(t, 7, len(acc.Metrics))
require.Empty(t, acc.Errors)
Expand All @@ -241,7 +245,7 @@ func TestFilterClusters(t *testing.T) {
}

acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

// no match by cluster
require.Exactly(t, 0, len(acc.Metrics))
Expand All @@ -260,7 +264,7 @@ func TestFilterGroups(t *testing.T) {
}

acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

require.Exactly(t, 1, len(acc.Metrics))
require.Empty(t, acc.Errors)
Expand All @@ -278,7 +282,7 @@ func TestFilterTopics(t *testing.T) {
}

acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.NoError(t, plugin.Gather(acc))

require.Exactly(t, 3, len(acc.Metrics))
require.Empty(t, acc.Errors)
Expand Down
47 changes: 11 additions & 36 deletions plugins/inputs/hddtemp/go-hddtemp/hddtemp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hddtemp

import (
"net"
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,10 +12,7 @@ func TestFetch(t *testing.T) {
defer l.Close()

disks, err := New().Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}
require.NoError(t, err)

expected := []Disk{
{
Expand All @@ -26,29 +22,20 @@ func TestFetch(t *testing.T) {
Unit: "C",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
require.Equal(t, expected, disks, "disks' slice is different from expected")
}

func TestFetchWrongAddress(t *testing.T) {
_, err := New().Fetch("127.0.0.1:1")

if err == nil {
t.Error("expecting err to be non-nil")
}
require.Error(t, err)
}

func TestFetchStatus(t *testing.T) {
l := serve(t, []byte("|/dev/sda|foobar|SLP|C|"))
defer l.Close()

disks, err := New().Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}
require.NoError(t, err)

expected := []Disk{
{
Expand All @@ -59,21 +46,15 @@ func TestFetchStatus(t *testing.T) {
Status: "SLP",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
require.Equal(t, expected, disks, "disks' slice is different from expected")
}

func TestFetchTwoDisks(t *testing.T) {
l := serve(t, []byte("|/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|"))
defer l.Close()

disks, err := New().Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}
require.NoError(t, err)

expected := []Disk{
{
Expand All @@ -90,26 +71,20 @@ func TestFetchTwoDisks(t *testing.T) {
Status: "SLP",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
require.Equal(t, expected, disks, "disks' slice is different from expected")
}

func serve(t *testing.T, data []byte) net.Listener {
l, err := net.Listen("tcp", "127.0.0.1:0")

if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

go func(t *testing.T) {
conn, err := l.Accept()

require.NoError(t, err)

conn.Write(data)
conn.Close()
_, err = conn.Write(data)
require.NoError(t, err)
require.NoError(t, conn.Close())
}(t)

return l
Expand Down
12 changes: 6 additions & 6 deletions plugins/inputs/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestHTTPwithJSONFormat(t *testing.T) {
plugin.SetParser(p)

var acc testutil.Accumulator
plugin.Init()
require.NoError(t, plugin.Init())
require.NoError(t, acc.GatherError(plugin.Gather))

require.Len(t, acc.Metrics, 1)
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestHTTPHeaders(t *testing.T) {
plugin.SetParser(p)

var acc testutil.Accumulator
plugin.Init()
require.NoError(t, plugin.Init())
require.NoError(t, acc.GatherError(plugin.Gather))
}

Expand All @@ -102,7 +102,7 @@ func TestInvalidStatusCode(t *testing.T) {
plugin.SetParser(p)

var acc testutil.Accumulator
plugin.Init()
require.NoError(t, plugin.Init())
require.Error(t, acc.GatherError(plugin.Gather))
}

Expand All @@ -126,7 +126,7 @@ func TestSuccessStatusCodes(t *testing.T) {
plugin.SetParser(p)

var acc testutil.Accumulator
plugin.Init()
require.NoError(t, plugin.Init())
require.NoError(t, acc.GatherError(plugin.Gather))
}

Expand All @@ -152,7 +152,7 @@ func TestMethod(t *testing.T) {
plugin.SetParser(p)

var acc testutil.Accumulator
plugin.Init()
require.NoError(t, plugin.Init())
require.NoError(t, acc.GatherError(plugin.Gather))
}

Expand Down Expand Up @@ -246,7 +246,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
tt.plugin.SetParser(parser)

var acc testutil.Accumulator
tt.plugin.Init()
require.NoError(t, tt.plugin.Init())
err = tt.plugin.Gather(&acc)
require.NoError(t, err)
})
Expand Down
Loading

0 comments on commit 8e7da35

Please sign in to comment.