test: Use `t.Setenv` to set env vars (#12621)

This commit is contained in:
Eng Zer Jun 2023-02-10 18:46:14 +08:00 committed by GitHub
parent aa0b9d7dcf
commit 4d0f05980e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 62 additions and 63 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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,18 +413,22 @@ func TestInitConfigErrors(t *testing.T) {
}
// Both invalid IP addresses
t.Run("Both invalid IP addresses", func(t *testing.T) {
p.NodeIP = "10.240.0.0.0"
require.NoError(t, os.Setenv("NODE_IP", "10.000.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())
require.NoError(t, os.Setenv("NODE_IP", "10.000.0.0"))
})
t.Run("Valid IP address", func(t *testing.T) {
t.Setenv("NODE_IP", "10.000.0.0")
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"
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"
@ -437,6 +441,7 @@ func TestInitConfigErrors(t *testing.T) {
err = p.Init()
expectedMessage = "the field selector spec.containerNames is not supported for pods"
require.Error(t, err, expectedMessage)
})
}
func TestInitConfigSelectors(t *testing.T) {

View File

@ -8,6 +8,7 @@ import (
)
func TestDockerHost(t *testing.T) {
t.Run("no DOCKER_HOST set", func(t *testing.T) {
err := os.Unsetenv("DOCKER_HOST")
require.NoError(t, err)
@ -16,18 +17,23 @@ func TestDockerHost(t *testing.T) {
if host != localhost {
t.Fatalf("Host should be localhost when DOCKER_HOST is not set. Current value [%s]", host)
}
})
t.Run("DOCKER_HOST with IP address only", func(t *testing.T) {
t.Setenv("DOCKER_HOST", "1.1.1.1")
host = GetLocalHost()
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.Run("DOCKER_HOST with protocol, IP address, and port", func(t *testing.T) {
t.Setenv("DOCKER_HOST", "tcp://1.1.1.1:8080")
host = GetLocalHost()
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"))
}
})
}