Skip to content

Commit

Permalink
test: Use t.Setenv to set env vars (influxdata#12621)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juneezee authored Feb 10, 2023
1 parent aa0b9d7 commit 4d0f059
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 72 deletions.
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions config/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"errors"
"fmt"
"os"
"testing"

"github.com/awnumar/memguard"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 5 additions & 13 deletions plugins/inputs/ecs/ecs_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ecs

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -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",
Expand All @@ -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: "",
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions plugins/inputs/execd/shim/shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package shim
import (
"bufio"
"io"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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{}
Expand Down
4 changes: 1 addition & 3 deletions plugins/inputs/leofs/leofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
57 changes: 31 additions & 26 deletions plugins/inputs/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"))

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 = "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)
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())
})

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"
require.Error(t, err, expectedMessage)
p.KubernetesLabelSelector = "label0==label"

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) {
Expand Down
50 changes: 28 additions & 22 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@ import (
)

func TestDockerHost(t *testing.T) {
err := os.Unsetenv("DOCKER_HOST")
require.NoError(t, err)

host := GetLocalHost()

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

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

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("no DOCKER_HOST set", func(t *testing.T) {
err := os.Unsetenv("DOCKER_HOST")
require.NoError(t, err)

host := GetLocalHost()

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

0 comments on commit 4d0f059

Please sign in to comment.