From 4d0f05980e7ecaf1dda9c3672d03d792bb5fe6f0 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Fri, 10 Feb 2023 18:46:14 +0800 Subject: [PATCH] test: Use `t.Setenv` to set env vars (#12621) --- config/config_test.go | 4 +- config/secret_test.go | 3 +- plugins/inputs/disk/disk_test.go | 2 +- plugins/inputs/ecs/ecs_test.go | 18 ++----- plugins/inputs/execd/shim/shim_test.go | 5 +- plugins/inputs/leofs/leofs_test.go | 4 +- plugins/inputs/prometheus/prometheus_test.go | 51 +++++++++++--------- testutil/testutil_test.go | 38 +++++++++------ 8 files changed, 62 insertions(+), 63 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 4f76d368a..a71a39a0b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -58,8 +58,8 @@ func TestReadBinaryFile(t *testing.T) { func TestConfig_LoadSingleInputWithEnvVars(t *testing.T) { c := NewConfig() - require.NoError(t, os.Setenv("MY_TEST_SERVER", "192.168.1.1")) - require.NoError(t, os.Setenv("TEST_INTERVAL", "10s")) + t.Setenv("MY_TEST_SERVER", "192.168.1.1") + t.Setenv("TEST_INTERVAL", "10s") require.NoError(t, c.LoadConfig("./testdata/single_plugin_env_vars.toml")) input := inputs.Inputs["memcached"]().(*MockupInputPlugin) diff --git a/config/secret_test.go b/config/secret_test.go index d253c9bc4..073378cf0 100644 --- a/config/secret_test.go +++ b/config/secret_test.go @@ -3,7 +3,6 @@ package config import ( "errors" "fmt" - "os" "testing" "github.com/awnumar/memguard" @@ -325,7 +324,7 @@ func TestSecretEnvironmentVariable(t *testing.T) { [[inputs.mockup]] secret = "$SOME_ENV_SECRET" `) - require.NoError(t, os.Setenv("SOME_ENV_SECRET", "an env secret")) + t.Setenv("SOME_ENV_SECRET", "an env secret") c := NewConfig() err := c.LoadConfigData(cfg) diff --git a/plugins/inputs/disk/disk_test.go b/plugins/inputs/disk/disk_test.go index 8ab2d13cb..e47bebb32 100644 --- a/plugins/inputs/disk/disk_test.go +++ b/plugins/inputs/disk/disk_test.go @@ -609,7 +609,7 @@ func TestDiskUsageIssues(t *testing.T) { // Get the partitions in the test-case os.Clearenv() - require.NoError(t, os.Setenv("HOST_PROC", hostProcPrefix)) + t.Setenv("HOST_PROC", hostProcPrefix) partitions, err := diskUtil.Partitions(true) require.NoError(t, err) diff --git a/plugins/inputs/ecs/ecs_test.go b/plugins/inputs/ecs/ecs_test.go index 5a837d1ae..078f807c6 100644 --- a/plugins/inputs/ecs/ecs_test.go +++ b/plugins/inputs/ecs/ecs_test.go @@ -1,7 +1,6 @@ package ecs import ( - "os" "testing" "time" @@ -774,8 +773,7 @@ func TestResolveEndpoint(t *testing.T) { name string given Ecs exp Ecs - preF func() - afterF func() + setEnv func(*testing.T) }{ { name: "Endpoint is explicitly set => use v2 metadata", @@ -799,11 +797,8 @@ func TestResolveEndpoint(t *testing.T) { }, { name: "Endpoint is not set, ECS_CONTAINER_METADATA_URI is set => use v3 metadata", - preF: func() { - require.NoError(t, os.Setenv("ECS_CONTAINER_METADATA_URI", "v3-endpoint.local")) - }, - afterF: func() { - require.NoError(t, os.Unsetenv("ECS_CONTAINER_METADATA_URI")) + setEnv: func(t *testing.T) { + t.Setenv("ECS_CONTAINER_METADATA_URI", "v3-endpoint.local") }, given: Ecs{ EndpointURL: "", @@ -816,11 +811,8 @@ func TestResolveEndpoint(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if tt.preF != nil { - tt.preF() - } - if tt.afterF != nil { - defer tt.afterF() + if tt.setEnv != nil { + tt.setEnv(t) } act := tt.given diff --git a/plugins/inputs/execd/shim/shim_test.go b/plugins/inputs/execd/shim/shim_test.go index 1059bc2b7..4e047cc8b 100644 --- a/plugins/inputs/execd/shim/shim_test.go +++ b/plugins/inputs/execd/shim/shim_test.go @@ -3,7 +3,6 @@ package shim import ( "bufio" "io" - "os" "strings" "testing" "time" @@ -113,8 +112,8 @@ func (i *testInput) Stop() { } func TestLoadConfig(t *testing.T) { - require.NoError(t, os.Setenv("SECRET_TOKEN", "xxxxxxxxxx")) - require.NoError(t, os.Setenv("SECRET_VALUE", `test"\test`)) + t.Setenv("SECRET_TOKEN", "xxxxxxxxxx") + t.Setenv("SECRET_VALUE", `test"\test`) inputs.Add("test", func() telegraf.Input { return &serviceInput{} diff --git a/plugins/inputs/leofs/leofs_test.go b/plugins/inputs/leofs/leofs_test.go index 9098e39b7..894e1a851 100644 --- a/plugins/inputs/leofs/leofs_test.go +++ b/plugins/inputs/leofs/leofs_test.go @@ -140,10 +140,8 @@ func testMain(t *testing.T, code string, endpoint string, serverType ServerType) currentWorkingDirectory, err := os.Getwd() require.NoError(t, err) - envPathOrigin := os.Getenv("PATH") // Refer to the fake snmpwalk - require.NoError(t, os.Setenv("PATH", currentWorkingDirectory)) - defer os.Setenv("PATH", envPathOrigin) + t.Setenv("PATH", currentWorkingDirectory) l := &LeoFS{ Servers: []string{endpoint}, diff --git a/plugins/inputs/prometheus/prometheus_test.go b/plugins/inputs/prometheus/prometheus_test.go index 376a776d3..b637a39f6 100644 --- a/plugins/inputs/prometheus/prometheus_test.go +++ b/plugins/inputs/prometheus/prometheus_test.go @@ -3,15 +3,15 @@ package prometheus import ( "errors" "fmt" - "github.com/influxdata/telegraf/config" "math" "net/http" "net/http/httptest" "net/url" - "os" "testing" "time" + "github.com/influxdata/telegraf/config" + "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/fields" @@ -413,30 +413,35 @@ func TestInitConfigErrors(t *testing.T) { } // Both invalid IP addresses - p.NodeIP = "10.240.0.0.0" - require.NoError(t, os.Setenv("NODE_IP", "10.000.0.0.0")) - err := p.Init() - require.Error(t, err) - expectedMessage := "the node_ip config and the environment variable NODE_IP are not set or invalid; " + - "cannot get pod list for monitor_kubernetes_pods using node scrape scope" - require.Equal(t, expectedMessage, err.Error()) - require.NoError(t, os.Setenv("NODE_IP", "10.000.0.0")) + t.Run("Both invalid IP addresses", func(t *testing.T) { + p.NodeIP = "10.240.0.0.0" + t.Setenv("NODE_IP", "10.000.0.0.0") + err := p.Init() + require.Error(t, err) + expectedMessage := "the node_ip config and the environment variable NODE_IP are not set or invalid; " + + "cannot get pod list for monitor_kubernetes_pods using node scrape scope" + require.Equal(t, expectedMessage, err.Error()) + }) - p.KubernetesLabelSelector = "label0==label0, label0 in (=)" - err = p.Init() - expectedMessage = "error parsing the specified label selector(s): unable to parse requirement: found '=', expected: ',', ')' or identifier" - require.Error(t, err, expectedMessage) - p.KubernetesLabelSelector = "label0==label" + t.Run("Valid IP address", func(t *testing.T) { + t.Setenv("NODE_IP", "10.000.0.0") - p.KubernetesFieldSelector = "field," - err = p.Init() - expectedMessage = "error parsing the specified field selector(s): invalid selector: 'field,'; can't understand 'field'" - require.Error(t, err, expectedMessage) + p.KubernetesLabelSelector = "label0==label0, label0 in (=)" + err := p.Init() + expectedMessage := "error parsing the specified label selector(s): unable to parse requirement: found '=', expected: ',', ')' or identifier" + require.Error(t, err, expectedMessage) + p.KubernetesLabelSelector = "label0==label" - p.KubernetesFieldSelector = "spec.containerNames=containerNames" - err = p.Init() - expectedMessage = "the field selector spec.containerNames is not supported for pods" - require.Error(t, err, expectedMessage) + p.KubernetesFieldSelector = "field," + err = p.Init() + expectedMessage = "error parsing the specified field selector(s): invalid selector: 'field,'; can't understand 'field'" + require.Error(t, err, expectedMessage) + + p.KubernetesFieldSelector = "spec.containerNames=containerNames" + err = p.Init() + expectedMessage = "the field selector spec.containerNames is not supported for pods" + require.Error(t, err, expectedMessage) + }) } func TestInitConfigSelectors(t *testing.T) { diff --git a/testutil/testutil_test.go b/testutil/testutil_test.go index f7894e47e..9d8ebcdca 100644 --- a/testutil/testutil_test.go +++ b/testutil/testutil_test.go @@ -8,26 +8,32 @@ import ( ) func TestDockerHost(t *testing.T) { - err := os.Unsetenv("DOCKER_HOST") - require.NoError(t, err) + t.Run("no DOCKER_HOST set", func(t *testing.T) { + err := os.Unsetenv("DOCKER_HOST") + require.NoError(t, err) - host := GetLocalHost() + host := GetLocalHost() - if host != localhost { - t.Fatalf("Host should be localhost when DOCKER_HOST is not set. Current value [%s]", host) - } + if host != localhost { + t.Fatalf("Host should be localhost when DOCKER_HOST is not set. Current value [%s]", host) + } + }) - t.Setenv("DOCKER_HOST", "1.1.1.1") - host = GetLocalHost() + t.Run("DOCKER_HOST with IP address only", func(t *testing.T) { + t.Setenv("DOCKER_HOST", "1.1.1.1") - 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")) - } + 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")) + } + }) - t.Setenv("DOCKER_HOST", "tcp://1.1.1.1:8080") - host = GetLocalHost() + t.Run("DOCKER_HOST with protocol, IP address, and port", func(t *testing.T) { + t.Setenv("DOCKER_HOST", "tcp://1.1.1.1:8080") - 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")) - } + 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")) + } + }) }