Skip to content

Commit b2a689b

Browse files
authored
test: add column ACLs test with real policy tag (googleapis#678)
* test: add column ACLs test with real policy tag * Use v1 version of the datacatalog client * Install datacatalog in pre-releease tests * Adjust test to actually make it work * Make sure taxonomy is properly cleaned up
1 parent b85c8d3 commit b2a689b

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

noxfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ def system(session):
142142
else:
143143
session.install("google-cloud-storage", "-c", constraints_path)
144144

145+
# Data Catalog needed for the column ACL test with a real Policy Tag.
146+
session.install("google-cloud-datacatalog", "-c", constraints_path)
147+
145148
session.install("-e", ".[all]", "-c", constraints_path)
146149
session.install("ipython", "-c", constraints_path)
147150

@@ -211,6 +214,7 @@ def prerelease_deps(session):
211214
session.install("--pre", "grpcio", "pandas")
212215
session.install(
213216
"freezegun",
217+
"google-cloud-datacatalog",
214218
"google-cloud-storage",
215219
"google-cloud-testutils",
216220
"IPython",

tests/system/test_client.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
from google.cloud._helpers import UTC
6969
from google.cloud.bigquery import dbapi, enums
7070
from google.cloud import storage
71+
from google.cloud.datacatalog_v1 import types as datacatalog_types
72+
from google.cloud.datacatalog_v1 import PolicyTagManagerClient
7173

7274
from test_utils.retry import RetryErrors
7375
from test_utils.retry import RetryInstanceState
@@ -167,6 +169,8 @@ def setUp(self):
167169
self.to_delete = [dataset]
168170

169171
def tearDown(self):
172+
policy_tag_client = PolicyTagManagerClient()
173+
170174
def _still_in_use(bad_request):
171175
return any(
172176
error["reason"] == "resourceInUse" for error in bad_request._errors
@@ -183,6 +187,8 @@ def _still_in_use(bad_request):
183187
retry_in_use(Config.CLIENT.delete_dataset)(doomed, delete_contents=True)
184188
elif isinstance(doomed, (Table, bigquery.TableReference)):
185189
retry_in_use(Config.CLIENT.delete_table)(doomed)
190+
elif isinstance(doomed, datacatalog_types.Taxonomy):
191+
policy_tag_client.delete_taxonomy(name=doomed.name)
186192
else:
187193
doomed.delete()
188194

@@ -381,6 +387,68 @@ def test_create_table_with_policy(self):
381387
table2 = Config.CLIENT.update_table(table, ["schema"])
382388
self.assertEqual(policy_2, table2.schema[1].policy_tags)
383389

390+
def test_create_table_with_real_custom_policy(self):
391+
from google.cloud.bigquery.schema import PolicyTagList
392+
393+
policy_tag_client = PolicyTagManagerClient()
394+
taxonomy_parent = f"projects/{Config.CLIENT.project}/locations/us"
395+
396+
new_taxonomy = datacatalog_types.Taxonomy(
397+
display_name="Custom test taxonomy",
398+
description="This taxonomy is ony used for a test.",
399+
activated_policy_types=[
400+
datacatalog_types.Taxonomy.PolicyType.FINE_GRAINED_ACCESS_CONTROL
401+
],
402+
)
403+
404+
taxonomy = policy_tag_client.create_taxonomy(
405+
parent=taxonomy_parent, taxonomy=new_taxonomy
406+
)
407+
self.to_delete.insert(0, taxonomy)
408+
409+
parent_policy_tag = policy_tag_client.create_policy_tag(
410+
parent=taxonomy.name,
411+
policy_tag=datacatalog_types.PolicyTag(
412+
display_name="Parent policy tag", parent_policy_tag=None
413+
),
414+
)
415+
child_policy_tag = policy_tag_client.create_policy_tag(
416+
parent=taxonomy.name,
417+
policy_tag=datacatalog_types.PolicyTag(
418+
display_name="Child policy tag",
419+
parent_policy_tag=parent_policy_tag.name,
420+
),
421+
)
422+
423+
dataset = self.temp_dataset(
424+
_make_dataset_id("create_table_with_real_custom_policy")
425+
)
426+
table_id = "test_table"
427+
policy_1 = PolicyTagList(names=[parent_policy_tag.name])
428+
policy_2 = PolicyTagList(names=[child_policy_tag.name])
429+
430+
schema = [
431+
bigquery.SchemaField(
432+
"first_name", "STRING", mode="REQUIRED", policy_tags=policy_1
433+
),
434+
bigquery.SchemaField(
435+
"age", "INTEGER", mode="REQUIRED", policy_tags=policy_2
436+
),
437+
]
438+
table_arg = Table(dataset.table(table_id), schema=schema)
439+
self.assertFalse(_table_exists(table_arg))
440+
441+
table = helpers.retry_403(Config.CLIENT.create_table)(table_arg)
442+
self.to_delete.insert(0, table)
443+
444+
self.assertTrue(_table_exists(table))
445+
self.assertCountEqual(
446+
list(table.schema[0].policy_tags.names), [parent_policy_tag.name]
447+
)
448+
self.assertCountEqual(
449+
list(table.schema[1].policy_tags.names), [child_policy_tag.name]
450+
)
451+
384452
def test_create_table_w_time_partitioning_w_clustering_fields(self):
385453
from google.cloud.bigquery.table import TimePartitioning
386454
from google.cloud.bigquery.table import TimePartitioningType

0 commit comments

Comments
 (0)