Skip to content

Commit 5e1784e

Browse files
m-strzelczykdandhlee
authored andcommitted
chore(docs): Securing tests against leaks. (#139)
* chore(docs): Securing tests against leaks. * chore(docs): Updating some docstrings.
1 parent 94c3c97 commit 5e1784e

File tree

2 files changed

+33
-63
lines changed

2 files changed

+33
-63
lines changed

compute/compute/snippets/sample_create_vm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def create_from_snapshot(
324324
project_id: str, zone: str, instance_name: str, snapshot_link: str
325325
):
326326
"""
327-
Create a new VM instance with Debian 10 operating system.
327+
Create a new VM instance with boot disk created from a snapshot.
328328
329329
Args:
330330
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(
350350
project_id: str, zone: str, instance_name: str, snapshot_link: str
351351
):
352352
"""
353-
Create a new VM instance with Debian 10 operating system.
353+
Create a new VM instance with Debian 10 operating system and data disk created from snapshot.
354354
355355
Args:
356356
project_id: project ID or project number of the Cloud project you want to use.

compute/compute/snippets/test_sample_create_vm.py

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from google.cloud import compute_v1
1818
import pytest
1919

20-
2120
from quickstart import delete_instance, wait_for_operation
21+
2222
from sample_create_vm import (
2323
create_from_custom_image,
2424
create_from_public_image,
@@ -29,7 +29,8 @@
2929
)
3030

3131
PROJECT = google.auth.default()[1]
32-
INSTANCE_ZONE = "europe-central2-b"
32+
REGION = 'us-central1'
33+
INSTANCE_ZONE = "us-central1-b"
3334

3435

3536
def get_active_debian():
@@ -48,13 +49,13 @@ def src_disk(request):
4849
op = disk_client.insert(project=PROJECT, zone=INSTANCE_ZONE, disk_resource=disk)
4950

5051
wait_for_operation(op, PROJECT)
51-
disk = disk_client.get(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name)
52-
request.cls.disk = disk
53-
54-
yield disk
55-
56-
op = disk_client.delete(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name)
57-
wait_for_operation(op, PROJECT)
52+
try:
53+
disk = disk_client.get(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name)
54+
request.cls.disk = disk
55+
yield disk
56+
finally:
57+
op = disk_client.delete(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name)
58+
wait_for_operation(op, PROJECT)
5859

5960

6061
@pytest.fixture(scope="class")
@@ -70,14 +71,16 @@ def snapshot(request, src_disk):
7071
snapshot_resource=snapshot,
7172
)
7273
wait_for_operation(op, PROJECT)
74+
try:
75+
request.cls.snapshot = snapshot_client.get(
76+
project=PROJECT, snapshot=snapshot.name
77+
)
78+
snapshot = request.cls.snapshot
7379

74-
request.cls.snapshot = snapshot_client.get(project=PROJECT, snapshot=snapshot.name)
75-
snapshot = request.cls.snapshot
76-
77-
yield snapshot
78-
79-
op = snapshot_client.delete(project=PROJECT, snapshot=snapshot.name)
80-
wait_for_operation(op, PROJECT)
80+
yield snapshot
81+
finally:
82+
op = snapshot_client.delete(project=PROJECT, snapshot=snapshot.name)
83+
wait_for_operation(op, PROJECT)
8184

8285

8386
@pytest.fixture(scope="class")
@@ -89,46 +92,13 @@ def image(request, src_disk):
8992
op = image_client.insert(project=PROJECT, image_resource=image)
9093

9194
wait_for_operation(op, PROJECT)
92-
93-
image = image_client.get(project=PROJECT, image=image.name)
94-
request.cls.image = image
95-
yield image
96-
97-
op = image_client.delete(project=PROJECT, image=image.name)
98-
wait_for_operation(op, PROJECT)
99-
100-
101-
@pytest.fixture()
102-
def subnetwork():
103-
network_client = compute_v1.NetworksClient()
104-
network = compute_v1.Network()
105-
network.name = "test-network-" + uuid.uuid4().hex[:10]
106-
network.auto_create_subnetworks = True
107-
op = network_client.insert(project=PROJECT, network_resource=network)
108-
wait_for_operation(op, PROJECT)
109-
network = network_client.get(project=PROJECT, network=network.name)
110-
111-
subnet = compute_v1.Subnetwork()
112-
subnet.name = "test-subnet-" + uuid.uuid4().hex[:10]
113-
subnet.network = network_client.get(project=PROJECT, network=network.name).self_link
114-
subnet.region = "europe-central2"
115-
subnet.ip_cidr_range = "10.0.0.0/20"
116-
subnet_client = compute_v1.SubnetworksClient()
117-
op = subnet_client.insert(
118-
project=PROJECT, region="europe-central2", subnetwork_resource=subnet
119-
)
120-
wait_for_operation(op, PROJECT)
121-
subnet = subnet_client.get(
122-
project=PROJECT, region="europe-central2", subnetwork=subnet.name
123-
)
124-
125-
yield subnet
126-
127-
op = subnet_client.delete(project=PROJECT, region='europe-central2', subnetwork=subnet.name)
128-
wait_for_operation(op, PROJECT)
129-
130-
op = network_client.delete(project=PROJECT, network=network.name)
131-
wait_for_operation(op, PROJECT)
95+
try:
96+
image = image_client.get(project=PROJECT, image=image.name)
97+
request.cls.image = image
98+
yield image
99+
finally:
100+
op = image_client.delete(project=PROJECT, image=image.name)
101+
wait_for_operation(op, PROJECT)
132102

133103

134104
@pytest.mark.usefixtures("image", "snapshot")
@@ -201,17 +171,17 @@ def test_create_with_snapshotted_data_disk(self):
201171
finally:
202172
delete_instance(PROJECT, INSTANCE_ZONE, instance_name)
203173

204-
def test_create_with_subnet(self, subnetwork):
174+
def test_create_with_subnet(self):
205175
instance_name = "i" + uuid.uuid4().hex[:10]
206176
instance = create_with_subnet(
207177
PROJECT,
208178
INSTANCE_ZONE,
209179
instance_name,
210-
subnetwork.network,
211-
subnetwork.self_link,
180+
"global/networks/default",
181+
f"regions/{REGION}/subnetworks/default",
212182
)
213183
try:
214-
assert instance.network_interfaces[0].name == subnetwork.network
215-
assert instance.network_interfaces[0].subnetwork == subnetwork.self_link
184+
assert instance.network_interfaces[0].name == "global/networks/default"
185+
assert instance.network_interfaces[0].subnetwork == f"regions/{REGION}/subnetworks/default"
216186
finally:
217187
delete_instance(PROJECT, INSTANCE_ZONE, instance_name)

0 commit comments

Comments
 (0)