Skip to content

Commit 09f4461

Browse files
committed
simplify for the time being
1 parent 235812a commit 09f4461

File tree

5 files changed

+154
-77
lines changed

5 files changed

+154
-77
lines changed

README.md

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66

77
<!-- TOC -->
88
* [Python Testing for Databricks](#python-testing-for-databricks)
9+
* [Installation](#installation)
910
* [Ecosystem](#ecosystem)
1011
* [PyTest Fixtures](#pytest-fixtures)
1112
* [Logging](#logging)
12-
* [Installation](#installation)
1313
* [`debug_env_name` fixture](#debug_env_name-fixture)
1414
* [`debug_env` fixture](#debug_env-fixture)
1515
* [`env_or_skip` fixture](#env_or_skip-fixture)
1616
* [`ws` fixture](#ws-fixture)
17+
* [`make_run_as` fixture](#make_run_as-fixture)
18+
* [`acc` fixture](#acc-fixture)
19+
* [`spark` fixture](#spark-fixture)
20+
* [`sql_backend` fixture](#sql_backend-fixture)
21+
* [`sql_exec` fixture](#sql_exec-fixture)
22+
* [`sql_fetch_all` fixture](#sql_fetch_all-fixture)
1723
* [`make_random` fixture](#make_random-fixture)
1824
* [`make_instance_pool` fixture](#make_instance_pool-fixture)
1925
* [`make_instance_pool_permissions` fixture](#make_instance_pool_permissions-fixture)
@@ -26,10 +32,12 @@
2632
* [`make_pipeline` fixture](#make_pipeline-fixture)
2733
* [`make_warehouse` fixture](#make_warehouse-fixture)
2834
* [`make_group` fixture](#make_group-fixture)
35+
* [`make_acc_group` fixture](#make_acc_group-fixture)
2936
* [`make_user` fixture](#make_user-fixture)
3037
* [`make_pipeline_permissions` fixture](#make_pipeline_permissions-fixture)
3138
* [`make_notebook` fixture](#make_notebook-fixture)
3239
* [`make_notebook_permissions` fixture](#make_notebook_permissions-fixture)
40+
* [`make_workspace_file` fixture](#make_workspace_file-fixture)
3341
* [`make_directory` fixture](#make_directory-fixture)
3442
* [`make_directory_permissions` fixture](#make_directory_permissions-fixture)
3543
* [`make_repo` fixture](#make_repo-fixture)
@@ -44,25 +52,27 @@
4452
* [`make_schema` fixture](#make_schema-fixture)
4553
* [`make_table` fixture](#make_table-fixture)
4654
* [`make_storage_credential` fixture](#make_storage_credential-fixture)
55+
* [`make_volume` fixture](#make_volume-fixture)
4756
* [`product_info` fixture](#product_info-fixture)
48-
* [`sql_backend` fixture](#sql_backend-fixture)
49-
* [`sql_exec` fixture](#sql_exec-fixture)
50-
* [`sql_fetch_all` fixture](#sql_fetch_all-fixture)
5157
* [`make_model` fixture](#make_model-fixture)
5258
* [`make_experiment` fixture](#make_experiment-fixture)
5359
* [`make_experiment_permissions` fixture](#make_experiment_permissions-fixture)
5460
* [`make_warehouse_permissions` fixture](#make_warehouse_permissions-fixture)
5561
* [`make_lakeview_dashboard_permissions` fixture](#make_lakeview_dashboard_permissions-fixture)
56-
* [`workspace_library` fixture](#workspace_library-fixture)
5762
* [`log_workspace_link` fixture](#log_workspace_link-fixture)
63+
* [`log_account_link` fixture](#log_account_link-fixture)
5864
* [`make_dashboard_permissions` fixture](#make_dashboard_permissions-fixture)
5965
* [`make_alert_permissions` fixture](#make_alert_permissions-fixture)
6066
* [`make_query` fixture](#make_query-fixture)
6167
* [`make_query_permissions` fixture](#make_query_permissions-fixture)
6268
* [`make_registered_model_permissions` fixture](#make_registered_model_permissions-fixture)
6369
* [`make_serving_endpoint` fixture](#make_serving_endpoint-fixture)
6470
* [`make_serving_endpoint_permissions` fixture](#make_serving_endpoint_permissions-fixture)
71+
* [`make_feature_table` fixture](#make_feature_table-fixture)
6572
* [`make_feature_table_permissions` fixture](#make_feature_table_permissions-fixture)
73+
* [`watchdog_remove_after` fixture](#watchdog_remove_after-fixture)
74+
* [`watchdog_purge_suffix` fixture](#watchdog_purge_suffix-fixture)
75+
* [`is_in_debug` fixture](#is_in_debug-fixture)
6676
* [Project Support](#project-support)
6777
<!-- TOC -->
6878

@@ -75,7 +85,7 @@ also install it directly from the command line:
7585
pip install databricks-labs-pytester
7686
```
7787

78-
If you use `hatch` as a build system, make sure to add `databricks-labs-pytester` as
88+
If you use `hatch` as a build system, make sure to add `databricks-labs-pytester` as
7989
a test-time dependency and not as a compile-time dependency, otherwise your wheels will
8090
transitively depend on `pytest`, which is not usually something you need.
8191

@@ -97,7 +107,7 @@ dependencies = [
97107
"pylint~=3.2.2",
98108
"pylint-pytest==2.0.0a0",
99109
"databricks-labs-pylint~=0.4.0",
100-
"databricks-labs-pytester~=0.2", # <= this library
110+
"databricks-labs-pytester~=0.2", # <= this library
101111
"pytest~=8.3.3",
102112
"pytest-cov~=4.1.0",
103113
"pytest-mock~=3.14.0",
@@ -116,7 +126,7 @@ dependencies = [
116126

117127
Built on top of [Databricks SDK for Python](https://github.com/databricks/databricks-sdk-py), this library is part of the Databricks Labs Python ecosystem, which includes the following projects:
118128
* [PyLint Plugin for Databricks](https://github.com/databrickslabs/pylint-plugin) for static code analysis and early bug detection.
119-
* [Blueprint](https://github.com/databrickslabs/blueprint) for
129+
* [Blueprint](https://github.com/databrickslabs/blueprint) for
120130
[Python-native pathlib.Path-like interfaces](https://github.com/databrickslabs/blueprint#python-native-pathlibpath-like-interfaces),
121131
[Managing Python App installations within Databricks Workspaces](https://github.com/databrickslabs/blueprint#application-and-installation-state),
122132
[Application Migrations](https://github.com/databrickslabs/blueprint#application-state-migrations), and
@@ -130,18 +140,18 @@ See [this video](https://www.youtube.com/watch?v=CNypO79IATc) for a quick overvi
130140

131141
## PyTest Fixtures
132142

133-
[PyTest Fixtures](https://docs.pytest.org/en/latest/explanation/fixtures.html) are a powerful way to manage test setup and teardown in Python. This library provides
143+
[PyTest Fixtures](https://docs.pytest.org/en/latest/explanation/fixtures.html) are a powerful way to manage test setup and teardown in Python. This library provides
134144
a set of fixtures to help you write integration tests for Databricks. These fixtures were incubated
135-
within the [Unity Catalog Automated Migrations project](https://github.com/databrickslabs/ucx/blame/df7f1d7647251fb8f0f23c56a548b99092484a7c/src/databricks/labs/ucx/mixins/fixtures.py)
145+
within the [Unity Catalog Automated Migrations project](https://github.com/databrickslabs/ucx/blame/df7f1d7647251fb8f0f23c56a548b99092484a7c/src/databricks/labs/ucx/mixins/fixtures.py)
136146
for more than a year and are now available for other projects to simplify integration testing with Databricks.
137147

138148
[[back to top](#python-testing-for-databricks)]
139149

140150
### Logging
141151

142-
This library is built on years of debugging integration tests for Databricks and its ecosystem.
152+
This library is built on years of debugging integration tests for Databricks and its ecosystem.
143153

144-
That's why it comes with a built-in logger that traces creation and deletion of dummy entities through links in
154+
That's why it comes with a built-in logger that traces creation and deletion of dummy entities through links in
145155
the Databricks Workspace UI. If you run the following code:
146156

147157
```python
@@ -281,6 +291,39 @@ def test_workspace_operations(ws):
281291
See also [`log_workspace_link`](#log_workspace_link-fixture), [`make_alert_permissions`](#make_alert_permissions-fixture), [`make_authorization_permissions`](#make_authorization_permissions-fixture), [`make_catalog`](#make_catalog-fixture), [`make_cluster`](#make_cluster-fixture), [`make_cluster_permissions`](#make_cluster_permissions-fixture), [`make_cluster_policy`](#make_cluster_policy-fixture), [`make_cluster_policy_permissions`](#make_cluster_policy_permissions-fixture), [`make_dashboard_permissions`](#make_dashboard_permissions-fixture), [`make_directory`](#make_directory-fixture), [`make_directory_permissions`](#make_directory_permissions-fixture), [`make_experiment`](#make_experiment-fixture), [`make_experiment_permissions`](#make_experiment_permissions-fixture), [`make_feature_table`](#make_feature_table-fixture), [`make_feature_table_permissions`](#make_feature_table_permissions-fixture), [`make_group`](#make_group-fixture), [`make_instance_pool`](#make_instance_pool-fixture), [`make_instance_pool_permissions`](#make_instance_pool_permissions-fixture), [`make_job`](#make_job-fixture), [`make_job_permissions`](#make_job_permissions-fixture), [`make_lakeview_dashboard_permissions`](#make_lakeview_dashboard_permissions-fixture), [`make_model`](#make_model-fixture), [`make_notebook`](#make_notebook-fixture), [`make_notebook_permissions`](#make_notebook_permissions-fixture), [`make_pipeline`](#make_pipeline-fixture), [`make_pipeline_permissions`](#make_pipeline_permissions-fixture), [`make_query`](#make_query-fixture), [`make_query_permissions`](#make_query_permissions-fixture), [`make_registered_model_permissions`](#make_registered_model_permissions-fixture), [`make_repo`](#make_repo-fixture), [`make_repo_permissions`](#make_repo_permissions-fixture), [`make_run_as`](#make_run_as-fixture), [`make_secret_scope`](#make_secret_scope-fixture), [`make_secret_scope_acl`](#make_secret_scope_acl-fixture), [`make_serving_endpoint`](#make_serving_endpoint-fixture), [`make_serving_endpoint_permissions`](#make_serving_endpoint_permissions-fixture), [`make_storage_credential`](#make_storage_credential-fixture), [`make_udf`](#make_udf-fixture), [`make_user`](#make_user-fixture), [`make_volume`](#make_volume-fixture), [`make_warehouse`](#make_warehouse-fixture), [`make_warehouse_permissions`](#make_warehouse_permissions-fixture), [`make_workspace_file`](#make_workspace_file-fixture), [`make_workspace_file_path_permissions`](#make_workspace_file_path_permissions-fixture), [`make_workspace_file_permissions`](#make_workspace_file_permissions-fixture), [`spark`](#spark-fixture), [`sql_backend`](#sql_backend-fixture), [`debug_env`](#debug_env-fixture), [`product_info`](#product_info-fixture).
282292

283293

294+
[[back to top](#python-testing-for-databricks)]
295+
296+
### `make_run_as` fixture
297+
This fixture provides a function to create an account service principal via [`acc` fixture](#acc-fixture) and
298+
assign it to a workspace. The service principal is removed after the test is complete. The service principal is
299+
created with a random display name and assigned to the workspace with the default permissions.
300+
301+
Use the `account_groups` argument to assign the service principal to account groups, which have the required
302+
permissions to perform a specific action.
303+
304+
Returned object has the following properties:
305+
* `ws`: Workspace client that is authenticated as the ephemeral service principal.
306+
* `sql_backend`: SQL backend that is authenticated as the ephemeral service principal.
307+
* `sql_exec`: Function to execute a SQL statement on behalf of the ephemeral service principal.
308+
* `sql_fetch_all`: Function to fetch all rows from a SQL statement on behalf of the ephemeral service principal.
309+
* `display_name`: Display name of the ephemeral service principal.
310+
* `application_id`: Application ID of the ephemeral service principal.
311+
* ... other fixtures are not currently available through the returned object yet, as it's quite complex to
312+
implement, but there's a possibility to add generic support for them in the future.
313+
314+
Example:
315+
316+
```python
317+
def test_run_as_lower_privilege_user(make_run_as, ws):
318+
run_as = make_run_as(account_groups=['account.group.name'])
319+
through_query = next(run_as.sql_fetch_all("SELECT CURRENT_USER() AS my_name"))
320+
me = ws.current_user.me()
321+
assert me.user_name != through_query.my_name
322+
```
323+
324+
See also [`acc`](#acc-fixture), [`ws`](#ws-fixture), [`make_random`](#make_random-fixture), [`env_or_skip`](#env_or_skip-fixture), [`log_account_link`](#log_account_link-fixture).
325+
326+
284327
[[back to top](#python-testing-for-databricks)]
285328

286329
### `acc` fixture
@@ -305,7 +348,7 @@ def test_listing_workspaces(acc):
305348
assert len(workspaces) >= 1
306349
```
307350

308-
See also [`make_acc_group`](#make_acc_group-fixture), [`make_run_as`](#make_run_as-fixture), [`debug_env`](#debug_env-fixture), [`product_info`](#product_info-fixture), [`env_or_skip`](#env_or_skip-fixture).
351+
See also [`log_account_link`](#log_account_link-fixture), [`make_acc_group`](#make_acc_group-fixture), [`make_run_as`](#make_run_as-fixture), [`debug_env`](#debug_env-fixture), [`product_info`](#product_info-fixture), [`env_or_skip`](#env_or_skip-fixture).
309352

310353

311354
[[back to top](#python-testing-for-databricks)]
@@ -349,14 +392,6 @@ Fetch all rows from a SQL statement.
349392
See also [`sql_backend`](#sql_backend-fixture).
350393

351394

352-
[[back to top](#python-testing-for-databricks)]
353-
354-
### `make_run_as` fixture
355-
_No description yet._
356-
357-
See also [`acc`](#acc-fixture), [`ws`](#ws-fixture), [`make_random`](#make_random-fixture), [`env_or_skip`](#env_or_skip-fixture).
358-
359-
360395
[[back to top](#python-testing-for-databricks)]
361396

362397
### `make_random` fixture
@@ -1067,6 +1102,14 @@ rns a function to log a workspace link.
10671102
See also [`ws`](#ws-fixture).
10681103

10691104

1105+
[[back to top](#python-testing-for-databricks)]
1106+
1107+
### `log_account_link` fixture
1108+
rns a function to log an account link.
1109+
1110+
See also [`make_run_as`](#make_run_as-fixture), [`acc`](#acc-fixture).
1111+
1112+
10701113
[[back to top](#python-testing-for-databricks)]
10711114

10721115
### `make_dashboard_permissions` fixture
@@ -1208,11 +1251,11 @@ See also [`debug_env`](#debug_env-fixture), [`env_or_skip`](#env_or_skip-fixture
12081251

12091252
# Project Support
12101253

1211-
Please note that this project is provided for your exploration only and is not
1212-
formally supported by Databricks with Service Level Agreements (SLAs). They are
1213-
provided AS-IS, and we do not make any guarantees of any kind. Please do not
1254+
Please note that this project is provided for your exploration only and is not
1255+
formally supported by Databricks with Service Level Agreements (SLAs). They are
1256+
provided AS-IS, and we do not make any guarantees of any kind. Please do not
12141257
submit a support ticket relating to any issues arising from the use of this project.
12151258

1216-
Any issues discovered through the use of this project should be filed as GitHub
1217-
[Issues on this repository](https://github.com/databrickslabs/pytester/issues).
1259+
Any issues discovered through the use of this project should be filed as GitHub
1260+
[Issues on this repository](https://github.com/databrickslabs/pytester/issues).
12181261
They will be reviewed as time permits, but no formal SLAs for support exist.

0 commit comments

Comments
 (0)