From 0140bc12568c75d1d9e91758cc5549aaa1d4fa3e Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Wed, 3 Nov 2021 16:05:07 +0100 Subject: [PATCH] chore(docs): Securing tests against leaks. (#139) * chore(docs): Securing tests against leaks. * chore(docs): Updating some docstrings. --- .../samples/snippets/sample_create_vm.py | 4 +- .../samples/snippets/test_sample_create_vm.py | 92 +++++++------------ 2 files changed, 33 insertions(+), 63 deletions(-) diff --git a/packages/google-cloud-compute/samples/snippets/sample_create_vm.py b/packages/google-cloud-compute/samples/snippets/sample_create_vm.py index 24331f9d76b4..21021e010dfd 100644 --- a/packages/google-cloud-compute/samples/snippets/sample_create_vm.py +++ b/packages/google-cloud-compute/samples/snippets/sample_create_vm.py @@ -324,7 +324,7 @@ def create_from_snapshot( project_id: str, zone: str, instance_name: str, snapshot_link: str ): """ - Create a new VM instance with Debian 10 operating system. + Create a new VM instance with boot disk created from a snapshot. Args: project_id: project ID or project number of the Cloud project you want to use. @@ -350,7 +350,7 @@ def create_with_snapshotted_data_disk( project_id: str, zone: str, instance_name: str, snapshot_link: str ): """ - Create a new VM instance with Debian 10 operating system. + Create a new VM instance with Debian 10 operating system and data disk created from snapshot. Args: project_id: project ID or project number of the Cloud project you want to use. diff --git a/packages/google-cloud-compute/samples/snippets/test_sample_create_vm.py b/packages/google-cloud-compute/samples/snippets/test_sample_create_vm.py index b69570f162a2..d610d4776fe9 100644 --- a/packages/google-cloud-compute/samples/snippets/test_sample_create_vm.py +++ b/packages/google-cloud-compute/samples/snippets/test_sample_create_vm.py @@ -17,8 +17,8 @@ from google.cloud import compute_v1 import pytest - from quickstart import delete_instance, wait_for_operation + from sample_create_vm import ( create_from_custom_image, create_from_public_image, @@ -29,7 +29,8 @@ ) PROJECT = google.auth.default()[1] -INSTANCE_ZONE = "europe-central2-b" +REGION = 'us-central1' +INSTANCE_ZONE = "us-central1-b" def get_active_debian(): @@ -48,13 +49,13 @@ def src_disk(request): op = disk_client.insert(project=PROJECT, zone=INSTANCE_ZONE, disk_resource=disk) wait_for_operation(op, PROJECT) - disk = disk_client.get(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name) - request.cls.disk = disk - - yield disk - - op = disk_client.delete(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name) - wait_for_operation(op, PROJECT) + try: + disk = disk_client.get(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name) + request.cls.disk = disk + yield disk + finally: + op = disk_client.delete(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name) + wait_for_operation(op, PROJECT) @pytest.fixture(scope="class") @@ -70,14 +71,16 @@ def snapshot(request, src_disk): snapshot_resource=snapshot, ) wait_for_operation(op, PROJECT) + try: + request.cls.snapshot = snapshot_client.get( + project=PROJECT, snapshot=snapshot.name + ) + snapshot = request.cls.snapshot - request.cls.snapshot = snapshot_client.get(project=PROJECT, snapshot=snapshot.name) - snapshot = request.cls.snapshot - - yield snapshot - - op = snapshot_client.delete(project=PROJECT, snapshot=snapshot.name) - wait_for_operation(op, PROJECT) + yield snapshot + finally: + op = snapshot_client.delete(project=PROJECT, snapshot=snapshot.name) + wait_for_operation(op, PROJECT) @pytest.fixture(scope="class") @@ -89,46 +92,13 @@ def image(request, src_disk): op = image_client.insert(project=PROJECT, image_resource=image) wait_for_operation(op, PROJECT) - - image = image_client.get(project=PROJECT, image=image.name) - request.cls.image = image - yield image - - op = image_client.delete(project=PROJECT, image=image.name) - wait_for_operation(op, PROJECT) - - -@pytest.fixture() -def subnetwork(): - network_client = compute_v1.NetworksClient() - network = compute_v1.Network() - network.name = "test-network-" + uuid.uuid4().hex[:10] - network.auto_create_subnetworks = True - op = network_client.insert(project=PROJECT, network_resource=network) - wait_for_operation(op, PROJECT) - network = network_client.get(project=PROJECT, network=network.name) - - subnet = compute_v1.Subnetwork() - subnet.name = "test-subnet-" + uuid.uuid4().hex[:10] - subnet.network = network_client.get(project=PROJECT, network=network.name).self_link - subnet.region = "europe-central2" - subnet.ip_cidr_range = "10.0.0.0/20" - subnet_client = compute_v1.SubnetworksClient() - op = subnet_client.insert( - project=PROJECT, region="europe-central2", subnetwork_resource=subnet - ) - wait_for_operation(op, PROJECT) - subnet = subnet_client.get( - project=PROJECT, region="europe-central2", subnetwork=subnet.name - ) - - yield subnet - - op = subnet_client.delete(project=PROJECT, region='europe-central2', subnetwork=subnet.name) - wait_for_operation(op, PROJECT) - - op = network_client.delete(project=PROJECT, network=network.name) - wait_for_operation(op, PROJECT) + try: + image = image_client.get(project=PROJECT, image=image.name) + request.cls.image = image + yield image + finally: + op = image_client.delete(project=PROJECT, image=image.name) + wait_for_operation(op, PROJECT) @pytest.mark.usefixtures("image", "snapshot") @@ -201,17 +171,17 @@ def test_create_with_snapshotted_data_disk(self): finally: delete_instance(PROJECT, INSTANCE_ZONE, instance_name) - def test_create_with_subnet(self, subnetwork): + def test_create_with_subnet(self): instance_name = "i" + uuid.uuid4().hex[:10] instance = create_with_subnet( PROJECT, INSTANCE_ZONE, instance_name, - subnetwork.network, - subnetwork.self_link, + "global/networks/default", + f"regions/{REGION}/subnetworks/default", ) try: - assert instance.network_interfaces[0].name == subnetwork.network - assert instance.network_interfaces[0].subnetwork == subnetwork.self_link + assert instance.network_interfaces[0].name == "global/networks/default" + assert instance.network_interfaces[0].subnetwork == f"regions/{REGION}/subnetworks/default" finally: delete_instance(PROJECT, INSTANCE_ZONE, instance_name)