13
13
# limitations under the License.
14
14
15
15
import os
16
+ import time
16
17
import uuid
17
18
18
19
import google .cloud .bigquery
36
37
BIGQUERY_TABLE_ID = "dlp_test_table" + UNIQUE_STRING
37
38
BIGQUERY_HARMFUL_TABLE_ID = "harmful" + UNIQUE_STRING
38
39
40
+ TIMEOUT = 30
41
+
39
42
40
43
# Create new custom topic/subscription
41
- @pytest .fixture (scope = "module" )
44
+ # We observe sometimes all the tests in this file fail. In a
45
+ # hypothesis where DLP service somehow loses the connection to the
46
+ # topic, now we use function scope for Pub/Sub fixtures.
47
+ @pytest .fixture (scope = "function" )
42
48
def topic_id ():
43
49
# Creates a pubsub topic, and tears it down.
44
50
publisher = google .cloud .pubsub .PublisherClient ()
@@ -53,7 +59,7 @@ def topic_id():
53
59
publisher .delete_topic (topic_path )
54
60
55
61
56
- @pytest .fixture (scope = "module " )
62
+ @pytest .fixture (scope = "function " )
57
63
def subscription_id (topic_id ):
58
64
# Subscribes to a topic.
59
65
subscriber = google .cloud .pubsub .SubscriberClient ()
@@ -160,7 +166,22 @@ def bigquery_project():
160
166
bigquery_client .delete_dataset (dataset_ref , delete_contents = True )
161
167
162
168
163
- @pytest .mark .flaky
169
+ def delay (err , * args ):
170
+ # 20 mins of delay. This sounds like too long a delay, but we
171
+ # occasionally observe consequtive time block where operations are
172
+ # slow which leads to the test failures. These situations tend to
173
+ # get self healed in 20 minutes or so, so I'm trying this strategy.
174
+ #
175
+ # There are 10 tests, so we don't want the retry delay happening
176
+ # for all the tests. When we exhaust the MAX_FLAKY_WAIT, we retry
177
+ # the test immediately.
178
+ wait_time = min (pytest .MAX_FLAKY_WAIT , 60 * 20 )
179
+ pytest .MAX_FLAKY_WAIT -= wait_time
180
+ time .sleep (wait_time )
181
+ return True
182
+
183
+
184
+ @pytest .mark .flaky (max_runs = 2 , min_passes = 1 , rerun_filter = delay )
164
185
def test_numerical_risk_analysis (
165
186
topic_id , subscription_id , bigquery_project , capsys
166
187
):
@@ -172,13 +193,14 @@ def test_numerical_risk_analysis(
172
193
NUMERIC_FIELD ,
173
194
topic_id ,
174
195
subscription_id ,
196
+ timeout = TIMEOUT ,
175
197
)
176
198
177
199
out , _ = capsys .readouterr ()
178
200
assert "Value Range:" in out
179
201
180
202
181
- @pytest .mark .flaky
203
+ @pytest .mark .flaky ( max_runs = 2 , min_passes = 1 , rerun_filter = delay )
182
204
def test_categorical_risk_analysis_on_string_field (
183
205
topic_id , subscription_id , bigquery_project , capsys
184
206
):
@@ -190,14 +212,14 @@ def test_categorical_risk_analysis_on_string_field(
190
212
UNIQUE_FIELD ,
191
213
topic_id ,
192
214
subscription_id ,
193
- timeout = 180 ,
215
+ timeout = TIMEOUT ,
194
216
)
195
217
196
218
out , _ = capsys .readouterr ()
197
219
assert "Most common value occurs" in out
198
220
199
221
200
- @pytest .mark .flaky
222
+ @pytest .mark .flaky ( max_runs = 2 , min_passes = 1 , rerun_filter = delay )
201
223
def test_categorical_risk_analysis_on_number_field (
202
224
topic_id , subscription_id , bigquery_project , capsys
203
225
):
@@ -209,13 +231,14 @@ def test_categorical_risk_analysis_on_number_field(
209
231
NUMERIC_FIELD ,
210
232
topic_id ,
211
233
subscription_id ,
234
+ timeout = TIMEOUT ,
212
235
)
213
236
214
237
out , _ = capsys .readouterr ()
215
238
assert "Most common value occurs" in out
216
239
217
240
218
- @pytest .mark .flaky
241
+ @pytest .mark .flaky ( max_runs = 2 , min_passes = 1 , rerun_filter = delay )
219
242
def test_k_anonymity_analysis_single_field (
220
243
topic_id , subscription_id , bigquery_project , capsys
221
244
):
@@ -227,14 +250,15 @@ def test_k_anonymity_analysis_single_field(
227
250
topic_id ,
228
251
subscription_id ,
229
252
[NUMERIC_FIELD ],
253
+ timeout = TIMEOUT ,
230
254
)
231
255
232
256
out , _ = capsys .readouterr ()
233
257
assert "Quasi-ID values:" in out
234
258
assert "Class size:" in out
235
259
236
260
237
- @pytest .mark .flaky (max_runs = 3 , min_passes = 1 )
261
+ @pytest .mark .flaky (max_runs = 2 , min_passes = 1 , rerun_filter = delay )
238
262
def test_k_anonymity_analysis_multiple_fields (
239
263
topic_id , subscription_id , bigquery_project , capsys
240
264
):
@@ -246,14 +270,15 @@ def test_k_anonymity_analysis_multiple_fields(
246
270
topic_id ,
247
271
subscription_id ,
248
272
[NUMERIC_FIELD , REPEATED_FIELD ],
273
+ timeout = TIMEOUT ,
249
274
)
250
275
251
276
out , _ = capsys .readouterr ()
252
277
assert "Quasi-ID values:" in out
253
278
assert "Class size:" in out
254
279
255
280
256
- @pytest .mark .flaky
281
+ @pytest .mark .flaky ( max_runs = 2 , min_passes = 1 , rerun_filter = delay )
257
282
def test_l_diversity_analysis_single_field (
258
283
topic_id , subscription_id , bigquery_project , capsys
259
284
):
@@ -266,6 +291,7 @@ def test_l_diversity_analysis_single_field(
266
291
subscription_id ,
267
292
UNIQUE_FIELD ,
268
293
[NUMERIC_FIELD ],
294
+ timeout = TIMEOUT ,
269
295
)
270
296
271
297
out , _ = capsys .readouterr ()
@@ -274,7 +300,7 @@ def test_l_diversity_analysis_single_field(
274
300
assert "Sensitive value" in out
275
301
276
302
277
- @pytest .mark .flaky (max_runs = 3 , min_passes = 1 )
303
+ @pytest .mark .flaky (max_runs = 2 , min_passes = 1 , rerun_filter = delay )
278
304
def test_l_diversity_analysis_multiple_field (
279
305
topic_id , subscription_id , bigquery_project , capsys
280
306
):
@@ -287,6 +313,7 @@ def test_l_diversity_analysis_multiple_field(
287
313
subscription_id ,
288
314
UNIQUE_FIELD ,
289
315
[NUMERIC_FIELD , REPEATED_FIELD ],
316
+ timeout = TIMEOUT ,
290
317
)
291
318
292
319
out , _ = capsys .readouterr ()
@@ -295,7 +322,7 @@ def test_l_diversity_analysis_multiple_field(
295
322
assert "Sensitive value" in out
296
323
297
324
298
- @pytest .mark .flaky
325
+ @pytest .mark .flaky ( max_runs = 2 , min_passes = 1 , rerun_filter = delay )
299
326
def test_k_map_estimate_analysis_single_field (
300
327
topic_id , subscription_id , bigquery_project , capsys
301
328
):
@@ -308,6 +335,7 @@ def test_k_map_estimate_analysis_single_field(
308
335
subscription_id ,
309
336
[NUMERIC_FIELD ],
310
337
["AGE" ],
338
+ timeout = TIMEOUT ,
311
339
)
312
340
313
341
out , _ = capsys .readouterr ()
@@ -316,7 +344,7 @@ def test_k_map_estimate_analysis_single_field(
316
344
assert "Values" in out
317
345
318
346
319
- @pytest .mark .flaky (max_runs = 3 , min_passes = 1 )
347
+ @pytest .mark .flaky (max_runs = 2 , min_passes = 1 , rerun_filter = delay )
320
348
def test_k_map_estimate_analysis_multiple_field (
321
349
topic_id , subscription_id , bigquery_project , capsys
322
350
):
@@ -329,6 +357,7 @@ def test_k_map_estimate_analysis_multiple_field(
329
357
subscription_id ,
330
358
[NUMERIC_FIELD , STRING_BOOLEAN_FIELD ],
331
359
["AGE" , "GENDER" ],
360
+ timeout = TIMEOUT ,
332
361
)
333
362
334
363
out , _ = capsys .readouterr ()
@@ -337,7 +366,7 @@ def test_k_map_estimate_analysis_multiple_field(
337
366
assert "Values" in out
338
367
339
368
340
- @pytest .mark .flaky
369
+ @pytest .mark .flaky ( max_runs = 2 , min_passes = 1 , rerun_filter = delay )
341
370
def test_k_map_estimate_analysis_quasi_ids_info_types_equal (
342
371
topic_id , subscription_id , bigquery_project
343
372
):
@@ -351,4 +380,5 @@ def test_k_map_estimate_analysis_quasi_ids_info_types_equal(
351
380
subscription_id ,
352
381
[NUMERIC_FIELD , STRING_BOOLEAN_FIELD ],
353
382
["AGE" ],
383
+ timeout = TIMEOUT ,
354
384
)
0 commit comments