From b2967cb51511490aa6110b3dc3a665e4c889248a Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:23:51 +0200 Subject: [PATCH] chore: Update go to v1.23.0 (#15766) Co-authored-by: Pawel Zak --- .circleci/config.yml | 21 +- .github/workflows/readme-linter.yml | 2 +- .golangci.yml | 1 - Makefile | 6 +- cmd/telegraf/telegraf.go | 2 +- cmd/telegraf/telegraf_windows.go | 6 +- config/config.go | 2 +- go.mod | 2 +- internal/globpath/globpath_test.go | 5 +- internal/internal.go | 10 +- internal/internal_test.go | 2 +- plugins/common/kafka/config_test.go | 6 +- plugins/inputs/activemq/activemq_test.go | 2 +- plugins/inputs/beanstalkd/beanstalkd.go | 1 + .../cloudwatch_metric_streams.go | 15 +- plugins/inputs/conntrack/conntrack_test.go | 6 +- .../directory_monitor/directory_monitor.go | 4 +- plugins/inputs/exec/exec_test.go | 1 + plugins/inputs/filecount/filecount_test.go | 1 + .../google_cloud_storage_test.go | 4 +- plugins/inputs/http_response/http_response.go | 2 +- plugins/inputs/intel_pmu/intel_pmu.go | 4 +- plugins/inputs/intel_pmu/resolver_test.go | 29 ++- plugins/inputs/intel_powerstat/metrics.go | 2 - plugins/inputs/intel_rdt/intel_rdt.go | 6 +- .../jolokia2_agent/jolokia2_agent_test.go | 2 +- .../jolokia2_proxy/jolokia2_proxy_test.go | 2 +- .../kafka_consumer/kafka_consumer_test.go | 6 +- plugins/inputs/net/net_test.go | 190 ++++++++++-------- plugins/inputs/netflow/type_conversion.go | 2 + plugins/inputs/ping/ping_windows.go | 8 +- plugins/inputs/processes/processes_test.go | 33 ++- plugins/inputs/smart/smart.go | 6 +- plugins/inputs/statsd/running_stats.go | 12 +- plugins/inputs/tacacs/tacacs_test.go | 140 ++++++++++--- plugins/inputs/temp/temp_test.go | 9 +- .../win_perf_counters_integration_test.go | 4 +- plugins/inputs/zipkin/codec/codec.go | 26 +-- .../outputs/azure_monitor/azure_monitor.go | 16 +- plugins/outputs/cloudwatch/cloudwatch.go | 16 +- plugins/outputs/sql/sqlite_test.go | 9 +- plugins/parsers/nagios/parser.go | 18 +- plugins/parsers/nagios/parser_test.go | 6 +- .../processors/port_name/port_name_test.go | 2 +- plugins/processors/starlark/starlark_test.go | 4 +- plugins/processors/topk/topk.go | 8 +- scripts/ci.docker | 2 +- scripts/installgo_linux.sh | 4 +- scripts/installgo_mac.sh | 6 +- scripts/installgo_windows.sh | 2 +- tools/custom_builder/packages.go | 2 + tools/readme_linter/rules.go | 5 +- 52 files changed, 376 insertions(+), 306 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e948971dff049..a12fcc66a24aa 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -8,7 +8,7 @@ executors: working_directory: '/go/src/github.com/influxdata/telegraf' resource_class: large docker: - - image: 'quay.io/influxdb/telegraf-ci:1.22.6' + - image: 'quay.io/influxdb/telegraf-ci:1.23.0' environment: GOFLAGS: -p=4 mac: @@ -106,10 +106,13 @@ jobs: - run: 'make check-deps' - run: name: "Install golangci-lint" - command: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 + command: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.3 - run: name: "golangci-lint/Linux" - command: GOGC=80 GOMEMLIMIT=6656MiB /go/bin/golangci-lint run --verbose + # There are only 4 vCPUs available for this executor, so use only 4 instead of the default number + # (the OS may report the number of CPUs on the host instead of the number of CPUs available to the guest). + command: GOGC=80 GOMEMLIMIT=6144MiB /go/bin/golangci-lint run --verbose --timeout=30m --concurrency 4 + no_output_timeout: 30m lint-macos: executor: telegraf-ci steps: @@ -117,10 +120,12 @@ jobs: - check-changed-files-or-halt - run: name: "Install golangci-lint" - command: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 + command: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.3 - run: name: "golangci-lint/macOS" - command: GOGC=80 GOMEMLIMIT=6656MiB GOOS=darwin /go/bin/golangci-lint run --verbose --timeout=30m + # There are only 4 vCPUs available for this executor, so use only 4 instead of the default number + # (the OS may report the number of CPUs on the host instead of the number of CPUs available to the guest). + command: GOGC=80 GOMEMLIMIT=6144MiB GOOS=darwin /go/bin/golangci-lint run --verbose --timeout=30m --concurrency 4 no_output_timeout: 30m lint-windows: executor: telegraf-ci @@ -129,10 +134,12 @@ jobs: - check-changed-files-or-halt - run: name: "Install golangci-lint" - command: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 + command: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.3 - run: name: "golangci-lint/Windows" - command: GOGC=80 GOMEMLIMIT=6656MiB GOOS=windows /go/bin/golangci-lint run --verbose --timeout=30m + # There are only 4 vCPUs available for this executor, so use only 4 instead of the default number + # (the OS may report the number of CPUs on the host instead of the number of CPUs available to the guest). + command: GOGC=80 GOMEMLIMIT=6144MiB GOOS=windows /go/bin/golangci-lint run --verbose --timeout=30m --concurrency 4 no_output_timeout: 30m test-go-linux: executor: telegraf-ci diff --git a/.github/workflows/readme-linter.yml b/.github/workflows/readme-linter.yml index f51dcf3f5a75e..3cbb5ede5eb31 100644 --- a/.github/workflows/readme-linter.yml +++ b/.github/workflows/readme-linter.yml @@ -11,7 +11,7 @@ jobs: steps: - uses: actions/setup-go@v5 with: - go-version: '1.22.6' + go-version: '1.23.0' - uses: actions/checkout@v4 with: fetch-depth: 0 diff --git a/.golangci.yml b/.golangci.yml index 4222035b86a4b..d8235e494e556 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,7 +11,6 @@ linters: - errcheck - errname - errorlint - - exportloopref - gocheckcompilerdirectives - gocritic - goprintffuncname diff --git a/Makefile b/Makefile index b8997addd05f8..a494cfef9a67a 100644 --- a/Makefile +++ b/Makefile @@ -180,7 +180,7 @@ vet: .PHONY: lint-install lint-install: @echo "Installing golangci-lint" - go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.3 @echo "Installing markdownlint" npm install -g markdownlint-cli @@ -257,8 +257,8 @@ plugins/parsers/influx/machine.go: plugins/parsers/influx/machine.go.rl .PHONY: ci ci: - docker build -t quay.io/influxdb/telegraf-ci:1.22.6 - < scripts/ci.docker - docker push quay.io/influxdb/telegraf-ci:1.22.6 + docker build -t quay.io/influxdb/telegraf-ci:1.23.0 - < scripts/ci.docker + docker push quay.io/influxdb/telegraf-ci:1.23.0 .PHONY: install install: $(buildbin) diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 216a6bcf76248..c6fa580934018 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -419,7 +419,7 @@ func (t *Telegraf) runAgent(ctx context.Context, reloadConfig bool) error { log.Printf("I! Found %d secrets...", c.NumberSecrets) msg := fmt.Sprintf("Insufficient lockable memory %dkb when %dkb is required.", available, required) msg += " Please increase the limit for Telegraf in your Operating System!" - log.Printf("W! " + color.RedString(msg)) + log.Print("W! " + color.RedString(msg)) } } ag := agent.NewAgent(c) diff --git a/cmd/telegraf/telegraf_windows.go b/cmd/telegraf/telegraf_windows.go index fee2bb6961a13..54d5c61ad931f 100644 --- a/cmd/telegraf/telegraf_windows.go +++ b/cmd/telegraf/telegraf_windows.go @@ -21,11 +21,11 @@ import ( func getLockedMemoryLimit() uint64 { handle := windows.CurrentProcess() - var min, max uintptr + var low, high uintptr var flag uint32 - windows.GetProcessWorkingSetSizeEx(handle, &min, &max, &flag) + windows.GetProcessWorkingSetSizeEx(handle, &low, &high, &flag) - return uint64(max) + return uint64(high) } func (t *Telegraf) Run() error { diff --git a/config/config.go b/config/config.go index 28bba1eb6a53a..aad8af1e153b9 100644 --- a/config/config.go +++ b/config/config.go @@ -1585,7 +1585,7 @@ func (c *Config) setLocalMissingTomlFieldTracker(counter map[string]int) { // All other elements are subtables of their respective plugin and // should just be hit once anyway. Therefore, we mark them with a // high number to handle them correctly later. - pt := reflect.PtrTo(t) + pt := reflect.PointerTo(t) root := pt.Implements(reflect.TypeOf((*telegraf.Input)(nil)).Elem()) root = root || pt.Implements(reflect.TypeOf((*telegraf.ServiceInput)(nil)).Elem()) root = root || pt.Implements(reflect.TypeOf((*telegraf.Output)(nil)).Elem()) diff --git a/go.mod b/go.mod index b3dd8d850d1ad..4ec0c0c6dc4ac 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/influxdata/telegraf -go 1.22.0 +go 1.23.0 require ( cloud.google.com/go/bigquery v1.62.0 diff --git a/internal/globpath/globpath_test.go b/internal/globpath/globpath_test.go index 0f5408095e844..6c629132489fa 100644 --- a/internal/globpath/globpath_test.go +++ b/internal/globpath/globpath_test.go @@ -85,10 +85,6 @@ func TestFindNestedTextFile(t *testing.T) { } func TestMatch_ErrPermission(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("Skipping Unix only test") - } - tests := []struct { input string expected []string @@ -106,6 +102,7 @@ func TestMatch_ErrPermission(t *testing.T) { } func TestWindowsSeparator(t *testing.T) { + //nolint:staticcheck // Silence linter for now as we plan to reenable tests for Windows later if runtime.GOOS != "windows" { t.Skip("Skipping Windows only test") } diff --git a/internal/internal.go b/internal/internal.go index 2fc0f646468a3..ac966413d231b 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -123,8 +123,8 @@ func SnakeCase(in string) string { // RandomSleep will sleep for a random amount of time up to max. // If the shutdown channel is closed, it will return before it has finished sleeping. -func RandomSleep(max time.Duration, shutdown chan struct{}) { - sleepDuration := RandomDuration(max) +func RandomSleep(limit time.Duration, shutdown chan struct{}) { + sleepDuration := RandomDuration(limit) if sleepDuration == 0 { return } @@ -140,12 +140,12 @@ func RandomSleep(max time.Duration, shutdown chan struct{}) { } // RandomDuration returns a random duration between 0 and max. -func RandomDuration(max time.Duration) time.Duration { - if max == 0 { +func RandomDuration(limit time.Duration) time.Duration { + if limit == 0 { return 0 } - return time.Duration(rand.Int63n(max.Nanoseconds())) //nolint:gosec // G404: not security critical + return time.Duration(rand.Int63n(limit.Nanoseconds())) //nolint:gosec // G404: not security critical } // SleepContext sleeps until the context is closed or the duration is reached. diff --git a/internal/internal_test.go b/internal/internal_test.go index 8f564815abf3f..f75f9b010d20b 100644 --- a/internal/internal_test.go +++ b/internal/internal_test.go @@ -234,7 +234,7 @@ func TestCompressWithGzipErrorPropagationCopy(t *testing.T) { rc := CompressWithGzip(r) n, err := io.Copy(io.Discard, rc) - require.Greater(t, n, int64(0)) + require.Positive(t, n) require.ErrorIs(t, err, expected) require.NoError(t, rc.Close()) } diff --git a/plugins/common/kafka/config_test.go b/plugins/common/kafka/config_test.go index 07989c45f54f0..20be607d8751e 100644 --- a/plugins/common/kafka/config_test.go +++ b/plugins/common/kafka/config_test.go @@ -9,13 +9,13 @@ import ( func TestBackoffFunc(t *testing.T) { b := 250 * time.Millisecond - max := 1100 * time.Millisecond + limit := 1100 * time.Millisecond - f := makeBackoffFunc(b, max) + f := makeBackoffFunc(b, limit) require.Equal(t, b, f(0, 0)) require.Equal(t, b*2, f(1, 0)) require.Equal(t, b*4, f(2, 0)) - require.Equal(t, max, f(3, 0)) // would be 2000 but that's greater than max + require.Equal(t, limit, f(3, 0)) // would be 2000 but that's greater than max f = makeBackoffFunc(b, 0) // max = 0 means no max require.Equal(t, b*8, f(3, 0)) // with no max, it's 2000 diff --git a/plugins/inputs/activemq/activemq_test.go b/plugins/inputs/activemq/activemq_test.go index 29889748c7abb..bbef2c88f36e3 100644 --- a/plugins/inputs/activemq/activemq_test.go +++ b/plugins/inputs/activemq/activemq_test.go @@ -161,7 +161,7 @@ func TestURLs(t *testing.T) { require.NoError(t, err) default: w.WriteHeader(http.StatusNotFound) - t.Fatalf("unexpected path: " + r.URL.Path) + t.Fatalf("unexpected path: %s", r.URL.Path) } }) diff --git a/plugins/inputs/beanstalkd/beanstalkd.go b/plugins/inputs/beanstalkd/beanstalkd.go index fb7f1e066ba5b..2b3935d8fbeb6 100644 --- a/plugins/inputs/beanstalkd/beanstalkd.go +++ b/plugins/inputs/beanstalkd/beanstalkd.go @@ -165,6 +165,7 @@ func (b *Beanstalkd) gatherTubeStats(connection *textproto.Conn, tube string, ac } func runQuery(connection *textproto.Conn, cmd string, result interface{}) error { + //nolint:govet // Keep dynamic command as the passed string is constant requestID, err := connection.Cmd(cmd) if err != nil { return err diff --git a/plugins/inputs/cloudwatch_metric_streams/cloudwatch_metric_streams.go b/plugins/inputs/cloudwatch_metric_streams/cloudwatch_metric_streams.go index f770230592c39..aef1c8d98f828 100644 --- a/plugins/inputs/cloudwatch_metric_streams/cloudwatch_metric_streams.go +++ b/plugins/inputs/cloudwatch_metric_streams/cloudwatch_metric_streams.go @@ -314,21 +314,18 @@ func (cms *CloudWatchMetricStreams) composeMetrics(data Data) { // Rename Statistics to match the CloudWatch API if in API Compatability mode if cms.APICompatability { - max, ok := fields["max"] - if ok { - fields["maximum"] = max + if v, ok := fields["max"]; ok { + fields["maximum"] = v delete(fields, "max") } - min, ok := fields["min"] - if ok { - fields["minimum"] = min + if v, ok := fields["min"]; ok { + fields["minimum"] = v delete(fields, "min") } - count, ok := fields["count"] - if ok { - fields["samplecount"] = count + if v, ok := fields["count"]; ok { + fields["samplecount"] = v delete(fields, "count") } } diff --git a/plugins/inputs/conntrack/conntrack_test.go b/plugins/inputs/conntrack/conntrack_test.go index b23f9426a93f3..cc8832478a106 100644 --- a/plugins/inputs/conntrack/conntrack_test.go +++ b/plugins/inputs/conntrack/conntrack_test.go @@ -78,9 +78,9 @@ func TestConfigsUsed(t *testing.T) { dfltFiles = []string{cntFname, maxFname} count := 1234321 - max := 9999999 + limit := 9999999 require.NoError(t, os.WriteFile(cntFile.Name(), []byte(strconv.Itoa(count)), 0640)) - require.NoError(t, os.WriteFile(maxFile.Name(), []byte(strconv.Itoa(max)), 0640)) + require.NoError(t, os.WriteFile(maxFile.Name(), []byte(strconv.Itoa(limit)), 0640)) c := &Conntrack{} require.NoError(t, c.Init()) acc := &testutil.Accumulator{} @@ -94,7 +94,7 @@ func TestConfigsUsed(t *testing.T) { acc.AssertContainsFields(t, inputName, map[string]interface{}{ fix(cntFname): float64(count), - fix(maxFname): float64(max), + fix(maxFname): float64(limit), }) } diff --git a/plugins/inputs/directory_monitor/directory_monitor.go b/plugins/inputs/directory_monitor/directory_monitor.go index b6c80013501db..5df2bd44c801d 100644 --- a/plugins/inputs/directory_monitor/directory_monitor.go +++ b/plugins/inputs/directory_monitor/directory_monitor.go @@ -215,7 +215,7 @@ func (monitor *DirectoryMonitor) read(filePath string) { // Handle a file read error. We don't halt execution but do document, log, and move the problematic file. if err != nil { - monitor.Log.Errorf("Error while reading file: '" + filePath + "'. " + err.Error()) + monitor.Log.Errorf("Error while reading file: %q: %v", filePath, err) monitor.filesDropped.Incr(1) monitor.filesDroppedDir.Incr(1) if monitor.ErrorDirectory != "" { @@ -343,7 +343,7 @@ func (monitor *DirectoryMonitor) moveFile(srcPath string, dstBaseDir string) { dstPath := filepath.Join(dstBaseDir, basePath) err := os.MkdirAll(filepath.Dir(dstPath), 0750) if err != nil { - monitor.Log.Errorf("Error creating directory hierarchy for " + srcPath + ". Error: " + err.Error()) + monitor.Log.Errorf("Error creating directory hierarchy for %q: %v", srcPath, err) } inputFile, err := os.Open(srcPath) diff --git a/plugins/inputs/exec/exec_test.go b/plugins/inputs/exec/exec_test.go index e1595cc026ee3..fa752f26a3490 100644 --- a/plugins/inputs/exec/exec_test.go +++ b/plugins/inputs/exec/exec_test.go @@ -312,6 +312,7 @@ func TestTruncate(t *testing.T) { } func TestRemoveCarriageReturns(t *testing.T) { + //nolint:staticcheck // Silence linter for now as we plan to reenable tests for Windows later if runtime.GOOS == "windows" { // Test that all carriage returns are removed for _, test := range crTests { diff --git a/plugins/inputs/filecount/filecount_test.go b/plugins/inputs/filecount/filecount_test.go index 02739bb9b2fe8..ffe321751b7a1 100644 --- a/plugins/inputs/filecount/filecount_test.go +++ b/plugins/inputs/filecount/filecount_test.go @@ -198,6 +198,7 @@ func getTestdataDir() string { var chunks []string var testDirectory string + //nolint:staticcheck // Silence linter for now as we plan to reenable tests for Windows later if runtime.GOOS == "windows" { chunks = strings.Split(dir, "\\") testDirectory = strings.Join(chunks[:], "\\") + "\\testdata" diff --git a/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go b/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go index 1d42a7100569b..367e825eb8a0a 100644 --- a/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go +++ b/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go @@ -269,7 +269,7 @@ func startMultipleItemGCSServer(t *testing.T) *httptest.Server { require.NoError(t, err) } else { w.WriteHeader(http.StatusNotFound) - t.Fatalf("unexpected path: " + r.URL.Path) + t.Fatalf("unexpected path: %s", r.URL.Path) } default: @@ -399,7 +399,7 @@ func serveJSONText(w http.ResponseWriter, jsonText []byte) { func failPath(path string, t *testing.T, w http.ResponseWriter) { w.WriteHeader(http.StatusNotFound) - t.Fatalf("unexpected path: " + path) + t.Fatalf("unexpected path: %s", path) } func parseJSONFromFile(t *testing.T, jsonFilePath string) map[string]interface{} { diff --git a/plugins/inputs/http_response/http_response.go b/plugins/inputs/http_response/http_response.go index ee7416fc264ac..e8298f3664386 100644 --- a/plugins/inputs/http_response/http_response.go +++ b/plugins/inputs/http_response/http_response.go @@ -376,7 +376,7 @@ func (h *HTTPResponse) httpGather(cl client) (map[string]interface{}, map[string // Set result in case of a body read error func (h *HTTPResponse) setBodyReadError(errorMsg string, bodyBytes []byte, fields map[string]interface{}, tags map[string]string) { - h.Log.Debugf(errorMsg) + h.Log.Debug(errorMsg) setResult("body_read_error", fields, tags) fields["content_length"] = len(bodyBytes) if h.ResponseStringMatch != "" { diff --git a/plugins/inputs/intel_pmu/intel_pmu.go b/plugins/inputs/intel_pmu/intel_pmu.go index 0467092c718c5..9fcd33a8e18a3 100644 --- a/plugins/inputs/intel_pmu/intel_pmu.go +++ b/plugins/inputs/intel_pmu/intel_pmu.go @@ -348,11 +348,11 @@ func readMaxFD(reader fileInfoProvider) (uint64, error) { if err != nil { return 0, fmt.Errorf("cannot open file %q: %w", fileMaxPath, err) } - max, err := strconv.ParseUint(strings.Trim(string(buf), "\n "), 10, 64) + limit, err := strconv.ParseUint(strings.Trim(string(buf), "\n "), 10, 64) if err != nil { return 0, fmt.Errorf("cannot parse file content of %q: %w", fileMaxPath, err) } - return max, nil + return limit, nil } func checkFiles(paths []string, fileInfo fileInfoProvider) error { diff --git a/plugins/inputs/intel_pmu/resolver_test.go b/plugins/inputs/intel_pmu/resolver_test.go index e5363fa013096..ed2c69ed96513 100644 --- a/plugins/inputs/intel_pmu/resolver_test.go +++ b/plugins/inputs/intel_pmu/resolver_test.go @@ -145,43 +145,38 @@ func TestResolveEntities(t *testing.T) { t.Run("uncore event found in core entity", func(t *testing.T) { mQuals := []string{"config1=0x23h"} - mOptions, err := ia.NewOptions().SetAttrModifiers(mQuals).Build() - require.NoError(t, err) eventName := "uncore event 1" - testCase := test{event: &eventWithQuals{name: eventName, qualifiers: mQuals}, - options: mOptions, - perfEvent: &ia.PerfEvent{Name: eventName, Uncore: true}} + testCase := test{ + event: &eventWithQuals{name: eventName, qualifiers: mQuals}, + perfEvent: &ia.PerfEvent{Name: eventName, Uncore: true}, + } matcher := ia.NewNameMatcher(eventName) mTransformer.On("Transform", nil, matcher).Return([]*ia.PerfEvent{testCase.perfEvent}, nil).Once() mCoreEntity := &CoreEventEntity{parsedEvents: []*eventWithQuals{testCase.event}, allEvents: false} - err = mResolver.resolveEntities([]*CoreEventEntity{mCoreEntity}, nil) - - require.Error(t, err) - require.Contains(t, err.Error(), fmt.Sprintf("uncore event %q found in core entity", eventName)) + err := mResolver.resolveEntities([]*CoreEventEntity{mCoreEntity}, nil) + require.ErrorContains(t, err, fmt.Sprintf("uncore event %q found in core entity", eventName)) mTransformer.AssertExpectations(t) }) t.Run("core event found in uncore entity", func(t *testing.T) { mQuals := []string{"config1=0x23h"} - mOptions, err := ia.NewOptions().SetAttrModifiers(mQuals).Build() - require.NoError(t, err) eventName := "core event 1" - testCase := test{event: &eventWithQuals{name: eventName, qualifiers: mQuals}, - options: mOptions, - perfEvent: &ia.PerfEvent{Name: eventName, Uncore: false}} + testCase := test{ + event: &eventWithQuals{name: eventName, qualifiers: mQuals}, + perfEvent: &ia.PerfEvent{Name: eventName, Uncore: false}, + } matcher := ia.NewNameMatcher(eventName) mTransformer.On("Transform", nil, matcher).Return([]*ia.PerfEvent{testCase.perfEvent}, nil).Once() mUncoreEntity := &UncoreEventEntity{parsedEvents: []*eventWithQuals{testCase.event}, allEvents: false} - err = mResolver.resolveEntities(nil, []*UncoreEventEntity{mUncoreEntity}) + err := mResolver.resolveEntities(nil, []*UncoreEventEntity{mUncoreEntity}) - require.Error(t, err) - require.Contains(t, err.Error(), fmt.Sprintf("core event %q found in uncore entity", eventName)) + require.ErrorContains(t, err, fmt.Sprintf("core event %q found in uncore entity", eventName)) mTransformer.AssertExpectations(t) }) diff --git a/plugins/inputs/intel_powerstat/metrics.go b/plugins/inputs/intel_powerstat/metrics.go index 166459ee1322f..28c94bd9ecef2 100644 --- a/plugins/inputs/intel_powerstat/metrics.go +++ b/plugins/inputs/intel_powerstat/metrics.go @@ -294,7 +294,6 @@ type packageMetric[T numeric] struct { fetchFn func(packageID int) (T, error) } -//nolint:revive // Confusing-naming caused by a generic type that implements this interface method. func (m *packageMetric[T]) fields() (map[string]interface{}, error) { val, err := m.fetchFn(m.packageID) if err != nil { @@ -306,7 +305,6 @@ func (m *packageMetric[T]) fields() (map[string]interface{}, error) { }, nil } -//nolint:revive // Confusing-naming caused by a generic type that implements this interface method. func (m *packageMetric[T]) tags() map[string]string { return map[string]string{ "package_id": strconv.Itoa(m.packageID), diff --git a/plugins/inputs/intel_rdt/intel_rdt.go b/plugins/inputs/intel_rdt/intel_rdt.go index 01f5d5f08c013..94040fb6057b3 100644 --- a/plugins/inputs/intel_rdt/intel_rdt.go +++ b/plugins/inputs/intel_rdt/intel_rdt.go @@ -537,10 +537,10 @@ func checkForDuplicates(values []int, valuesToCheck []int) bool { return false } -func makeRange(min, max int) []int { - a := make([]int, max-min+1) +func makeRange(low, high int) []int { + a := make([]int, high-low+1) for i := range a { - a[i] = min + i + a[i] = low + i } return a } diff --git a/plugins/inputs/jolokia2_agent/jolokia2_agent_test.go b/plugins/inputs/jolokia2_agent/jolokia2_agent_test.go index ccb8c5d4046c7..632cb98fd9051 100644 --- a/plugins/inputs/jolokia2_agent/jolokia2_agent_test.go +++ b/plugins/inputs/jolokia2_agent/jolokia2_agent_test.go @@ -650,7 +650,7 @@ func TestJolokia2_ClientAuthRequest(t *testing.T) { require.EqualValuesf(t, "sally", username, "Expected to post with username %s, but was %s", "sally", username) require.EqualValuesf(t, "seashore", password, "Expected to post with password %s, but was %s", "seashore", password) - require.NotZero(t, len(requests), "Expected to post a request body, but was empty.") + require.NotEmpty(t, requests, "Expected to post a request body, but was empty.") request := requests[0]["mbean"] require.EqualValuesf(t, "hello:foo=bar", request, "Expected to query mbean %s, but was %s", "hello:foo=bar", request) diff --git a/plugins/inputs/jolokia2_proxy/jolokia2_proxy_test.go b/plugins/inputs/jolokia2_proxy/jolokia2_proxy_test.go index 6213c3e175581..bf7ebe787d3a4 100644 --- a/plugins/inputs/jolokia2_proxy/jolokia2_proxy_test.go +++ b/plugins/inputs/jolokia2_proxy/jolokia2_proxy_test.go @@ -113,7 +113,7 @@ func TestJolokia2_ClientProxyAuthRequest(t *testing.T) { require.NoError(t, plugin.Gather(&acc)) require.EqualValuesf(t, "sally", username, "Expected to post with username %s, but was %s", "sally", username) require.EqualValuesf(t, "seashore", password, "Expected to post with password %s, but was %s", "seashore", password) - require.NotZero(t, len(requests), "Expected to post a request body, but was empty.") + require.NotEmpty(t, requests, "Expected to post a request body, but was empty.") request := requests[0] expected := "hello:foo=bar" diff --git a/plugins/inputs/kafka_consumer/kafka_consumer_test.go b/plugins/inputs/kafka_consumer/kafka_consumer_test.go index 96aa736f17390..0b18dea497638 100644 --- a/plugins/inputs/kafka_consumer/kafka_consumer_test.go +++ b/plugins/inputs/kafka_consumer/kafka_consumer_test.go @@ -629,7 +629,7 @@ func TestExponentialBackoff(t *testing.T) { var err error backoff := 10 * time.Millisecond - max := 3 + limit := 3 // get an unused port by listening on next available port, then closing it listener, err := net.Listen("tcp", "127.0.0.1:0") @@ -650,7 +650,7 @@ func TestExponentialBackoff(t *testing.T) { ReadConfig: kafka.ReadConfig{ Config: kafka.Config{ - MetadataRetryMax: max, + MetadataRetryMax: limit, MetadataRetryBackoff: config.Duration(backoff), MetadataRetryType: "exponential", }, @@ -670,7 +670,7 @@ func TestExponentialBackoff(t *testing.T) { t.Logf("elapsed %d", elapsed) var expectedRetryDuration time.Duration - for i := 0; i < max; i++ { + for i := 0; i < limit; i++ { expectedRetryDuration += backoff * time.Duration(math.Pow(2, float64(i))) } t.Logf("expected > %d", expectedRetryDuration) diff --git a/plugins/inputs/net/net_test.go b/plugins/inputs/net/net_test.go index 19b60f480954a..7cdc9130e9b41 100644 --- a/plugins/inputs/net/net_test.go +++ b/plugins/inputs/net/net_test.go @@ -1,10 +1,12 @@ package net import ( - "os" "path/filepath" "testing" + "time" + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/inputs/system" "github.com/influxdata/telegraf/testutil" "github.com/shirou/gopsutil/v3/net" @@ -13,9 +15,7 @@ import ( func TestNetIOStats(t *testing.T) { var mps system.MockPS - var err error defer mps.AssertExpectations(t) - var acc testutil.Accumulator netio := net.IOCountersStat{ Name: "eth0", @@ -42,43 +42,47 @@ func TestNetIOStats(t *testing.T) { } mps.On("NetProto").Return(netprotos, nil) - require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys"))) - - err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc) - require.NoError(t, err) + t.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys")) - ntags := map[string]string{ - "interface": "eth0", - } + plugin := &NetIOStats{ps: &mps, skipChecks: true} - fields1 := map[string]interface{}{ - "bytes_sent": uint64(1123), - "bytes_recv": uint64(8734422), - "packets_sent": uint64(781), - "packets_recv": uint64(23456), - "err_in": uint64(832), - "err_out": uint64(8), - "drop_in": uint64(7), - "drop_out": uint64(1), - "speed": int64(100), - } - acc.AssertContainsTaggedFields(t, "net", fields1, ntags) - - fields2 := map[string]interface{}{ - "udp_noports": int64(892592), - "udp_indatagrams": int64(4655), - } - ntags = map[string]string{ - "interface": "all", + var acc testutil.Accumulator + require.NoError(t, plugin.Gather(&acc)) + + expected := []telegraf.Metric{ + metric.New( + "net", + map[string]string{"interface": "eth0"}, + map[string]interface{}{ + "bytes_sent": uint64(1123), + "bytes_recv": uint64(8734422), + "packets_sent": uint64(781), + "packets_recv": uint64(23456), + "err_in": uint64(832), + "err_out": uint64(8), + "drop_in": uint64(7), + "drop_out": uint64(1), + "speed": int64(100), + }, + time.Unix(0, 0), + telegraf.Counter, + ), + metric.New( + "net", + map[string]string{"interface": "all"}, + map[string]interface{}{ + "udp_noports": int64(892592), + "udp_indatagrams": int64(4655), + }, + time.Unix(0, 0), + ), } - acc.AssertContainsTaggedFields(t, "net", fields2, ntags) + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) } func TestNetIOStatsSpeedUnsupported(t *testing.T) { var mps system.MockPS - var err error defer mps.AssertExpectations(t) - var acc testutil.Accumulator netio := net.IOCountersStat{ Name: "eth1", @@ -105,43 +109,47 @@ func TestNetIOStatsSpeedUnsupported(t *testing.T) { } mps.On("NetProto").Return(netprotos, nil) - require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys"))) - - err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc) - require.NoError(t, err) + t.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys")) - ntags := map[string]string{ - "interface": "eth1", - } - - fields1 := map[string]interface{}{ - "bytes_sent": uint64(1123), - "bytes_recv": uint64(8734422), - "packets_sent": uint64(781), - "packets_recv": uint64(23456), - "err_in": uint64(832), - "err_out": uint64(8), - "drop_in": uint64(7), - "drop_out": uint64(1), - "speed": int64(-1), - } - acc.AssertContainsTaggedFields(t, "net", fields1, ntags) + plugin := &NetIOStats{ps: &mps, skipChecks: true} - fields2 := map[string]interface{}{ - "udp_noports": int64(892592), - "udp_indatagrams": int64(4655), - } - ntags = map[string]string{ - "interface": "all", + var acc testutil.Accumulator + require.NoError(t, plugin.Gather(&acc)) + + expected := []telegraf.Metric{ + metric.New( + "net", + map[string]string{"interface": "eth1"}, + map[string]interface{}{ + "bytes_sent": uint64(1123), + "bytes_recv": uint64(8734422), + "packets_sent": uint64(781), + "packets_recv": uint64(23456), + "err_in": uint64(832), + "err_out": uint64(8), + "drop_in": uint64(7), + "drop_out": uint64(1), + "speed": int64(-1), + }, + time.Unix(0, 0), + telegraf.Counter, + ), + metric.New( + "net", + map[string]string{"interface": "all"}, + map[string]interface{}{ + "udp_noports": int64(892592), + "udp_indatagrams": int64(4655), + }, + time.Unix(0, 0), + ), } - acc.AssertContainsTaggedFields(t, "net", fields2, ntags) + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) } func TestNetIOStatsNoSpeedFile(t *testing.T) { var mps system.MockPS - var err error defer mps.AssertExpectations(t) - var acc testutil.Accumulator netio := net.IOCountersStat{ Name: "eth2", @@ -168,34 +176,40 @@ func TestNetIOStatsNoSpeedFile(t *testing.T) { } mps.On("NetProto").Return(netprotos, nil) - require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys"))) - - err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc) - require.NoError(t, err) + t.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys")) - ntags := map[string]string{ - "interface": "eth2", - } + plugin := &NetIOStats{ps: &mps, skipChecks: true} - fields1 := map[string]interface{}{ - "bytes_sent": uint64(1123), - "bytes_recv": uint64(8734422), - "packets_sent": uint64(781), - "packets_recv": uint64(23456), - "err_in": uint64(832), - "err_out": uint64(8), - "drop_in": uint64(7), - "drop_out": uint64(1), - "speed": int64(-1), - } - acc.AssertContainsTaggedFields(t, "net", fields1, ntags) - - fields2 := map[string]interface{}{ - "udp_noports": int64(892592), - "udp_indatagrams": int64(4655), - } - ntags = map[string]string{ - "interface": "all", + var acc testutil.Accumulator + require.NoError(t, plugin.Gather(&acc)) + + expected := []telegraf.Metric{ + metric.New( + "net", + map[string]string{"interface": "eth2"}, + map[string]interface{}{ + "bytes_sent": uint64(1123), + "bytes_recv": uint64(8734422), + "packets_sent": uint64(781), + "packets_recv": uint64(23456), + "err_in": uint64(832), + "err_out": uint64(8), + "drop_in": uint64(7), + "drop_out": uint64(1), + "speed": int64(-1), + }, + time.Unix(0, 0), + telegraf.Counter, + ), + metric.New( + "net", + map[string]string{"interface": "all"}, + map[string]interface{}{ + "udp_noports": int64(892592), + "udp_indatagrams": int64(4655), + }, + time.Unix(0, 0), + ), } - acc.AssertContainsTaggedFields(t, "net", fields2, ntags) + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) } diff --git a/plugins/inputs/netflow/type_conversion.go b/plugins/inputs/netflow/type_conversion.go index 3e48495cc2062..2301a7e91170d 100644 --- a/plugins/inputs/netflow/type_conversion.go +++ b/plugins/inputs/netflow/type_conversion.go @@ -209,6 +209,8 @@ func decodeTCPFlags(b []byte) (interface{}, error) { results = append(results, ".") } } + + //nolint:gosec // False positive (b[1] is not out of range - it is ensured by above checks) return strings.Join(results, "") + mapTCPFlags(b[1]), nil } diff --git a/plugins/inputs/ping/ping_windows.go b/plugins/inputs/ping/ping_windows.go index 09c019a3e0cde..5ddce76ba194b 100644 --- a/plugins/inputs/ping/ping_windows.go +++ b/plugins/inputs/ping/ping_windows.go @@ -151,11 +151,11 @@ func processPingOutput(out string) (statistics, error) { if len(approxs) != 4 { return stats, err } - min, err := strconv.Atoi(approxs[1]) + low, err := strconv.Atoi(approxs[1]) if err != nil { return stats, err } - max, err := strconv.Atoi(approxs[2]) + high, err := strconv.Atoi(approxs[2]) if err != nil { return stats, err } @@ -165,8 +165,8 @@ func processPingOutput(out string) (statistics, error) { } stats.avg = avg - stats.min = min - stats.max = max + stats.min = low + stats.max = high return stats, err } diff --git a/plugins/inputs/processes/processes_test.go b/plugins/inputs/processes/processes_test.go index 2e4c1b0b67315..5844c16542ae6 100644 --- a/plugins/inputs/processes/processes_test.go +++ b/plugins/inputs/processes/processes_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" @@ -25,9 +26,7 @@ func TestProcesses(t *testing.T) { readProcFile: tester.testProcFile, } var acc testutil.Accumulator - - err := processes.Gather(&acc) - require.NoError(t, err) + require.NoError(t, processes.Gather(&acc)) require.True(t, acc.HasInt64Field("processes", "running")) require.True(t, acc.HasInt64Field("processes", "sleeping")) @@ -35,7 +34,7 @@ func TestProcesses(t *testing.T) { require.True(t, acc.HasInt64Field("processes", "total")) total, ok := acc.Get("processes") require.True(t, ok) - require.Greater(t, total.Fields["total"].(int64), int64(0)) + require.Positive(t, total.Fields["total"]) } func TestFromPS(t *testing.T) { @@ -46,8 +45,7 @@ func TestFromPS(t *testing.T) { } var acc testutil.Accumulator - err := processes.Gather(&acc) - require.NoError(t, err) + require.NoError(t, processes.Gather(&acc)) fields := getEmptyFields() fields["blocked"] = int64(3) @@ -68,8 +66,7 @@ func TestFromPSError(t *testing.T) { } var acc testutil.Accumulator - err := processes.Gather(&acc) - require.Error(t, err) + require.Error(t, processes.Gather(&acc)) } func TestFromProcFiles(t *testing.T) { @@ -84,8 +81,7 @@ func TestFromProcFiles(t *testing.T) { } var acc testutil.Accumulator - err := processes.Gather(&acc) - require.NoError(t, err) + require.NoError(t, processes.Gather(&acc)) fields := getEmptyFields() fields["sleeping"] = tester.calls @@ -107,8 +103,7 @@ func TestFromProcFilesWithSpaceInCmd(t *testing.T) { } var acc testutil.Accumulator - err := processes.Gather(&acc) - require.NoError(t, err) + require.NoError(t, processes.Gather(&acc)) fields := getEmptyFields() fields["sleeping"] = tester.calls @@ -141,8 +136,7 @@ func TestParkedProcess(t *testing.T) { } var acc testutil.Accumulator - err := plugin.Gather(&acc) - require.NoError(t, err) + require.NoError(t, plugin.Gather(&acc)) expected := []telegraf.Metric{ testutil.MustMetric( @@ -164,13 +158,12 @@ func TestParkedProcess(t *testing.T) { telegraf.Gauge, ), } - actual := acc.GetTelegrafMetrics() - for _, a := range actual { - a.RemoveField("total") - a.RemoveField("total_threads") + + options := []cmp.Option{ + testutil.IgnoreTime(), + testutil.IgnoreFields("total", "total_threads"), } - testutil.RequireMetricsEqual(t, expected, actual, - testutil.IgnoreTime()) + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), options...) } func testExecPS(out string) func(_ bool) ([]byte, error) { diff --git a/plugins/inputs/smart/smart.go b/plugins/inputs/smart/smart.go index e8d5bf53507c2..18d9fa856731b 100644 --- a/plugins/inputs/smart/smart.go +++ b/plugins/inputs/smart/smart.go @@ -995,12 +995,12 @@ func parseThermalThrottle(acc telegraf.Accumulator, fields map[string]interface{ } func parseWearLeveling(acc telegraf.Accumulator, fields map[string]interface{}, tags map[string]string, str string) error { - var min, max, avg int64 + var vmin, vmax, avg int64 - if _, err := fmt.Sscanf(str, "min: %d, max: %d, avg: %d", &min, &max, &avg); err != nil { + if _, err := fmt.Sscanf(str, "min: %d, max: %d, avg: %d", &vmin, &vmax, &avg); err != nil { return err } - values := []int64{min, max, avg} + values := []int64{vmin, vmax, avg} for i, submetricName := range []string{"Min", "Max", "Avg"} { fields["raw_value"] = values[i] tags["name"] = "Wear_Leveling_" + submetricName diff --git a/plugins/inputs/statsd/running_stats.go b/plugins/inputs/statsd/running_stats.go index f5bdeb7d609e5..1098754aa12d5 100644 --- a/plugins/inputs/statsd/running_stats.go +++ b/plugins/inputs/statsd/running_stats.go @@ -146,15 +146,5 @@ func (rs *RunningStats) Percentile(n float64) float64 { } i := float64(len(rs.perc)) * n / float64(100) - return rs.perc[clamp(i, 0, len(rs.perc)-1)] -} - -func clamp(i float64, min int, max int) int { - if i < float64(min) { - return min - } - if i > float64(max) { - return max - } - return int(i) + return rs.perc[max(0, min(int(i), len(rs.perc)-1))] } diff --git a/plugins/inputs/tacacs/tacacs_test.go b/plugins/inputs/tacacs/tacacs_test.go index 9b808fdd065d1..e5eccc84b28df 100644 --- a/plugins/inputs/tacacs/tacacs_test.go +++ b/plugins/inputs/tacacs/tacacs_test.go @@ -6,11 +6,14 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" "github.com/nwaples/tacplus" "github.com/stretchr/testify/require" "github.com/testcontainers/testcontainers-go/wait" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" + "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/testutil" ) @@ -49,7 +52,7 @@ func (t testRequestHandler) HandleAuthorRequest(_ context.Context, a *tacplus.Au return &tacplus.AuthorResponse{Status: tacplus.AuthorStatusFail} } -func (t testRequestHandler) HandleAcctRequest(_ context.Context, _ *tacplus.AcctRequest, _ *tacplus.ServerSession) *tacplus.AcctReply { +func (testRequestHandler) HandleAcctRequest(context.Context, *tacplus.AcctRequest, *tacplus.ServerSession) *tacplus.AcctReply { return &tacplus.AcctReply{Status: tacplus.AcctStatusSuccess} } @@ -105,7 +108,6 @@ func TestTacacsInit(t *testing.T) { } err := plugin.Init() - if tt.errContains == "" { require.NoError(t, err) if tt.requestAddr == "" { @@ -145,8 +147,9 @@ func TestTacacsLocal(t *testing.T) { } go func() { - err = srv.Serve(l) - require.NoError(t, err, "local srv.Serve failed to start serving on "+srvLocal) + if err := srv.Serve(l); err != nil { + t.Logf("local srv.Serve failed to start serving on %s", srvLocal) + } }() var testset = []struct { @@ -190,16 +193,6 @@ func TestTacacsLocal(t *testing.T) { requestAddr: "127.0.0.1", errContains: "error on new tacacs authentication start request to " + srvLocal + " : bad secret or packet", }, - { - name: "unreachable", - testingTimeout: config.Duration(time.Nanosecond * 1000), - serverToTest: []string{"unreachable.test:49"}, - usedUsername: config.NewSecret([]byte(`testusername`)), - usedPassword: config.NewSecret([]byte(`testpassword`)), - usedSecret: config.NewSecret([]byte(`testsecret`)), - requestAddr: "127.0.0.1", - errContains: "error on new tacacs authentication start request to unreachable.test:49 : dial tcp", - }, } for _, tt := range testset { @@ -221,23 +214,97 @@ func TestTacacsLocal(t *testing.T) { if tt.errContains == "" { require.Empty(t, acc.Errors) - require.True(t, acc.HasMeasurement("tacacs")) - require.True(t, acc.HasTag("tacacs", "source")) - require.Equal(t, srvLocal, acc.TagValue("tacacs", "source")) - require.True(t, acc.HasInt64Field("tacacs", "responsetime_ms")) - require.True(t, acc.HasStringField("tacacs", "response_status")) - require.Equal(t, tt.reqRespStatus, acc.Metrics[0].Fields["response_status"]) + expected := []telegraf.Metric{ + metric.New( + "tacacs", + map[string]string{"source": srvLocal}, + map[string]interface{}{ + "responsetime_ms": int64(0), + "response_status": tt.reqRespStatus, + }, + time.Unix(0, 0), + ), + } + options := []cmp.Option{ + testutil.IgnoreTime(), + testutil.IgnoreFields("responsetime_ms"), + } + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), options...) } else { require.Len(t, acc.Errors, 1) require.ErrorContains(t, acc.FirstError(), tt.errContains) - require.False(t, acc.HasTag("tacacs", "source")) - require.False(t, acc.HasInt64Field("tacacs", "responsetime_ms")) - require.False(t, acc.HasStringField("tacacs", "response_status")) + require.Empty(t, acc.GetTelegrafMetrics()) } }) } } +func TestTacacsLocalTimeout(t *testing.T) { + testHandler := tacplus.ServerConnHandler{ + Handler: &testRequestHandler{ + "testusername": { + password: "testpassword", + }, + }, + ConnConfig: tacplus.ConnConfig{ + Secret: []byte(`testsecret`), + Mux: true, + }, + } + l, err := net.Listen("tcp", "localhost:0") + require.NoError(t, err, "local net listen failed to start listening") + + srvLocal := l.Addr().String() + + srv := &tacplus.Server{ + ServeConn: func(nc net.Conn) { + testHandler.Serve(nc) + }, + } + + go func() { + if err := srv.Serve(l); err != nil { + t.Logf("local srv.Serve failed to start serving on %s", srvLocal) + } + }() + + // Initialize the plugin + plugin := &Tacacs{ + ResponseTimeout: config.Duration(time.Microsecond), + Servers: []string{"unreachable.test:49"}, + Username: config.NewSecret([]byte(`testusername`)), + Password: config.NewSecret([]byte(`testpassword`)), + Secret: config.NewSecret([]byte(`testsecret`)), + RequestAddr: "127.0.0.1", + Log: &testutil.Logger{}, + } + require.NoError(t, plugin.Init()) + + // Try to connect, this will return a metric with the timeout... + var acc testutil.Accumulator + require.NoError(t, plugin.Gather(&acc)) + + expected := []telegraf.Metric{ + metric.New( + "tacacs", + map[string]string{"source": "unreachable.test:49"}, + map[string]interface{}{ + "response_status": string("Timeout"), + "responsetime_ms": int64(0), + }, + time.Unix(0, 0), + ), + } + + options := []cmp.Option{ + testutil.IgnoreTime(), + testutil.IgnoreFields("responsetime_ms"), + } + + require.Empty(t, acc.Errors) + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), options...) +} + func TestTacacsIntegration(t *testing.T) { if testing.Short() { t.Skip("Skipping integration test in short mode") @@ -250,8 +317,7 @@ func TestTacacsIntegration(t *testing.T) { wait.ForLog("Starting server..."), ), } - err := container.Start() - require.NoError(t, err, "failed to start container") + require.NoError(t, container.Start(), "failed to start container") defer container.Terminate() port := container.Ports["49"] @@ -302,14 +368,22 @@ func TestTacacsIntegration(t *testing.T) { require.NoError(t, plugin.Gather(&acc)) require.NoError(t, acc.FirstError()) - - require.True(t, acc.HasMeasurement("tacacs")) - require.True(t, acc.HasStringField("tacacs", "response_status")) - require.True(t, acc.HasInt64Field("tacacs", "responsetime_ms")) - require.True(t, acc.HasTag("tacacs", "source")) - - require.Equal(t, tt.reqRespStatus, acc.Metrics[0].Fields["response_status"]) - require.Equal(t, container.Address+":"+port, acc.TagValue("tacacs", "source")) + expected := []telegraf.Metric{ + metric.New( + "tacacs", + map[string]string{"source": container.Address + ":" + port}, + map[string]interface{}{ + "responsetime_ms": int64(0), + "response_status": tt.reqRespStatus, + }, + time.Unix(0, 0), + ), + } + options := []cmp.Option{ + testutil.IgnoreTime(), + testutil.IgnoreFields("responsetime_ms"), + } + testutil.RequireMetricsStructureEqual(t, expected, acc.GetTelegrafMetrics(), options...) }) } } diff --git a/plugins/inputs/temp/temp_test.go b/plugins/inputs/temp/temp_test.go index 654651eae765c..3f24f75603779 100644 --- a/plugins/inputs/temp/temp_test.go +++ b/plugins/inputs/temp/temp_test.go @@ -38,7 +38,8 @@ func TestInvalidMetricFormat(t *testing.T) { } func TestNameCollisions(t *testing.T) { - require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testcases", "with_name", "sys"))) + t.Setenv("HOST_SYS", filepath.Join("testcases", "with_name", "sys")) + plugin := &Temperature{Log: &testutil.Logger{}} require.NoError(t, plugin.Init()) @@ -87,7 +88,7 @@ func TestCases(t *testing.T) { } // Prepare the environment - require.NoError(t, os.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys"))) + t.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys")) // Configure the plugin cfg := config.NewConfig() @@ -120,7 +121,7 @@ func TestCases(t *testing.T) { } // Prepare the environment - require.NoError(t, os.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys"))) + t.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys")) // Configure the plugin cfg := config.NewConfig() @@ -221,7 +222,7 @@ func TestRegression(t *testing.T) { } // Prepare the environment - require.NoError(t, os.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys"))) + t.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys")) // Use the v1.28.x code to compare against var acc testutil.Accumulator diff --git a/plugins/inputs/win_perf_counters/win_perf_counters_integration_test.go b/plugins/inputs/win_perf_counters/win_perf_counters_integration_test.go index 7ad2c23509f46..9f3dd308c2582 100644 --- a/plugins/inputs/win_perf_counters/win_perf_counters_integration_test.go +++ b/plugins/inputs/win_perf_counters/win_perf_counters_integration_test.go @@ -580,7 +580,7 @@ func TestWinPerfCountersCollectRawIntegration(t *testing.T) { require.True(t, ok, "Expected presence of %s field", expectedCounter) valInt64, ok := val.(int64) require.Truef(t, ok, "Expected int64, got %T", val) - require.Greaterf(t, valInt64, int64(0), "Expected > 0, got %d, for %#v", valInt64, metric) + require.Positivef(t, valInt64, "Value not positive for metric %#v", metric) } // Test *Array way @@ -604,6 +604,6 @@ func TestWinPerfCountersCollectRawIntegration(t *testing.T) { require.True(t, ok, "Expected presence of %s field", expectedCounter) valInt64, ok := val.(int64) require.Truef(t, ok, "Expected int64, got %T", val) - require.Greaterf(t, valInt64, int64(0), "Expected > 0, got %d, for %#v", valInt64, metric) + require.Positivef(t, valInt64, "Value not positive for metric %#v", metric) } } diff --git a/plugins/inputs/zipkin/codec/codec.go b/plugins/inputs/zipkin/codec/codec.go index 9e0f7237c8bef..3266374bb8ca6 100644 --- a/plugins/inputs/zipkin/codec/codec.go +++ b/plugins/inputs/zipkin/codec/codec.go @@ -134,21 +134,21 @@ func NewBinaryAnnotations(annotations []BinaryAnnotation, endpoint Endpoint) []t } func minMax(span Span) (time.Time, time.Time) { - min := now().UTC() - max := time.Time{}.UTC() + low := now().UTC() + high := time.Time{}.UTC() for _, annotation := range span.Annotations() { ts := annotation.Timestamp() - if !ts.IsZero() && ts.Before(min) { - min = ts + if !ts.IsZero() && ts.Before(low) { + low = ts } - if !ts.IsZero() && ts.After(max) { - max = ts + if !ts.IsZero() && ts.After(high) { + high = ts } } - if max.IsZero() { - max = min + if high.IsZero() { + high = low } - return min, max + return low, high } func guessTimestamp(span Span) time.Time { @@ -157,8 +157,8 @@ func guessTimestamp(span Span) time.Time { return ts } - min, _ := minMax(span) - return min + low, _ := minMax(span) + return low } func convertDuration(span Span) time.Duration { @@ -166,8 +166,8 @@ func convertDuration(span Span) time.Duration { if duration != 0 { return duration } - min, max := minMax(span) - return max.Sub(min) + low, high := minMax(span) + return high.Sub(low) } func parentID(span Span) (string, error) { diff --git a/plugins/outputs/azure_monitor/azure_monitor.go b/plugins/outputs/azure_monitor/azure_monitor.go index 9060f4f2355e2..743a7fba2566f 100644 --- a/plugins/outputs/azure_monitor/azure_monitor.go +++ b/plugins/outputs/azure_monitor/azure_monitor.go @@ -380,19 +380,19 @@ func translate(m telegraf.Metric, prefix string) (*azureMonitorMetric, error) { dimensionValues = append(dimensionValues, tag.Value) } - min, err := getFloatField(m, "min") + vmin, err := getFloatField(m, "min") if err != nil { return nil, err } - max, err := getFloatField(m, "max") + vmax, err := getFloatField(m, "max") if err != nil { return nil, err } - sum, err := getFloatField(m, "sum") + vsum, err := getFloatField(m, "sum") if err != nil { return nil, err } - count, err := getIntField(m, "count") + vcount, err := getIntField(m, "count") if err != nil { return nil, err } @@ -417,10 +417,10 @@ func translate(m telegraf.Metric, prefix string) (*azureMonitorMetric, error) { Series: []*azureMonitorSeries{ { DimensionValues: dimensionValues, - Min: min, - Max: max, - Sum: sum, - Count: count, + Min: vmin, + Max: vmax, + Sum: vsum, + Count: vcount, }, }, }, diff --git a/plugins/outputs/cloudwatch/cloudwatch.go b/plugins/outputs/cloudwatch/cloudwatch.go index 23bff0a2487d5..077ac70d92bcb 100644 --- a/plugins/outputs/cloudwatch/cloudwatch.go +++ b/plugins/outputs/cloudwatch/cloudwatch.go @@ -69,20 +69,20 @@ func (f *statisticField) buildDatum() []types.MetricDatum { if f.hasAllFields() { // If we have all required fields, we build datum with StatisticValues - min := f.values[statisticTypeMin] - max := f.values[statisticTypeMax] - sum := f.values[statisticTypeSum] - count := f.values[statisticTypeCount] + vmin := f.values[statisticTypeMin] + vmax := f.values[statisticTypeMax] + vsum := f.values[statisticTypeSum] + vcount := f.values[statisticTypeCount] datum := types.MetricDatum{ MetricName: aws.String(strings.Join([]string{f.metricName, f.fieldName}, "_")), Dimensions: BuildDimensions(f.tags), Timestamp: aws.Time(f.timestamp), StatisticValues: &types.StatisticSet{ - Minimum: aws.Float64(min), - Maximum: aws.Float64(max), - Sum: aws.Float64(sum), - SampleCount: aws.Float64(count), + Minimum: aws.Float64(vmin), + Maximum: aws.Float64(vmax), + Sum: aws.Float64(vsum), + SampleCount: aws.Float64(vcount), }, StorageResolution: aws.Int32(int32(f.storageResolution)), } diff --git a/plugins/outputs/sql/sqlite_test.go b/plugins/outputs/sql/sqlite_test.go index ec227fbec3808..b3cad593157ab 100644 --- a/plugins/outputs/sql/sqlite_test.go +++ b/plugins/outputs/sql/sqlite_test.go @@ -15,7 +15,7 @@ import ( ) func TestSqlite(t *testing.T) { - dbfile := filepath.Join(os.TempDir(), "db") + dbfile := filepath.Join(t.TempDir(), "db") defer os.Remove(dbfile) // Use the plugin to write to the database address := @@ -27,9 +27,8 @@ func TestSqlite(t *testing.T) { p.DataSourceName = address require.NoError(t, p.Connect()) - require.NoError(t, p.Write( - testMetrics, - )) + defer p.Close() + require.NoError(t, p.Write(testMetrics)) //read directly from the database db, err := gosql.Open("sqlite", address) @@ -41,7 +40,7 @@ func TestSqlite(t *testing.T) { require.Equal(t, 1, countMetricOne) var countMetricTwo int - require.NoError(t, db.QueryRow("select count(*) from metric_one").Scan(&countMetricTwo)) + require.NoError(t, db.QueryRow("select count(*) from metric_two").Scan(&countMetricTwo)) require.Equal(t, 1, countMetricTwo) var rows *gosql.Rows diff --git a/plugins/parsers/nagios/parser.go b/plugins/parsers/nagios/parser.go index 6a5847d3d6f56..7472deed50c04 100644 --- a/plugins/parsers/nagios/parser.go +++ b/plugins/parsers/nagios/parser.go @@ -278,30 +278,30 @@ const ( var ErrBadThresholdFormat = errors.New("bad threshold format") // Handles all cases from https://nagios-plugins.org/doc/guidelines.html#THRESHOLDFORMAT -func parseThreshold(threshold string) (min float64, max float64, err error) { +func parseThreshold(threshold string) (vmin, vmax float64, err error) { thresh := strings.Split(threshold, ":") switch len(thresh) { case 1: - max, err = strconv.ParseFloat(thresh[0], 64) + vmax, err = strconv.ParseFloat(thresh[0], 64) if err != nil { return 0, 0, ErrBadThresholdFormat } - return 0, max, nil + return 0, vmax, nil case 2: if thresh[0] == "~" { - min = MinFloat64 + vmin = MinFloat64 } else { - min, err = strconv.ParseFloat(thresh[0], 64) + vmin, err = strconv.ParseFloat(thresh[0], 64) if err != nil { - min = 0 + vmin = 0 } } if thresh[1] == "" { - max = MaxFloat64 + vmax = MaxFloat64 } else { - max, err = strconv.ParseFloat(thresh[1], 64) + vmax, err = strconv.ParseFloat(thresh[1], 64) if err != nil { return 0, 0, ErrBadThresholdFormat } @@ -310,7 +310,7 @@ func parseThreshold(threshold string) (min float64, max float64, err error) { return 0, 0, ErrBadThresholdFormat } - return min, max, err + return vmin, vmax, err } func init() { diff --git a/plugins/parsers/nagios/parser_test.go b/plugins/parsers/nagios/parser_test.go index 31f2b1b156cbc..c7a111c0c4f41 100644 --- a/plugins/parsers/nagios/parser_test.go +++ b/plugins/parsers/nagios/parser_test.go @@ -518,9 +518,9 @@ func TestParseThreshold(t *testing.T) { } for i := range tests { - min, max, err := parseThreshold(tests[i].input) - require.InDelta(t, tests[i].eMin, min, testutil.DefaultDelta) - require.InDelta(t, tests[i].eMax, max, testutil.DefaultDelta) + vmin, vmax, err := parseThreshold(tests[i].input) + require.InDelta(t, tests[i].eMin, vmin, testutil.DefaultDelta) + require.InDelta(t, tests[i].eMax, vmax, testutil.DefaultDelta) require.Equal(t, tests[i].eErr, err) } } diff --git a/plugins/processors/port_name/port_name_test.go b/plugins/processors/port_name/port_name_test.go index dabade5d81209..fae74010152f2 100644 --- a/plugins/processors/port_name/port_name_test.go +++ b/plugins/processors/port_name/port_name_test.go @@ -19,7 +19,7 @@ tftp 69/udp` func TestReadServicesFile(t *testing.T) { readServicesFile() - require.NotZero(t, len(services)) + require.NotEmpty(t, services) } func TestFakeServices(t *testing.T) { diff --git a/plugins/processors/starlark/starlark_test.go b/plugins/processors/starlark/starlark_test.go index 78e412d453e6b..a93f71d066d1a 100644 --- a/plugins/processors/starlark/starlark_test.go +++ b/plugins/processors/starlark/starlark_test.go @@ -3755,7 +3755,7 @@ func parseMetricsFrom(t *testing.T, lines []string, header string) (metrics []te parser := &influx.Parser{} require.NoError(t, parser.Init()) - require.NotZero(t, len(lines), "Expected some lines to parse from .star file, found none") + require.NotEmpty(t, lines, "Expected some lines to parse from .star file, found none") startIdx := -1 endIdx := len(lines) for i := range lines { @@ -3782,7 +3782,7 @@ func parseMetricsFrom(t *testing.T, lines []string, header string) (metrics []te // parses error message out of line protocol following a header func parseErrorMessage(t *testing.T, lines []string, header string) string { - require.NotZero(t, len(lines), "Expected some lines to parse from .star file, found none") + require.NotEmpty(t, lines, "Expected some lines to parse from .star file, found none") startIdx := -1 for i := range lines { if strings.TrimLeft(lines[i], "# ") == header { diff --git a/plugins/processors/topk/topk.go b/plugins/processors/topk/topk.go index 9e57ed383c4f7..61f756d2b9994 100644 --- a/plugins/processors/topk/topk.go +++ b/plugins/processors/topk/topk.go @@ -299,7 +299,7 @@ func (t *TopK) getAggregationFunction(aggOperation string) (func([]telegraf.Metr case "min": return func(ms []telegraf.Metric, fields []string) map[string]float64 { - min := func(agg map[string]float64, val float64, field string) { + vmin := func(agg map[string]float64, val float64, field string) { // If this field has not been set, set it to the maximum float64 _, ok := agg[field] if !ok { @@ -311,12 +311,12 @@ func (t *TopK) getAggregationFunction(aggOperation string) (func([]telegraf.Metr agg[field] = val } } - return aggregator(ms, fields, min) + return aggregator(ms, fields, vmin) }, nil case "max": return func(ms []telegraf.Metric, fields []string) map[string]float64 { - max := func(agg map[string]float64, val float64, field string) { + vmax := func(agg map[string]float64, val float64, field string) { // If this field has not been set, set it to the minimum float64 _, ok := agg[field] if !ok { @@ -328,7 +328,7 @@ func (t *TopK) getAggregationFunction(aggOperation string) (func([]telegraf.Metr agg[field] = val } } - return aggregator(ms, fields, max) + return aggregator(ms, fields, vmax) }, nil case "mean": diff --git a/scripts/ci.docker b/scripts/ci.docker index b3cf819f4fe70..9f4a6a8426831 100644 --- a/scripts/ci.docker +++ b/scripts/ci.docker @@ -1,4 +1,4 @@ -FROM golang:1.22.6 +FROM golang:1.23.0 RUN chmod -R 755 "$GOPATH" diff --git a/scripts/installgo_linux.sh b/scripts/installgo_linux.sh index 2cdfff1da1aac..d62b78b2312e7 100644 --- a/scripts/installgo_linux.sh +++ b/scripts/installgo_linux.sh @@ -2,10 +2,10 @@ set -eux -GO_VERSION="1.22.6" +GO_VERSION="1.23.0" GO_ARCH="linux-amd64" # from https://golang.org/dl -GO_VERSION_SHA="999805bed7d9039ec3da1a53bfbcafc13e367da52aa823cb60b68ba22d44c616" +GO_VERSION_SHA="905a297f19ead44780548933e0ff1a1b86e8327bb459e92f9c0012569f76f5e3" # Download Go and verify Go tarball setup_go () { diff --git a/scripts/installgo_mac.sh b/scripts/installgo_mac.sh index 01ef60790caea..2b5893c8cb51e 100644 --- a/scripts/installgo_mac.sh +++ b/scripts/installgo_mac.sh @@ -3,9 +3,9 @@ set -eux ARCH=$(uname -m) -GO_VERSION="1.22.6" -GO_VERSION_SHA_arm64="ebac39fd44fc22feed1bb519af431c84c55776e39b30f4fd62930da9c0cfd1e3" # from https://golang.org/dl -GO_VERSION_SHA_amd64="9c3c0124b01b5365f73a1489649f78f971ecf84844ad9ca58fde133096ddb61b" # from https://golang.org/dl +GO_VERSION="1.23.0" +GO_VERSION_SHA_arm64="b770812aef17d7b2ea406588e2b97689e9557aac7e646fe76218b216e2c51406" # from https://golang.org/dl +GO_VERSION_SHA_amd64="ffd070acf59f054e8691b838f274d540572db0bd09654af851e4e76ab88403dc" # from https://golang.org/dl if [ "$ARCH" = 'arm64' ]; then GO_ARCH="darwin-arm64" diff --git a/scripts/installgo_windows.sh b/scripts/installgo_windows.sh index 1ceab7ac60d36..107e9ecaf81cb 100644 --- a/scripts/installgo_windows.sh +++ b/scripts/installgo_windows.sh @@ -2,7 +2,7 @@ set -eux -GO_VERSION="1.22.6" +GO_VERSION="1.23.0" setup_go () { choco upgrade golang --allow-downgrade --version=${GO_VERSION} diff --git a/tools/custom_builder/packages.go b/tools/custom_builder/packages.go index f167432618517..bcf7cca2140ec 100644 --- a/tools/custom_builder/packages.go +++ b/tools/custom_builder/packages.go @@ -255,6 +255,7 @@ func extractPluginInfo(file *ast.File, pluginType string, declarations map[strin return registeredNames, nil } +//nolint:staticcheck // Use deprecated ast.Package for now func extractPackageDeclarations(pkg *ast.Package) map[string]string { declarations := make(map[string]string) @@ -286,6 +287,7 @@ func extractPackageDeclarations(pkg *ast.Package) map[string]string { return declarations } +//nolint:staticcheck // Use deprecated ast.Package for now func extractRegisteredNames(pkg *ast.Package, pluginType string) []string { var registeredNames []string diff --git a/tools/readme_linter/rules.go b/tools/readme_linter/rules.go index 8b2017603787b..a1781f3823946 100644 --- a/tools/readme_linter/rules.go +++ b/tools/readme_linter/rules.go @@ -32,9 +32,8 @@ func firstSection(t *T, root ast.Node) error { n = n.NextSibling() t.assertKind(ast.KindParagraph, n) length := len(n.Text(t.markdown)) - min := 30 - if length < min { - t.assertNodef(n, "short first section. Please add short description of plugin. length %d, minimum %d", length, min) + if length < 30 { + t.assertNodef(n, "short first section. Please add short description of plugin. length %d, minimum 30", length) } return nil