66from functools import partial
77
88from databricks .sdk .retries import retried
9+ from databricks .sdk .service .catalog import TableInfo , SchemaInfo
910
1011from databricks .labs .ucx .hive_metastore .grants import Grant , GrantsCrawler
1112from databricks .labs .ucx .workspace_access .base import Permissions
1718logger = logging .getLogger (__name__ )
1819
1920
21+ @retried (on = [AssertionError ], timeout = dt .timedelta (seconds = 30 ))
22+ def assert_grants_with_retry (
23+ grants_crawler : GrantsCrawler ,
24+ object_type : str ,
25+ migrated_group : MigratedGroup ,
26+ table_info : TableInfo | None = None ,
27+ schema_info : SchemaInfo | None = None ,
28+ catalog_name : str | None = None ,
29+ schema_name : str | None = None ,
30+ expected_grants : set [str ] | None = None ,
31+ udf_names_grants_mapping : dict [str , set [str ]] | None = None ,
32+ ) -> None :
33+ if object_type == "UDF" and udf_names_grants_mapping is not None :
34+ for udf_name , expected_udf_grants in udf_names_grants_mapping .items ():
35+ actual_grants = defaultdict (set )
36+ for grant in grants_crawler .grants (catalog = catalog_name , database = schema_name , udf = udf_name ):
37+ actual_grants [grant .principal ].add (grant .action_type )
38+ # Note: the following assert is the source of the KeyError (and why we might need to re-load the permissions).
39+ assert expected_udf_grants == actual_grants [migrated_group .name_in_account ]
40+ elif object_type == "TABLE" and table_info is not None :
41+ actual_grants = grants_crawler .for_table_info (table_info )
42+ # Note: the following assert is the source of the KeyError (and why we might need to re-load the permissions).
43+ assert expected_grants == actual_grants [migrated_group .name_in_account ]
44+ elif object_type == "SCHEMA" and schema_info is not None :
45+ actual_grants = grants_crawler .for_schema_info (schema_info )
46+ # Note: the following assert is the source of the KeyError (and why we might need to re-load the permissions).
47+ assert expected_grants == actual_grants [migrated_group .name_in_account ]
48+
49+
2050def test_grants_with_permission_migration_api (runtime_ctx , ws , migrated_group , sql_backend ):
2151 schema_a = runtime_ctx .make_schema ()
2252 table_a = runtime_ctx .make_table (schema_name = schema_a .name )
@@ -34,11 +64,21 @@ def test_grants_with_permission_migration_api(runtime_ctx, ws, migrated_group, s
3464
3565 MigrationState ([migrated_group ]).apply_to_groups_with_different_names (ws )
3666
37- new_table_grants = {"a" : grants .for_table_info (table_a )}
38- assert {"SELECT" } == new_table_grants ["a" ][migrated_group .name_in_account ]
67+ assert_grants_with_retry (
68+ grants_crawler = grants ,
69+ migrated_group = migrated_group ,
70+ table_info = table_a ,
71+ object_type = "TABLE" ,
72+ expected_grants = {"SELECT" },
73+ )
3974
40- new_schema_grants = {"a" : grants .for_schema_info (schema_a )}
41- assert {"USAGE" , "OWN" } == new_schema_grants ["a" ][migrated_group .name_in_account ]
75+ assert_grants_with_retry (
76+ grants_crawler = grants ,
77+ migrated_group = migrated_group ,
78+ schema_info = schema_a ,
79+ object_type = "SCHEMA" ,
80+ expected_grants = {"USAGE" , "OWN" },
81+ )
4282
4383
4484def test_permission_for_files_anonymous_func_migration_api (runtime_ctx , migrated_group ) -> None :
@@ -97,25 +137,13 @@ def test_permission_for_udfs_migration_api(ws, sql_backend, runtime_ctx, migrate
97137
98138 MigrationState ([migrated_group ]).apply_to_groups_with_different_names (ws )
99139
100- @retried (on = [AssertionError ], timeout = dt .timedelta (seconds = 10 ))
101- def assert_udf_grants_with_retry (
102- grants_crawler : GrantsCrawler ,
103- catalog_name : str ,
104- schema_name : str ,
105- udf_names_grants_mapping : dict [str , set [str ]],
106- ) -> None :
107- for udf_name , expected_grants in udf_names_grants_mapping .items ():
108- actual_grants = defaultdict (set )
109- for grant in grants_crawler .grants (catalog = catalog_name , database = schema_name , udf = udf_name ):
110- actual_grants [grant .principal ].add (grant .action_type )
111- # Note: the following assert is the source of the KeyError (and why we might need to re-load the permissions).
112- assert expected_grants == actual_grants [migrated_group .name_in_account ]
113-
114- assert_udf_grants_with_retry (
115- grants ,
116- schema .catalog_name ,
117- schema .name ,
118- {
140+ assert_grants_with_retry (
141+ grants_crawler = grants ,
142+ object_type = "UDF" ,
143+ catalog_name = schema .catalog_name ,
144+ schema_name = schema .name ,
145+ migrated_group = migrated_group ,
146+ udf_names_grants_mapping = {
119147 udf_a .name : {"SELECT" , "OWN" },
120148 udf_b .name : {"READ_METADATA" },
121149 },
0 commit comments