2022-09-05 18:25:54 +00:00
|
|
|
package configure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/bricks/cmd/root"
|
|
|
|
"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)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, key.Value(), expectedValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
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", "")
|
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
|
|
|
|
|
2022-09-06 14:37:58 +00:00
|
|
|
root.RootCmd.SetArgs([]string{"configure", "--token", "--no-interactive", "--host", "host"})
|
2022-09-05 18:25:54 +00:00
|
|
|
|
|
|
|
err := root.RootCmd.ExecuteContext(ctx)
|
|
|
|
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)
|
|
|
|
|
|
|
|
assertKeyValueInSection(t, defaultSection, "host", "host")
|
|
|
|
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)
|
|
|
|
cfgFileDir := filepath.Join(tempHomeDir, "test")
|
2022-09-07 11:24:11 +00:00
|
|
|
t.Setenv("DATABRICKS_CONFIG_FILE", cfgFileDir)
|
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
|
|
|
|
|
2022-09-06 14:37:58 +00:00
|
|
|
root.RootCmd.SetArgs([]string{"configure", "--token", "--no-interactive", "--host", "host"})
|
2022-09-05 18:25:54 +00:00
|
|
|
|
|
|
|
err := root.RootCmd.ExecuteContext(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cfgPath := filepath.Join(cfgFileDir, ".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)
|
|
|
|
|
|
|
|
assertKeyValueInSection(t, defaultSection, "host", "host")
|
|
|
|
assertKeyValueInSection(t, defaultSection, "token", "token")
|
|
|
|
}
|
2022-09-06 14:37:58 +00:00
|
|
|
|
|
|
|
func TestCustomProfileConfigureNoInteractive(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tempHomeDir := setup(t)
|
|
|
|
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
|
|
|
|
|
|
|
|
root.RootCmd.SetArgs([]string{"configure", "--token", "--no-interactive", "--host", "host", "--profile", "CUSTOM"})
|
|
|
|
|
|
|
|
err := root.RootCmd.ExecuteContext(ctx)
|
|
|
|
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("CUSTOM")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assertKeyValueInSection(t, defaultSection, "host", "host")
|
|
|
|
assertKeyValueInSection(t, defaultSection, "token", "token")
|
|
|
|
}
|