Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

chore(docs): Securing tests against leaks. #139

Merged
merged 6 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions samples/snippets/sample_create_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
92 changes: 31 additions & 61 deletions samples/snippets/test_sample_create_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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():
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-strzelczyk Non-blocking question: What's the benefit of using fixtures at the class scope instead of at the module scope for these tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit faster. I don't create a new disk/image/snapshot for every test. As those are used in read-only way I can share them between the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, didn't notice you mentioned module scope. This would work as well, I just picked the class scope because I found the example for it :P

yield image
finally:
op = image_client.delete(project=PROJECT, image=image.name)
wait_for_operation(op, PROJECT)


@pytest.mark.usefixtures("image", "snapshot")
Expand Down Expand Up @@ -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)