Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Linter fixes for plugins/inputs/[h-j]* #9986

Merged
merged 3 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugins/inputs/haproxy/haproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
"strings"
"testing"

"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/testutil"
)

type statServer struct{}
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestHaproxyGeneratesMetricsUsingSocket(t *testing.T) {
}

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

s := statServer{}
go s.serverSocket(sock)
Expand Down
9 changes: 5 additions & 4 deletions plugins/inputs/hddtemp/hddtemp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package hddtemp
import (
"testing"

hddtemp "github.com/influxdata/telegraf/plugins/inputs/hddtemp/go-hddtemp"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/plugins/inputs/hddtemp/go-hddtemp"
"github.com/influxdata/telegraf/testutil"
)

type mockFetcher struct {
Expand All @@ -33,14 +34,14 @@ func newMockFetcher() *mockFetcher {
}

func TestFetch(t *testing.T) {
hddtemp := &HDDTemp{
hddTemp := &HDDTemp{
fetcher: newMockFetcher(),
Address: "localhost",
Devices: []string{"*"},
}

acc := &testutil.Accumulator{}
err := hddtemp.Gather(acc)
err := hddTemp.Gather(acc)

require.NoError(t, err)
assert.Equal(t, acc.NFields(), 2)
Expand Down
63 changes: 32 additions & 31 deletions plugins/inputs/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"net/url"
"testing"

"github.com/stretchr/testify/require"

httpconfig "github.com/influxdata/telegraf/plugins/common/http"
oauth "github.com/influxdata/telegraf/plugins/common/oauth"
plugin "github.com/influxdata/telegraf/plugins/inputs/http"
"github.com/influxdata/telegraf/plugins/common/oauth"
httpplugin "github.com/influxdata/telegraf/plugins/inputs/http"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

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

url := fakeServer.URL + "/endpoint"
plugin := &plugin.HTTP{
URLs: []string{url},
address := fakeServer.URL + "/endpoint"
plugin := &httpplugin.HTTP{
URLs: []string{address},
}
metricName := "metricName"

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

func TestHTTPHeaders(t *testing.T) {
Expand All @@ -69,9 +70,9 @@ func TestHTTPHeaders(t *testing.T) {
}))
defer fakeServer.Close()

url := fakeServer.URL + "/endpoint"
plugin := &plugin.HTTP{
URLs: []string{url},
address := fakeServer.URL + "/endpoint"
plugin := &httpplugin.HTTP{
URLs: []string{address},
Headers: map[string]string{header: headerValue},
}

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

url := fakeServer.URL + "/endpoint"
plugin := &plugin.HTTP{
URLs: []string{url},
address := fakeServer.URL + "/endpoint"
plugin := &httpplugin.HTTP{
URLs: []string{address},
}

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

url := fakeServer.URL + "/endpoint"
plugin := &plugin.HTTP{
URLs: []string{url},
address := fakeServer.URL + "/endpoint"
plugin := &httpplugin.HTTP{
URLs: []string{address},
SuccessStatusCodes: []int{200, 202},
}

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

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

url := fmt.Sprintf("http://%s", ts.Listener.Addr().String())
address := fmt.Sprintf("http://%s", ts.Listener.Addr().String())

tests := []struct {
name string
plugin *plugin.HTTP
plugin *httpplugin.HTTP
queryHandlerFunc func(t *testing.T, w http.ResponseWriter, r *http.Request)
}{
{
name: "no body",
plugin: &plugin.HTTP{
plugin: &httpplugin.HTTP{
Method: "POST",
URLs: []string{url},
URLs: []string{address},
},
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
Expand All @@ -191,8 +192,8 @@ func TestBodyAndContentEncoding(t *testing.T) {
},
{
name: "post body",
plugin: &plugin.HTTP{
URLs: []string{url},
plugin: &httpplugin.HTTP{
URLs: []string{address},
Method: "POST",
Body: "test",
},
Expand All @@ -205,8 +206,8 @@ func TestBodyAndContentEncoding(t *testing.T) {
},
{
name: "get method body is sent",
plugin: &plugin.HTTP{
URLs: []string{url},
plugin: &httpplugin.HTTP{
URLs: []string{address},
Method: "GET",
Body: "test",
},
Expand All @@ -219,8 +220,8 @@ func TestBodyAndContentEncoding(t *testing.T) {
},
{
name: "gzip encoding",
plugin: &plugin.HTTP{
URLs: []string{url},
plugin: &httpplugin.HTTP{
URLs: []string{address},
Method: "GET",
Body: "test",
ContentEncoding: "gzip",
Expand Down Expand Up @@ -269,13 +270,13 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {

tests := []struct {
name string
plugin *plugin.HTTP
plugin *httpplugin.HTTP
tokenHandler TestHandlerFunc
handler TestHandlerFunc
}{
{
name: "no credentials",
plugin: &plugin.HTTP{
plugin: &httpplugin.HTTP{
URLs: []string{u.String()},
},
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
Expand All @@ -285,7 +286,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
},
{
name: "success",
plugin: &plugin.HTTP{
plugin: &httpplugin.HTTP{
URLs: []string{u.String() + "/write"},
HTTPClientConfig: httpconfig.HTTPClientConfig{
OAuth2Config: oauth.OAuth2Config{
Expand Down
4 changes: 3 additions & 1 deletion plugins/inputs/http_listener_v2/http_listener_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
"time"

"github.com/golang/snappy"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -371,6 +372,7 @@ func TestWriteHTTPGzippedData(t *testing.T) {
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
require.EqualValues(t, 204, resp.StatusCode)

hostTags := []string{"server02", "server03",
Expand Down
12 changes: 6 additions & 6 deletions plugins/inputs/icinga2/icinga2.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (i *Icinga2) SampleConfig() string {

func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
for _, check := range checks {
url, err := url.Parse(i.Server)
serverURL, err := url.Parse(i.Server)
if err != nil {
i.Log.Error(err.Error())
continue
Expand All @@ -106,9 +106,9 @@ func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
"check_command": check.Attrs.CheckCommand,
"source": source,
"state": levels[state],
"server": url.Hostname(),
"scheme": url.Scheme,
"port": url.Port(),
"server": serverURL.Hostname(),
"scheme": serverURL.Scheme,
"port": serverURL.Port(),
}

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

url := fmt.Sprintf(requestURL, i.Server, i.ObjectType)
address := fmt.Sprintf(requestURL, i.Server, i.ObjectType)

req, err := http.NewRequest("GET", url, nil)
req, err := http.NewRequest("GET", address, nil)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion plugins/inputs/influxdb_listener/influxdb_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -416,6 +417,7 @@ func TestWriteGzippedData(t *testing.T) {
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
require.EqualValues(t, 204, resp.StatusCode)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -374,6 +375,7 @@ func TestWriteGzippedData(t *testing.T) {
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
require.EqualValues(t, 204, resp.StatusCode)

hostTags := []string{"server02", "server03",
Expand Down
28 changes: 18 additions & 10 deletions plugins/inputs/interrupts/interrupts.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func parseInterrupts(r io.Reader) ([]IRQ, error) {
if scanner.Scan() {
cpus := strings.Fields(scanner.Text())
if cpus[0] != "CPU0" {
return nil, fmt.Errorf("Expected first line to start with CPU0, but was %s", scanner.Text())
return nil, fmt.Errorf("expected first line to start with CPU0, but was %s", scanner.Text())
}
cpucount = len(cpus)
}
Expand Down Expand Up @@ -93,7 +93,7 @@ scan:
irqs = append(irqs, *irq)
}
if scanner.Err() != nil {
return nil, fmt.Errorf("Error scanning file: %s", scanner.Err())
return nil, fmt.Errorf("error scanning file: %s", scanner.Err())
}
return irqs, nil
}
Expand All @@ -110,22 +110,30 @@ func gatherTagsFields(irq IRQ) (map[string]string, map[string]interface{}) {

func (s *Interrupts) Gather(acc telegraf.Accumulator) error {
for measurement, file := range map[string]string{"interrupts": "/proc/interrupts", "soft_interrupts": "/proc/softirqs"} {
f, err := os.Open(file)
irqs, err := parseFile(file)
if err != nil {
acc.AddError(fmt.Errorf("Could not open file: %s", file))
continue
}
defer f.Close()
irqs, err := parseInterrupts(f)
if err != nil {
acc.AddError(fmt.Errorf("Parsing %s: %s", file, err))
acc.AddError(err)
continue
}
reportMetrics(measurement, irqs, acc, s.CPUAsTag)
}
return nil
}

func parseFile(file string) ([]IRQ, error) {
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("could not open file: %s", file)
}
defer f.Close()

irqs, err := parseInterrupts(f)
if err != nil {
return nil, fmt.Errorf("parsing %s: %s", file, err)
}
return irqs, nil
}

func reportMetrics(measurement string, irqs []IRQ, acc telegraf.Accumulator, cpusAsTags bool) {
for _, irq := range irqs {
tags, fields := gatherTagsFields(irq)
Expand Down
Loading