From a828bee9782f5176e0df687fdb8798d787441b3e Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 29 Jun 2021 19:58:23 -0400 Subject: [PATCH] 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. --- .../snippets/v3/alerts-client/snippets_test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/monitoring/snippets/v3/alerts-client/snippets_test.py b/monitoring/snippets/v3/alerts-client/snippets_test.py index 7acc98123e1e..b1c62af2fb61 100644 --- a/monitoring/snippets/v3/alerts-client/snippets_test.py +++ b/monitoring/snippets/v3/alerts-client/snippets_test.py @@ -14,6 +14,7 @@ from __future__ import print_function +from datetime import datetime import random import string import time @@ -62,6 +63,17 @@ def __init__(self): monitoring_v3.NotificationChannelServiceClient() ) + # delete all existing policies older than 1 hour prior to testing + for policy in self.alert_policy_client.list_alert_policies(name=self.project_name): + seconds_since_creation = datetime.timestamp(datetime.utcnow())-datetime.timestamp(policy.creation_record.mutate_time) + if seconds_since_creation > 3600: + try: + self.alert_policy_client.delete_alert_policy( + name=policy.name + ) + except NotFound: + print("Ignored NotFound when deleting a policy.") + def __enter__(self): @retry( wait_exponential_multiplier=1000,