mirror of https://github.com/databricks/cli.git
parent
27f1ae3826
commit
9cff7c6619
|
@ -8,6 +8,7 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/cli/bundle"
|
||||||
"github.com/databricks/cli/internal/testutil"
|
"github.com/databricks/cli/internal/testutil"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -37,7 +38,7 @@ func emptyCommand(t *testing.T) *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupWithHost(t *testing.T, cmd *cobra.Command, host string) {
|
func setupWithHost(t *testing.T, cmd *cobra.Command, host string) *bundle.Bundle {
|
||||||
setupDatabricksCfg(t)
|
setupDatabricksCfg(t)
|
||||||
|
|
||||||
rootPath := t.TempDir()
|
rootPath := t.TempDir()
|
||||||
|
@ -49,9 +50,13 @@ workspace:
|
||||||
`, host)
|
`, host)
|
||||||
err := os.WriteFile(filepath.Join(rootPath, "databricks.yml"), []byte(contents), 0o644)
|
err := os.WriteFile(filepath.Join(rootPath, "databricks.yml"), []byte(contents), 0o644)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
b, diags := MustConfigureBundle(cmd)
|
||||||
|
require.NoError(t, diags.Error())
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupWithProfile(t *testing.T, cmd *cobra.Command, profile string) {
|
func setupWithProfile(t *testing.T, cmd *cobra.Command, profile string) *bundle.Bundle {
|
||||||
setupDatabricksCfg(t)
|
setupDatabricksCfg(t)
|
||||||
|
|
||||||
rootPath := t.TempDir()
|
rootPath := t.TempDir()
|
||||||
|
@ -63,164 +68,157 @@ workspace:
|
||||||
`, profile)
|
`, profile)
|
||||||
err := os.WriteFile(filepath.Join(rootPath, "databricks.yml"), []byte(contents), 0o644)
|
err := os.WriteFile(filepath.Join(rootPath, "databricks.yml"), []byte(contents), 0o644)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
b, diags := MustConfigureBundle(cmd)
|
||||||
|
require.NoError(t, diags.Error())
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBundleConfigureProfile(t *testing.T) {
|
func TestBundleConfigureDefault(t *testing.T) {
|
||||||
tcases := []struct {
|
testutil.CleanupEnvironment(t)
|
||||||
name string
|
|
||||||
hostInConfig string
|
|
||||||
|
|
||||||
// --profile flag
|
cmd := emptyCommand(t)
|
||||||
profileFlag string
|
b := setupWithHost(t, cmd, "https://x.com")
|
||||||
// DATABRICKS_CONFIG_PROFILE environment variable
|
|
||||||
profileEnvVar string
|
|
||||||
// profile in config
|
|
||||||
profileInConfig string
|
|
||||||
|
|
||||||
expectedError string
|
client, err := b.InitializeWorkspaceClient()
|
||||||
expectedHost string
|
require.NoError(t, err)
|
||||||
expectedProfile string
|
assert.Equal(t, "https://x.com", client.Config.Host)
|
||||||
expectedToken string
|
}
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "no match, keep host",
|
|
||||||
hostInConfig: "https://x.com",
|
|
||||||
|
|
||||||
expectedHost: "https://x.com",
|
func TestBundleConfigureWithMultipleMatches(t *testing.T) {
|
||||||
},
|
testutil.CleanupEnvironment(t)
|
||||||
{
|
|
||||||
name: "multiple profile matches",
|
|
||||||
hostInConfig: "https://a.com",
|
|
||||||
|
|
||||||
expectedError: "multiple profiles matched: PROFILE-1, PROFILE-2",
|
cmd := emptyCommand(t)
|
||||||
},
|
b := setupWithHost(t, cmd, "https://a.com")
|
||||||
{
|
|
||||||
name: "non-existent profile",
|
|
||||||
profileFlag: "NOEXIST",
|
|
||||||
hostInConfig: "https://x.com",
|
|
||||||
|
|
||||||
expectedError: "has no NOEXIST profile configured",
|
_, err := b.InitializeWorkspaceClient()
|
||||||
},
|
assert.ErrorContains(t, err, "multiple profiles matched: PROFILE-1, PROFILE-2")
|
||||||
{
|
}
|
||||||
name: "mismatched profile",
|
|
||||||
hostInConfig: "https://x.com",
|
|
||||||
profileFlag: "PROFILE-1",
|
|
||||||
|
|
||||||
expectedError: "config host mismatch: profile uses host https://a.com, but CLI configured to use https://x.com",
|
func TestBundleConfigureWithNonExistentProfileFlag(t *testing.T) {
|
||||||
},
|
testutil.CleanupEnvironment(t)
|
||||||
{
|
|
||||||
name: "profile flag specified",
|
|
||||||
hostInConfig: "https://a.com",
|
|
||||||
profileFlag: "PROFILE-1",
|
|
||||||
|
|
||||||
expectedHost: "https://a.com",
|
cmd := emptyCommand(t)
|
||||||
expectedProfile: "PROFILE-1",
|
err := cmd.Flag("profile").Value.Set("NOEXIST")
|
||||||
},
|
require.NoError(t, err)
|
||||||
{
|
b := setupWithHost(t, cmd, "https://x.com")
|
||||||
name: "mismatched profile env variable",
|
|
||||||
hostInConfig: "https://x.com",
|
|
||||||
profileEnvVar: "PROFILE-1",
|
|
||||||
|
|
||||||
expectedError: "config host mismatch: profile uses host https://a.com, but CLI configured to use https://x.com",
|
_, err = b.InitializeWorkspaceClient()
|
||||||
},
|
assert.ErrorContains(t, err, "has no NOEXIST profile configured")
|
||||||
{
|
}
|
||||||
// The --profile flag takes precedence over the DATABRICKS_CONFIG_PROFILE environment variable
|
|
||||||
name: "(host) profile flag takes precedence over env variable",
|
|
||||||
hostInConfig: "https://a.com",
|
|
||||||
profileFlag: "PROFILE-1",
|
|
||||||
profileEnvVar: "NOEXIST",
|
|
||||||
|
|
||||||
expectedHost: "https://a.com",
|
func TestBundleConfigureWithMismatchedProfile(t *testing.T) {
|
||||||
expectedProfile: "PROFILE-1",
|
testutil.CleanupEnvironment(t)
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "profile from config",
|
|
||||||
profileInConfig: "PROFILE-1",
|
|
||||||
|
|
||||||
expectedHost: "https://a.com",
|
cmd := emptyCommand(t)
|
||||||
expectedProfile: "PROFILE-1",
|
err := cmd.Flag("profile").Value.Set("PROFILE-1")
|
||||||
expectedToken: "a",
|
require.NoError(t, err)
|
||||||
},
|
b := setupWithHost(t, cmd, "https://x.com")
|
||||||
{
|
|
||||||
// The --profile flag takes precedence over the profile in the databricks.yml file
|
|
||||||
name: "profile flag takes precedence",
|
|
||||||
profileInConfig: "PROFILE-1",
|
|
||||||
profileFlag: "PROFILE-2",
|
|
||||||
|
|
||||||
expectedHost: "https://a.com",
|
_, err = b.InitializeWorkspaceClient()
|
||||||
expectedProfile: "PROFILE-2",
|
assert.ErrorContains(t, err, "config host mismatch: profile uses host https://a.com, but CLI configured to use https://x.com")
|
||||||
expectedToken: "b",
|
}
|
||||||
},
|
|
||||||
{
|
|
||||||
// The DATABRICKS_CONFIG_PROFILE environment variable takes precedence over the profile in the databricks.yml file
|
|
||||||
name: "profile env variable takes precedence",
|
|
||||||
profileInConfig: "PROFILE-1",
|
|
||||||
profileEnvVar: "PROFILE-2",
|
|
||||||
|
|
||||||
expectedHost: "https://a.com",
|
func TestBundleConfigureWithCorrectProfile(t *testing.T) {
|
||||||
expectedProfile: "PROFILE-2",
|
testutil.CleanupEnvironment(t)
|
||||||
expectedToken: "b",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// The --profile flag takes precedence over the DATABRICKS_CONFIG_PROFILE environment variable
|
|
||||||
name: "profile flag takes precedence over env variable",
|
|
||||||
profileInConfig: "PROFILE-1",
|
|
||||||
profileFlag: "PROFILE-2",
|
|
||||||
profileEnvVar: "NOEXIST",
|
|
||||||
|
|
||||||
expectedHost: "https://a.com",
|
cmd := emptyCommand(t)
|
||||||
expectedProfile: "PROFILE-2",
|
err := cmd.Flag("profile").Value.Set("PROFILE-1")
|
||||||
expectedToken: "b",
|
require.NoError(t, err)
|
||||||
},
|
b := setupWithHost(t, cmd, "https://a.com")
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range tcases {
|
client, err := b.InitializeWorkspaceClient()
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
require.NoError(t, err)
|
||||||
testutil.CleanupEnvironment(t)
|
assert.Equal(t, "https://a.com", client.Config.Host)
|
||||||
|
assert.Equal(t, "PROFILE-1", client.Config.Profile)
|
||||||
|
}
|
||||||
|
|
||||||
cmd := emptyCommand(t)
|
func TestBundleConfigureWithMismatchedProfileEnvVariable(t *testing.T) {
|
||||||
|
testutil.CleanupEnvironment(t)
|
||||||
|
|
||||||
// Set up host in databricks.yml
|
t.Setenv("DATABRICKS_CONFIG_PROFILE", "PROFILE-1")
|
||||||
if tc.hostInConfig != "" {
|
cmd := emptyCommand(t)
|
||||||
setupWithHost(t, cmd, tc.hostInConfig)
|
b := setupWithHost(t, cmd, "https://x.com")
|
||||||
}
|
|
||||||
|
|
||||||
// Set up profile in databricks.yml
|
_, err := b.InitializeWorkspaceClient()
|
||||||
if tc.profileInConfig != "" {
|
assert.ErrorContains(t, err, "config host mismatch: profile uses host https://a.com, but CLI configured to use https://x.com")
|
||||||
setupWithProfile(t, cmd, tc.profileInConfig)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Set --profile flag
|
func TestBundleConfigureWithProfileFlagAndEnvVariable(t *testing.T) {
|
||||||
if tc.profileFlag != "" {
|
testutil.CleanupEnvironment(t)
|
||||||
err := cmd.Flag("profile").Value.Set(tc.profileFlag)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set DATABRICKS_CONFIG_PROFILE environment variable
|
t.Setenv("DATABRICKS_CONFIG_PROFILE", "NOEXIST")
|
||||||
if tc.profileEnvVar != "" {
|
cmd := emptyCommand(t)
|
||||||
t.Setenv("DATABRICKS_CONFIG_PROFILE", tc.profileEnvVar)
|
err := cmd.Flag("profile").Value.Set("PROFILE-1")
|
||||||
}
|
require.NoError(t, err)
|
||||||
|
b := setupWithHost(t, cmd, "https://a.com")
|
||||||
|
|
||||||
_, diags := MustConfigureBundle(cmd)
|
client, err := b.InitializeWorkspaceClient()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "https://a.com", client.Config.Host)
|
||||||
|
assert.Equal(t, "PROFILE-1", client.Config.Profile)
|
||||||
|
}
|
||||||
|
|
||||||
if tc.expectedError != "" {
|
func TestBundleConfigureProfileDefault(t *testing.T) {
|
||||||
assert.ErrorContains(t, diags.Error(), tc.expectedError)
|
testutil.CleanupEnvironment(t)
|
||||||
} else {
|
|
||||||
assert.NoError(t, diags.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assert on the resolved configuration values
|
// The profile in the databricks.yml file is used
|
||||||
if tc.expectedHost != "" {
|
cmd := emptyCommand(t)
|
||||||
assert.Equal(t, tc.expectedHost, ConfigUsed(cmd.Context()).Host)
|
b := setupWithProfile(t, cmd, "PROFILE-1")
|
||||||
}
|
|
||||||
if tc.expectedProfile != "" {
|
client, err := b.InitializeWorkspaceClient()
|
||||||
assert.Equal(t, tc.expectedProfile, ConfigUsed(cmd.Context()).Profile)
|
require.NoError(t, err)
|
||||||
}
|
assert.Equal(t, "https://a.com", client.Config.Host)
|
||||||
if tc.expectedToken != "" {
|
assert.Equal(t, "a", client.Config.Token)
|
||||||
assert.Equal(t, tc.expectedToken, ConfigUsed(cmd.Context()).Token)
|
assert.Equal(t, "PROFILE-1", client.Config.Profile)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
func TestBundleConfigureProfileFlag(t *testing.T) {
|
||||||
|
testutil.CleanupEnvironment(t)
|
||||||
|
|
||||||
|
// The --profile flag takes precedence over the profile in the databricks.yml file
|
||||||
|
cmd := emptyCommand(t)
|
||||||
|
err := cmd.Flag("profile").Value.Set("PROFILE-2")
|
||||||
|
require.NoError(t, err)
|
||||||
|
b := setupWithProfile(t, cmd, "PROFILE-1")
|
||||||
|
|
||||||
|
client, err := b.InitializeWorkspaceClient()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "https://a.com", client.Config.Host)
|
||||||
|
assert.Equal(t, "b", client.Config.Token)
|
||||||
|
assert.Equal(t, "PROFILE-2", client.Config.Profile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBundleConfigureProfileEnvVariable(t *testing.T) {
|
||||||
|
testutil.CleanupEnvironment(t)
|
||||||
|
|
||||||
|
// The DATABRICKS_CONFIG_PROFILE environment variable takes precedence over the profile in the databricks.yml file
|
||||||
|
t.Setenv("DATABRICKS_CONFIG_PROFILE", "PROFILE-2")
|
||||||
|
cmd := emptyCommand(t)
|
||||||
|
b := setupWithProfile(t, cmd, "PROFILE-1")
|
||||||
|
|
||||||
|
client, err := b.InitializeWorkspaceClient()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "https://a.com", client.Config.Host)
|
||||||
|
assert.Equal(t, "b", client.Config.Token)
|
||||||
|
assert.Equal(t, "PROFILE-2", client.Config.Profile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBundleConfigureProfileFlagAndEnvVariable(t *testing.T) {
|
||||||
|
testutil.CleanupEnvironment(t)
|
||||||
|
|
||||||
|
// The --profile flag takes precedence over the DATABRICKS_CONFIG_PROFILE environment variable
|
||||||
|
t.Setenv("DATABRICKS_CONFIG_PROFILE", "NOEXIST")
|
||||||
|
cmd := emptyCommand(t)
|
||||||
|
err := cmd.Flag("profile").Value.Set("PROFILE-2")
|
||||||
|
require.NoError(t, err)
|
||||||
|
b := setupWithProfile(t, cmd, "PROFILE-1")
|
||||||
|
|
||||||
|
client, err := b.InitializeWorkspaceClient()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "https://a.com", client.Config.Host)
|
||||||
|
assert.Equal(t, "b", client.Config.Token)
|
||||||
|
assert.Equal(t, "PROFILE-2", client.Config.Profile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTargetFlagFull(t *testing.T) {
|
func TestTargetFlagFull(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue