2023-07-12 12:09:25 +00:00
|
|
|
package root
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2023-07-26 11:17:09 +00:00
|
|
|
"github.com/spf13/cobra"
|
2023-07-12 12:09:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupDatabricksCfg(t *testing.T) {
|
|
|
|
tempHomeDir := t.TempDir()
|
|
|
|
homeEnvVar := "HOME"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
homeEnvVar = "USERPROFILE"
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := []byte("[PROFILE-1]\nhost = https://a.com\ntoken = a\n[PROFILE-2]\nhost = https://a.com\ntoken = b\n")
|
|
|
|
err := os.WriteFile(filepath.Join(tempHomeDir, ".databrickscfg"), cfg, 0644)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
t.Setenv("DATABRICKS_CONFIG_FILE", "")
|
|
|
|
t.Setenv(homeEnvVar, tempHomeDir)
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
func emptyCommand(t *testing.T) *cobra.Command {
|
2023-07-12 12:09:25 +00:00
|
|
|
ctx := context.Background()
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd := &cobra.Command{}
|
|
|
|
cmd.SetContext(ctx)
|
|
|
|
initProfileFlag(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
2023-07-12 12:09:25 +00:00
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
func setup(t *testing.T, cmd *cobra.Command, host string) *bundle.Bundle {
|
|
|
|
setupDatabricksCfg(t)
|
|
|
|
|
2023-08-11 12:28:05 +00:00
|
|
|
err := configureBundle(cmd, []string{"validate"}, func(_ context.Context) (*bundle.Bundle, error) {
|
2023-07-12 12:09:25 +00:00
|
|
|
return &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Name: "test",
|
|
|
|
},
|
|
|
|
Workspace: config.Workspace{
|
|
|
|
Host: host,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2023-07-26 11:17:09 +00:00
|
|
|
return bundle.Get(cmd.Context())
|
2023-07-12 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleConfigureDefault(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd := emptyCommand(t)
|
|
|
|
b := setup(t, cmd, "https://x.com")
|
2023-07-12 12:09:25 +00:00
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
b.WorkspaceClient()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleConfigureWithMultipleMatches(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd := emptyCommand(t)
|
|
|
|
b := setup(t, cmd, "https://a.com")
|
2023-07-12 12:09:25 +00:00
|
|
|
assert.Panics(t, func() {
|
|
|
|
b.WorkspaceClient()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleConfigureWithNonExistentProfileFlag(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd := emptyCommand(t)
|
|
|
|
cmd.Flag("profile").Value.Set("NOEXIST")
|
2023-07-12 12:09:25 +00:00
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
b := setup(t, cmd, "https://x.com")
|
2023-07-12 12:09:25 +00:00
|
|
|
assert.PanicsWithError(t, "no matching config profiles found", func() {
|
|
|
|
b.WorkspaceClient()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleConfigureWithMismatchedProfile(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd := emptyCommand(t)
|
|
|
|
cmd.Flag("profile").Value.Set("PROFILE-1")
|
2023-07-12 12:09:25 +00:00
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
b := setup(t, cmd, "https://x.com")
|
2023-07-12 12:09:25 +00:00
|
|
|
assert.PanicsWithError(t, "config host mismatch: profile uses host https://a.com, but CLI configured to use https://x.com", func() {
|
|
|
|
b.WorkspaceClient()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleConfigureWithCorrectProfile(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd := emptyCommand(t)
|
|
|
|
cmd.Flag("profile").Value.Set("PROFILE-1")
|
2023-07-12 12:09:25 +00:00
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
b := setup(t, cmd, "https://a.com")
|
2023-07-12 12:09:25 +00:00
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
b.WorkspaceClient()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleConfigureWithMismatchedProfileEnvVariable(t *testing.T) {
|
|
|
|
t.Setenv("DATABRICKS_CONFIG_PROFILE", "PROFILE-1")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
t.Setenv("DATABRICKS_CONFIG_PROFILE", "")
|
|
|
|
})
|
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd := emptyCommand(t)
|
|
|
|
b := setup(t, cmd, "https://x.com")
|
2023-07-12 12:09:25 +00:00
|
|
|
assert.PanicsWithError(t, "config host mismatch: profile uses host https://a.com, but CLI configured to use https://x.com", func() {
|
|
|
|
b.WorkspaceClient()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleConfigureWithProfileFlagAndEnvVariable(t *testing.T) {
|
|
|
|
t.Setenv("DATABRICKS_CONFIG_PROFILE", "NOEXIST")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
t.Setenv("DATABRICKS_CONFIG_PROFILE", "")
|
|
|
|
})
|
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd := emptyCommand(t)
|
|
|
|
cmd.Flag("profile").Value.Set("PROFILE-1")
|
|
|
|
|
|
|
|
b := setup(t, cmd, "https://a.com")
|
2023-07-12 12:09:25 +00:00
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
b.WorkspaceClient()
|
|
|
|
})
|
|
|
|
}
|
2023-08-17 15:22:32 +00:00
|
|
|
|
|
|
|
func TestTargetFlagFull(t *testing.T) {
|
|
|
|
cmd := emptyCommand(t)
|
|
|
|
initTargetFlag(cmd)
|
|
|
|
cmd.SetArgs([]string{"version", "--target", "development"})
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
err := cmd.ExecuteContext(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-08-28 17:05:55 +00:00
|
|
|
assert.Equal(t, getTarget(cmd), "development")
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTargetFlagShort(t *testing.T) {
|
|
|
|
cmd := emptyCommand(t)
|
|
|
|
initTargetFlag(cmd)
|
|
|
|
cmd.SetArgs([]string{"version", "-t", "production"})
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
err := cmd.ExecuteContext(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-08-28 17:05:55 +00:00
|
|
|
assert.Equal(t, getTarget(cmd), "production")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove when environment flag is fully deprecated
|
|
|
|
func TestTargetEnvironmentFlag(t *testing.T) {
|
|
|
|
cmd := emptyCommand(t)
|
|
|
|
initTargetFlag(cmd)
|
|
|
|
initEnvironmentFlag(cmd)
|
|
|
|
cmd.SetArgs([]string{"version", "--environment", "development"})
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
err := cmd.ExecuteContext(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, getTarget(cmd), "development")
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|