2020import google .api_core .exceptions
2121import google .cloud .bigquery
2222import google .cloud .datastore
23+ import google .cloud .dlp_v2
2324import google .cloud .exceptions
2425import google .cloud .pubsub
2526import google .cloud .storage
@@ -94,9 +95,7 @@ def subscription_id(topic_id):
9495 # Subscribes to a topic.
9596 subscriber = google .cloud .pubsub .SubscriberClient ()
9697 topic_path = subscriber .topic_path (GCLOUD_PROJECT , topic_id )
97- subscription_path = subscriber .subscription_path (
98- GCLOUD_PROJECT , SUBSCRIPTION_ID
99- )
98+ subscription_path = subscriber .subscription_path (GCLOUD_PROJECT , SUBSCRIPTION_ID )
10099 try :
101100 subscriber .create_subscription (subscription_path , topic_path )
102101 except google .api_core .exceptions .AlreadyExists :
@@ -290,7 +289,6 @@ def test_inspect_image_file(capsys):
290289 assert "Info type: PHONE_NUMBER" in out
291290
292291
293- @flaky
294292def test_inspect_gcs_file (bucket , topic_id , subscription_id , capsys ):
295293 inspect_content .inspect_gcs_file (
296294 GCLOUD_PROJECT ,
@@ -303,10 +301,14 @@ def test_inspect_gcs_file(bucket, topic_id, subscription_id, capsys):
303301 )
304302
305303 out , _ = capsys .readouterr ()
306- assert "Info type: EMAIL_ADDRESS" in out
304+ assert "Inspection operation started" in out
305+ # Cancel the operation
306+ operation_id = out .split ("Inspection operation started: " )[1 ].split ("\n " )[0 ]
307+ print (operation_id )
308+ client = google .cloud .dlp_v2 .DlpServiceClient ()
309+ client .cancel_dlp_job (operation_id )
307310
308311
309- @flaky
310312def test_inspect_gcs_file_with_custom_info_types (
311313 bucket , topic_id , subscription_id , capsys
312314):
@@ -326,14 +328,16 @@ def test_inspect_gcs_file_with_custom_info_types(
326328 )
327329
328330 out , _ = capsys .readouterr ()
329- assert "Info type: CUSTOM_DICTIONARY_0" in out
330- assert "Info type: CUSTOM_REGEX_0" in out
331331
332+ assert "Inspection operation started" in out
333+ # Cancel the operation
334+ operation_id = out .split ("Inspection operation started: " )[1 ].split ("\n " )[0 ]
335+ print (operation_id )
336+ client = google .cloud .dlp_v2 .DlpServiceClient ()
337+ client .cancel_dlp_job (operation_id )
332338
333- @flaky
334- def test_inspect_gcs_file_no_results (
335- bucket , topic_id , subscription_id , capsys
336- ):
339+
340+ def test_inspect_gcs_file_no_results (bucket , topic_id , subscription_id , capsys ):
337341 inspect_content .inspect_gcs_file (
338342 GCLOUD_PROJECT ,
339343 bucket .name ,
@@ -345,7 +349,13 @@ def test_inspect_gcs_file_no_results(
345349 )
346350
347351 out , _ = capsys .readouterr ()
348- assert "No findings" in out
352+
353+ assert "Inspection operation started" in out
354+ # Cancel the operation
355+ operation_id = out .split ("Inspection operation started: " )[1 ].split ("\n " )[0 ]
356+ print (operation_id )
357+ client = google .cloud .dlp_v2 .DlpServiceClient ()
358+ client .cancel_dlp_job (operation_id )
349359
350360
351361@pytest .mark .skip (reason = "nondeterministically failing" )
@@ -363,7 +373,6 @@ def test_inspect_gcs_image_file(bucket, topic_id, subscription_id, capsys):
363373 assert "Info type: EMAIL_ADDRESS" in out
364374
365375
366- @flaky
367376def test_inspect_gcs_multiple_files (bucket , topic_id , subscription_id , capsys ):
368377 inspect_content .inspect_gcs_file (
369378 GCLOUD_PROJECT ,
@@ -375,14 +384,17 @@ def test_inspect_gcs_multiple_files(bucket, topic_id, subscription_id, capsys):
375384 )
376385
377386 out , _ = capsys .readouterr ()
378- assert "Info type: EMAIL_ADDRESS" in out
379- assert "Info type: PHONE_NUMBER" in out
387+
388+ assert "Inspection operation started" in out
389+ # Cancel the operation
390+ operation_id = out .split ("Inspection operation started: " )[1 ].split ("\n " )[0 ]
391+ print (operation_id )
392+ client = google .cloud .dlp_v2 .DlpServiceClient ()
393+ client .cancel_dlp_job (operation_id )
380394
381395
382396@flaky
383- def test_inspect_datastore (
384- datastore_project , topic_id , subscription_id , capsys
385- ):
397+ def test_inspect_datastore (datastore_project , topic_id , subscription_id , capsys ):
386398 @eventually_consistent .call
387399 def _ ():
388400 inspect_content .inspect_datastore (
@@ -402,17 +414,19 @@ def _():
402414def test_inspect_datastore_no_results (
403415 datastore_project , topic_id , subscription_id , capsys
404416):
405- inspect_content .inspect_datastore (
406- GCLOUD_PROJECT ,
407- datastore_project ,
408- DATASTORE_KIND ,
409- topic_id ,
410- subscription_id ,
411- ["PHONE_NUMBER" ],
412- )
417+ @eventually_consistent .call
418+ def _ ():
419+ inspect_content .inspect_datastore (
420+ GCLOUD_PROJECT ,
421+ datastore_project ,
422+ DATASTORE_KIND ,
423+ topic_id ,
424+ subscription_id ,
425+ ["PHONE_NUMBER" ],
426+ )
413427
414- out , _ = capsys .readouterr ()
415- assert "No findings" in out
428+ out , _ = capsys .readouterr ()
429+ assert "No findings" in out
416430
417431
418432@pytest .mark .skip (reason = "unknown issue" )
0 commit comments