Skip to content

Commit

Permalink
Linter fixes for plugins/inputs/[fg]* (#9387)
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel authored Jul 27, 2021
1 parent 1a42c7d commit 87c94e4
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 157 deletions.
6 changes: 6 additions & 0 deletions plugins/inputs/fail2ban/fail2ban_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,35 @@ func TestHelperProcess(_ *testing.T) {
if !strings.HasSuffix(cmd, "fail2ban-client") {
//nolint:errcheck,revive // Test will fail anyway
fmt.Fprint(os.Stdout, "command not found")
//nolint:revive // os.Exit called intentionally
os.Exit(1)
}

if len(args) == 1 && args[0] == "status" {
//nolint:errcheck,revive // Test will fail anyway
fmt.Fprint(os.Stdout, execStatusOutput)
//nolint:revive // os.Exit called intentionally
os.Exit(0)
} else if len(args) == 2 && args[0] == "status" {
if args[1] == "sshd" {
//nolint:errcheck,revive // Test will fail anyway
fmt.Fprint(os.Stdout, execStatusSshdOutput)
//nolint:revive // os.Exit called intentionally
os.Exit(0)
} else if args[1] == "postfix" {
//nolint:errcheck,revive // Test will fail anyway
fmt.Fprint(os.Stdout, execStatusPostfixOutput)
//nolint:revive // os.Exit called intentionally
os.Exit(0)
} else if args[1] == "dovecot" {
//nolint:errcheck,revive // Test will fail anyway
fmt.Fprint(os.Stdout, execStatusDovecotOutput)
//nolint:revive // os.Exit called intentionally
os.Exit(0)
}
}
//nolint:errcheck,revive // Test will fail anyway
fmt.Fprint(os.Stdout, "invalid argument")
//nolint:revive // os.Exit called intentionally
os.Exit(1)
}
8 changes: 6 additions & 2 deletions plugins/inputs/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

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

func TestRefreshFilePaths(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)

r := File{
Files: []string{filepath.Join(wd, "dev/testfiles/**.log")},
}
Expand Down Expand Up @@ -100,7 +103,8 @@ func TestGrokParser(t *testing.T) {
require.NoError(t, err)

err = r.Gather(&acc)
require.Equal(t, len(acc.Metrics), 2)
require.NoError(t, err)
require.Len(t, acc.Metrics, 2)
}

func TestCharacterEncoding(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions plugins/inputs/filecount/filesystem_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ func TestRealFS(t *testing.T) {
fs = getTestFileSystem()
// now, the same test as above will return an error as the file doesn't exist in our fake fs
expectedError := "Stat " + getTestdataDir() + "/qux: No such file or directory"
fileInfo, err = fs.Stat(getTestdataDir() + "/qux")
require.Equal(t, expectedError, err.Error())
_, err = fs.Stat(getTestdataDir() + "/qux")
require.Error(t, err, expectedError)
// and verify that what we DO expect to find, we do
fileInfo, err = fs.Stat("/testdata/foo")
require.NoError(t, err)
require.NotNil(t, fileInfo)
}

func getTestFileSystem() fakeFileSystem {
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/filestat/filestat.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
}

if f.Md5 {
md5, err := getMd5(fileName)
md5Hash, err := getMd5(fileName)
if err != nil {
acc.AddError(err)
} else {
fields["md5_sum"] = md5
fields["md5_sum"] = md5Hash
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/filestat/filestat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestGetMd5(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "5a7e9b77fa25e7bb411dbd17cf403c1f", md5)

md5, err = getMd5("/tmp/foo/bar/fooooo")
_, err = getMd5("/tmp/foo/bar/fooooo")
require.Error(t, err)
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/fluentd/fluentd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func parse(data []byte) (datapointArray []pluginData, err error) {

if err = json.Unmarshal(data, &endpointData); err != nil {
err = fmt.Errorf("processing JSON structure")
return
return nil, err
}

datapointArray = append(datapointArray, endpointData.Payload...)
return
return datapointArray, err
}

// Description - display description
Expand Down
5 changes: 4 additions & 1 deletion plugins/inputs/fluentd/fluentd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"net/url"
"testing"

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

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

// sampleJSON from fluentd version '0.14.9'
Expand Down Expand Up @@ -127,6 +128,8 @@ func Test_Gather(t *testing.T) {
}))

requestURL, err := url.Parse(fluentdTest.Endpoint)
require.NoError(t, err)
require.NotNil(t, requestURL)

ts.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))

Expand Down
31 changes: 16 additions & 15 deletions plugins/inputs/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"sync"
"time"

"github.com/google/go-github/v32/github"
githubLib "github.com/google/go-github/v32/github"
"golang.org/x/oauth2"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/selfstat"
"golang.org/x/oauth2"
)

// GitHub - plugin main structure
Expand All @@ -23,7 +24,7 @@ type GitHub struct {
AdditionalFields []string `toml:"additional_fields"`
EnterpriseBaseURL string `toml:"enterprise_base_url"`
HTTPTimeout config.Duration `toml:"http_timeout"`
githubClient *github.Client
githubClient *githubLib.Client

obfuscatedToken string

Expand Down Expand Up @@ -68,7 +69,7 @@ func (g *GitHub) Description() string {
}

// Create GitHub Client
func (g *GitHub) createGitHubClient(ctx context.Context) (*github.Client, error) {
func (g *GitHub) createGitHubClient(ctx context.Context) (*githubLib.Client, error) {
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Expand All @@ -93,11 +94,11 @@ func (g *GitHub) createGitHubClient(ctx context.Context) (*github.Client, error)
return g.newGithubClient(httpClient)
}

func (g *GitHub) newGithubClient(httpClient *http.Client) (*github.Client, error) {
func (g *GitHub) newGithubClient(httpClient *http.Client) (*githubLib.Client, error) {
if g.EnterpriseBaseURL != "" {
return github.NewEnterpriseClient(g.EnterpriseBaseURL, "", httpClient)
return githubLib.NewEnterpriseClient(g.EnterpriseBaseURL, "", httpClient)
}
return github.NewClient(httpClient), nil
return githubLib.NewClient(httpClient), nil
}

// Gather GitHub Metrics
Expand Down Expand Up @@ -172,16 +173,16 @@ func (g *GitHub) Gather(acc telegraf.Accumulator) error {
return nil
}

func (g *GitHub) handleRateLimit(response *github.Response, err error) {
func (g *GitHub) handleRateLimit(response *githubLib.Response, err error) {
if err == nil {
g.RateLimit.Set(int64(response.Rate.Limit))
g.RateRemaining.Set(int64(response.Rate.Remaining))
} else if _, ok := err.(*github.RateLimitError); ok {
} else if _, ok := err.(*githubLib.RateLimitError); ok {
g.RateLimitErrors.Incr(1)
}
}

func splitRepositoryName(repositoryName string) (string, string, error) {
func splitRepositoryName(repositoryName string) (owner string, repository string, err error) {
splits := strings.SplitN(repositoryName, "/", 2)

if len(splits) != 2 {
Expand All @@ -191,15 +192,15 @@ func splitRepositoryName(repositoryName string) (string, string, error) {
return splits[0], splits[1], nil
}

func getLicense(rI *github.Repository) string {
func getLicense(rI *githubLib.Repository) string {
if licenseName := rI.GetLicense().GetName(); licenseName != "" {
return licenseName
}

return "None"
}

func getTags(repositoryInfo *github.Repository) map[string]string {
func getTags(repositoryInfo *githubLib.Repository) map[string]string {
return map[string]string{
"owner": repositoryInfo.GetOwner().GetLogin(),
"name": repositoryInfo.GetName(),
Expand All @@ -208,7 +209,7 @@ func getTags(repositoryInfo *github.Repository) map[string]string {
}
}

func getFields(repositoryInfo *github.Repository) map[string]interface{} {
func getFields(repositoryInfo *githubLib.Repository) map[string]interface{} {
return map[string]interface{}{
"stars": repositoryInfo.GetStargazersCount(),
"subscribers": repositoryInfo.GetSubscribersCount(),
Expand All @@ -221,9 +222,9 @@ func getFields(repositoryInfo *github.Repository) map[string]interface{} {
}

func (g *GitHub) getPullRequestFields(ctx context.Context, owner, repo string) (map[string]interface{}, error) {
options := github.SearchOptions{
options := githubLib.SearchOptions{
TextMatch: false,
ListOptions: github.ListOptions{
ListOptions: githubLib.ListOptions{
PerPage: 100,
Page: 1,
},
Expand Down
Loading

0 comments on commit 87c94e4

Please sign in to comment.