Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 214
Resolve configuration before performing verification#890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package config | ||
| import ( | ||
| "context" | ||
| "io/fs" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
| "github.com/databricks/cli/internal/testutil" | ||
| "github.com/databricks/cli/libs/databrickscfg" | ||
| "github.com/databricks/databricks-sdk-go/config" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
| func setupWorkspaceTest(t *testing.T) string { | ||
| testutil.CleanupEnvironment(t) | ||
| home := t.TempDir() | ||
| t.Setenv("HOME", home) | ||
| if runtime.GOOS == "windows" { | ||
| t.Setenv("USERPROFILE", home) | ||
| } | ||
| return home | ||
| } | ||
| func TestWorkspaceResolveProfileFromHost(t *testing.T) { | ||
| // If only a workspace host is specified, try to find a profile that uses | ||
| // the same workspace host (unambiguously). | ||
| w := Workspace{ | ||
| Host: "https://abc.cloud.databricks.com", | ||
| } | ||
| t.Run("no config file", func(t *testing.T) { | ||
| setupWorkspaceTest(t) | ||
| _, err := w.Client() | ||
| assert.NoError(t, err) | ||
| }) | ||
| t.Run("default config file", func(t *testing.T) { | ||
| setupWorkspaceTest(t) | ||
| // This works if there is a config file with a matching profile. | ||
| databrickscfg.SaveToProfile(context.Background(), &config.Config{ | ||
| Profile: "default", | ||
| Host: "https://abc.cloud.databricks.com", | ||
| Token: "123", | ||
| }) | ||
| client, err := w.Client() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "default", client.Config.Profile) | ||
| }) | ||
| t.Run("custom config file", func(t *testing.T) { | ||
| home := setupWorkspaceTest(t) | ||
| // This works if there is a config file with a matching profile. | ||
| databrickscfg.SaveToProfile(context.Background(), &config.Config{ | ||
| ConfigFile: filepath.Join(home, "customcfg"), | ||
| Profile: "custom", | ||
| Host: "https://abc.cloud.databricks.com", | ||
| Token: "123", | ||
| }) | ||
| t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(home, "customcfg")) | ||
| client, err := w.Client() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "custom", client.Config.Profile) | ||
| }) | ||
| } | ||
| func TestWorkspaceVerifyProfileForHost(t *testing.T) { | ||
| // If both a workspace host and a profile are specified, | ||
| // verify that the host configured in the profile matches | ||
| // the host configured in the bundle configuration. | ||
| w := Workspace{ | ||
| Host: "https://abc.cloud.databricks.com", | ||
| Profile: "abc", | ||
| } | ||
| t.Run("no config file", func(t *testing.T) { | ||
| setupWorkspaceTest(t) | ||
| _, err := w.Client() | ||
| assert.ErrorIs(t, err, fs.ErrNotExist) | ||
| }) | ||
| t.Run("default config file with match", func(t *testing.T) { | ||
| setupWorkspaceTest(t) | ||
| // This works if there is a config file with a matching profile. | ||
| databrickscfg.SaveToProfile(context.Background(), &config.Config{ | ||
| Profile: "abc", | ||
| Host: "https://abc.cloud.databricks.com", | ||
| }) | ||
| _, err := w.Client() | ||
| assert.NoError(t, err) | ||
| }) | ||
| t.Run("default config file with mismatch", func(t *testing.T) { | ||
| setupWorkspaceTest(t) | ||
| // This works if there is a config file with a matching profile. | ||
| databrickscfg.SaveToProfile(context.Background(), &config.Config{ | ||
| Profile: "abc", | ||
| Host: "https://def.cloud.databricks.com", | ||
| }) | ||
| _, err := w.Client() | ||
| assert.ErrorContains(t, err, "config host mismatch") | ||
| }) | ||
| t.Run("custom config file with match", func(t *testing.T) { | ||
| home := setupWorkspaceTest(t) | ||
| // This works if there is a config file with a matching profile. | ||
| databrickscfg.SaveToProfile(context.Background(), &config.Config{ | ||
| ConfigFile: filepath.Join(home, "customcfg"), | ||
| Profile: "abc", | ||
| Host: "https://abc.cloud.databricks.com", | ||
| }) | ||
| t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(home, "customcfg")) | ||
| _, err := w.Client() | ||
| assert.NoError(t, err) | ||
| }) | ||
| t.Run("custom config file with mismatch", func(t *testing.T) { | ||
| home := setupWorkspaceTest(t) | ||
| // This works if there is a config file with a matching profile. | ||
| databrickscfg.SaveToProfile(context.Background(), &config.Config{ | ||
| ConfigFile: filepath.Join(home, "customcfg"), | ||
| Profile: "abc", | ||
| Host: "https://def.cloud.databricks.com", | ||
| }) | ||
| t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(home, "customcfg")) | ||
| _, err := w.Client() | ||
| assert.ErrorContains(t, err, "config host mismatch") | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -83,7 +83,7 @@ func TestBundleConfigureWithNonExistentProfileFlag(t *testing.T) { | ||
| cmd.Flag("profile").Value.Set("NOEXIST") | ||
| b := setup(t, cmd, "https://x.com") | ||
| assert.PanicsWithError(t, "no matching config profiles found", func() { | ||
| assert.Panics(t, func() { | ||
pietern marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| b.WorkspaceClient() | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -103,6 +103,7 @@ func (l profileFromHostLoader) Configure(cfg *config.Config) error { | ||
| return fmt.Errorf("%s %s profile: %w", configFile.Path(), match.Name(), err) | ||
| } | ||
| cfg.Profile = match.Name() | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Write back the profile after selecting one so we can assert on it in tests. | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -59,7 +59,7 @@ func TestLoaderErrorsOnInvalidFile(t *testing.T) { | ||
| assert.ErrorContains(t, err, "unclosed section: ") | ||
| } | ||
| func TestLoaderSkipssNoMatchingHost(t *testing.T) { | ||
| func TestLoaderSkipsNoMatchingHost(t *testing.T) { | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo. | ||
| cfg := config.Config{ | ||
| Loaders: []config.Loader{ | ||
| ResolveProfileFromHost, | ||
| @@ -73,20 +73,6 @@ func TestLoaderSkipssNoMatchingHost(t *testing.T) { | ||
| assert.Empty(t, cfg.Token) | ||
| } | ||
| func TestLoaderConfiguresMatchingHost(t *testing.T) { | ||
| cfg := config.Config{ | ||
| Loaders: []config.Loader{ | ||
| ResolveProfileFromHost, | ||
| }, | ||
| ConfigFile: "testdata/databrickscfg", | ||
| Host: "https://default/?foo=bar", | ||
| } | ||
| err := cfg.EnsureResolved() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "default", cfg.Token) | ||
| } | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is superfluous. The same is covered by | ||
| func TestLoaderMatchingHost(t *testing.T) { | ||
| cfg := config.Config{ | ||
| Loaders: []config.Loader{ | ||
| @@ -99,6 +85,7 @@ func TestLoaderMatchingHost(t *testing.T) { | ||
| err := cfg.EnsureResolved() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "default", cfg.Token) | ||
| assert.Equal(t, "DEFAULT", cfg.Profile) | ||
| } | ||
| func TestLoaderMatchingHostWithQuery(t *testing.T) { | ||
| @@ -113,6 +100,7 @@ func TestLoaderMatchingHostWithQuery(t *testing.T) { | ||
| err := cfg.EnsureResolved() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "query", cfg.Token) | ||
| assert.Equal(t, "query", cfg.Profile) | ||
| } | ||
| func TestLoaderErrorsOnMultipleMatches(t *testing.T) { | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this, all environment variables, and optionally a profile, have been loaded.