Skip to content

Commit

Permalink
Linter fixes (unhandled errors) -- Part 2 (influxdata#9122)
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored Apr 22, 2021
1 parent 4d00e21 commit 03b2dae
Show file tree
Hide file tree
Showing 56 changed files with 640 additions and 460 deletions.
21 changes: 12 additions & 9 deletions plugins/inputs/activemq/activemq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGatherQueuesMetrics(t *testing.T) {

queues := Queues{}

xml.Unmarshal([]byte(s), &queues)
require.NoError(t, xml.Unmarshal([]byte(s), &queues))

records := make(map[string]interface{})
tags := make(map[string]string)
Expand All @@ -49,7 +49,7 @@ func TestGatherQueuesMetrics(t *testing.T) {
activeMQ := new(ActiveMQ)
activeMQ.Server = "localhost"
activeMQ.Port = 8161
activeMQ.Init()
require.NoError(t, activeMQ.Init())

activeMQ.GatherQueuesMetrics(&acc, queues)
acc.AssertContainsTaggedFields(t, "activemq_queues", records, tags)
Expand All @@ -76,7 +76,7 @@ func TestGatherTopicsMetrics(t *testing.T) {

topics := Topics{}

xml.Unmarshal([]byte(s), &topics)
require.NoError(t, xml.Unmarshal([]byte(s), &topics))

records := make(map[string]interface{})
tags := make(map[string]string)
Expand All @@ -95,7 +95,7 @@ func TestGatherTopicsMetrics(t *testing.T) {
activeMQ := new(ActiveMQ)
activeMQ.Server = "localhost"
activeMQ.Port = 8161
activeMQ.Init()
require.NoError(t, activeMQ.Init())

activeMQ.GatherTopicsMetrics(&acc, topics)
acc.AssertContainsTaggedFields(t, "activemq_topics", records, tags)
Expand All @@ -110,7 +110,7 @@ func TestGatherSubscribersMetrics(t *testing.T) {

subscribers := Subscribers{}

xml.Unmarshal([]byte(s), &subscribers)
require.NoError(t, xml.Unmarshal([]byte(s), &subscribers))

records := make(map[string]interface{})
tags := make(map[string]string)
Expand All @@ -135,7 +135,7 @@ func TestGatherSubscribersMetrics(t *testing.T) {
activeMQ := new(ActiveMQ)
activeMQ.Server = "localhost"
activeMQ.Port = 8161
activeMQ.Init()
require.NoError(t, activeMQ.Init())

activeMQ.GatherSubscribersMetrics(&acc, subscribers)
acc.AssertContainsTaggedFields(t, "activemq_subscribers", records, tags)
Expand All @@ -149,13 +149,16 @@ func TestURLs(t *testing.T) {
switch r.URL.Path {
case "/admin/xml/queues.jsp":
w.WriteHeader(http.StatusOK)
w.Write([]byte("<queues></queues>"))
_, err := w.Write([]byte("<queues></queues>"))
require.NoError(t, err)
case "/admin/xml/topics.jsp":
w.WriteHeader(http.StatusOK)
w.Write([]byte("<topics></topics>"))
_, err := w.Write([]byte("<topics></topics>"))
require.NoError(t, err)
case "/admin/xml/subscribers.jsp":
w.WriteHeader(http.StatusOK)
w.Write([]byte("<subscribers></subscribers>"))
_, err := w.Write([]byte("<subscribers></subscribers>"))
require.NoError(t, err)
default:
w.WriteHeader(http.StatusNotFound)
t.Fatalf("unexpected path: " + r.URL.Path)
Expand Down
3 changes: 2 additions & 1 deletion plugins/inputs/apache/apache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Scoreboard: WW_____W_RW_R_W__RRR____WR_W___WW________W_WW_W_____R__R_WR__WRWR_RR
func TestHTTPApache(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, apacheStatus)
_, err := fmt.Fprintln(w, apacheStatus)
require.NoError(t, err)
}))
defer ts.Close()

Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/apcupsd/apcupsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func listen(ctx context.Context, t *testing.T, out [][]byte) (string, error) {
continue
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(time.Second))
require.NoError(t, conn.SetReadDeadline(time.Now().Add(time.Second)))

in := make([]byte, 128)
n, err := conn.Read(in)
Expand Down
4 changes: 3 additions & 1 deletion plugins/inputs/aurora/aurora.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ func (a *Aurora) gatherRole(ctx context.Context, origin *url.URL) (RoleType, err
if err != nil {
return Unknown, err
}
resp.Body.Close()
if err := resp.Body.Close(); err != nil {
return Unknown, fmt.Errorf("closing body failed: %v", err)
}

switch resp.StatusCode {
case http.StatusOK:
Expand Down
24 changes: 16 additions & 8 deletions plugins/inputs/aurora/aurora_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func TestAurora(t *testing.T) {
"variable_scrape_micros_total_per_sec": 1485.0
}`
w.WriteHeader(http.StatusOK)
w.Write([]byte(body))
_, err := w.Write([]byte(body))
require.NoError(t, err)
},
check: func(t *testing.T, err error, acc *testutil.Accumulator) {
require.NoError(t, err)
Expand Down Expand Up @@ -86,7 +87,8 @@ func TestAurora(t *testing.T) {
},
varsjson: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
_, err := w.Write([]byte("{}"))
require.NoError(t, err)
},
check: func(t *testing.T, err error, acc *testutil.Accumulator) {
require.NoError(t, err)
Expand All @@ -104,7 +106,8 @@ func TestAurora(t *testing.T) {
"foo": "bar"
}`
w.WriteHeader(http.StatusOK)
w.Write([]byte(body))
_, err := w.Write([]byte(body))
require.NoError(t, err)
},
check: func(t *testing.T, err error, acc *testutil.Accumulator) {
require.NoError(t, err)
Expand All @@ -123,7 +126,8 @@ func TestAurora(t *testing.T) {
"foo": 1e309
}`
w.WriteHeader(http.StatusOK)
w.Write([]byte(body))
_, err := w.Write([]byte(body))
require.NoError(t, err)
},
check: func(t *testing.T, err error, acc *testutil.Accumulator) {
require.NoError(t, err)
Expand All @@ -142,7 +146,8 @@ func TestAurora(t *testing.T) {
"foo": 9223372036854775808
}`
w.WriteHeader(http.StatusOK)
w.Write([]byte(body))
_, err := w.Write([]byte(body))
require.NoError(t, err)
},
check: func(t *testing.T, err error, acc *testutil.Accumulator) {
require.NoError(t, err)
Expand All @@ -158,7 +163,8 @@ func TestAurora(t *testing.T) {
varsjson: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
body := `{]`
w.WriteHeader(http.StatusOK)
w.Write([]byte(body))
_, err := w.Write([]byte(body))
require.NoError(t, err)
},
check: func(t *testing.T, err error, acc *testutil.Accumulator) {
require.NoError(t, err)
Expand All @@ -176,7 +182,8 @@ func TestAurora(t *testing.T) {
"value": 42
}`
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(body))
_, err := w.Write([]byte(body))
require.NoError(t, err)
},
check: func(t *testing.T, err error, acc *testutil.Accumulator) {
require.NoError(t, err)
Expand Down Expand Up @@ -244,7 +251,8 @@ func TestBasicAuth(t *testing.T) {
require.Equal(t, tt.username, username)
require.Equal(t, tt.password, password)
w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
_, err := w.Write([]byte("{}"))
require.NoError(t, err)
})

var acc testutil.Accumulator
Expand Down
7 changes: 5 additions & 2 deletions plugins/inputs/bcache/bcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package bcache

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -128,7 +129,7 @@ func (b *Bcache) Gather(acc telegraf.Accumulator) error {
}
bdevs, _ := filepath.Glob(bcachePath + "/*/bdev*")
if len(bdevs) < 1 {
return errors.New("Can't find any bcache device")
return errors.New("can't find any bcache device")
}
for _, bdev := range bdevs {
if restrictDevs {
Expand All @@ -137,7 +138,9 @@ func (b *Bcache) Gather(acc telegraf.Accumulator) error {
continue
}
}
b.gatherBcache(bdev, acc)
if err := b.gatherBcache(bdev, acc); err != nil {
return fmt.Errorf("gathering bcache failed: %v", err)
}
}
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion plugins/inputs/beanstalkd/beanstalkd.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func (b *Beanstalkd) Gather(acc telegraf.Accumulator) error {
for _, tube := range tubes {
wg.Add(1)
go func(tube string) {
b.gatherTubeStats(connection, tube, acc)
err := b.gatherTubeStats(connection, tube, acc)
if err != nil {
acc.AddError(err)
}
wg.Done()
}(tube)
}
Expand Down
44 changes: 32 additions & 12 deletions plugins/inputs/beanstalkd/beanstalkd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestBeanstalkd(t *testing.T) {
tubesConfig []string
expectedTubes []tubeStats
notExpectedTubes []tubeStats
expectedError string
}{
{
name: "All tubes stats",
Expand Down Expand Up @@ -50,15 +51,14 @@ func TestBeanstalkd(t *testing.T) {
{name: "default", fields: defaultTubeFields},
{name: "test", fields: testTubeFields},
},
expectedError: "input does not match format",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
server, err := startTestServer(t)
if err != nil {
t.Fatalf("Unable to create test server")
}
require.NoError(t, err, "Unable to create test server")
defer server.Close()

serverAddress := server.Addr().String()
Expand All @@ -68,8 +68,13 @@ func TestBeanstalkd(t *testing.T) {
}

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

err = acc.GatherError(plugin.Gather)
if test.expectedError == "" {
require.NoError(t, err)
} else {
require.Error(t, err)
require.Equal(t, test.expectedError, err.Error())
}
acc.AssertContainsTaggedFields(t, "beanstalkd_overview",
overviewFields,
getOverviewTags(serverAddress),
Expand Down Expand Up @@ -110,8 +115,8 @@ func startTestServer(t *testing.T) (net.Listener, error) {
tp := textproto.NewConn(connection)
defer tp.Close()

sendSuccessResponse := func(body string) {
tp.PrintfLine("OK %d\r\n%s", len(body), body)
sendSuccessResponse := func(body string) error {
return tp.PrintfLine("OK %d\r\n%s", len(body), body)
}

for {
Expand All @@ -125,15 +130,30 @@ func startTestServer(t *testing.T) (net.Listener, error) {

switch cmd {
case "list-tubes":
sendSuccessResponse(listTubesResponse)
if err := sendSuccessResponse(listTubesResponse); err != nil {
t.Logf("sending response %q failed: %v", listTubesResponse, err)
return
}
case "stats":
sendSuccessResponse(statsResponse)
if err := sendSuccessResponse(statsResponse); err != nil {
t.Logf("sending response %q failed: %v", statsResponse, err)
return
}
case "stats-tube default":
sendSuccessResponse(statsTubeDefaultResponse)
if err := sendSuccessResponse(statsTubeDefaultResponse); err != nil {
t.Logf("sending response %q failed: %v", statsTubeDefaultResponse, err)
return
}
case "stats-tube test":
sendSuccessResponse(statsTubeTestResponse)
if err := sendSuccessResponse(statsTubeTestResponse); err != nil {
t.Logf("sending response %q failed: %v", statsTubeTestResponse, err)
return
}
case "stats-tube unknown":
tp.PrintfLine("NOT_FOUND")
if err := tp.PrintfLine("NOT_FOUND"); err != nil {
t.Logf("sending response %q failed: %v", "NOT_FOUND", err)
return
}
default:
t.Log("Test server: unknown command")
}
Expand Down
Loading

0 comments on commit 03b2dae

Please sign in to comment.