-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Changes This PR implements some fixture improvements that were originally implemented downstream: - When creating an account or workspace group, we now require the group to be visible via two subsequent `.get()` and `.list()` calls. This double-check approach mitigates (but doesn't completely eliminate) problems with consistency of the APIs for working with groups. - The `wait_for_provisioning` argument to the `make_group` and `make_acc_group` has now been removed: we always wait. (For compatibility the argument is still accepted but will trigger a deprecation warning.) An incidental change is the internal unit-test plumbing can now be provided with mock fixtures specific to the test; this is needed to ensure the double-check implementation can be tested from the unit tests. ### Linked issues Supersedes databrickslabs/ucx#2634. ### Tests - added/updated unit tests - existing integration tests
- Loading branch information
Showing
4 changed files
with
132 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,75 @@ | ||
from databricks.labs.pytester.fixtures.iam import make_user, make_group, make_acc_group | ||
from databricks.labs.pytester.fixtures.unwrap import call_stateful | ||
import sys | ||
import warnings | ||
from functools import partial | ||
from unittest.mock import call | ||
|
||
import pytest | ||
|
||
def test_make_user_no_args(): | ||
from databricks.labs.pytester.fixtures.iam import make_acc_group, make_group, make_user, Group | ||
from databricks.labs.pytester.fixtures.unwrap import call_stateful, CallContext | ||
|
||
|
||
def test_make_user_no_args() -> None: | ||
ctx, user = call_stateful(make_user) | ||
assert ctx is not None | ||
assert user is not None | ||
ctx['ws'].users.create.assert_called_once() | ||
ctx['ws'].users.delete.assert_called_once() | ||
|
||
|
||
def test_make_group_no_args(): | ||
ctx, group = call_stateful(make_group) | ||
assert ctx is not None | ||
def _setup_groups_api(call_context: CallContext, *, client_fixture_name: str) -> CallContext: | ||
"""Minimum mocking of the specific client so that when a group is created it is also visible via the list() method. | ||
This is required because the make_group and make_acc_group fixtures double-check after creating a group to ensure | ||
the group is visible.""" | ||
mock_group = Group(id="an_id") | ||
call_context[client_fixture_name].groups.create.return_value = mock_group | ||
call_context[client_fixture_name].groups.list.return_value = [mock_group] | ||
return call_context | ||
|
||
|
||
def test_make_group_no_args() -> None: | ||
ctx, group = call_stateful(make_group, call_context_setup=partial(_setup_groups_api, client_fixture_name="ws")) | ||
|
||
assert group is not None | ||
ctx['ws'].groups.create.assert_called_once() | ||
assert ctx['ws'].groups.get.call_args_list == [call("an_id"), call("an_id")] | ||
assert ctx['ws'].groups.list.call_args_list == [ | ||
call(attributes="id", filter='id eq "an_id"'), | ||
call(attributes="id", filter='id eq "an_id"'), | ||
] | ||
ctx['ws'].groups.delete.assert_called_once() | ||
|
||
|
||
def test_make_acc_group_no_args(): | ||
ctx, group = call_stateful(make_acc_group) | ||
assert ctx is not None | ||
def test_make_acc_group_no_args() -> None: | ||
ctx, group = call_stateful(make_acc_group, call_context_setup=partial(_setup_groups_api, client_fixture_name="acc")) | ||
|
||
assert group is not None | ||
ctx['acc'].groups.create.assert_called_once() | ||
assert ctx['acc'].groups.get.call_args_list == [call("an_id"), call("an_id")] | ||
assert ctx['acc'].groups.list.call_args_list == [ | ||
call(attributes="id", filter='id eq "an_id"'), | ||
call(attributes="id", filter='id eq "an_id"'), | ||
] | ||
ctx['acc'].groups.delete.assert_called_once() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"make_group_fixture, client_fixture_name", | ||
[(make_group, "ws"), (make_acc_group, "acc")], | ||
) | ||
def test_make_group_deprecated_arg(make_group_fixture, client_fixture_name) -> None: | ||
with warnings.catch_warnings(record=True) as w: | ||
warnings.simplefilter("always") | ||
|
||
call_stateful( | ||
make_group_fixture, | ||
wait_for_provisioning=True, | ||
call_context_setup=partial(_setup_groups_api, client_fixture_name=client_fixture_name), | ||
) | ||
|
||
# Check that the expected warning was emitted and attributed to the caller. | ||
(the_warning, *other_warnings) = w | ||
assert not other_warnings | ||
assert issubclass(the_warning.category, DeprecationWarning) | ||
assert "wait_for_provisioning when making a group is deprecated" in str(the_warning.message) | ||
assert the_warning.filename == sys.modules[call_stateful.__module__].__file__ |