Skip to content

Commit 3080a07

Browse files
committed
moved fixture to respective module
1 parent 2049351 commit 3080a07

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,10 @@ See also [`log_account_link`](#log_account_link-fixture), [`make_acc_group`](#ma
376376
Get Databricks Connect Spark session. Requires `databricks-connect` package to be installed.
377377

378378
To enable serverless set the local environment variable `DATABRICKS_SERVERLESS_COMPUTE_ID` to `"auto"`.
379-
If this environment variable is set, Databricks Connect ignores the cluster id.
379+
If this environment variable is set, Databricks Connect ignores the cluster_id.
380+
If `DATABRICKS_SERVERLESS_COMPUTE_ID` is set to a specific serverless cluster ID, that cluster will be used instead.
381+
However, this is not recommended, as serverless clusters are ephemeral by design.
382+
See more details here: https://docs.databricks.com/en/dev-tools/databricks-connect/cluster-config.html#configure-a-connection-to-serverless-compute
380383

381384
Usage:
382385
```python

src/databricks/labs/pytester/fixtures/connect.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def spark(ws: WorkspaceClient):
1313
1414
To enable serverless set the local environment variable `DATABRICKS_SERVERLESS_COMPUTE_ID` to `"auto"`.
1515
If this environment variable is set, Databricks Connect ignores the cluster_id.
16+
If `DATABRICKS_SERVERLESS_COMPUTE_ID` is set to a specific serverless cluster ID, that cluster will be used instead.
17+
However, this is not recommended, as serverless clusters are ephemeral by design.
18+
See more details here: https://docs.databricks.com/en/dev-tools/databricks-connect/cluster-config.html#configure-a-connection-to-serverless-compute
1619
1720
Usage:
1821
```python
@@ -21,21 +24,32 @@ def test_databricks_connect(spark):
2124
assert rows[0][0] == 1
2225
```
2326
"""
24-
if not ws.config.serverless_compute_id:
25-
if not ws.config.cluster_id:
26-
skip("No cluster_id found in the environment")
27-
ws.clusters.ensure_cluster_is_running(ws.config.cluster_id)
27+
cluster_id = ws.config.cluster_id
28+
serverless_cluster_id = ws.config.serverless_compute_id
29+
print(f"cluster id is: {serverless_cluster_id}")
30+
31+
if not serverless_cluster_id:
32+
ensure_cluster_is_running(cluster_id, ws)
33+
34+
if serverless_cluster_id and serverless_cluster_id != "auto":
35+
ensure_cluster_is_running(serverless_cluster_id, ws)
2836

2937
try:
3038
# pylint: disable-next=import-outside-toplevel
3139
from databricks.connect import ( # type: ignore[import-untyped]
3240
DatabricksSession,
3341
)
3442

35-
if ws.config.serverless_compute_id:
43+
if serverless_cluster_id:
3644
logging.debug("Using serverless compute")
3745
return DatabricksSession.builder.serverless(True).getOrCreate()
3846
return DatabricksSession.builder.sdkConfig(ws.config).getOrCreate()
3947
except ImportError:
4048
skip("Please run `pip install databricks-connect`")
4149
return None
50+
51+
52+
def ensure_cluster_is_running(cluster_id, ws):
53+
if not cluster_id:
54+
skip("No cluster_id found in the environment")
55+
ws.clusters.ensure_cluster_is_running(cluster_id)

tests/integration/conftest.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,3 @@ def debug_env_name():
1818
@fixture
1919
def product_info():
2020
return 'pytester', __version__
21-
22-
23-

tests/integration/fixtures/test_connect.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22
from pytest import fixture
33

44

5-
def test_databricks_connect(spark):
5+
def test_databricks_connect(ws, spark):
66
rows = spark.sql("SELECT 1").collect()
77
assert rows[0][0] == 1
88

9+
creator = get_cluster_creator(spark, ws)
10+
assert creator # non-serverless clusters must have assigned creator
11+
12+
13+
def test_databricks_connect_serverless(serverless_env, ws, spark):
14+
rows = spark.sql("SELECT 1").collect()
15+
assert rows[0][0] == 1
16+
17+
creator = get_cluster_creator(spark, ws)
18+
assert not creator # serverless clusters don't have assigned creator
19+
920

1021
@fixture
1122
def serverless_env():
@@ -14,6 +25,10 @@ def serverless_env():
1425
os.environ.pop('DATABRICKS_SERVERLESS_COMPUTE_ID')
1526

1627

17-
def test_databricks_connect_serverless(serverless_env, spark):
18-
rows = spark.sql("SELECT 1").collect()
19-
assert rows[0][0] == 1
28+
def get_cluster_creator(spark, ws):
29+
"""
30+
Get the creator of the cluster that the Spark session is connected to.
31+
"""
32+
cluster_id = spark.conf.get("spark.databricks.clusterUsageTags.clusterId")
33+
creator = ws.clusters.get(cluster_id).creator_user_name
34+
return creator

0 commit comments

Comments
 (0)