mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Refactor tests to follow a safer way of setting envvars (#1215)
Signed-off-by: Dmitry Kisler <admin@dkisler.com>
This commit is contained in:
parent
e84b2f7f95
commit
7d73f2bbe6
@ -116,14 +116,9 @@ func TestMain_cliArgsFromEnv(t *testing.T) {
|
||||
|
||||
for i, tc := range cases {
|
||||
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
||||
os.Unsetenv(EnvCLI)
|
||||
defer os.Unsetenv(EnvCLI)
|
||||
|
||||
// Set the env var value
|
||||
if tc.Value != "" {
|
||||
if err := os.Setenv(EnvCLI, tc.Value); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
t.Setenv(EnvCLI, tc.Value)
|
||||
}
|
||||
|
||||
// Set up the args
|
||||
@ -223,14 +218,9 @@ func TestMain_cliArgsFromEnvAdvanced(t *testing.T) {
|
||||
return testCommand, nil
|
||||
}
|
||||
|
||||
os.Unsetenv(tc.EnvVar)
|
||||
defer os.Unsetenv(tc.EnvVar)
|
||||
|
||||
// Set the env var value
|
||||
if tc.Value != "" {
|
||||
if err := os.Setenv(tc.EnvVar, tc.Value); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
t.Setenv(tc.EnvVar, tc.Value)
|
||||
}
|
||||
|
||||
// Set up the args
|
||||
@ -274,8 +264,7 @@ func TestMain_autoComplete(t *testing.T) {
|
||||
return &testCommandCLI{}, nil
|
||||
}
|
||||
|
||||
os.Setenv("COMP_LINE", "tofu versio")
|
||||
defer os.Unsetenv("COMP_LINE")
|
||||
t.Setenv("COMP_LINE", "tofu versio")
|
||||
|
||||
// Run it!
|
||||
os.Args = []string{"tofu", "tofu", "versio"}
|
||||
|
@ -210,7 +210,7 @@ func setupBackend(t *testing.T, bucket, prefix, key string, encrypt bool) backen
|
||||
}
|
||||
|
||||
if os.Getenv(PROVIDER_REGION) == "" {
|
||||
os.Setenv(PROVIDER_REGION, "ap-guangzhou")
|
||||
t.Setenv(PROVIDER_REGION, "ap-guangzhou")
|
||||
}
|
||||
|
||||
appId := os.Getenv("TF_COS_APPID")
|
||||
|
@ -4,7 +4,6 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -109,17 +108,17 @@ func TestHTTPClientFactoryWithEnv(t *testing.T) {
|
||||
"retry_wait_max": "150",
|
||||
}
|
||||
|
||||
defer testWithEnv(t, "TF_HTTP_ADDRESS", conf["address"])()
|
||||
defer testWithEnv(t, "TF_HTTP_UPDATE_METHOD", conf["update_method"])()
|
||||
defer testWithEnv(t, "TF_HTTP_LOCK_ADDRESS", conf["lock_address"])()
|
||||
defer testWithEnv(t, "TF_HTTP_UNLOCK_ADDRESS", conf["unlock_address"])()
|
||||
defer testWithEnv(t, "TF_HTTP_LOCK_METHOD", conf["lock_method"])()
|
||||
defer testWithEnv(t, "TF_HTTP_UNLOCK_METHOD", conf["unlock_method"])()
|
||||
defer testWithEnv(t, "TF_HTTP_USERNAME", conf["username"])()
|
||||
defer testWithEnv(t, "TF_HTTP_PASSWORD", conf["password"])()
|
||||
defer testWithEnv(t, "TF_HTTP_RETRY_MAX", conf["retry_max"])()
|
||||
defer testWithEnv(t, "TF_HTTP_RETRY_WAIT_MIN", conf["retry_wait_min"])()
|
||||
defer testWithEnv(t, "TF_HTTP_RETRY_WAIT_MAX", conf["retry_wait_max"])()
|
||||
t.Setenv("TF_HTTP_ADDRESS", conf["address"])
|
||||
t.Setenv("TF_HTTP_UPDATE_METHOD", conf["update_method"])
|
||||
t.Setenv("TF_HTTP_LOCK_ADDRESS", conf["lock_address"])
|
||||
t.Setenv("TF_HTTP_UNLOCK_ADDRESS", conf["unlock_address"])
|
||||
t.Setenv("TF_HTTP_LOCK_METHOD", conf["lock_method"])
|
||||
t.Setenv("TF_HTTP_UNLOCK_METHOD", conf["unlock_method"])
|
||||
t.Setenv("TF_HTTP_USERNAME", conf["username"])
|
||||
t.Setenv("TF_HTTP_PASSWORD", conf["password"])
|
||||
t.Setenv("TF_HTTP_RETRY_MAX", conf["retry_max"])
|
||||
t.Setenv("TF_HTTP_RETRY_WAIT_MIN", conf["retry_wait_min"])
|
||||
t.Setenv("TF_HTTP_RETRY_WAIT_MAX", conf["retry_wait_max"])
|
||||
|
||||
b := backend.TestBackendConfig(t, New(), nil).(*Backend)
|
||||
client := b.client
|
||||
@ -152,16 +151,3 @@ func TestHTTPClientFactoryWithEnv(t *testing.T) {
|
||||
t.Fatalf("Expected retry_wait_max \"%s\", got \"%s\"", 150*time.Second, client.Client.RetryWaitMax)
|
||||
}
|
||||
}
|
||||
|
||||
// testWithEnv sets an environment variable and returns a deferable func to clean up
|
||||
func testWithEnv(t *testing.T, key string, value string) func() {
|
||||
if err := os.Setenv(key, value); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
return func() {
|
||||
if err := os.Unsetenv(key); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
|
||||
// verify that we are doing ACC tests or the OSS tests specifically
|
||||
func testACC(t *testing.T) {
|
||||
t.Helper()
|
||||
skip := os.Getenv("TF_ACC") == "" && os.Getenv("TF_OSS_TEST") == ""
|
||||
if skip {
|
||||
t.Log("oss backend tests require setting TF_ACC or TF_OSS_TEST")
|
||||
@ -28,9 +29,7 @@ func testACC(t *testing.T) {
|
||||
if skip {
|
||||
t.Fatal("oss backend tests require setting ALICLOUD_ACCESS_KEY or ALICLOUD_ACCESS_KEY_ID")
|
||||
}
|
||||
if os.Getenv("ALICLOUD_REGION") == "" {
|
||||
os.Setenv("ALICLOUD_REGION", "cn-beijing")
|
||||
}
|
||||
t.Setenv("ALICLOUD_REGION", "cn-beijing")
|
||||
}
|
||||
|
||||
func TestBackend_impl(t *testing.T) {
|
||||
|
@ -694,9 +694,9 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
|
||||
}
|
||||
|
||||
if tc.EnableWebIdentityEnvVars {
|
||||
os.Setenv("AWS_ROLE_ARN", servicemocks.MockStsAssumeRoleWithWebIdentityArn)
|
||||
os.Setenv("AWS_ROLE_SESSION_NAME", servicemocks.MockStsAssumeRoleWithWebIdentitySessionName)
|
||||
os.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", file.Name())
|
||||
t.Setenv("AWS_ROLE_ARN", servicemocks.MockStsAssumeRoleWithWebIdentityArn)
|
||||
t.Setenv("AWS_ROLE_SESSION_NAME", servicemocks.MockStsAssumeRoleWithWebIdentitySessionName)
|
||||
t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", file.Name())
|
||||
} /*else if tc.EnableWebIdentityConfig {
|
||||
tc.Config.AssumeRoleWithWebIdentity = &AssumeRoleWithWebIdentity{
|
||||
RoleARN: servicemocks.MockStsAssumeRoleWithWebIdentityArn,
|
||||
@ -726,7 +726,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
|
||||
t.Fatalf("unexpected error writing shared configuration file: %s", err)
|
||||
}
|
||||
|
||||
setSharedConfigFile(file.Name())
|
||||
setSharedConfigFile(t, file.Name())
|
||||
}
|
||||
|
||||
if tc.SharedCredentialsFile != "" {
|
||||
@ -753,7 +753,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
|
||||
}
|
||||
|
||||
for k, v := range tc.EnvironmentVariables {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
|
||||
b, diags := configureBackend(t, tc.config)
|
||||
@ -1167,7 +1167,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
|
||||
t.Fatalf("unexpected error writing shared configuration file: %s", err)
|
||||
}
|
||||
|
||||
setSharedConfigFile(file.Name())
|
||||
setSharedConfigFile(t, file.Name())
|
||||
}
|
||||
|
||||
if tc.SharedCredentialsFile != "" {
|
||||
@ -1192,7 +1192,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
|
||||
}
|
||||
|
||||
for k, v := range tc.EnvironmentVariables {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
|
||||
b, diags := configureBackend(t, tc.config)
|
||||
@ -1569,7 +1569,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
|
||||
t.Fatalf("unexpected error writing shared configuration file: %s", err)
|
||||
}
|
||||
|
||||
setSharedConfigFile(file.Name())
|
||||
setSharedConfigFile(t, file.Name())
|
||||
}
|
||||
|
||||
if tc.SharedCredentialsFile != "" {
|
||||
@ -1596,7 +1596,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
|
||||
}
|
||||
|
||||
for k, v := range tc.EnvironmentVariables {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
|
||||
b, diags := configureBackend(t, tc.config)
|
||||
@ -1809,7 +1809,7 @@ web_identity_token_file = no-such-file
|
||||
}
|
||||
|
||||
for k, v := range tc.EnvironmentVariables {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
|
||||
ts := servicemocks.MockAwsApiServer("STS", tc.MockStsEndpoints)
|
||||
@ -1817,12 +1817,7 @@ web_identity_token_file = no-such-file
|
||||
|
||||
tc.config["sts_endpoint"] = ts.URL
|
||||
|
||||
tempdir, err := os.MkdirTemp("", "temp")
|
||||
if err != nil {
|
||||
t.Fatalf("error creating temp dir: %s", err)
|
||||
}
|
||||
defer os.Remove(tempdir)
|
||||
os.Setenv("TMPDIR", tempdir)
|
||||
t.Setenv("TMPDIR", t.TempDir())
|
||||
|
||||
tokenFile, err := os.CreateTemp("", "aws-sdk-go-base-web-identity-token-file")
|
||||
if err != nil {
|
||||
@ -1855,7 +1850,7 @@ web_identity_token_file = no-such-file
|
||||
}
|
||||
|
||||
if tc.SetTokenFileEnvironmentVariable {
|
||||
os.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", tokenFileName)
|
||||
t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", tokenFileName)
|
||||
}
|
||||
|
||||
if tc.SharedConfigurationFile != "" {
|
||||
@ -2066,7 +2061,7 @@ region = us-west-2
|
||||
tc.config["key"] = "key"
|
||||
|
||||
for k, v := range tc.EnvironmentVariables {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
|
||||
if tc.IMDSRegion != "" {
|
||||
@ -2101,7 +2096,7 @@ region = us-west-2
|
||||
t.Fatalf("unexpected error writing shared configuration file: %s", err)
|
||||
}
|
||||
|
||||
setSharedConfigFile(file.Name())
|
||||
setSharedConfigFile(t, file.Name())
|
||||
}
|
||||
|
||||
tc.config["skip_credentials_validation"] = true
|
||||
@ -2176,7 +2171,7 @@ func TestBackendConfig_RetryMode(t *testing.T) {
|
||||
tc.config["region"] = "us-east-1"
|
||||
|
||||
for k, v := range tc.EnvironmentVariables {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
|
||||
sts := servicemocks.MockAwsApiServer("STS", []*servicemocks.MockEndpoint{
|
||||
@ -2199,9 +2194,10 @@ func TestBackendConfig_RetryMode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func setSharedConfigFile(filename string) {
|
||||
os.Setenv("AWS_SDK_LOAD_CONFIG", "1")
|
||||
os.Setenv("AWS_CONFIG_FILE", filename)
|
||||
func setSharedConfigFile(t *testing.T, filename string) {
|
||||
t.Helper()
|
||||
t.Setenv("AWS_SDK_LOAD_CONFIG", "1")
|
||||
t.Setenv("AWS_CONFIG_FILE", filename)
|
||||
}
|
||||
|
||||
func configureBackend(t *testing.T, config map[string]any) (*Backend, tfdiags.Diagnostics) {
|
||||
|
@ -45,14 +45,13 @@ var (
|
||||
|
||||
// verify that we are doing ACC tests or the S3 tests specifically
|
||||
func testACC(t *testing.T) {
|
||||
t.Helper()
|
||||
skip := os.Getenv("TF_ACC") == "" && os.Getenv("TF_S3_TEST") == ""
|
||||
if skip {
|
||||
t.Log("s3 backend tests require setting TF_ACC or TF_S3_TEST")
|
||||
t.Skip()
|
||||
}
|
||||
if os.Getenv("AWS_DEFAULT_REGION") == "" {
|
||||
os.Setenv("AWS_DEFAULT_REGION", "us-west-2")
|
||||
}
|
||||
t.Setenv("AWS_DEFAULT_REGION", "us-west-2")
|
||||
}
|
||||
|
||||
func TestBackend_impl(t *testing.T) {
|
||||
@ -177,13 +176,8 @@ func TestBackendConfig_RegionEnvVar(t *testing.T) {
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
for k, v := range tc.vars {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for k := range tc.vars {
|
||||
os.Unsetenv(k)
|
||||
}
|
||||
})
|
||||
|
||||
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
|
||||
|
||||
@ -229,13 +223,8 @@ func TestBackendConfig_DynamoDBEndpoint(t *testing.T) {
|
||||
|
||||
if tc.vars != nil {
|
||||
for k, v := range tc.vars {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for k := range tc.vars {
|
||||
os.Unsetenv(k)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if tc.config != nil {
|
||||
@ -284,13 +273,8 @@ func TestBackendConfig_S3Endpoint(t *testing.T) {
|
||||
|
||||
if tc.vars != nil {
|
||||
for k, v := range tc.vars {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for k := range tc.vars {
|
||||
os.Unsetenv(k)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if tc.config != nil {
|
||||
@ -362,10 +346,7 @@ func TestBackendConfig_STSEndpoint(t *testing.T) {
|
||||
defer closeSts()
|
||||
|
||||
if tc.setEnvVars {
|
||||
os.Setenv("AWS_STS_ENDPOINT", endpoint)
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv("AWS_STS_ENDPOINT")
|
||||
})
|
||||
t.Setenv("AWS_STS_ENDPOINT", endpoint)
|
||||
}
|
||||
|
||||
if tc.setConfig {
|
||||
@ -898,7 +879,7 @@ func TestBackendConfig_PrepareConfigWithEnvVars(t *testing.T) {
|
||||
b := New()
|
||||
|
||||
for k, v := range tc.vars {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
|
||||
_, valDiags := b.PrepareConfig(populateSchema(t, b.ConfigSchema(), tc.config))
|
||||
@ -1235,10 +1216,7 @@ func TestBackendSSECustomerKeyEnvVar(t *testing.T) {
|
||||
"region": "us-west-1",
|
||||
}
|
||||
|
||||
os.Setenv("AWS_SSE_CUSTOMER_KEY", testCase.customerKey)
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv("AWS_SSE_CUSTOMER_KEY")
|
||||
})
|
||||
t.Setenv("AWS_SSE_CUSTOMER_KEY", testCase.customerKey)
|
||||
|
||||
b := New().(*Backend)
|
||||
diags := b.Configure(populateSchema(t, b.ConfigSchema(), hcl2shim.HCL2ValueFromConfigValue(config)))
|
||||
|
@ -961,10 +961,7 @@ func TestRemote_applyWithAutoApply(t *testing.T) {
|
||||
func TestRemote_applyForceLocal(t *testing.T) {
|
||||
// Set TF_FORCE_LOCAL_BACKEND so the remote backend will use
|
||||
// the local backend with itself as embedded backend.
|
||||
if err := os.Setenv("TF_FORCE_LOCAL_BACKEND", "1"); err != nil {
|
||||
t.Fatalf("error setting environment variable TF_FORCE_LOCAL_BACKEND: %v", err)
|
||||
}
|
||||
defer os.Unsetenv("TF_FORCE_LOCAL_BACKEND")
|
||||
t.Setenv("TF_FORCE_LOCAL_BACKEND", "1")
|
||||
|
||||
b, bCleanup := testBackendDefault(t)
|
||||
defer bCleanup()
|
||||
|
@ -699,10 +699,7 @@ func TestRemote_planNoChanges(t *testing.T) {
|
||||
func TestRemote_planForceLocal(t *testing.T) {
|
||||
// Set TF_FORCE_LOCAL_BACKEND so the remote backend will use
|
||||
// the local backend with itself as embedded backend.
|
||||
if err := os.Setenv("TF_FORCE_LOCAL_BACKEND", "1"); err != nil {
|
||||
t.Fatalf("error setting environment variable TF_FORCE_LOCAL_BACKEND: %v", err)
|
||||
}
|
||||
defer os.Unsetenv("TF_FORCE_LOCAL_BACKEND")
|
||||
t.Setenv("TF_FORCE_LOCAL_BACKEND", "1")
|
||||
|
||||
b, bCleanup := testBackendDefault(t)
|
||||
defer bCleanup()
|
||||
|
@ -5,7 +5,6 @@ package remote
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/opentofu/opentofu/internal/backend"
|
||||
@ -43,9 +42,7 @@ func TestRemoteClient_stateLock(t *testing.T) {
|
||||
|
||||
func TestRemoteClient_Put_withRunID(t *testing.T) {
|
||||
// Set the TFE_RUN_ID environment variable before creating the client!
|
||||
if err := os.Setenv("TFE_RUN_ID", cloud.GenerateID("run-")); err != nil {
|
||||
t.Fatalf("error setting env var TFE_RUN_ID: %v", err)
|
||||
}
|
||||
t.Setenv("TFE_RUN_ID", cloud.GenerateID("run-"))
|
||||
|
||||
// Create a new test client.
|
||||
client := testRemoteClient(t)
|
||||
|
@ -1066,10 +1066,7 @@ func TestCloud_applyWithAutoApprove(t *testing.T) {
|
||||
func TestCloud_applyForceLocal(t *testing.T) {
|
||||
// Set TF_FORCE_LOCAL_BACKEND so the cloud backend will use
|
||||
// the local backend with itself as embedded backend.
|
||||
if err := os.Setenv("TF_FORCE_LOCAL_BACKEND", "1"); err != nil {
|
||||
t.Fatalf("error setting environment variable TF_FORCE_LOCAL_BACKEND: %v", err)
|
||||
}
|
||||
defer os.Unsetenv("TF_FORCE_LOCAL_BACKEND")
|
||||
t.Setenv("TF_FORCE_LOCAL_BACKEND", "1")
|
||||
|
||||
b, bCleanup := testBackendWithName(t)
|
||||
defer bCleanup()
|
||||
|
@ -695,10 +695,7 @@ func TestCloud_planNoChanges(t *testing.T) {
|
||||
func TestCloud_planForceLocal(t *testing.T) {
|
||||
// Set TF_FORCE_LOCAL_BACKEND so the cloud backend will use
|
||||
// the local backend with itself as embedded backend.
|
||||
if err := os.Setenv("TF_FORCE_LOCAL_BACKEND", "1"); err != nil {
|
||||
t.Fatalf("error setting environment variable TF_FORCE_LOCAL_BACKEND: %v", err)
|
||||
}
|
||||
defer os.Unsetenv("TF_FORCE_LOCAL_BACKEND")
|
||||
t.Setenv("TF_FORCE_LOCAL_BACKEND", "1")
|
||||
|
||||
b, bCleanup := testBackendWithName(t)
|
||||
defer bCleanup()
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -328,13 +327,8 @@ func TestCloud_PrepareConfigWithEnvVars(t *testing.T) {
|
||||
b := New(testDisco(s))
|
||||
|
||||
for k, v := range tc.vars {
|
||||
os.Setenv(k, v)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for k := range tc.vars {
|
||||
os.Unsetenv(k)
|
||||
}
|
||||
})
|
||||
|
||||
_, valDiags := b.PrepareConfig(tc.config)
|
||||
if (valDiags.Err() == nil) != (tc.expectedErr == "") {
|
||||
|
@ -36,8 +36,7 @@ func TestLoadConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLoadConfig_envSubst(t *testing.T) {
|
||||
defer os.Unsetenv("TFTEST")
|
||||
os.Setenv("TFTEST", "hello")
|
||||
t.Setenv("TFTEST", "hello")
|
||||
|
||||
c, err := loadConfigFile(filepath.Join(fixtureDir, "config-env"))
|
||||
if err != nil {
|
||||
@ -63,8 +62,7 @@ func TestLoadConfig_non_existing_file(t *testing.T) {
|
||||
tmpDir := os.TempDir()
|
||||
cliTmpFile := filepath.Join(tmpDir, "dev.tfrc")
|
||||
|
||||
os.Setenv("TF_CLI_CONFIG_FILE", cliTmpFile)
|
||||
defer os.Unsetenv("TF_CLI_CONFIG_FILE")
|
||||
t.Setenv("TF_CLI_CONFIG_FILE", cliTmpFile)
|
||||
|
||||
c, errs := LoadConfig()
|
||||
if errs.HasErrors() || c.Validate().HasErrors() {
|
||||
|
@ -5,7 +5,6 @@ package cliconfig
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@ -89,12 +88,9 @@ func TestCredentialsForHost(t *testing.T) {
|
||||
})
|
||||
t.Run("set in environment", func(t *testing.T) {
|
||||
envName := "TF_TOKEN_configured_example_com"
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv(envName)
|
||||
})
|
||||
|
||||
expectedToken := "configured-by-env"
|
||||
os.Setenv(envName, expectedToken)
|
||||
t.Setenv(envName, expectedToken)
|
||||
|
||||
creds, err := credSrc.ForHost(svchost.Hostname("configured.example.com"))
|
||||
if err != nil {
|
||||
@ -112,12 +108,9 @@ func TestCredentialsForHost(t *testing.T) {
|
||||
|
||||
t.Run("punycode name set in environment", func(t *testing.T) {
|
||||
envName := "TF_TOKEN_env_xn--eckwd4c7cu47r2wf_com"
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv(envName)
|
||||
})
|
||||
|
||||
expectedToken := "configured-by-env"
|
||||
os.Setenv(envName, expectedToken)
|
||||
t.Setenv(envName, expectedToken)
|
||||
|
||||
hostname, _ := svchost.ForComparison("env.ドメイン名例.com")
|
||||
creds, err := credSrc.ForHost(hostname)
|
||||
@ -138,11 +131,8 @@ func TestCredentialsForHost(t *testing.T) {
|
||||
t.Run("hyphens can be encoded as double underscores", func(t *testing.T) {
|
||||
envName := "TF_TOKEN_env_xn____caf__dma_fr"
|
||||
expectedToken := "configured-by-fallback"
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv(envName)
|
||||
})
|
||||
|
||||
os.Setenv(envName, expectedToken)
|
||||
t.Setenv(envName, expectedToken)
|
||||
|
||||
hostname, _ := svchost.ForComparison("env.café.fr")
|
||||
creds, err := credSrc.ForHost(hostname)
|
||||
@ -163,11 +153,8 @@ func TestCredentialsForHost(t *testing.T) {
|
||||
t.Run("periods are ok", func(t *testing.T) {
|
||||
envName := "TF_TOKEN_configured.example.com"
|
||||
expectedToken := "configured-by-env"
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv(envName)
|
||||
})
|
||||
|
||||
os.Setenv(envName, expectedToken)
|
||||
t.Setenv(envName, expectedToken)
|
||||
|
||||
hostname, _ := svchost.ForComparison("configured.example.com")
|
||||
creds, err := credSrc.ForHost(hostname)
|
||||
@ -189,10 +176,7 @@ func TestCredentialsForHost(t *testing.T) {
|
||||
envName := "TF_TOKEN_CONFIGUREDUPPERCASE_EXAMPLE_COM"
|
||||
expectedToken := "configured-by-env"
|
||||
|
||||
os.Setenv(envName, expectedToken)
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv(envName)
|
||||
})
|
||||
t.Setenv(envName, expectedToken)
|
||||
|
||||
hostname, _ := svchost.ForComparison("configureduppercase.example.com")
|
||||
creds, err := credSrc.ForHost(hostname)
|
||||
|
@ -97,8 +97,6 @@ func TestMetaInputMode(t *testing.T) {
|
||||
func TestMetaInputMode_envVar(t *testing.T) {
|
||||
test = false
|
||||
defer func() { test = true }()
|
||||
old := os.Getenv(InputModeEnvVar)
|
||||
defer os.Setenv(InputModeEnvVar, old)
|
||||
|
||||
m := new(Meta)
|
||||
args := []string{}
|
||||
@ -121,7 +119,7 @@ func TestMetaInputMode_envVar(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
os.Setenv(InputModeEnvVar, tc.EnvVar)
|
||||
t.Setenv(InputModeEnvVar, tc.EnvVar)
|
||||
if m.InputMode() != tc.Expected {
|
||||
t.Fatalf("expected InputMode: %#v, got: %#v", tc.Expected, m.InputMode())
|
||||
}
|
||||
@ -219,10 +217,6 @@ func TestMeta_Env(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMeta_Workspace_override(t *testing.T) {
|
||||
defer func(value string) {
|
||||
os.Setenv(WorkspaceNameEnvVar, value)
|
||||
}(os.Getenv(WorkspaceNameEnvVar))
|
||||
|
||||
m := new(Meta)
|
||||
|
||||
testCases := map[string]struct {
|
||||
@ -245,7 +239,7 @@ func TestMeta_Workspace_override(t *testing.T) {
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
os.Setenv(WorkspaceNameEnvVar, name)
|
||||
t.Setenv(WorkspaceNameEnvVar, name)
|
||||
workspace, err := m.Workspace()
|
||||
if workspace != tc.workspace {
|
||||
t.Errorf("Unexpected workspace\n got: %s\nwant: %s\n", workspace, tc.workspace)
|
||||
|
@ -10,7 +10,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@ -36,11 +35,10 @@ func TestConfigureDiscoveryRetry(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("configured retry", func(t *testing.T) {
|
||||
defer func(retryEnv string) {
|
||||
os.Setenv(registryDiscoveryRetryEnvName, retryEnv)
|
||||
defer func() {
|
||||
discoveryRetry = defaultRetry
|
||||
}(os.Getenv(registryDiscoveryRetryEnvName))
|
||||
os.Setenv(registryDiscoveryRetryEnvName, "2")
|
||||
}()
|
||||
t.Setenv(registryDiscoveryRetryEnvName, "2")
|
||||
|
||||
configureDiscoveryRetry()
|
||||
expected := 2
|
||||
@ -72,11 +70,10 @@ func TestConfigureRegistryClientTimeout(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("configured timeout", func(t *testing.T) {
|
||||
defer func(timeoutEnv string) {
|
||||
os.Setenv(registryClientTimeoutEnvName, timeoutEnv)
|
||||
defer func() {
|
||||
requestTimeout = defaultRequestTimeout
|
||||
}(os.Getenv(registryClientTimeoutEnvName))
|
||||
os.Setenv(registryClientTimeoutEnvName, "20")
|
||||
}()
|
||||
t.Setenv(registryClientTimeoutEnvName, "20")
|
||||
|
||||
configureRequestTimeout()
|
||||
expected := 20 * time.Second
|
||||
|
@ -5,7 +5,6 @@ package httpclient
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/opentofu/opentofu/version"
|
||||
@ -13,11 +12,6 @@ import (
|
||||
|
||||
func TestUserAgentString_env(t *testing.T) {
|
||||
expectedBase := fmt.Sprintf("%s/%s", DefaultApplicationName, version.Version)
|
||||
if oldenv, isSet := os.LookupEnv(appendUaEnvVar); isSet {
|
||||
defer os.Setenv(appendUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(appendUaEnvVar)
|
||||
}
|
||||
|
||||
for i, c := range []struct {
|
||||
expected string
|
||||
@ -33,10 +27,8 @@ func TestUserAgentString_env(t *testing.T) {
|
||||
{fmt.Sprintf("%s test/4", expectedBase), "test/4 \n"},
|
||||
} {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
if c.additional == "" {
|
||||
os.Unsetenv(appendUaEnvVar)
|
||||
} else {
|
||||
os.Setenv(appendUaEnvVar, c.additional)
|
||||
if c.additional != "" {
|
||||
t.Setenv(appendUaEnvVar, c.additional)
|
||||
}
|
||||
|
||||
actual := OpenTofuUserAgent(version.Version)
|
||||
@ -49,12 +41,6 @@ func TestUserAgentString_env(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserAgentAppendViaEnvVar(t *testing.T) {
|
||||
if oldenv, isSet := os.LookupEnv(appendUaEnvVar); isSet {
|
||||
defer os.Setenv(appendUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(appendUaEnvVar)
|
||||
}
|
||||
|
||||
expectedBase := "OpenTofu/0.0.0"
|
||||
|
||||
testCases := []struct {
|
||||
@ -70,8 +56,7 @@ func TestUserAgentAppendViaEnvVar(t *testing.T) {
|
||||
|
||||
for i, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
os.Unsetenv(appendUaEnvVar)
|
||||
os.Setenv(appendUaEnvVar, tc.envVarValue)
|
||||
t.Setenv(appendUaEnvVar, tc.envVarValue)
|
||||
givenUA := OpenTofuUserAgent("0.0.0")
|
||||
if givenUA != tc.expected {
|
||||
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
|
||||
@ -80,12 +65,6 @@ func TestUserAgentAppendViaEnvVar(t *testing.T) {
|
||||
}
|
||||
}
|
||||
func TestCustomUserAgentViaEnvVar(t *testing.T) {
|
||||
if oldenv, isSet := os.LookupEnv(customUaEnvVar); isSet {
|
||||
defer os.Setenv(customUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(customUaEnvVar)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
envVarValue string
|
||||
}{
|
||||
@ -97,8 +76,7 @@ func TestCustomUserAgentViaEnvVar(t *testing.T) {
|
||||
|
||||
for i, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
os.Unsetenv(customUaEnvVar)
|
||||
os.Setenv(customUaEnvVar, tc.envVarValue)
|
||||
t.Setenv(customUaEnvVar, tc.envVarValue)
|
||||
givenUA := OpenTofuUserAgent("0.0.0")
|
||||
if givenUA != tc.envVarValue {
|
||||
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.envVarValue, givenUA)
|
||||
@ -107,17 +85,6 @@ func TestCustomUserAgentViaEnvVar(t *testing.T) {
|
||||
}
|
||||
}
|
||||
func TestCustomUserAgentAndAppendViaEnvVar(t *testing.T) {
|
||||
if oldenv, isSet := os.LookupEnv(appendUaEnvVar); isSet {
|
||||
defer os.Setenv(appendUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(appendUaEnvVar)
|
||||
}
|
||||
if oldenv, isSet := os.LookupEnv(customUaEnvVar); isSet {
|
||||
defer os.Setenv(customUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(customUaEnvVar)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
customUaValue string
|
||||
appendUaValue string
|
||||
@ -132,10 +99,8 @@ func TestCustomUserAgentAndAppendViaEnvVar(t *testing.T) {
|
||||
|
||||
for i, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
os.Unsetenv(customUaEnvVar)
|
||||
os.Unsetenv(appendUaEnvVar)
|
||||
os.Setenv(customUaEnvVar, tc.customUaValue)
|
||||
os.Setenv(appendUaEnvVar, tc.appendUaValue)
|
||||
t.Setenv(customUaEnvVar, tc.customUaValue)
|
||||
t.Setenv(appendUaEnvVar, tc.appendUaValue)
|
||||
givenUA := OpenTofuUserAgent("0.0.0")
|
||||
if givenUA != tc.expected {
|
||||
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
|
||||
|
@ -5,7 +5,6 @@ package schema
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
@ -2115,9 +2114,7 @@ func TestResourceDataSet(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
oldEnv := os.Getenv(PanicOnErr)
|
||||
os.Setenv(PanicOnErr, "")
|
||||
defer os.Setenv(PanicOnErr, oldEnv)
|
||||
t.Setenv(PanicOnErr, "")
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.TestName, func(t *testing.T) {
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
@ -21,30 +20,24 @@ import (
|
||||
|
||||
func TestEnvDefaultFunc(t *testing.T) {
|
||||
key := "TF_TEST_ENV_DEFAULT_FUNC"
|
||||
defer os.Unsetenv(key)
|
||||
|
||||
f := EnvDefaultFunc(key, "42")
|
||||
if err := os.Setenv(key, "foo"); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual, err := f()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if actual != "foo" {
|
||||
if actual != "42" {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
|
||||
if err := os.Unsetenv(key); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
t.Setenv(key, "foo")
|
||||
|
||||
actual, err = f()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if actual != "42" {
|
||||
if actual != "foo" {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
}
|
||||
@ -54,56 +47,46 @@ func TestMultiEnvDefaultFunc(t *testing.T) {
|
||||
"TF_TEST_MULTI_ENV_DEFAULT_FUNC1",
|
||||
"TF_TEST_MULTI_ENV_DEFAULT_FUNC2",
|
||||
}
|
||||
defer func() {
|
||||
for _, k := range keys {
|
||||
os.Unsetenv(k)
|
||||
|
||||
const dv = "42"
|
||||
|
||||
t.Run("shall return the default value", func(t *testing.T) {
|
||||
f := MultiEnvDefaultFunc(keys, dv)
|
||||
|
||||
actual, err := f()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}()
|
||||
if actual != dv {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
})
|
||||
|
||||
// Test that the first key is returned first
|
||||
f := MultiEnvDefaultFunc(keys, "42")
|
||||
if err := os.Setenv(keys[0], "foo"); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
t.Run("shall return the value of the first key", func(t *testing.T) {
|
||||
f := MultiEnvDefaultFunc(keys, dv)
|
||||
t.Setenv(keys[0], "foo")
|
||||
|
||||
actual, err := f()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if actual != "foo" {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
actual, err := f()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if actual != "foo" {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
})
|
||||
|
||||
if err := os.Unsetenv(keys[0]); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
t.Run("shall return the value of the second key", func(t *testing.T) {
|
||||
f := MultiEnvDefaultFunc(keys, dv)
|
||||
t.Setenv(keys[1], "bar")
|
||||
|
||||
// Test that the second key is returned if the first one is empty
|
||||
f = MultiEnvDefaultFunc(keys, "42")
|
||||
if err := os.Setenv(keys[1], "foo"); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual, err = f()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if actual != "foo" {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
|
||||
if err := os.Unsetenv(keys[1]); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Test that the default value is returned when no keys are set
|
||||
actual, err = f()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if actual != "42" {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
actual, err := f()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if actual != "bar" {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValueType_Zero(t *testing.T) {
|
||||
|
@ -36,11 +36,10 @@ func TestConfigureDiscoveryRetry(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("configured retry", func(t *testing.T) {
|
||||
defer func(retryEnv string) {
|
||||
os.Setenv(registryDiscoveryRetryEnvName, retryEnv)
|
||||
defer func() {
|
||||
discoveryRetry = defaultRetry
|
||||
}(os.Getenv(registryDiscoveryRetryEnvName))
|
||||
os.Setenv(registryDiscoveryRetryEnvName, "2")
|
||||
}()
|
||||
t.Setenv(registryDiscoveryRetryEnvName, "2")
|
||||
|
||||
configureDiscoveryRetry()
|
||||
expected := 2
|
||||
@ -72,11 +71,10 @@ func TestConfigureRegistryClientTimeout(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("configured timeout", func(t *testing.T) {
|
||||
defer func(timeoutEnv string) {
|
||||
os.Setenv(registryClientTimeoutEnvName, timeoutEnv)
|
||||
defer func() {
|
||||
requestTimeout = defaultRequestTimeout
|
||||
}(os.Getenv(registryClientTimeoutEnvName))
|
||||
os.Setenv(registryClientTimeoutEnvName, "20")
|
||||
}()
|
||||
t.Setenv(registryClientTimeoutEnvName, "20")
|
||||
|
||||
configureRequestTimeout()
|
||||
expected := 20 * time.Second
|
||||
|
Loading…
Reference in New Issue
Block a user