Skip to content

Commit 9a4e951

Browse files
authored
chore: clean up alert policies older than 1 hour in snippets test (#170)
As a next step in troubleshooting the failure from #149, I'd like to clean up the existing alert policies in the test environment before running tests.
1 parent 499c5da commit 9a4e951

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

monitoring/snippets/v3/alerts-client/snippets_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import print_function
1616

17+
from datetime import datetime
1718
import random
1819
import string
1920
import time
@@ -62,6 +63,17 @@ def __init__(self):
6263
monitoring_v3.NotificationChannelServiceClient()
6364
)
6465

66+
# delete all existing policies older than 1 hour prior to testing
67+
for policy in self.alert_policy_client.list_alert_policies(name=self.project_name):
68+
seconds_since_creation = datetime.timestamp(datetime.utcnow())-datetime.timestamp(policy.creation_record.mutate_time)
69+
if seconds_since_creation > 3600:
70+
try:
71+
self.alert_policy_client.delete_alert_policy(
72+
name=policy.name
73+
)
74+
except NotFound:
75+
print("Ignored NotFound when deleting a policy.")
76+
6577
def __enter__(self):
6678
@retry(
6779
wait_exponential_multiplier=1000,

0 commit comments

Comments
 (0)