|
| 1 | +package mutator_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/databricks/cli/bundle" |
| 7 | + "github.com/databricks/cli/bundle/config" |
| 8 | + "github.com/databricks/cli/bundle/config/mutator" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestNormalizeHostURL(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + host string |
| 17 | + workspaceID string |
| 18 | + accountID string |
| 19 | + wantHost string |
| 20 | + wantWorkspaceID string |
| 21 | + wantAccountID string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "empty host", |
| 25 | + host: "", |
| 26 | + wantHost: "", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "host without query params", |
| 30 | + host: "https://api.databricks.com", |
| 31 | + wantHost: "https://api.databricks.com", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "host with ?o= extracts workspace_id", |
| 35 | + host: "https://dogfood.staging.databricks.com/?o=6051921418418893", |
| 36 | + wantHost: "https://dogfood.staging.databricks.com/", |
| 37 | + wantWorkspaceID: "6051921418418893", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "host with ?o= and ?a= extracts both", |
| 41 | + host: "https://dogfood.staging.databricks.com/?o=605&a=abc123", |
| 42 | + wantHost: "https://dogfood.staging.databricks.com/", |
| 43 | + wantWorkspaceID: "605", |
| 44 | + wantAccountID: "abc123", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "host with ?account_id= extracts account_id", |
| 48 | + host: "https://accounts.cloud.databricks.com/?account_id=968367da-1234", |
| 49 | + wantHost: "https://accounts.cloud.databricks.com/", |
| 50 | + wantAccountID: "968367da-1234", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "host with ?workspace_id= extracts workspace_id", |
| 54 | + host: "https://dogfood.staging.databricks.com/?workspace_id=12345", |
| 55 | + wantHost: "https://dogfood.staging.databricks.com/", |
| 56 | + wantWorkspaceID: "12345", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "explicit workspace_id takes precedence over ?o=", |
| 60 | + host: "https://dogfood.staging.databricks.com/?o=999", |
| 61 | + workspaceID: "explicit-id", |
| 62 | + wantHost: "https://dogfood.staging.databricks.com/", |
| 63 | + wantWorkspaceID: "explicit-id", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "explicit account_id takes precedence over ?a=", |
| 67 | + host: "https://dogfood.staging.databricks.com/?a=from-url", |
| 68 | + accountID: "explicit-account", |
| 69 | + wantHost: "https://dogfood.staging.databricks.com/", |
| 70 | + wantAccountID: "explicit-account", |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tt := range tests { |
| 75 | + t.Run(tt.name, func(t *testing.T) { |
| 76 | + b := &bundle.Bundle{ |
| 77 | + Config: config.Root{ |
| 78 | + Workspace: config.Workspace{ |
| 79 | + Host: tt.host, |
| 80 | + WorkspaceID: tt.workspaceID, |
| 81 | + AccountID: tt.accountID, |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + diags := bundle.Apply(t.Context(), b, mutator.NormalizeHostURL()) |
| 87 | + require.NoError(t, diags.Error()) |
| 88 | + |
| 89 | + assert.Equal(t, tt.wantHost, b.Config.Workspace.Host) |
| 90 | + assert.Equal(t, tt.wantWorkspaceID, b.Config.Workspace.WorkspaceID) |
| 91 | + assert.Equal(t, tt.wantAccountID, b.Config.Workspace.AccountID) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
0 commit comments