Skip to content

Commit 5a87ecd

Browse files
authored
Added acc and make_acc_group fixtures (#42)
1 parent 2bc7842 commit 5a87ecd

File tree

9 files changed

+172
-20
lines changed

9 files changed

+172
-20
lines changed

README.md

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Loads environment variables specified in [`debug_env_name` fixture](#debug_env_n
200200
for local debugging in IDEs, otherwise allowing the tests to run with the default environment variables
201201
specified in the CI/CD pipeline.
202202

203-
See also [`env_or_skip`](#env_or_skip-fixture), [`ws`](#ws-fixture), [`debug_env_name`](#debug_env_name-fixture), [`is_in_debug`](#is_in_debug-fixture).
203+
See also [`acc`](#acc-fixture), [`env_or_skip`](#env_or_skip-fixture), [`ws`](#ws-fixture), [`debug_env_name`](#debug_env_name-fixture), [`is_in_debug`](#is_in_debug-fixture).
204204

205205

206206
[[back to top](#python-testing-for-databricks)]
@@ -219,7 +219,7 @@ def test_something(env_or_skip):
219219
assert token is not None
220220
```
221221

222-
See also [`make_udf`](#make_udf-fixture), [`sql_backend`](#sql_backend-fixture), [`debug_env`](#debug_env-fixture), [`is_in_debug`](#is_in_debug-fixture).
222+
See also [`acc`](#acc-fixture), [`make_udf`](#make_udf-fixture), [`sql_backend`](#sql_backend-fixture), [`debug_env`](#debug_env-fixture), [`is_in_debug`](#is_in_debug-fixture).
223223

224224

225225
[[back to top](#python-testing-for-databricks)]
@@ -245,6 +245,33 @@ def test_workspace_operations(ws):
245245
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_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_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_warehouse`](#make_warehouse-fixture), [`make_warehouse_permissions`](#make_warehouse_permissions-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).
246246

247247

248+
[[back to top](#python-testing-for-databricks)]
249+
250+
### `acc` fixture
251+
Create and provide a Databricks AccountClient object.
252+
253+
This fixture initializes a Databricks AccountClient object, which can be used
254+
to interact with the Databricks account API. The created instance of AccountClient
255+
is shared across all test functions within the test session.
256+
257+
Requires `DATABRICKS_ACCOUNT_ID` environment variable to be set. If `DATABRICKS_HOST`
258+
points to a workspace host, the fixture would automatically determine the account host
259+
from it.
260+
261+
See [detailed documentation](https://databricks-sdk-py.readthedocs.io/en/latest/authentication.html) for the list
262+
of environment variables that can be used to authenticate the AccountClient.
263+
264+
In your test functions, include this fixture as an argument to use the AccountClient:
265+
266+
```python
267+
def test_listing_workspaces(acc):
268+
workspaces = acc.workspaces.list()
269+
assert len(workspaces) >= 1
270+
```
271+
272+
See also [`make_acc_group`](#make_acc_group-fixture), [`debug_env`](#debug_env-fixture), [`product_info`](#product_info-fixture), [`env_or_skip`](#env_or_skip-fixture).
273+
274+
248275
[[back to top](#python-testing-for-databricks)]
249276

250277
### `spark` fixture
@@ -309,7 +336,7 @@ random_string = make_random(k=8)
309336
assert len(random_string) == 8
310337
```
311338

312-
See also [`make_catalog`](#make_catalog-fixture), [`make_cluster`](#make_cluster-fixture), [`make_cluster_policy`](#make_cluster_policy-fixture), [`make_directory`](#make_directory-fixture), [`make_experiment`](#make_experiment-fixture), [`make_group`](#make_group-fixture), [`make_instance_pool`](#make_instance_pool-fixture), [`make_job`](#make_job-fixture), [`make_model`](#make_model-fixture), [`make_notebook`](#make_notebook-fixture), [`make_pipeline`](#make_pipeline-fixture), [`make_query`](#make_query-fixture), [`make_repo`](#make_repo-fixture), [`make_schema`](#make_schema-fixture), [`make_secret_scope`](#make_secret_scope-fixture), [`make_serving_endpoint`](#make_serving_endpoint-fixture), [`make_table`](#make_table-fixture), [`make_udf`](#make_udf-fixture), [`make_user`](#make_user-fixture), [`make_warehouse`](#make_warehouse-fixture).
339+
See also [`make_acc_group`](#make_acc_group-fixture), [`make_catalog`](#make_catalog-fixture), [`make_cluster`](#make_cluster-fixture), [`make_cluster_policy`](#make_cluster_policy-fixture), [`make_directory`](#make_directory-fixture), [`make_experiment`](#make_experiment-fixture), [`make_group`](#make_group-fixture), [`make_instance_pool`](#make_instance_pool-fixture), [`make_job`](#make_job-fixture), [`make_model`](#make_model-fixture), [`make_notebook`](#make_notebook-fixture), [`make_pipeline`](#make_pipeline-fixture), [`make_query`](#make_query-fixture), [`make_repo`](#make_repo-fixture), [`make_schema`](#make_schema-fixture), [`make_secret_scope`](#make_secret_scope-fixture), [`make_serving_endpoint`](#make_serving_endpoint-fixture), [`make_table`](#make_table-fixture), [`make_udf`](#make_udf-fixture), [`make_user`](#make_user-fixture), [`make_warehouse`](#make_warehouse-fixture).
313340

314341

315342
[[back to top](#python-testing-for-databricks)]
@@ -504,6 +531,26 @@ def test_new_group(make_group, make_user, ws):
504531
See also [`ws`](#ws-fixture), [`make_random`](#make_random-fixture), [`watchdog_purge_suffix`](#watchdog_purge_suffix-fixture).
505532

506533

534+
[[back to top](#python-testing-for-databricks)]
535+
536+
### `make_acc_group` fixture
537+
This fixture provides a function to manage Databricks account groups. Groups can be created with
538+
specified members and roles, and they will be deleted after the test is complete.
539+
540+
Has the same arguments and behavior as [`make_group` fixture](#make_group-fixture) but uses the account
541+
client instead of the workspace client.
542+
543+
Example usage:
544+
```python
545+
def test_new_account_group(make_acc_group, acc):
546+
group = make_acc_group()
547+
loaded = acc.groups.get(group.id)
548+
assert group.display_name == loaded.display_name
549+
```
550+
551+
See also [`acc`](#acc-fixture), [`make_random`](#make_random-fixture), [`watchdog_purge_suffix`](#watchdog_purge_suffix-fixture).
552+
553+
507554
[[back to top](#python-testing-for-databricks)]
508555

509556
### `make_user` fixture
@@ -819,7 +866,7 @@ See also [`ws`](#ws-fixture).
819866
### `product_info` fixture
820867
_No description yet._
821868

822-
See also [`ws`](#ws-fixture).
869+
See also [`acc`](#acc-fixture), [`ws`](#ws-fixture).
823870

824871

825872
[[back to top](#python-testing-for-databricks)]
@@ -1020,13 +1067,16 @@ See also [`make_cluster`](#make_cluster-fixture), [`make_instance_pool`](#make_i
10201067
### `watchdog_purge_suffix` fixture
10211068
HEX-encoded purge time suffix for test objects.
10221069

1023-
See also [`make_cluster_policy`](#make_cluster_policy-fixture), [`make_directory`](#make_directory-fixture), [`make_experiment`](#make_experiment-fixture), [`make_group`](#make_group-fixture), [`make_notebook`](#make_notebook-fixture), [`make_pipeline`](#make_pipeline-fixture), [`make_query`](#make_query-fixture), [`make_repo`](#make_repo-fixture), [`make_user`](#make_user-fixture), [`watchdog_remove_after`](#watchdog_remove_after-fixture).
1070+
See also [`make_acc_group`](#make_acc_group-fixture), [`make_cluster_policy`](#make_cluster_policy-fixture), [`make_directory`](#make_directory-fixture), [`make_experiment`](#make_experiment-fixture), [`make_group`](#make_group-fixture), [`make_notebook`](#make_notebook-fixture), [`make_pipeline`](#make_pipeline-fixture), [`make_query`](#make_query-fixture), [`make_repo`](#make_repo-fixture), [`make_user`](#make_user-fixture), [`watchdog_remove_after`](#watchdog_remove_after-fixture).
10241071

10251072

10261073
[[back to top](#python-testing-for-databricks)]
10271074

10281075
### `is_in_debug` fixture
1029-
_No description yet._
1076+
Returns true if the test is running from a debugger in IDE, otherwise false.
1077+
1078+
The following IDE are supported: IntelliJ IDEA (including Community Edition),
1079+
PyCharm (including Community Edition), and Visual Studio Code.
10301080

10311081
See also [`debug_env`](#debug_env-fixture), [`env_or_skip`](#env_or_skip-fixture).
10321082

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

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from pytest import fixture
66

7-
from databricks.sdk import WorkspaceClient
7+
from databricks.sdk import WorkspaceClient, AccountClient
8+
from databricks.sdk.config import Config
89
from databricks.sdk.errors import DatabricksError
910

1011
_LOG = logging.getLogger(__name__)
@@ -138,26 +139,70 @@ def test_workspace_operations(ws):
138139
```
139140
"""
140141
product_name, product_version = product_info
141-
# ignores fixed in https://github.com/databricks/databricks-sdk-py/pull/760
142142
return WorkspaceClient(
143143
host=debug_env["DATABRICKS_HOST"],
144-
auth_type=debug_env.get("DATABRICKS_AUTH_TYPE"), # type: ignore
145-
token=debug_env.get("DATABRICKS_TOKEN"), # type: ignore
146-
username=debug_env.get("DATABRICKS_USERNAME"), # type: ignore
147-
password=debug_env.get("DATABRICKS_PASSWORD"), # type: ignore
148-
client_id=debug_env.get("DATABRICKS_CLIENT_ID"), # type: ignore
149-
client_secret=debug_env.get("DATABRICKS_CLIENT_SECRET"), # type: ignore
144+
auth_type=debug_env.get("DATABRICKS_AUTH_TYPE"),
145+
token=debug_env.get("DATABRICKS_TOKEN"),
146+
username=debug_env.get("DATABRICKS_USERNAME"),
147+
password=debug_env.get("DATABRICKS_PASSWORD"),
148+
client_id=debug_env.get("DATABRICKS_CLIENT_ID"),
149+
client_secret=debug_env.get("DATABRICKS_CLIENT_SECRET"),
150150
debug_truncate_bytes=debug_env.get("DATABRICKS_DEBUG_TRUNCATE_BYTES"), # type: ignore
151151
debug_headers=debug_env.get("DATABRICKS_DEBUG_HEADERS"), # type: ignore
152-
azure_client_id=debug_env.get("ARM_CLIENT_ID"), # type: ignore
153-
azure_tenant_id=debug_env.get("ARM_TENANT_ID"), # type: ignore
154-
azure_client_secret=debug_env.get("ARM_CLIENT_SECRET"), # type: ignore
155-
cluster_id=debug_env.get("DATABRICKS_CLUSTER_ID"), # type: ignore
152+
azure_client_id=debug_env.get("ARM_CLIENT_ID"),
153+
azure_tenant_id=debug_env.get("ARM_TENANT_ID"),
154+
azure_client_secret=debug_env.get("ARM_CLIENT_SECRET"),
155+
cluster_id=debug_env.get("DATABRICKS_CLUSTER_ID"),
156156
product=product_name,
157157
product_version=product_version,
158158
)
159159

160160

161+
@fixture
162+
def acc(debug_env: dict[str, str], product_info: tuple[str, str], env_or_skip) -> AccountClient:
163+
"""
164+
Create and provide a Databricks AccountClient object.
165+
166+
This fixture initializes a Databricks AccountClient object, which can be used
167+
to interact with the Databricks account API. The created instance of AccountClient
168+
is shared across all test functions within the test session.
169+
170+
Requires `DATABRICKS_ACCOUNT_ID` environment variable to be set. If `DATABRICKS_HOST`
171+
points to a workspace host, the fixture would automatically determine the account host
172+
from it.
173+
174+
See [detailed documentation](https://databricks-sdk-py.readthedocs.io/en/latest/authentication.html) for the list
175+
of environment variables that can be used to authenticate the AccountClient.
176+
177+
In your test functions, include this fixture as an argument to use the AccountClient:
178+
179+
```python
180+
def test_listing_workspaces(acc):
181+
workspaces = acc.workspaces.list()
182+
assert len(workspaces) >= 1
183+
```
184+
"""
185+
product_name, product_version = product_info
186+
config = Config(
187+
host=debug_env["DATABRICKS_HOST"],
188+
account_id=env_or_skip("DATABRICKS_ACCOUNT_ID"),
189+
auth_type=debug_env.get("DATABRICKS_AUTH_TYPE"),
190+
username=debug_env.get("DATABRICKS_USERNAME"),
191+
password=debug_env.get("DATABRICKS_PASSWORD"),
192+
client_id=debug_env.get("DATABRICKS_CLIENT_ID"),
193+
client_secret=debug_env.get("DATABRICKS_CLIENT_SECRET"),
194+
debug_truncate_bytes=debug_env.get("DATABRICKS_DEBUG_TRUNCATE_BYTES"), # type: ignore
195+
debug_headers=debug_env.get("DATABRICKS_DEBUG_HEADERS"), # type: ignore
196+
azure_client_id=debug_env.get("ARM_CLIENT_ID"),
197+
azure_tenant_id=debug_env.get("ARM_TENANT_ID"),
198+
azure_client_secret=debug_env.get("ARM_CLIENT_SECRET"),
199+
product=product_name,
200+
product_version=product_version,
201+
)
202+
config.host = config.environment.deployment_url('accounts')
203+
return AccountClient(config=config)
204+
205+
161206
@fixture
162207
def log_workspace_link(ws):
163208
"""Returns a function to log a workspace link."""

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
@fixture
1313
def is_in_debug() -> bool:
14+
"""
15+
Returns true if the test is running from a debugger in IDE, otherwise false.
16+
17+
The following IDE are supported: IntelliJ IDEA (including Community Edition),
18+
PyCharm (including Community Edition), and Visual Studio Code.
19+
"""
1420
return os.path.basename(sys.argv[0]) in {"_jb_pytest_runner.py", "testlauncher.py"}
1521

1622

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ def test_new_group(make_group, make_user, ws):
7070
yield from _make_group("workspace group", ws.config, ws.groups, make_random, watchdog_purge_suffix)
7171

7272

73+
@fixture
74+
def make_acc_group(acc, make_random, watchdog_purge_suffix):
75+
"""
76+
This fixture provides a function to manage Databricks account groups. Groups can be created with
77+
specified members and roles, and they will be deleted after the test is complete.
78+
79+
Has the same arguments and behavior as [`make_group` fixture](#make_group-fixture) but uses the account
80+
client instead of the workspace client.
81+
82+
Example usage:
83+
```python
84+
def test_new_account_group(make_acc_group, acc):
85+
group = make_acc_group()
86+
loaded = acc.groups.get(group.id)
87+
assert group.display_name == loaded.display_name
88+
```
89+
"""
90+
yield from _make_group("account group", acc.config, acc.groups, make_random, watchdog_purge_suffix)
91+
92+
7393
def _scim_values(ids: list[str]) -> list[iam.ComplexValue]:
7494
return [iam.ComplexValue(value=x) for x in ids]
7595

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from databricks.labs.pytester.fixtures.baseline import (
44
ws,
5+
acc,
56
make_random,
67
product_info,
78
log_workspace_link,
@@ -15,7 +16,7 @@
1516
make_pipeline,
1617
make_warehouse,
1718
)
18-
from databricks.labs.pytester.fixtures.iam import make_group, make_user
19+
from databricks.labs.pytester.fixtures.iam import make_group, make_acc_group, make_user
1920
from databricks.labs.pytester.fixtures.catalog import (
2021
make_udf,
2122
make_catalog,
@@ -58,6 +59,7 @@
5859
'debug_env',
5960
'env_or_skip',
6061
'ws',
62+
'acc',
6163
'spark',
6264
'sql_backend',
6365
'sql_exec',
@@ -74,6 +76,7 @@
7476
'make_pipeline',
7577
'make_warehouse',
7678
'make_group',
79+
'make_acc_group',
7780
'make_user',
7881
'make_pipeline_permissions',
7982
'make_notebook',

tests/integration/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from pytest import fixture
44
from databricks.labs.blueprint.logger import install_logger
55

6+
from databricks.labs.pytester.__about__ import __version__
7+
68
install_logger()
79

810
logging.getLogger('databricks.labs.pytester').setLevel(logging.DEBUG)
@@ -11,3 +13,8 @@
1113
@fixture
1214
def debug_env_name():
1315
return "ucws"
16+
17+
18+
@fixture
19+
def product_info():
20+
return 'pytester', __version__
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def test_listing_workspaces(acc):
2+
workspaces = acc.workspaces.list()
3+
assert len(workspaces) >= 1

tests/integration/fixtures/test_iam.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ def test_new_group(make_group, make_user, ws):
1313
loaded = ws.groups.get(group.id)
1414
assert group.display_name == loaded.display_name
1515
assert group.members == loaded.members
16+
17+
18+
def test_new_account_group(make_acc_group, acc):
19+
group = make_acc_group()
20+
loaded = acc.groups.get(group.id)
21+
assert group.display_name == loaded.display_name

tests/unit/fixtures/test_iam.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
from databricks.labs.pytester.fixtures.iam import make_user, make_group
1+
from databricks.labs.pytester.fixtures.iam import make_user, make_group, make_acc_group
22
from databricks.labs.pytester.fixtures.unwrap import call_stateful
33

44

55
def test_make_user_no_args():
66
ctx, user = call_stateful(make_user)
77
assert ctx is not None
88
assert user is not None
9+
ctx['ws'].users.create.assert_called_once()
10+
ctx['ws'].users.delete.assert_called_once()
911

1012

1113
def test_make_group_no_args():
1214
ctx, group = call_stateful(make_group)
1315
assert ctx is not None
1416
assert group is not None
17+
ctx['ws'].groups.create.assert_called_once()
18+
ctx['ws'].groups.delete.assert_called_once()
19+
20+
21+
def test_make_acc_group_no_args():
22+
ctx, group = call_stateful(make_acc_group)
23+
assert ctx is not None
24+
assert group is not None
25+
ctx['acc'].groups.create.assert_called_once()
26+
ctx['acc'].groups.delete.assert_called_once()

0 commit comments

Comments
 (0)