From bb2169a85bf66df42b408c90fadf74b6cdcb18a5 Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Mon, 3 Jan 2022 15:04:21 +0100 Subject: [PATCH 1/6] chore(samples): Removing some leaked instances. --- samples/snippets/test_sample_custom_types.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/samples/snippets/test_sample_custom_types.py b/samples/snippets/test_sample_custom_types.py index 812b04b50..deb9b79a8 100644 --- a/samples/snippets/test_sample_custom_types.py +++ b/samples/snippets/test_sample_custom_types.py @@ -16,7 +16,7 @@ import google.auth import pytest -from quickstart import create_instance, delete_instance +from quickstart import create_instance, delete_instance, list_instances from sample_custom_types import ( add_extended_memory_to_instance, create_custom_instance, @@ -192,3 +192,15 @@ def test_from_str_creation(): assert cmt.extra_memory_used is True assert cmt.cpu_series is CustomMachineType.CPUSeries.N2D assert cmt.core_count == 8 + + +def test_cleanup(): + """ + This is not really a test, this is code to be executed to clean up leaked instances. + """ + instances = list_instances(PROJECT, INSTANCE_ZONE) + for instance in instances: + if not instance.name.startswith("test-instance"): + continue + if instance.creation_timestamp < '2022-01-03T00': + delete_instance(PROJECT, INSTANCE_ZONE, instance.name) From d553d17d861145854b1d301d69b090668b484dd1 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Mon, 3 Jan 2022 14:13:04 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- samples/snippets/test_sample_custom_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/test_sample_custom_types.py b/samples/snippets/test_sample_custom_types.py index deb9b79a8..9dd3bc765 100644 --- a/samples/snippets/test_sample_custom_types.py +++ b/samples/snippets/test_sample_custom_types.py @@ -202,5 +202,5 @@ def test_cleanup(): for instance in instances: if not instance.name.startswith("test-instance"): continue - if instance.creation_timestamp < '2022-01-03T00': + if instance.creation_timestamp < "2022-01-03T00": delete_instance(PROJECT, INSTANCE_ZONE, instance.name) From 37743ea431656c99966eeffd98ff9f82499a910a Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Mon, 3 Jan 2022 16:06:05 +0100 Subject: [PATCH 3/6] chore(samples): Removing more leaked instances. --- samples/snippets/test_quickstart.py | 14 +++++++++++++- samples/snippets/test_sample_firewall.py | 12 ++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/samples/snippets/test_quickstart.py b/samples/snippets/test_quickstart.py index 705707656..0c43d2100 100644 --- a/samples/snippets/test_quickstart.py +++ b/samples/snippets/test_quickstart.py @@ -18,7 +18,7 @@ import google.auth -from quickstart import main +from quickstart import main, list_instances, delete_instance PROJECT = google.auth.default()[1] INSTANCE_NAME = "i" + uuid.uuid4().hex[:10] @@ -34,3 +34,15 @@ def test_main(capsys: typing.Any) -> None: assert re.search(f"Instances found in {INSTANCE_ZONE}:.+{INSTANCE_NAME}", out) assert re.search(f"zones/{INSTANCE_ZONE}:.+{INSTANCE_NAME}", out) assert f"Instance {INSTANCE_NAME} deleted." in out + + +def test_cleanup_europe(): + """ + This is not really a test, this is code to be executed to clean up leaked instances. + """ + instances = list_instances(PROJECT, INSTANCE_ZONE) + for instance in instances: + if not instance.name.startswith("i"): + continue + if instance.creation_timestamp < '2022-01-03T00': + delete_instance(PROJECT, INSTANCE_ZONE, instance.name) \ No newline at end of file diff --git a/samples/snippets/test_sample_firewall.py b/samples/snippets/test_sample_firewall.py index 1bb63cfd8..47e99bc9c 100644 --- a/samples/snippets/test_sample_firewall.py +++ b/samples/snippets/test_sample_firewall.py @@ -70,8 +70,16 @@ def test_create_delete(): rule = get_firewall_rule(PROJECT, rule_name) assert rule.name == rule_name assert "web" in rule.target_tags - delete_firewall_rule(PROJECT, rule_name) - assert all(rule.name != rule_name for rule in list_firewall_rules(PROJECT)) + try: + delete_firewall_rule(PROJECT, rule_name) + assert all(rule.name != rule_name for rule in list_firewall_rules(PROJECT)) + except google.api_core.exceptions.BadRequest as err: + if err.code == 400 and "is not ready" in err.message: + # We can ignore this, this is most likely GCE Enforcer removing the rule before us. + pass + else: + # Something else went wrong, let's escalate it. + raise err def test_patch_rule(firewall_rule): From e90a326d2ed2cbee93066eb49f812c10f85650b3 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Mon, 3 Jan 2022 15:15:10 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- samples/snippets/test_quickstart.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/test_quickstart.py b/samples/snippets/test_quickstart.py index 0c43d2100..354013d9f 100644 --- a/samples/snippets/test_quickstart.py +++ b/samples/snippets/test_quickstart.py @@ -44,5 +44,5 @@ def test_cleanup_europe(): for instance in instances: if not instance.name.startswith("i"): continue - if instance.creation_timestamp < '2022-01-03T00': - delete_instance(PROJECT, INSTANCE_ZONE, instance.name) \ No newline at end of file + if instance.creation_timestamp < "2022-01-03T00": + delete_instance(PROJECT, INSTANCE_ZONE, instance.name) From e77e558f2f5b2532f21626ec88c3eb0abc7be2ba Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Mon, 3 Jan 2022 17:31:15 +0100 Subject: [PATCH 5/6] chore(samples): Removing cleanup code, leaving only the firewall test fix. --- samples/snippets/test_quickstart.py | 14 +------------- samples/snippets/test_sample_custom_types.py | 14 +------------- 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/samples/snippets/test_quickstart.py b/samples/snippets/test_quickstart.py index 0c43d2100..705707656 100644 --- a/samples/snippets/test_quickstart.py +++ b/samples/snippets/test_quickstart.py @@ -18,7 +18,7 @@ import google.auth -from quickstart import main, list_instances, delete_instance +from quickstart import main PROJECT = google.auth.default()[1] INSTANCE_NAME = "i" + uuid.uuid4().hex[:10] @@ -34,15 +34,3 @@ def test_main(capsys: typing.Any) -> None: assert re.search(f"Instances found in {INSTANCE_ZONE}:.+{INSTANCE_NAME}", out) assert re.search(f"zones/{INSTANCE_ZONE}:.+{INSTANCE_NAME}", out) assert f"Instance {INSTANCE_NAME} deleted." in out - - -def test_cleanup_europe(): - """ - This is not really a test, this is code to be executed to clean up leaked instances. - """ - instances = list_instances(PROJECT, INSTANCE_ZONE) - for instance in instances: - if not instance.name.startswith("i"): - continue - if instance.creation_timestamp < '2022-01-03T00': - delete_instance(PROJECT, INSTANCE_ZONE, instance.name) \ No newline at end of file diff --git a/samples/snippets/test_sample_custom_types.py b/samples/snippets/test_sample_custom_types.py index 9dd3bc765..812b04b50 100644 --- a/samples/snippets/test_sample_custom_types.py +++ b/samples/snippets/test_sample_custom_types.py @@ -16,7 +16,7 @@ import google.auth import pytest -from quickstart import create_instance, delete_instance, list_instances +from quickstart import create_instance, delete_instance from sample_custom_types import ( add_extended_memory_to_instance, create_custom_instance, @@ -192,15 +192,3 @@ def test_from_str_creation(): assert cmt.extra_memory_used is True assert cmt.cpu_series is CustomMachineType.CPUSeries.N2D assert cmt.core_count == 8 - - -def test_cleanup(): - """ - This is not really a test, this is code to be executed to clean up leaked instances. - """ - instances = list_instances(PROJECT, INSTANCE_ZONE) - for instance in instances: - if not instance.name.startswith("test-instance"): - continue - if instance.creation_timestamp < "2022-01-03T00": - delete_instance(PROJECT, INSTANCE_ZONE, instance.name) From 42e304da00540e4aafef9d295b5b941c1f8508eb Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Wed, 5 Jan 2022 13:55:53 +0100 Subject: [PATCH 6/6] chore(samples): Restructuring firewall test. --- samples/snippets/test_sample_firewall.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/samples/snippets/test_sample_firewall.py b/samples/snippets/test_sample_firewall.py index 47e99bc9c..161ffbf0c 100644 --- a/samples/snippets/test_sample_firewall.py +++ b/samples/snippets/test_sample_firewall.py @@ -64,15 +64,14 @@ def firewall_rule(): raise err -def test_create_delete(): +@pytest.fixture +def autodelete_firewall_name(): + """ + Provide a name for a firewall rule and then delete the rule. + """ rule_name = "firewall-sample-test-" + uuid.uuid4().hex[:10] - create_firewall_rule(PROJECT, rule_name) - rule = get_firewall_rule(PROJECT, rule_name) - assert rule.name == rule_name - assert "web" in rule.target_tags try: delete_firewall_rule(PROJECT, rule_name) - assert all(rule.name != rule_name for rule in list_firewall_rules(PROJECT)) except google.api_core.exceptions.BadRequest as err: if err.code == 400 and "is not ready" in err.message: # We can ignore this, this is most likely GCE Enforcer removing the rule before us. @@ -82,6 +81,13 @@ def test_create_delete(): raise err +def test_create(autodelete_firewall_name): + create_firewall_rule(PROJECT, autodelete_firewall_name) + rule = get_firewall_rule(PROJECT, autodelete_firewall_name) + assert rule.name == autodelete_firewall_name + assert "web" in rule.target_tags + + def test_patch_rule(firewall_rule): fw_client = compute_v1.FirewallsClient() assert firewall_rule.priority == 1000