Skip to content

Commit 7724897

Browse files
authored
fix: Linter fixes for plugins/inputs/[h-j]* (#9986)
1 parent ecd4d37 commit 7724897

17 files changed

Lines changed: 155 additions & 111 deletions

File tree

plugins/inputs/haproxy/haproxy_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
"strings"
1313
"testing"
1414

15-
"github.com/influxdata/telegraf/testutil"
1615
"github.com/stretchr/testify/require"
16+
17+
"github.com/influxdata/telegraf/testutil"
1718
)
1819

1920
type statServer struct{}
@@ -134,7 +135,7 @@ func TestHaproxyGeneratesMetricsUsingSocket(t *testing.T) {
134135
}
135136

136137
sockets[i] = sock
137-
defer sock.Close()
138+
defer sock.Close() //nolint:revive // done on purpose, closing will be executed properly
138139

139140
s := statServer{}
140141
go s.serverSocket(sock)

plugins/inputs/hddtemp/hddtemp_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package hddtemp
33
import (
44
"testing"
55

6-
hddtemp "github.com/influxdata/telegraf/plugins/inputs/hddtemp/go-hddtemp"
7-
"github.com/influxdata/telegraf/testutil"
86
"github.com/stretchr/testify/assert"
97
"github.com/stretchr/testify/require"
8+
9+
"github.com/influxdata/telegraf/plugins/inputs/hddtemp/go-hddtemp"
10+
"github.com/influxdata/telegraf/testutil"
1011
)
1112

1213
type mockFetcher struct {
@@ -33,14 +34,14 @@ func newMockFetcher() *mockFetcher {
3334
}
3435

3536
func TestFetch(t *testing.T) {
36-
hddtemp := &HDDTemp{
37+
hddTemp := &HDDTemp{
3738
fetcher: newMockFetcher(),
3839
Address: "localhost",
3940
Devices: []string{"*"},
4041
}
4142

4243
acc := &testutil.Accumulator{}
43-
err := hddtemp.Gather(acc)
44+
err := hddTemp.Gather(acc)
4445

4546
require.NoError(t, err)
4647
assert.Equal(t, acc.NFields(), 2)

plugins/inputs/http/http_test.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ import (
99
"net/url"
1010
"testing"
1111

12+
"github.com/stretchr/testify/require"
13+
1214
httpconfig "github.com/influxdata/telegraf/plugins/common/http"
13-
oauth "github.com/influxdata/telegraf/plugins/common/oauth"
14-
plugin "github.com/influxdata/telegraf/plugins/inputs/http"
15+
"github.com/influxdata/telegraf/plugins/common/oauth"
16+
httpplugin "github.com/influxdata/telegraf/plugins/inputs/http"
1517
"github.com/influxdata/telegraf/plugins/parsers"
1618
"github.com/influxdata/telegraf/testutil"
17-
"github.com/stretchr/testify/require"
1819
)
1920

20-
func TestHTTPwithJSONFormat(t *testing.T) {
21+
func TestHTTPWithJSONFormat(t *testing.T) {
2122
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2223
if r.URL.Path == "/endpoint" {
2324
_, _ = w.Write([]byte(simpleJSON))
@@ -27,9 +28,9 @@ func TestHTTPwithJSONFormat(t *testing.T) {
2728
}))
2829
defer fakeServer.Close()
2930

30-
url := fakeServer.URL + "/endpoint"
31-
plugin := &plugin.HTTP{
32-
URLs: []string{url},
31+
address := fakeServer.URL + "/endpoint"
32+
plugin := &httpplugin.HTTP{
33+
URLs: []string{address},
3334
}
3435
metricName := "metricName"
3536

@@ -50,7 +51,7 @@ func TestHTTPwithJSONFormat(t *testing.T) {
5051
require.Equal(t, metric.Measurement, metricName)
5152
require.Len(t, acc.Metrics[0].Fields, 1)
5253
require.Equal(t, acc.Metrics[0].Fields["a"], 1.2)
53-
require.Equal(t, acc.Metrics[0].Tags["url"], url)
54+
require.Equal(t, acc.Metrics[0].Tags["url"], address)
5455
}
5556

5657
func TestHTTPHeaders(t *testing.T) {
@@ -69,9 +70,9 @@ func TestHTTPHeaders(t *testing.T) {
6970
}))
7071
defer fakeServer.Close()
7172

72-
url := fakeServer.URL + "/endpoint"
73-
plugin := &plugin.HTTP{
74-
URLs: []string{url},
73+
address := fakeServer.URL + "/endpoint"
74+
plugin := &httpplugin.HTTP{
75+
URLs: []string{address},
7576
Headers: map[string]string{header: headerValue},
7677
}
7778

@@ -92,9 +93,9 @@ func TestInvalidStatusCode(t *testing.T) {
9293
}))
9394
defer fakeServer.Close()
9495

95-
url := fakeServer.URL + "/endpoint"
96-
plugin := &plugin.HTTP{
97-
URLs: []string{url},
96+
address := fakeServer.URL + "/endpoint"
97+
plugin := &httpplugin.HTTP{
98+
URLs: []string{address},
9899
}
99100

100101
metricName := "metricName"
@@ -115,9 +116,9 @@ func TestSuccessStatusCodes(t *testing.T) {
115116
}))
116117
defer fakeServer.Close()
117118

118-
url := fakeServer.URL + "/endpoint"
119-
plugin := &plugin.HTTP{
120-
URLs: []string{url},
119+
address := fakeServer.URL + "/endpoint"
120+
plugin := &httpplugin.HTTP{
121+
URLs: []string{address},
121122
SuccessStatusCodes: []int{200, 202},
122123
}
123124

@@ -143,7 +144,7 @@ func TestMethod(t *testing.T) {
143144
}))
144145
defer fakeServer.Close()
145146

146-
plugin := &plugin.HTTP{
147+
plugin := &httpplugin.HTTP{
147148
URLs: []string{fakeServer.URL},
148149
Method: "POST",
149150
}
@@ -169,18 +170,18 @@ func TestBodyAndContentEncoding(t *testing.T) {
169170
ts := httptest.NewServer(http.NotFoundHandler())
170171
defer ts.Close()
171172

172-
url := fmt.Sprintf("http://%s", ts.Listener.Addr().String())
173+
address := fmt.Sprintf("http://%s", ts.Listener.Addr().String())
173174

174175
tests := []struct {
175176
name string
176-
plugin *plugin.HTTP
177+
plugin *httpplugin.HTTP
177178
queryHandlerFunc func(t *testing.T, w http.ResponseWriter, r *http.Request)
178179
}{
179180
{
180181
name: "no body",
181-
plugin: &plugin.HTTP{
182+
plugin: &httpplugin.HTTP{
182183
Method: "POST",
183-
URLs: []string{url},
184+
URLs: []string{address},
184185
},
185186
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
186187
body, err := io.ReadAll(r.Body)
@@ -191,8 +192,8 @@ func TestBodyAndContentEncoding(t *testing.T) {
191192
},
192193
{
193194
name: "post body",
194-
plugin: &plugin.HTTP{
195-
URLs: []string{url},
195+
plugin: &httpplugin.HTTP{
196+
URLs: []string{address},
196197
Method: "POST",
197198
Body: "test",
198199
},
@@ -205,8 +206,8 @@ func TestBodyAndContentEncoding(t *testing.T) {
205206
},
206207
{
207208
name: "get method body is sent",
208-
plugin: &plugin.HTTP{
209-
URLs: []string{url},
209+
plugin: &httpplugin.HTTP{
210+
URLs: []string{address},
210211
Method: "GET",
211212
Body: "test",
212213
},
@@ -219,8 +220,8 @@ func TestBodyAndContentEncoding(t *testing.T) {
219220
},
220221
{
221222
name: "gzip encoding",
222-
plugin: &plugin.HTTP{
223-
URLs: []string{url},
223+
plugin: &httpplugin.HTTP{
224+
URLs: []string{address},
224225
Method: "GET",
225226
Body: "test",
226227
ContentEncoding: "gzip",
@@ -269,13 +270,13 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
269270

270271
tests := []struct {
271272
name string
272-
plugin *plugin.HTTP
273+
plugin *httpplugin.HTTP
273274
tokenHandler TestHandlerFunc
274275
handler TestHandlerFunc
275276
}{
276277
{
277278
name: "no credentials",
278-
plugin: &plugin.HTTP{
279+
plugin: &httpplugin.HTTP{
279280
URLs: []string{u.String()},
280281
},
281282
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
@@ -285,7 +286,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
285286
},
286287
{
287288
name: "success",
288-
plugin: &plugin.HTTP{
289+
plugin: &httpplugin.HTTP{
289290
URLs: []string{u.String() + "/write"},
290291
HTTPClientConfig: httpconfig.HTTPClientConfig{
291292
OAuth2Config: oauth.OAuth2Config{

plugins/inputs/http_listener_v2/http_listener_v2_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"time"
1515

1616
"github.com/golang/snappy"
17+
"github.com/stretchr/testify/require"
18+
1719
"github.com/influxdata/telegraf/config"
1820
"github.com/influxdata/telegraf/plugins/parsers"
1921
"github.com/influxdata/telegraf/testutil"
20-
"github.com/stretchr/testify/require"
2122
)
2223

2324
const (
@@ -371,6 +372,7 @@ func TestWriteHTTPGzippedData(t *testing.T) {
371372
client := &http.Client{}
372373
resp, err := client.Do(req)
373374
require.NoError(t, err)
375+
require.NoError(t, resp.Body.Close())
374376
require.EqualValues(t, 204, resp.StatusCode)
375377

376378
hostTags := []string{"server02", "server03",

plugins/inputs/icinga2/icinga2.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (i *Icinga2) SampleConfig() string {
8282

8383
func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
8484
for _, check := range checks {
85-
url, err := url.Parse(i.Server)
85+
serverURL, err := url.Parse(i.Server)
8686
if err != nil {
8787
i.Log.Error(err.Error())
8888
continue
@@ -106,9 +106,9 @@ func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
106106
"check_command": check.Attrs.CheckCommand,
107107
"source": source,
108108
"state": levels[state],
109-
"server": url.Hostname(),
110-
"scheme": url.Scheme,
111-
"port": url.Port(),
109+
"server": serverURL.Hostname(),
110+
"scheme": serverURL.Scheme,
111+
"port": serverURL.Port(),
112112
}
113113

114114
acc.AddFields(fmt.Sprintf("icinga2_%s", i.ObjectType), fields, tags)
@@ -152,9 +152,9 @@ func (i *Icinga2) Gather(acc telegraf.Accumulator) error {
152152
requestURL += "&attrs=host_name"
153153
}
154154

155-
url := fmt.Sprintf(requestURL, i.Server, i.ObjectType)
155+
address := fmt.Sprintf(requestURL, i.Server, i.ObjectType)
156156

157-
req, err := http.NewRequest("GET", url, nil)
157+
req, err := http.NewRequest("GET", address, nil)
158158
if err != nil {
159159
return err
160160
}

plugins/inputs/influxdb_listener/influxdb_listener_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
"testing"
1414
"time"
1515

16+
"github.com/stretchr/testify/require"
17+
1618
"github.com/influxdata/telegraf"
1719
"github.com/influxdata/telegraf/config"
1820
"github.com/influxdata/telegraf/testutil"
19-
"github.com/stretchr/testify/require"
2021
)
2122

2223
const (
@@ -416,6 +417,7 @@ func TestWriteGzippedData(t *testing.T) {
416417
client := &http.Client{}
417418
resp, err := client.Do(req)
418419
require.NoError(t, err)
420+
require.NoError(t, resp.Body.Close())
419421
require.EqualValues(t, 204, resp.StatusCode)
420422

421423
hostTags := []string{"server02", "server03",
@@ -526,6 +528,7 @@ func TestQuery(t *testing.T) {
526528
resp, err := http.Post(
527529
createURL(listener, "http", "/query", "db=&q=CREATE+DATABASE+IF+NOT+EXISTS+%22mydb%22"), "", nil)
528530
require.NoError(t, err)
531+
require.NoError(t, resp.Body.Close())
529532
require.EqualValues(t, 200, resp.StatusCode)
530533
}
531534

plugins/inputs/influxdb_v2_listener/influxdb_v2_listener_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
"testing"
1616
"time"
1717

18+
"github.com/stretchr/testify/require"
19+
1820
"github.com/influxdata/telegraf/config"
1921
"github.com/influxdata/telegraf/testutil"
20-
"github.com/stretchr/testify/require"
2122
)
2223

2324
const (
@@ -374,6 +375,7 @@ func TestWriteGzippedData(t *testing.T) {
374375
client := &http.Client{}
375376
resp, err := client.Do(req)
376377
require.NoError(t, err)
378+
require.NoError(t, resp.Body.Close())
377379
require.EqualValues(t, 204, resp.StatusCode)
378380

379381
hostTags := []string{"server02", "server03",

plugins/inputs/interrupts/interrupts.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func parseInterrupts(r io.Reader) ([]IRQ, error) {
5757
if scanner.Scan() {
5858
cpus := strings.Fields(scanner.Text())
5959
if cpus[0] != "CPU0" {
60-
return nil, fmt.Errorf("Expected first line to start with CPU0, but was %s", scanner.Text())
60+
return nil, fmt.Errorf("expected first line to start with CPU0, but was %s", scanner.Text())
6161
}
6262
cpucount = len(cpus)
6363
}
@@ -93,7 +93,7 @@ scan:
9393
irqs = append(irqs, *irq)
9494
}
9595
if scanner.Err() != nil {
96-
return nil, fmt.Errorf("Error scanning file: %s", scanner.Err())
96+
return nil, fmt.Errorf("error scanning file: %s", scanner.Err())
9797
}
9898
return irqs, nil
9999
}
@@ -110,22 +110,30 @@ func gatherTagsFields(irq IRQ) (map[string]string, map[string]interface{}) {
110110

111111
func (s *Interrupts) Gather(acc telegraf.Accumulator) error {
112112
for measurement, file := range map[string]string{"interrupts": "/proc/interrupts", "soft_interrupts": "/proc/softirqs"} {
113-
f, err := os.Open(file)
113+
irqs, err := parseFile(file)
114114
if err != nil {
115-
acc.AddError(fmt.Errorf("Could not open file: %s", file))
116-
continue
117-
}
118-
defer f.Close()
119-
irqs, err := parseInterrupts(f)
120-
if err != nil {
121-
acc.AddError(fmt.Errorf("Parsing %s: %s", file, err))
115+
acc.AddError(err)
122116
continue
123117
}
124118
reportMetrics(measurement, irqs, acc, s.CPUAsTag)
125119
}
126120
return nil
127121
}
128122

123+
func parseFile(file string) ([]IRQ, error) {
124+
f, err := os.Open(file)
125+
if err != nil {
126+
return nil, fmt.Errorf("could not open file: %s", file)
127+
}
128+
defer f.Close()
129+
130+
irqs, err := parseInterrupts(f)
131+
if err != nil {
132+
return nil, fmt.Errorf("parsing %s: %s", file, err)
133+
}
134+
return irqs, nil
135+
}
136+
129137
func reportMetrics(measurement string, irqs []IRQ, acc telegraf.Accumulator, cpusAsTags bool) {
130138
for _, irq := range irqs {
131139
tags, fields := gatherTagsFields(irq)

0 commit comments

Comments
 (0)