Skip to content

Commit ae92d07

Browse files
Update test files
1 parent dbee730 commit ae92d07

File tree

5 files changed

+192
-4
lines changed

5 files changed

+192
-4
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""This file configures pytest."""
2+
3+
import os, sys, pathlib
4+
from contextlib import contextmanager
5+
6+
7+
try:
8+
from databricks.connect import DatabricksSession
9+
from databricks.sdk import WorkspaceClient
10+
from pyspark.sql import SparkSession
11+
import pytest
12+
except ImportError:
13+
raise ImportError("Test dependencies not found.\n\nRun tests using 'uv run pytest'. See http://docs.astral.sh/uv to learn more about uv.")
14+
15+
16+
def enable_fallback_compute():
17+
"""Enable serverless compute if no compute is specified."""
18+
conf = WorkspaceClient().config
19+
if conf.serverless_compute_id or conf.cluster_id or os.environ.get("SPARK_REMOTE"):
20+
return
21+
22+
url = "https://docs.databricks.com/dev-tools/databricks-connect/cluster-config"
23+
print("☁️ no compute specified, falling back to serverless compute", file=sys.stderr)
24+
print(f" see {url} for manual configuration", file=sys.stdout)
25+
26+
os.environ["DATABRICKS_SERVERLESS_COMPUTE_ID"] = "auto"
27+
28+
29+
@contextmanager
30+
def allow_stderr_output(config: pytest.Config):
31+
"""Temporarily disable pytest output capture."""
32+
capman = config.pluginmanager.get_plugin("capturemanager")
33+
if capman:
34+
with capman.global_and_fixture_disabled():
35+
yield
36+
else:
37+
yield
38+
39+
40+
def pytest_configure(config: pytest.Config):
41+
"""Configure pytest session."""
42+
with allow_stderr_output(config):
43+
enable_fallback_compute()
44+
45+
# Initialize Spark session eagerly, so it is available even when
46+
# SparkSession.builder.getOrCreate() is used. For DB Connect 15+,
47+
# we validate version compatibility with the remote cluster.
48+
if hasattr(DatabricksSession.builder, "validateSession"):
49+
DatabricksSession.builder.validateSession().getOrCreate()
50+
else:
51+
DatabricksSession.builder.getOrCreate()
52+
53+
54+
@pytest.fixture(scope="session")
55+
def spark() -> SparkSession:
56+
"""Provide a SparkSession fixture for tests."""
57+
return DatabricksSession.builder.getOrCreate()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""This file configures pytest."""
2+
3+
import os, sys, pathlib
4+
from contextlib import contextmanager
5+
6+
7+
try:
8+
from databricks.connect import DatabricksSession
9+
from databricks.sdk import WorkspaceClient
10+
from pyspark.sql import SparkSession
11+
import pytest
12+
except ImportError:
13+
raise ImportError("Test dependencies not found.\n\nRun tests using 'uv run pytest'. See http://docs.astral.sh/uv to learn more about uv.")
14+
15+
16+
def enable_fallback_compute():
17+
"""Enable serverless compute if no compute is specified."""
18+
conf = WorkspaceClient().config
19+
if conf.serverless_compute_id or conf.cluster_id or os.environ.get("SPARK_REMOTE"):
20+
return
21+
22+
url = "https://docs.databricks.com/dev-tools/databricks-connect/cluster-config"
23+
print("☁️ no compute specified, falling back to serverless compute", file=sys.stderr)
24+
print(f" see {url} for manual configuration", file=sys.stdout)
25+
26+
os.environ["DATABRICKS_SERVERLESS_COMPUTE_ID"] = "auto"
27+
28+
29+
@contextmanager
30+
def allow_stderr_output(config: pytest.Config):
31+
"""Temporarily disable pytest output capture."""
32+
capman = config.pluginmanager.get_plugin("capturemanager")
33+
if capman:
34+
with capman.global_and_fixture_disabled():
35+
yield
36+
else:
37+
yield
38+
39+
40+
def pytest_configure(config: pytest.Config):
41+
"""Configure pytest session."""
42+
with allow_stderr_output(config):
43+
enable_fallback_compute()
44+
45+
# Initialize Spark session eagerly, so it is available even when
46+
# SparkSession.builder.getOrCreate() is used. For DB Connect 15+,
47+
# we validate version compatibility with the remote cluster.
48+
if hasattr(DatabricksSession.builder, "validateSession"):
49+
DatabricksSession.builder.validateSession().getOrCreate()
50+
else:
51+
DatabricksSession.builder.getOrCreate()
52+
53+
54+
@pytest.fixture(scope="session")
55+
def spark() -> SparkSession:
56+
"""Provide a SparkSession fixture for tests."""
57+
return DatabricksSession.builder.getOrCreate()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
>>> [CLI] apps run-local --prepare-environment --debug --port 8081 --debug-port 5252 --app-port 8080
3+
4+
up to date, audited 68 packages in 617ms
5+
6+
14 packages are looking for funding
7+
run `npm fund` for details
8+
9+
found 0 vulnerabilities
10+
11+
> app@1.0.0 build
12+
> echo 'Building app...'
13+
14+
Building app...
15+
Running command: npm run run-app
16+
To debug your app, attach a debugger to port $(debug_port)
17+
To access your app go to http://localhost:8081
18+
listen tcp 127.0.0.1:$(port): bind: address already in use
19+
20+
> app@1.0.0 run-app
21+
> node app.js
22+
23+
Server is running on port 8080
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
>>> [CLI] apps run-local --prepare-environment --debug --port 8081 --debug-port 5252 --app-port 8080
3+
Using CPython 3.13.5
4+
Creating virtual environment at: .venv
5+
warning: A virtual environment already exists at `.venv`. In the future, uv will require `--clear` to replace it
6+
Activate with: source .venv/bin/activate
7+
Resolved 129 packages in 43ms
8+
Uninstalled 2 packages in 18ms
9+
Installed 2 packages in 6ms
10+
- flask==3.1.1
11+
+ flask==3.0.3
12+
- werkzeug==3.1.3
13+
+ werkzeug==3.0.6
14+
Resolved 7 packages in 2ms
15+
Uninstalled 2 packages in 15ms
16+
Installed 2 packages in 5ms
17+
- flask==3.0.3
18+
+ flask==3.1.1
19+
- werkzeug==3.0.6
20+
+ werkzeug==3.1.3
21+
Running command: uv run python -m debugpy --listen 5252 -m flask run
22+
To debug your app, attach a debugger to port $(debug_port)
23+
To access your app go to http://localhost:8081
24+
listen tcp 127.0.0.1:$(port): bind: address already in use
25+
0.00s - Debugger warning: It seems that frozen modules are being used, which may
26+
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
27+
0.00s - to python to disable frozen modules.
28+
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
29+
Traceback (most recent call last):
30+
File "<frozen runpy>", line 198, in _run_module_as_main
31+
File "<frozen runpy>", line 88, in _run_code
32+
File "[TEST_TMP_DIR]/app/.venv/lib/python3.13/site-packages/debugpy/__main__.py", line 71, in <module>
33+
cli.main()
34+
~~~~~~~~^^
35+
File "[TEST_TMP_DIR]/app/.venv/lib/python3.13/site-packages/debugpy/server/cli.py", line 508, in main
36+
run()
37+
~~~^^
38+
File "[TEST_TMP_DIR]/app/.venv/lib/python3.13/site-packages/debugpy/server/cli.py", line 376, in run_module
39+
start_debugging(argv_0)
40+
~~~~~~~~~~~~~~~^^^^^^^^
41+
File "[TEST_TMP_DIR]/app/.venv/lib/python3.13/site-packages/debugpy/server/cli.py", line 328, in start_debugging
42+
debugpy.listen(options.address)
43+
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
44+
File "[TEST_TMP_DIR]/app/.venv/lib/python3.13/site-packages/debugpy/public_api.py", line 47, in wrapper
45+
return wrapped(*args, **kwargs)
46+
File "[TEST_TMP_DIR]/app/.venv/lib/python3.13/site-packages/debugpy/server/api.py", line 133, in debug
47+
log.reraise_exception("{0}() failed:", func.__name__, level="info")
48+
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
49+
File "[TEST_TMP_DIR]/app/.venv/lib/python3.13/site-packages/debugpy/server/api.py", line 131, in debug
50+
return func(address, settrace_kwargs, **kwargs)
51+
File "[TEST_TMP_DIR]/app/.venv/lib/python3.13/site-packages/debugpy/server/api.py", line 260, in listen
52+
raise RuntimeError(str(endpoints["error"]))
53+
RuntimeError: Can't listen for client connections: [Errno 48] Address already in use
54+
Error: exit status 1

acceptance/cmd/workspace/apps/run-local/output.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,4 @@ Hello, world
88

99
=== Starting the app in background...
1010
=== Waiting
11-
=== Checking app is running...
12-
>>> curl -s -o - http://127.0.0.1:$(port)
13-
14-
Exit code: 7
11+
Error: Test script killed due to a timeout

0 commit comments

Comments
 (0)