chore: Fix linter findings for tenv (#12622)

Co-authored-by: Pawel Zak <Pawel Zak>
This commit is contained in:
Paweł Żak 2023-02-07 17:12:42 +01:00 committed by GitHub
parent 9a0cecc788
commit 1260b4523b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 39 deletions

View File

@ -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

View File

@ -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)

View File

@ -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{}

View File

@ -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://", ""))
}

View File

@ -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())
}

View File

@ -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" {