Skip to content

Commit

Permalink
chore(docs): Securing tests against leaks. (#139)
Browse files Browse the repository at this point in the history
* chore(docs): Securing tests against leaks.
* chore(docs): Updating some docstrings.
  • Loading branch information
m-strzelczyk authored Nov 3, 2021
1 parent ca03982 commit 0140bc1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 63 deletions.
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
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
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)

0 comments on commit 0140bc1

Please sign in to comment.