Skip to content

Commit c43618b

Browse files
authored
Fix dlp tests (#3058)
Since the tests are flaky and timing out, I'm proposing we do the ML API approach of creating an operation then canceling it. It would fix #2809 fix #2810 fix #2811 fix #2812
1 parent dd8984f commit c43618b

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

dlp/inspect_content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def inspect_gcs_file(
473473
}
474474

475475
operation = dlp.create_dlp_job(parent, inspect_job=inspect_job)
476-
476+
print("Inspection operation started: {}".format(operation.name))
477477
# Create a Pub/Sub client and find the subscription. The subscription is
478478
# expected to already be listening to the topic.
479479
subscriber = google.cloud.pubsub.SubscriberClient()

dlp/inspect_content_test.py

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import google.api_core.exceptions
2121
import google.cloud.bigquery
2222
import google.cloud.datastore
23+
import google.cloud.dlp_v2
2324
import google.cloud.exceptions
2425
import google.cloud.pubsub
2526
import 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
294292
def 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
310312
def 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
367376
def 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 _():
402414
def 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")

dlp/requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
google-cloud-dlp==0.12.1
1+
google-cloud-dlp==0.13.0
22
google-cloud-storage==1.26.0
3-
google-cloud-pubsub==1.0.0
4-
google-cloud-datastore==1.9.0
5-
google-cloud-bigquery==1.20.0
3+
google-cloud-pubsub==1.3.1
4+
google-cloud-datastore==1.11.0
5+
google-cloud-bigquery==1.24.0

0 commit comments

Comments
 (0)