2023-07-27 10:03:08 +00:00
|
|
|
package configure_test
|
2022-09-05 18:25:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
"github.com/databricks/cli/cmd"
|
2022-09-05 18:25:54 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertKeyValueInSection(t *testing.T, section *ini.Section, keyName, expectedValue string) {
|
|
|
|
key, err := section.GetKey(keyName)
|
2023-07-26 09:37:18 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, expectedValue, key.Value())
|
|
|
|
}
|
2022-09-05 18:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setup(t *testing.T) string {
|
|
|
|
tempHomeDir := t.TempDir()
|
|
|
|
homeEnvVar := "HOME"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
homeEnvVar = "USERPROFILE"
|
|
|
|
}
|
2022-09-07 11:24:11 +00:00
|
|
|
t.Setenv(homeEnvVar, tempHomeDir)
|
|
|
|
t.Setenv("DATABRICKS_CONFIG_FILE", "")
|
2023-07-26 09:37:18 +00:00
|
|
|
t.Setenv("DATABRICKS_TOKEN", "")
|
2022-09-05 18:25:54 +00:00
|
|
|
return tempHomeDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTempFileWithContent(t *testing.T, tempHomeDir string, content string) *os.File {
|
|
|
|
inp, err := os.CreateTemp(tempHomeDir, "input")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = inp.WriteString(content)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = inp.Sync()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = inp.Seek(0, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
return inp
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultConfigureNoInteractive(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tempHomeDir := setup(t)
|
2022-09-06 14:37:58 +00:00
|
|
|
inp := getTempFileWithContent(t, tempHomeDir, "token\n")
|
2022-09-05 18:25:54 +00:00
|
|
|
oldStdin := os.Stdin
|
2022-11-28 10:34:25 +00:00
|
|
|
defer inp.Close()
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Stdin = oldStdin
|
|
|
|
})
|
2022-09-05 18:25:54 +00:00
|
|
|
os.Stdin = inp
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
cmd := cmd.New(ctx)
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.SetArgs([]string{"configure", "--token", "--host", "https://host"})
|
2022-09-05 18:25:54 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
err := cmd.ExecuteContext(ctx)
|
2022-09-05 18:25:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cfgPath := filepath.Join(tempHomeDir, ".databrickscfg")
|
|
|
|
_, err = os.Stat(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cfg, err := ini.Load(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
defaultSection, err := cfg.GetSection("DEFAULT")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-06-15 12:50:19 +00:00
|
|
|
assertKeyValueInSection(t, defaultSection, "host", "https://host")
|
2022-09-05 18:25:54 +00:00
|
|
|
assertKeyValueInSection(t, defaultSection, "token", "token")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigFileFromEnvNoInteractive(t *testing.T) {
|
|
|
|
//TODO: Replace with similar test code from go SDK, once we start using it directly
|
|
|
|
ctx := context.Background()
|
|
|
|
tempHomeDir := setup(t)
|
2023-06-15 12:50:19 +00:00
|
|
|
cfgPath := filepath.Join(tempHomeDir, ".databrickscfg")
|
|
|
|
t.Setenv("DATABRICKS_CONFIG_FILE", cfgPath)
|
2022-09-05 18:25:54 +00:00
|
|
|
|
2022-09-06 14:37:58 +00:00
|
|
|
inp := getTempFileWithContent(t, tempHomeDir, "token\n")
|
2022-11-28 10:34:25 +00:00
|
|
|
defer inp.Close()
|
2022-09-05 18:25:54 +00:00
|
|
|
oldStdin := os.Stdin
|
2022-09-06 14:37:58 +00:00
|
|
|
t.Cleanup(func() { os.Stdin = oldStdin })
|
2022-09-05 18:25:54 +00:00
|
|
|
os.Stdin = inp
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
cmd := cmd.New(ctx)
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.SetArgs([]string{"configure", "--token", "--host", "https://host"})
|
2022-09-05 18:25:54 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
err := cmd.ExecuteContext(ctx)
|
2022-09-05 18:25:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = os.Stat(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cfg, err := ini.Load(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
defaultSection, err := cfg.GetSection("DEFAULT")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-06-15 12:50:19 +00:00
|
|
|
assertKeyValueInSection(t, defaultSection, "host", "https://host")
|
2022-09-05 18:25:54 +00:00
|
|
|
assertKeyValueInSection(t, defaultSection, "token", "token")
|
|
|
|
}
|
2022-09-06 14:37:58 +00:00
|
|
|
|
2023-11-10 14:03:57 +00:00
|
|
|
func TestEnvVarsConfigureNoInteractive(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tempHomeDir := setup(t)
|
|
|
|
cfgPath := filepath.Join(tempHomeDir, ".databrickscfg")
|
|
|
|
inp := getTempFileWithContent(t, tempHomeDir, "token\n")
|
|
|
|
defer inp.Close()
|
|
|
|
oldStdin := os.Stdin
|
|
|
|
t.Cleanup(func() { os.Stdin = oldStdin })
|
|
|
|
os.Stdin = inp
|
|
|
|
|
|
|
|
t.Setenv("DATABRICKS_HOST", "https://host")
|
|
|
|
t.Setenv("DATABRICKS_AUTH_TYPE", "metadata-service")
|
|
|
|
t.Setenv("DATABRICKS_METADATA_SERVICE_URL", "https://metadata")
|
|
|
|
|
|
|
|
cmd := cmd.New(ctx)
|
|
|
|
cmd.SetArgs([]string{"configure", "--token"})
|
|
|
|
|
|
|
|
err := cmd.ExecuteContext(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = os.Stat(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cfg, err := ini.Load(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
defaultSection, err := cfg.GetSection("DEFAULT")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assertKeyValueInSection(t, defaultSection, "host", "https://host")
|
|
|
|
assertKeyValueInSection(t, defaultSection, "token", "token")
|
|
|
|
|
|
|
|
// We should only save host and token for a profile, other env variables should not be saved
|
|
|
|
_, err = defaultSection.GetKey("auth_type")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
_, err = defaultSection.GetKey("metadata_service_url")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvVarsConfigureNoArgsNoInteractive(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tempHomeDir := setup(t)
|
|
|
|
cfgPath := filepath.Join(tempHomeDir, ".databrickscfg")
|
|
|
|
|
|
|
|
t.Setenv("DATABRICKS_HOST", "https://host")
|
|
|
|
t.Setenv("DATABRICKS_TOKEN", "secret")
|
|
|
|
|
|
|
|
cmd := cmd.New(ctx)
|
|
|
|
cmd.SetArgs([]string{"configure"})
|
|
|
|
|
|
|
|
err := cmd.ExecuteContext(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = os.Stat(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cfg, err := ini.Load(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
defaultSection, err := cfg.GetSection("DEFAULT")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assertKeyValueInSection(t, defaultSection, "host", "https://host")
|
|
|
|
assertKeyValueInSection(t, defaultSection, "token", "secret")
|
|
|
|
}
|
|
|
|
|
2022-09-06 14:37:58 +00:00
|
|
|
func TestCustomProfileConfigureNoInteractive(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tempHomeDir := setup(t)
|
2023-06-15 12:50:19 +00:00
|
|
|
cfgPath := filepath.Join(tempHomeDir, ".databrickscfg")
|
2022-09-06 14:37:58 +00:00
|
|
|
inp := getTempFileWithContent(t, tempHomeDir, "token\n")
|
2022-11-28 10:34:25 +00:00
|
|
|
defer inp.Close()
|
2022-09-06 14:37:58 +00:00
|
|
|
oldStdin := os.Stdin
|
|
|
|
t.Cleanup(func() { os.Stdin = oldStdin })
|
|
|
|
os.Stdin = inp
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
cmd := cmd.New(ctx)
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.SetArgs([]string{"configure", "--token", "--host", "https://host", "--profile", "CUSTOM"})
|
2022-09-06 14:37:58 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
err := cmd.ExecuteContext(ctx)
|
2022-09-06 14:37:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = os.Stat(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cfg, err := ini.Load(cfgPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
defaultSection, err := cfg.GetSection("CUSTOM")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-06-15 12:50:19 +00:00
|
|
|
assertKeyValueInSection(t, defaultSection, "host", "https://host")
|
2022-09-06 14:37:58 +00:00
|
|
|
assertKeyValueInSection(t, defaultSection, "token", "token")
|
|
|
|
}
|