|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/databricks/cli/cmd/root" |
| 7 | + "github.com/databricks/cli/libs/cmdctx" |
| 8 | + "github.com/databricks/cli/libs/databrickscfg/profile" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestValidateProfileHostConflict(t *testing.T) { |
| 14 | + profiler := profile.InMemoryProfiler{ |
| 15 | + Profiles: profile.Profiles{ |
| 16 | + {Name: "logfood", Host: "https://logfood.cloud.databricks.com"}, |
| 17 | + {Name: "staging", Host: "https://staging.cloud.databricks.com"}, |
| 18 | + {Name: "no-host", Host: ""}, |
| 19 | + }, |
| 20 | + } |
| 21 | + |
| 22 | + cases := []struct { |
| 23 | + name string |
| 24 | + profileName string |
| 25 | + host string |
| 26 | + wantErr string |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "matching hosts are allowed", |
| 30 | + profileName: "logfood", |
| 31 | + host: "https://logfood.cloud.databricks.com", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "matching hosts with trailing slash", |
| 35 | + profileName: "logfood", |
| 36 | + host: "https://logfood.cloud.databricks.com/", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "conflicting hosts produce error", |
| 40 | + profileName: "logfood", |
| 41 | + host: "https://other.cloud.databricks.com", |
| 42 | + wantErr: `--profile "logfood" has host "https://logfood.cloud.databricks.com", which conflicts with --host "https://other.cloud.databricks.com". Use --profile only to select a profile`, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "profile not found skips check", |
| 46 | + profileName: "nonexistent", |
| 47 | + host: "https://any.cloud.databricks.com", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "profile with no host skips check", |
| 51 | + profileName: "no-host", |
| 52 | + host: "https://any.cloud.databricks.com", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tc := range cases { |
| 57 | + t.Run(tc.name, func(t *testing.T) { |
| 58 | + ctx := t.Context() |
| 59 | + err := validateProfileHostConflict(ctx, tc.profileName, tc.host, profiler) |
| 60 | + if tc.wantErr != "" { |
| 61 | + assert.ErrorContains(t, err, tc.wantErr) |
| 62 | + } else { |
| 63 | + require.NoError(t, err) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// TestProfileHostConflictViaCobra verifies that the conflict check runs |
| 70 | +// through Cobra's lifecycle (PreRunE on login) and that the root command's |
| 71 | +// PersistentPreRunE is NOT shadowed (it initializes logging, IO, user agent). |
| 72 | +func TestProfileHostConflictViaCobra(t *testing.T) { |
| 73 | + // Point at a config file that has "profile-1" with host https://www.host1.com. |
| 74 | + t.Setenv("DATABRICKS_CONFIG_FILE", "./testdata/.databrickscfg") |
| 75 | + |
| 76 | + ctx := cmdctx.GenerateExecId(t.Context()) |
| 77 | + cli := root.New(ctx) |
| 78 | + cli.AddCommand(New()) |
| 79 | + |
| 80 | + // Set args: auth login --profile profile-1 --host https://other.host.com |
| 81 | + cli.SetArgs([]string{ |
| 82 | + "auth", "login", |
| 83 | + "--profile", "profile-1", |
| 84 | + "--host", "https://other.host.com", |
| 85 | + }) |
| 86 | + |
| 87 | + _, err := cli.ExecuteContextC(ctx) |
| 88 | + require.Error(t, err) |
| 89 | + assert.Contains(t, err.Error(), `--profile "profile-1" has host "https://www.host1.com", which conflicts with --host "https://other.host.com"`) |
| 90 | + assert.Contains(t, err.Error(), "Use --profile only to select a profile") |
| 91 | +} |
| 92 | + |
| 93 | +// TestProfileHostConflictTokenViaCobra verifies the conflict check on the token subcommand. |
| 94 | +func TestProfileHostConflictTokenViaCobra(t *testing.T) { |
| 95 | + t.Setenv("DATABRICKS_CONFIG_FILE", "./testdata/.databrickscfg") |
| 96 | + |
| 97 | + ctx := cmdctx.GenerateExecId(t.Context()) |
| 98 | + cli := root.New(ctx) |
| 99 | + cli.AddCommand(New()) |
| 100 | + |
| 101 | + cli.SetArgs([]string{ |
| 102 | + "auth", "token", |
| 103 | + "--profile", "profile-1", |
| 104 | + "--host", "https://other.host.com", |
| 105 | + }) |
| 106 | + |
| 107 | + _, err := cli.ExecuteContextC(ctx) |
| 108 | + require.Error(t, err) |
| 109 | + assert.Contains(t, err.Error(), `--profile "profile-1" has host "https://www.host1.com", which conflicts with --host "https://other.host.com"`) |
| 110 | +} |
| 111 | + |
| 112 | +// TestProfileHostCompatibleViaCobra verifies that matching --profile and --host |
| 113 | +// pass the conflict check (the command will fail later for other reasons, but |
| 114 | +// NOT with a conflict error). |
| 115 | +func TestProfileHostCompatibleViaCobra(t *testing.T) { |
| 116 | + t.Setenv("DATABRICKS_CONFIG_FILE", "./testdata/.databrickscfg") |
| 117 | + |
| 118 | + ctx := cmdctx.GenerateExecId(t.Context()) |
| 119 | + cli := root.New(ctx) |
| 120 | + cli.AddCommand(New()) |
| 121 | + |
| 122 | + cli.SetArgs([]string{ |
| 123 | + "auth", "login", |
| 124 | + "--profile", "profile-1", |
| 125 | + "--host", "https://www.host1.com", |
| 126 | + }) |
| 127 | + |
| 128 | + _, err := cli.ExecuteContextC(ctx) |
| 129 | + // The command may fail for other reasons (no browser, non-interactive, etc.) |
| 130 | + // but it should NOT fail with a conflict error. |
| 131 | + if err != nil { |
| 132 | + assert.NotContains(t, err.Error(), "conflicts with --host") |
| 133 | + } |
| 134 | +} |
0 commit comments