2023-04-11 11:11:31 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-23 11:51:15 +00:00
|
|
|
"os"
|
2023-04-11 11:11:31 +00:00
|
|
|
"os/exec"
|
2024-04-02 12:56:27 +00:00
|
|
|
"path/filepath"
|
2023-05-23 11:51:15 +00:00
|
|
|
"runtime"
|
2023-06-14 19:58:26 +00:00
|
|
|
"strings"
|
2023-04-11 11:11:31 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2024-04-02 12:56:27 +00:00
|
|
|
"github.com/databricks/cli/bundle/internal/tf/schema"
|
2024-04-12 15:22:30 +00:00
|
|
|
"github.com/databricks/cli/internal/testutil"
|
2024-04-02 12:56:27 +00:00
|
|
|
"github.com/databricks/cli/libs/env"
|
|
|
|
"github.com/hashicorp/hc-install/product"
|
2023-05-23 11:51:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-11 11:11:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-14 19:58:26 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2023-04-11 11:11:31 +00:00
|
|
|
)
|
|
|
|
|
2023-05-23 11:51:15 +00:00
|
|
|
func unsetEnv(t *testing.T, name string) {
|
|
|
|
t.Setenv(name, "")
|
|
|
|
err := os.Unsetenv(name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:11:31 +00:00
|
|
|
func TestInitEnvironmentVariables(t *testing.T) {
|
|
|
|
_, err := exec.LookPath("terraform")
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("cannot find terraform binary: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2024-03-27 09:03:24 +00:00
|
|
|
RootPath: t.TempDir(),
|
2023-04-11 11:11:31 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Target: "whatever",
|
2023-04-11 11:11:31 +00:00
|
|
|
Terraform: &config.Terraform{
|
|
|
|
ExecPath: "terraform",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger initialization of workspace client.
|
|
|
|
// TODO(pietern): create test fixture that initializes a mocked client.
|
|
|
|
t.Setenv("DATABRICKS_HOST", "https://x")
|
|
|
|
t.Setenv("DATABRICKS_TOKEN", "foobar")
|
2023-11-15 14:03:36 +00:00
|
|
|
b.WorkspaceClient()
|
2023-04-11 11:11:31 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, Initialize())
|
|
|
|
require.NoError(t, diags.Error())
|
2023-04-11 11:11:31 +00:00
|
|
|
}
|
2023-05-23 11:51:15 +00:00
|
|
|
|
|
|
|
func TestSetTempDirEnvVarsForUnixWithTmpDirSet(t *testing.T) {
|
|
|
|
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &bundle.Bundle{
|
2024-03-27 09:03:24 +00:00
|
|
|
RootPath: t.TempDir(),
|
2023-05-23 11:51:15 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Target: "whatever",
|
2023-05-23 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set TMPDIR environment variable
|
|
|
|
t.Setenv("TMPDIR", "/foo/bar")
|
|
|
|
|
|
|
|
// compute env
|
|
|
|
env := make(map[string]string, 0)
|
2023-09-11 08:18:43 +00:00
|
|
|
err := setTempDirEnvVars(context.Background(), env, b)
|
2023-05-23 11:51:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-26 11:05:30 +00:00
|
|
|
// Assert that we pass through TMPDIR.
|
2023-05-23 11:51:15 +00:00
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"TMPDIR": "/foo/bar",
|
|
|
|
}, env)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetTempDirEnvVarsForUnixWithTmpDirNotSet(t *testing.T) {
|
|
|
|
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &bundle.Bundle{
|
2024-03-27 09:03:24 +00:00
|
|
|
RootPath: t.TempDir(),
|
2023-05-23 11:51:15 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Target: "whatever",
|
2023-05-23 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unset TMPDIR environment variable confirm it's not set
|
|
|
|
unsetEnv(t, "TMPDIR")
|
|
|
|
|
|
|
|
// compute env
|
|
|
|
env := make(map[string]string, 0)
|
2023-09-11 08:18:43 +00:00
|
|
|
err := setTempDirEnvVars(context.Background(), env, b)
|
2023-05-23 11:51:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-26 11:05:30 +00:00
|
|
|
// Assert that we don't pass through TMPDIR.
|
|
|
|
assert.Equal(t, map[string]string{}, env)
|
2023-05-23 11:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetTempDirEnvVarsForWindowWithAllTmpDirEnvVarsSet(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &bundle.Bundle{
|
2024-03-27 09:03:24 +00:00
|
|
|
RootPath: t.TempDir(),
|
2023-05-23 11:51:15 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Target: "whatever",
|
2023-05-23 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set environment variables
|
|
|
|
t.Setenv("TMP", "c:\\foo\\a")
|
|
|
|
t.Setenv("TEMP", "c:\\foo\\b")
|
|
|
|
t.Setenv("USERPROFILE", "c:\\foo\\c")
|
|
|
|
|
|
|
|
// compute env
|
|
|
|
env := make(map[string]string, 0)
|
2023-09-11 08:18:43 +00:00
|
|
|
err := setTempDirEnvVars(context.Background(), env, b)
|
2023-05-23 11:51:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert that we pass through the highest priority env var value
|
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"TMP": "c:\\foo\\a",
|
|
|
|
}, env)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetTempDirEnvVarsForWindowWithUserProfileAndTempSet(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &bundle.Bundle{
|
2024-03-27 09:03:24 +00:00
|
|
|
RootPath: t.TempDir(),
|
2023-05-23 11:51:15 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Target: "whatever",
|
2023-05-23 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set environment variables
|
|
|
|
unsetEnv(t, "TMP")
|
|
|
|
t.Setenv("TEMP", "c:\\foo\\b")
|
|
|
|
t.Setenv("USERPROFILE", "c:\\foo\\c")
|
|
|
|
|
|
|
|
// compute env
|
|
|
|
env := make(map[string]string, 0)
|
2023-09-11 08:18:43 +00:00
|
|
|
err := setTempDirEnvVars(context.Background(), env, b)
|
2023-05-23 11:51:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert that we pass through the highest priority env var value
|
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"TEMP": "c:\\foo\\b",
|
|
|
|
}, env)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetTempDirEnvVarsForWindowsWithoutAnyTempDirEnvVarsSet(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &bundle.Bundle{
|
2024-03-27 09:03:24 +00:00
|
|
|
RootPath: t.TempDir(),
|
2023-05-23 11:51:15 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Target: "whatever",
|
2023-05-23 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// unset all env vars
|
|
|
|
unsetEnv(t, "TMP")
|
|
|
|
unsetEnv(t, "TEMP")
|
|
|
|
unsetEnv(t, "USERPROFILE")
|
|
|
|
|
|
|
|
// compute env
|
|
|
|
env := make(map[string]string, 0)
|
2023-09-11 08:18:43 +00:00
|
|
|
err := setTempDirEnvVars(context.Background(), env, b)
|
2023-05-23 11:51:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert TMP is set to b.CacheDir("tmp")
|
2023-09-11 08:18:43 +00:00
|
|
|
tmpDir, err := b.CacheDir(context.Background(), "tmp")
|
2023-05-23 11:51:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"TMP": tmpDir,
|
|
|
|
}, env)
|
|
|
|
}
|
2023-06-14 19:58:26 +00:00
|
|
|
|
|
|
|
func TestSetProxyEnvVars(t *testing.T) {
|
|
|
|
b := &bundle.Bundle{
|
2024-03-27 09:03:24 +00:00
|
|
|
RootPath: t.TempDir(),
|
2023-06-14 19:58:26 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Target: "whatever",
|
2023-06-14 19:58:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Temporarily clear environment variables.
|
|
|
|
clearEnv := func() {
|
|
|
|
for _, v := range []string{"http_proxy", "https_proxy", "no_proxy"} {
|
|
|
|
for _, v := range []string{strings.ToUpper(v), strings.ToLower(v)} {
|
|
|
|
t.Setenv(v, "foo")
|
|
|
|
os.Unsetenv(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No proxy env vars set.
|
|
|
|
clearEnv()
|
|
|
|
env := make(map[string]string, 0)
|
2023-09-11 08:18:43 +00:00
|
|
|
err := setProxyEnvVars(context.Background(), env, b)
|
2023-06-14 19:58:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, env, 0)
|
|
|
|
|
|
|
|
// Lower case set.
|
|
|
|
clearEnv()
|
|
|
|
t.Setenv("http_proxy", "foo")
|
|
|
|
t.Setenv("https_proxy", "foo")
|
|
|
|
t.Setenv("no_proxy", "foo")
|
|
|
|
env = make(map[string]string, 0)
|
2023-09-11 08:18:43 +00:00
|
|
|
err = setProxyEnvVars(context.Background(), env, b)
|
2023-06-14 19:58:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env))
|
|
|
|
|
|
|
|
// Upper case set.
|
|
|
|
clearEnv()
|
|
|
|
t.Setenv("HTTP_PROXY", "foo")
|
|
|
|
t.Setenv("HTTPS_PROXY", "foo")
|
|
|
|
t.Setenv("NO_PROXY", "foo")
|
|
|
|
env = make(map[string]string, 0)
|
2023-09-11 08:18:43 +00:00
|
|
|
err = setProxyEnvVars(context.Background(), env, b)
|
2023-06-14 19:58:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env))
|
|
|
|
}
|
2023-07-07 11:20:37 +00:00
|
|
|
|
2024-07-01 07:46:37 +00:00
|
|
|
func TestSetUserAgentExtraEnvVar(t *testing.T) {
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
RootPath: t.TempDir(),
|
|
|
|
Config: config.Root{
|
|
|
|
Experimental: &config.Experimental{
|
|
|
|
PyDABs: config.PyDABs{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
env := make(map[string]string, 0)
|
|
|
|
err := setUserAgentExtraEnvVar(env, b)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"DATABRICKS_USER_AGENT_EXTRA": "databricks-pydabs/0.0.0",
|
|
|
|
}, env)
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:20:37 +00:00
|
|
|
func TestInheritEnvVars(t *testing.T) {
|
|
|
|
env := map[string]string{}
|
|
|
|
|
|
|
|
t.Setenv("HOME", "/home/testuser")
|
2023-09-06 07:54:35 +00:00
|
|
|
t.Setenv("PATH", "/foo:/bar")
|
2023-07-07 11:20:37 +00:00
|
|
|
t.Setenv("TF_CLI_CONFIG_FILE", "/tmp/config.tfrc")
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
err := inheritEnvVars(context.Background(), env)
|
2023-07-07 11:20:37 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-22 09:16:28 +00:00
|
|
|
require.Equal(t, env["HOME"], "/home/testuser")
|
|
|
|
require.Equal(t, env["PATH"], "/foo:/bar")
|
|
|
|
require.Equal(t, env["TF_CLI_CONFIG_FILE"], "/tmp/config.tfrc")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetUserProfileFromInheritEnvVars(t *testing.T) {
|
|
|
|
t.Setenv("USERPROFILE", "c:\\foo\\c")
|
|
|
|
|
|
|
|
env := make(map[string]string, 0)
|
|
|
|
err := inheritEnvVars(context.Background(), env)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, env, "USERPROFILE")
|
|
|
|
assert.Equal(t, env["USERPROFILE"], "c:\\foo\\c")
|
2023-07-07 11:20:37 +00:00
|
|
|
}
|
2024-04-02 12:56:27 +00:00
|
|
|
|
|
|
|
func TestInheritEnvVarsWithAbsentTFConfigFile(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
envMap := map[string]string{}
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_PROVIDER_VERSION", schema.ProviderVersion)
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_CLI_CONFIG_FILE", "/tmp/config.tfrc")
|
|
|
|
err := inheritEnvVars(ctx, envMap)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotContains(t, envMap, "TF_CLI_CONFIG_FILE")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInheritEnvVarsWithWrongTFProviderVersion(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
envMap := map[string]string{}
|
|
|
|
configFile := createTempFile(t, t.TempDir(), "config.tfrc", false)
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_PROVIDER_VERSION", "wrong")
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_CLI_CONFIG_FILE", configFile)
|
|
|
|
err := inheritEnvVars(ctx, envMap)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotContains(t, envMap, "TF_CLI_CONFIG_FILE")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInheritEnvVarsWithCorrectTFCLIConfigFile(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
envMap := map[string]string{}
|
|
|
|
configFile := createTempFile(t, t.TempDir(), "config.tfrc", false)
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_PROVIDER_VERSION", schema.ProviderVersion)
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_CLI_CONFIG_FILE", configFile)
|
|
|
|
err := inheritEnvVars(ctx, envMap)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, envMap, "TF_CLI_CONFIG_FILE")
|
|
|
|
require.Equal(t, configFile, envMap["TF_CLI_CONFIG_FILE"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindExecPathFromEnvironmentWithWrongVersion(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
m := &initialize{}
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
RootPath: t.TempDir(),
|
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "whatever",
|
|
|
|
Terraform: &config.Terraform{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Create a pre-existing terraform bin to avoid downloading it
|
|
|
|
cacheDir, _ := b.CacheDir(ctx, "bin")
|
|
|
|
existingExecPath := createTempFile(t, cacheDir, product.Terraform.BinaryName(), true)
|
|
|
|
// Create a new terraform binary and expose it through env vars
|
|
|
|
tmpBinPath := createTempFile(t, t.TempDir(), "terraform-bin", true)
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_VERSION", "1.2.3")
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_EXEC_PATH", tmpBinPath)
|
|
|
|
_, err := m.findExecPath(ctx, b, b.Config.Bundle.Terraform)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, existingExecPath, b.Config.Bundle.Terraform.ExecPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindExecPathFromEnvironmentWithCorrectVersionAndNoBinary(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
m := &initialize{}
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
RootPath: t.TempDir(),
|
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "whatever",
|
|
|
|
Terraform: &config.Terraform{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Create a pre-existing terraform bin to avoid downloading it
|
|
|
|
cacheDir, _ := b.CacheDir(ctx, "bin")
|
|
|
|
existingExecPath := createTempFile(t, cacheDir, product.Terraform.BinaryName(), true)
|
|
|
|
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_VERSION", TerraformVersion.String())
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_EXEC_PATH", "/tmp/terraform")
|
|
|
|
_, err := m.findExecPath(ctx, b, b.Config.Bundle.Terraform)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, existingExecPath, b.Config.Bundle.Terraform.ExecPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindExecPathFromEnvironmentWithCorrectVersionAndBinary(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
m := &initialize{}
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
RootPath: t.TempDir(),
|
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "whatever",
|
|
|
|
Terraform: &config.Terraform{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Create a pre-existing terraform bin to avoid downloading it
|
|
|
|
cacheDir, _ := b.CacheDir(ctx, "bin")
|
|
|
|
createTempFile(t, cacheDir, product.Terraform.BinaryName(), true)
|
|
|
|
// Create a new terraform binary and expose it through env vars
|
|
|
|
tmpBinPath := createTempFile(t, t.TempDir(), "terraform-bin", true)
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_VERSION", TerraformVersion.String())
|
|
|
|
ctx = env.Set(ctx, "DATABRICKS_TF_EXEC_PATH", tmpBinPath)
|
|
|
|
_, err := m.findExecPath(ctx, b, b.Config.Bundle.Terraform)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tmpBinPath, b.Config.Bundle.Terraform.ExecPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTempFile(t *testing.T, dest string, name string, executable bool) string {
|
|
|
|
binPath := filepath.Join(dest, name)
|
|
|
|
f, err := os.Create(binPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
err = f.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
if executable {
|
|
|
|
err = f.Chmod(0777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
return binPath
|
|
|
|
}
|
2024-04-12 15:22:30 +00:00
|
|
|
|
|
|
|
func TestGetEnvVarWithMatchingVersion(t *testing.T) {
|
|
|
|
envVarName := "FOO"
|
|
|
|
versionVarName := "FOO_VERSION"
|
|
|
|
|
|
|
|
tmp := t.TempDir()
|
2024-04-19 15:05:36 +00:00
|
|
|
file := testutil.Touch(t, tmp, "bar")
|
2024-04-12 15:22:30 +00:00
|
|
|
|
|
|
|
var tc = []struct {
|
|
|
|
envValue string
|
|
|
|
versionValue string
|
|
|
|
currentVersion string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2024-04-19 15:05:36 +00:00
|
|
|
envValue: file,
|
2024-04-12 15:22:30 +00:00
|
|
|
versionValue: "1.2.3",
|
|
|
|
currentVersion: "1.2.3",
|
2024-04-19 15:05:36 +00:00
|
|
|
expected: file,
|
2024-04-12 15:22:30 +00:00
|
|
|
},
|
|
|
|
{
|
2024-04-19 15:05:36 +00:00
|
|
|
envValue: "does-not-exist",
|
2024-04-12 15:22:30 +00:00
|
|
|
versionValue: "1.2.3",
|
|
|
|
currentVersion: "1.2.3",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
2024-04-19 15:05:36 +00:00
|
|
|
envValue: file,
|
2024-04-12 15:22:30 +00:00
|
|
|
versionValue: "1.2.3",
|
|
|
|
currentVersion: "1.2.4",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
envValue: "",
|
|
|
|
versionValue: "1.2.3",
|
|
|
|
currentVersion: "1.2.3",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
2024-04-19 15:05:36 +00:00
|
|
|
envValue: file,
|
2024-04-12 15:22:30 +00:00
|
|
|
versionValue: "",
|
|
|
|
currentVersion: "1.2.3",
|
2024-04-19 15:05:36 +00:00
|
|
|
expected: file,
|
2024-04-12 15:22:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range tc {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
t.Setenv(envVarName, c.envValue)
|
|
|
|
t.Setenv(versionVarName, c.versionValue)
|
|
|
|
|
|
|
|
actual, err := getEnvVarWithMatchingVersion(context.Background(), envVarName, versionVarName, c.currentVersion)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, c.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|