diff --git a/.golangci.yml b/.golangci.yml index ccd76566e..045c43045 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -27,6 +27,7 @@ linters: - revive - sqlclosecheck - staticcheck + - tenv - tparallel - typecheck - unconvert @@ -149,6 +150,11 @@ linters-settings: nakedret: # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 max-func-lines: 1 + tenv: + # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. + # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. + # Default: false + all: true run: # timeout for analysis, e.g. 30s, 5m, default is 1m diff --git a/config/config_test.go b/config/config_test.go index 7a262f166..4f76d368a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -480,8 +480,7 @@ func TestConfig_getDefaultConfigPathFromEnvURL(t *testing.T) { defer ts.Close() c := NewConfig() - err := os.Setenv("TELEGRAF_CONFIG_PATH", ts.URL) - require.NoError(t, err) + t.Setenv("TELEGRAF_CONFIG_PATH", ts.URL) configPath, err := getDefaultConfigPath() require.NoError(t, err) require.Equal(t, []string{ts.URL}, configPath) diff --git a/plugins/common/shim/config_test.go b/plugins/common/shim/config_test.go index 69c18394a..6bb807496 100644 --- a/plugins/common/shim/config_test.go +++ b/plugins/common/shim/config_test.go @@ -1,7 +1,6 @@ package shim import ( - "os" "testing" "time" @@ -14,10 +13,8 @@ import ( ) func TestLoadConfig(t *testing.T) { - err := os.Setenv("SECRET_TOKEN", "xxxxxxxxxx") - require.NoError(t, err) - err = os.Setenv("SECRET_VALUE", `test"\test`) - require.NoError(t, err) + t.Setenv("SECRET_TOKEN", "xxxxxxxxxx") + t.Setenv("SECRET_VALUE", `test"\test`) inputs.Add("test", func() telegraf.Input { return &serviceInput{} 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 0740793b4..ced2274fa 100644 --- a/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go +++ b/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go @@ -416,7 +416,5 @@ func readJSON(t *testing.T, jsonFilePath string) []byte { } func emulatorSetEnv(t *testing.T, srv *httptest.Server) { - if err := os.Setenv("STORAGE_EMULATOR_HOST", strings.ReplaceAll(srv.URL, "http://", "")); err != nil { - t.Error(err) - } + t.Setenv("STORAGE_EMULATOR_HOST", strings.ReplaceAll(srv.URL, "http://", "")) } diff --git a/plugins/outputs/azure_monitor/azure_monitor_test.go b/plugins/outputs/azure_monitor/azure_monitor_test.go index 9645bd485..139a07893 100644 --- a/plugins/outputs/azure_monitor/azure_monitor_test.go +++ b/plugins/outputs/azure_monitor/azure_monitor_test.go @@ -6,15 +6,15 @@ import ( "encoding/json" "net/http" "net/http/httptest" - "os" "testing" "time" "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/adal" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/require" ) func TestAggregate(t *testing.T) { @@ -214,7 +214,7 @@ func TestAggregate(t *testing.T) { msiEndpoint, err := adal.GetMSIVMEndpoint() require.NoError(t, err) - os.Setenv("MSI_ENDPOINT", msiEndpoint) + t.Setenv("MSI_ENDPOINT", msiEndpoint) err = tt.plugin.Connect() require.NoError(t, err) @@ -236,6 +236,13 @@ func TestAggregate(t *testing.T) { } func TestWrite(t *testing.T) { + // Set up a fake environment for Authorizer + // This used to fake an MSI environment, but since https://github.com/Azure/go-autorest/pull/670/files it's no longer possible, + // So we fake a user/password authentication + t.Setenv("AZURE_CLIENT_ID", "fake") + t.Setenv("AZURE_USERNAME", "fake") + t.Setenv("AZURE_PASSWORD", "fake") + readBody := func(r *http.Request) ([]*azureMonitorMetric, error) { gz, err := gzip.NewReader(r.Body) if err != nil { @@ -373,23 +380,3 @@ func TestWrite(t *testing.T) { }) } } - -func TestMain(m *testing.M) { - // Set up a fake environment for Authorizer - // This used to fake an MSI environment, but since https://github.com/Azure/go-autorest/pull/670/files it's no longer possible, - // So we fake a user/password authentication - err := os.Setenv("AZURE_CLIENT_ID", "fake") - if err != nil { - panic(err) - } - err = os.Setenv("AZURE_USERNAME", "fake") - if err != nil { - panic(err) - } - err = os.Setenv("AZURE_PASSWORD", "fake") - if err != nil { - panic(err) - } - - os.Exit(m.Run()) -} diff --git a/testutil/testutil_test.go b/testutil/testutil_test.go index d9856bfc5..f7894e47e 100644 --- a/testutil/testutil_test.go +++ b/testutil/testutil_test.go @@ -17,18 +17,14 @@ func TestDockerHost(t *testing.T) { t.Fatalf("Host should be localhost when DOCKER_HOST is not set. Current value [%s]", host) } - err = os.Setenv("DOCKER_HOST", "1.1.1.1") - require.NoError(t, err) - + t.Setenv("DOCKER_HOST", "1.1.1.1") host = GetLocalHost() if host != "1.1.1.1" { t.Fatalf("Host should take DOCKER_HOST value when set. Current value is [%s] and DOCKER_HOST is [%s]", host, os.Getenv("DOCKER_HOST")) } - err = os.Setenv("DOCKER_HOST", "tcp://1.1.1.1:8080") - require.NoError(t, err) - + t.Setenv("DOCKER_HOST", "tcp://1.1.1.1:8080") host = GetLocalHost() if host != "1.1.1.1" {