|
| 1 | +import * as assert from "assert"; |
| 2 | +import {instance, mock, when} from "ts-mockito"; |
| 3 | +import {AuthProvider, DatabricksCliAuthProvider} from "./AuthProvider"; |
| 4 | +import {CliWrapper} from "../../cli/CliWrapper"; |
| 5 | + |
| 6 | +describe(__filename, () => { |
| 7 | + describe("DatabricksCliAuthProvider.toEnv", () => { |
| 8 | + const host = new URL("https://test.cloud.databricks.com"); |
| 9 | + const cliPath = "/path/to/bin/databricks"; |
| 10 | + |
| 11 | + function createProvider(profile?: string, workspaceId?: string) { |
| 12 | + return new DatabricksCliAuthProvider( |
| 13 | + host, |
| 14 | + cliPath, |
| 15 | + instance(mock(CliWrapper)), |
| 16 | + profile, |
| 17 | + workspaceId |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + it("should expose DATABRICKS_CLI_PATH so the SDK/Terraform provider can locate the bundled CLI", () => { |
| 22 | + const env = createProvider("dev").toEnv(); |
| 23 | + |
| 24 | + assert.equal(env["DATABRICKS_CLI_PATH"], cliPath); |
| 25 | + assert.equal(env["DATABRICKS_HOST"], host.toString()); |
| 26 | + assert.equal(env["DATABRICKS_AUTH_TYPE"], "databricks-cli"); |
| 27 | + assert.equal(env["DATABRICKS_CONFIG_PROFILE"], "dev"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should include DATABRICKS_CLI_PATH even without a profile or workspace id", () => { |
| 31 | + const env = createProvider().toEnv(); |
| 32 | + |
| 33 | + assert.equal(env["DATABRICKS_CLI_PATH"], cliPath); |
| 34 | + assert.ok(!("DATABRICKS_CONFIG_PROFILE" in env)); |
| 35 | + assert.ok(!("DATABRICKS_WORKSPACE_ID" in env)); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("AuthProvider.fromJSON", () => { |
| 40 | + it("should ignore a persisted databricksPath and use the freshly resolved bundled CLI path", () => { |
| 41 | + // Simulate an upgraded install: the new extension resolves a |
| 42 | + // versioned, platform-correct path, while project.json still holds |
| 43 | + // an old, extensionless snapshot from the previous version. |
| 44 | + const freshPath = |
| 45 | + "/ext/databricks.databricks-2.12.0/bin/databricks.exe"; |
| 46 | + const stalePath = |
| 47 | + "/ext/databricks.databricks-2.11.0/bin/databricks"; |
| 48 | + |
| 49 | + const cliMock = mock(CliWrapper); |
| 50 | + when(cliMock.cliPath).thenReturn(freshPath); |
| 51 | + |
| 52 | + const provider = AuthProvider.fromJSON( |
| 53 | + { |
| 54 | + host: "https://test.cloud.databricks.com", |
| 55 | + authType: "databricks-cli", |
| 56 | + databricksPath: stalePath, |
| 57 | + profile: "dev", |
| 58 | + }, |
| 59 | + instance(cliMock) |
| 60 | + ); |
| 61 | + |
| 62 | + assert.ok(provider instanceof DatabricksCliAuthProvider); |
| 63 | + assert.equal( |
| 64 | + (provider as DatabricksCliAuthProvider).cliPath, |
| 65 | + freshPath |
| 66 | + ); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments