Skip to content

Commit f68686c

Browse files
authored
Enforce protected-access pylint rule (#956)
1 parent fd8772b commit f68686c

File tree

10 files changed

+40
-37
lines changed

10 files changed

+40
-37
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ disable = [
552552
"missing-module-docstring",
553553
"missing-class-docstring",
554554
"missing-function-docstring",
555-
"protected-access", # TODO: enable back
555+
# "protected-access", # TODO: enable back
556556
"too-few-public-methods",
557557
"line-too-long",
558558
"too-many-lines",

src/databricks/labs/ucx/azure/access.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def save_spn_permissions(self) -> str | None:
8080
return None
8181
return self._installation.save(storage_account_infos, filename=self._filename)
8282

83+
def load(self):
84+
return self._installation.save(list[StoragePermissionMapping], filename=self._filename)
85+
8386
def _get_storage_accounts(self) -> list[str]:
8487
external_locations = self._locations.snapshot()
8588
storage_accounts = []

src/databricks/labs/ucx/workspace_access/groups.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def partial_info(cls, workspace: iam.Group, account: iam.Group):
5353
external_id=workspace.external_id,
5454
)
5555

56+
def decode_members(self):
57+
return [iam.ComplexValue.from_dict(_) for _ in json.loads(self.members)]
58+
5659

5760
class MigrationState:
5861
"""Holds migration state of workspace-to-account groups"""
@@ -451,6 +454,14 @@ def validate_group_membership(self) -> list[dict]:
451454
logger.info("There are groups with different membership between account and workspace")
452455
return mismatch_group
453456

457+
def has_workspace_group(self, name):
458+
groups = self._workspace_groups_in_workspace()
459+
return name in groups
460+
461+
def has_account_group(self, name):
462+
groups = self._account_groups_in_workspace()
463+
return name in groups
464+
454465
def _workspace_groups_in_workspace(self) -> dict[str, Group]:
455466
attributes = "id,displayName,meta,externalId,members,roles,entitlements"
456467
groups = {}

tests/integration/azure/test_access.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ def test_azure_storage_accounts(ws, sql_backend, inventory_schema, make_random):
2222
location = ExternalLocations(ws, sql_backend, inventory_schema)
2323
installation = Installation(ws, make_random)
2424
az_res_perm = AzureResourcePermissions(installation, ws, AzureResources(ws), location)
25-
accounts = list(az_res_perm._get_storage_accounts())
26-
assert len(accounts) == 1
27-
assert accounts[0] == "labsazurethings"
25+
az_res_perm.save_spn_permissions()
26+
27+
mapping = az_res_perm.load()
28+
assert len(mapping) == 1
29+
assert mapping[0].prefix == "labsazurethings"
2830

2931

3032
@pytest.mark.skip

tests/integration/workspace_access/test_groups.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,11 @@ def test_delete_ws_groups_should_not_delete_non_reflected_acc_groups(ws, make_uc
128128

129129

130130
def validate_migrate_groups(group_manager: GroupManager, ws_group: Group, to_group: Group):
131-
workspace_groups = group_manager._workspace_groups_in_workspace()
132-
assert ws_group.display_name in workspace_groups
131+
assert group_manager.has_workspace_group(ws_group.display_name), f'missing workspace group: {ws_group.display_name}'
133132
group_manager.rename_groups()
134-
workspace_groups = group_manager._workspace_groups_in_workspace()
135-
assert f"ucx-temp-{ws_group.display_name}" in workspace_groups
133+
assert group_manager.has_workspace_group(f"ucx-temp-{ws_group.display_name}"), 'missing temp group'
136134
group_manager.reflect_account_groups_on_workspace()
137-
account_workspace_groups = group_manager._account_groups_in_workspace()
138-
assert to_group.display_name in account_workspace_groups
135+
assert group_manager.has_account_group(to_group.display_name), f'missing account group: {to_group.display_name}'
139136

140137

141138
@retried(on=[NotFound], timeout=timedelta(minutes=2))

tests/integration/workspace_access/test_permissions_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_permissions_save_and_load(ws, sql_backend, inventory_schema, env_or_ski
1010
Permissions(object_id="efg", object_type="fgh", raw="ghi"),
1111
]
1212

13-
pi._save(saved)
13+
pi._save(saved) # pylint: disable=protected-access
1414
loaded = pi.load_all()
1515

1616
assert saved == loaded

tests/unit/assessment/test_azure.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,9 @@ def test_list_all_pipeline_with_conf_spn_secret_avlb():
189189

190190

191191
def test_azure_spn_info_with_secret_unavailable():
192-
ws = workspace_client_mock(cluster_ids=['simplest-autoscale'], pipeline_ids=['empty-spec'], secret_exists=False)
193-
spark_conf = {
194-
"spark.hadoop.fs.azure.account."
195-
"oauth2.client.id.abcde.dfs.core.windows.net": "{{secrets/abcff/sp_app_client_id}}",
196-
"spark.hadoop.fs.azure.account."
197-
"oauth2.client.endpoint.abcde.dfs.core.windows.net": "https://login.microsoftonline.com/dedededede"
198-
"/token",
199-
"spark.hadoop.fs.azure.account."
200-
"oauth2.client.secret.abcde.dfs.core.windows.net": "{{secrets/abcff/sp_secret}}",
201-
}
202-
crawler = AzureServicePrincipalCrawler(ws, MockBackend(), "ucx")._get_azure_spn_from_config(spark_conf)
203-
204-
assert crawler == set()
192+
ws = workspace_client_mock(cluster_ids=['azure-spn-secret'], secret_exists=False)
193+
result = AzureServicePrincipalCrawler(ws, MockBackend(), "ucx").snapshot()
194+
assert len(result) == 0
205195

206196

207197
def test_jobs_assessment_with_spn_cluster_policy_not_found():

tests/unit/workspace_access/test_groups.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -539,24 +539,24 @@ def my_side_effect(group_id, **_):
539539
raise NotImplementedError
540540

541541
wsclient.groups.get.side_effect = my_side_effect
542+
wsclient.api_client.do.return_value = {
543+
'Resources': [
544+
{'displayName': 'group_1'},
545+
{'displayName': 'group_2'},
546+
{'displayName': 'group_3'},
547+
]
548+
}
542549

543-
# Test when attributes do not contain "members"
544550
gm = GroupManager(backend, wsclient, inventory_database="inv")
545-
result = gm._list_workspace_groups("WorkspaceGroup", "id,displayName,meta")
546-
assert len(result) == 3
547-
assert result[0].display_name == "group_1"
548-
assert result[0].members is None
549-
wsclient.groups.get.assert_not_called()
551+
result = gm.snapshot()
550552

551-
# Test when attributes contain "members"
552-
result = gm._list_workspace_groups("WorkspaceGroup", "id,displayName,meta,members")
553553
assert len(result) == 3
554-
assert result[0].display_name == "group_1"
555-
assert result[0].members == [
554+
assert result[0].name_in_workspace == "group_1"
555+
assert result[0].decode_members() == [
556556
ComplexValue(display="test-user-1", value="20"),
557557
ComplexValue(display="test-user-2", value="21"),
558558
]
559-
wsclient.groups.get.assert_called()
559+
assert wsclient.groups.get.call_count == 3
560560

561561

562562
def test_snapshot_with_group_matched_by_suffix():

tests/unit/workspace_access/test_listing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_list_and_analyze_should_separate_folders_and_other_objects():
5252
client.workspace.list.return_value = [file, directory, notebook]
5353

5454
listing_instance = listing.WorkspaceListing(client, 1)
55-
directories, others = listing_instance._list_and_analyze(rootobj)
55+
directories, others = listing_instance._list_and_analyze(rootobj) # pylint: disable=protected-access
5656

5757
assert compare(others, [file, notebook])
5858
assert compare(directories, [directory])

tests/unit/workspace_access/test_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_cleanup(b):
3333
def test_save(b):
3434
pi = PermissionManager(b, "test_database", [])
3535

36-
pi._save([Permissions("object1", "clusters", "test acl")])
36+
pi._save([Permissions("object1", "clusters", "test acl")]) # pylint: disable=protected-access
3737

3838
assert [Permissions(object_id="object1", object_type="clusters", raw="test acl")] == b.rows_written_for(
3939
"hive_metastore.test_database.permissions", "append"

0 commit comments

Comments
 (0)