Skip to content

Commit

Permalink
chore: Fix linter findings for tenv (influxdata#12622)
Browse files Browse the repository at this point in the history
Co-authored-by: Pawel Zak <Pawel Zak>
  • Loading branch information
zak-pawel authored Feb 7, 2023
1 parent 9a0cecc commit 1260b45
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ linters:
- revive
- sqlclosecheck
- staticcheck
- tenv
- tparallel
- typecheck
- unconvert
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions plugins/common/shim/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package shim

import (
"os"
"testing"
"time"

Expand All @@ -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{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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://", ""))
}
33 changes: 10 additions & 23 deletions plugins/outputs/azure_monitor/azure_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand Down Expand Up @@ -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())
}
8 changes: 2 additions & 6 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 1260b45

Please sign in to comment.