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

Commit

Permalink
Merge branch 'master' into samples
Browse files Browse the repository at this point in the history
  • Loading branch information
m-strzelczyk committed Jun 15, 2021
2 parents 353053f + aa5491c commit 10b2701
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 82 deletions.
11 changes: 11 additions & 0 deletions tests/system/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from google.cloud.compute_v1.services.global_operations.client import (
GlobalOperationsClient,
)
from google.cloud.compute_v1.types import Operation


class TestBase(unittest.TestCase):
Expand Down Expand Up @@ -51,6 +52,16 @@ def wait_for_zonal_operation(self, operation):
)
if result.error:
self.fail("Zonal operation {} has errors".format(operation))
op = client.get(
operation=operation, zone=self.DEFAULT_ZONE, project=self.DEFAULT_PROJECT
)
# this is a workaround, some operations take up to 3 min, currently we cant set timeout for wait()
if op.status != Operation.Status.DONE:
client.wait(
operation=operation,
zone=self.DEFAULT_ZONE,
project=self.DEFAULT_PROJECT,
)

def wait_for_regional_operation(self, operation):
client = RegionOperationsClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
)
from google.cloud.compute_v1.types import (
InsertInstanceRequest,
InstanceGroupManager,
InstanceTemplate,
Instance,
AttachedDisk,
NetworkInterface,
Expand Down Expand Up @@ -57,19 +59,42 @@ def tearDown(self) -> None:
project=self.DEFAULT_PROJECT, instance_template=template
)

""" Resize fails due to
def test_instance_group_resize(self):
template_name = self.get_unique_name('template')
igm_name = self.get_unique_name('igm')
initialize_params = AttachedDiskInitializeParams(source_image=self.DISK_IMAGE)
disk = AttachedDisk(
auto_delete=True,
boot=True,
type_=AttachedDisk.Type.PERSISTENT,
initialize_params=initialize_params,
)
network_interface = NetworkInterface(name="default")
instance = Instance(
name=self.name,
description="test",
disks=[disk],
machine_type=self.MACHINE_TYPE,
network_interfaces=[network_interface],
)
request = InsertInstanceRequest(
zone=self.DEFAULT_ZONE,
project=self.DEFAULT_PROJECT,
instance_resource=instance,
)
operation = self.inst_client.insert(request=request)
self.wait_for_zonal_operation(operation.name)
self.instances.append(self.name)

instance = self.insert_instance().target_link
template_name = self.get_unique_name("template")
igm_name = self.get_unique_name("igm")

instance = operation.target_link

template_resource = InstanceTemplate(
name=template_name,
source_instance=instance
name=template_name, source_instance=instance
)
operation = self.template_client.insert(
project=self.DEFAULT_PROJECT, instance_template_resource=template_resource
)
operation = self.template_client.insert(project=self.DEFAULT_PROJECT,
instance_template_resource=template_resource)
self.wait_for_global_operation(operation.name)
self.templates.append(template_name)
template = operation.target_link
Expand All @@ -78,46 +103,42 @@ def test_instance_group_resize(self):
base_instance_name="gapicinst",
instance_template=template,
name=igm_name,
target_size=1)
operation = self.igm_client.insert(project=self.DEFAULT_PROJECT, zone=self.DEFAULT_ZONE,
instance_group_manager_resource=igm_resource)
target_size=0,
)
operation = self.igm_client.insert(
project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE,
instance_group_manager_resource=igm_resource,
)
self.wait_for_zonal_operation(operation.name)
self.igms.append(igm_name)

instance_group = self.igm_client.get(project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE, instance_group_manager=igm_name)
self.assertEqual(instance_group.target_size, 1)
resize_op = self.igm_client.resize(project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE, size=0, instance_group_manager=igm_name)
self.wait_for_zonal_operation(resize_op.name)
igm = self.igm_client.get(project=self.DEFAULT_PROJECT, zone=self.DEFAULT_ZONE,
instance_group_manager=igm_name)
self.assertEqual(igm.target_size, 0)
"""

def insert_instance(self):
disk = AttachedDisk()
initialize_params = AttachedDiskInitializeParams()
initialize_params.source_image = self.DISK_IMAGE
disk.initialize_params = initialize_params
disk.auto_delete = True
disk.boot = True
disk.type_ = AttachedDisk.Type.PERSISTENT
instance_group = self.igm_client.get(
project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE,
instance_group_manager=igm_name,
)
self.assertEqual(instance_group.target_size, 0)

network_interface = NetworkInterface()
network_interface.name = "default"
resize_op = self.igm_client.resize(
project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE,
size=1,
instance_group_manager=igm_name,
)
self.wait_for_zonal_operation(resize_op.name)

instance = Instance()
instance.name = self.name
instance.disks = [disk]
instance.machine_type = self.MACHINE_TYPE
instance.network_interfaces = [network_interface]
instance_group = self.igm_client.get(
project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE,
instance_group_manager=igm_name,
)
self.assertEqual(instance_group.target_size, 1)

request = InsertInstanceRequest()
request.zone = self.DEFAULT_ZONE
request.project = self.DEFAULT_PROJECT
request.instance_resource = instance
operation = self.inst_client.insert(request=request)
self.wait_for_zonal_operation(operation.name)
self.instances.append(self.name)
return operation
# Resize to zero fails, uncomment once fixed b/189145532.
# resize_0_op = self.igm_client.resize(project=self.DEFAULT_PROJECT,
# zone=self.DEFAULT_ZONE, size=0, instance_group_manager=igm_name)
# self.wait_for_zonal_operation(resize_0_op.name)
# igm = self.igm_client.get(project=self.DEFAULT_PROJECT, zone=self.DEFAULT_ZONE,
# instance_group_manager=igm_name)
# self.assertEqual(igm.target_size, 0)
106 changes: 69 additions & 37 deletions tests/system/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,6 @@ def test_insert_instance(self):
self.insert_instance()
self.assert_instance()

def insert_instance(self):
disk = AttachedDisk()
initialize_params = AttachedDiskInitializeParams()
initialize_params.source_image = self.DISK_IMAGE
disk.initialize_params = initialize_params
disk.auto_delete = True
disk.boot = True
disk.type_ = AttachedDisk.Type.PERSISTENT

network_interface = NetworkInterface()
network_interface.name = "default"

instance = Instance()
instance.name = self.name
instance.disks = [disk]
instance.machine_type = self.MACHINE_TYPE
instance.network_interfaces = [network_interface]

request = InsertInstanceRequest()
request.zone = self.DEFAULT_ZONE
request.project = self.DEFAULT_PROJECT
request.instance_resource = instance
operation = self.client.insert(request=request)
self.wait_for_zonal_operation(operation.name)
self.instances.append(self.name)

def test_aggregated_list(self):
presented = False
self.insert_instance()
Expand Down Expand Up @@ -104,17 +78,6 @@ def test_zero_values(self):
str(ex.exception.args),
)

def get_instance(self):
return self.client.get(
project=self.DEFAULT_PROJECT, zone=self.DEFAULT_ZONE, instance=self.name
)

def assert_instance(self):
instance = self.get_instance()
self.assertEqual(getattr(instance, "name"), self.name)
self.assertEqual(len(getattr(instance, "network_interfaces")), 1)
self.assertEqual(len(getattr(instance, "disks")), 1)

def test_patch(self):
self.insert_instance()
instance = self.get_instance()
Expand Down Expand Up @@ -157,3 +120,72 @@ def test_list(self):
presented = True
break
self.assertTrue(presented)

def test_update_description_empty(self):
self.insert_instance()
instance = self.get_instance()
self.assertEqual(instance.description, "test")
self.assertEqual(instance.scheduling.min_node_cpus, 0)
instance.description = ""
update_op = self.client.update(
project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE,
instance=self.name,
instance_resource=instance,
)
self.wait_for_zonal_operation(update_op.name)
fetched = self.get_instance()
self.assertEqual(fetched.description, "")
self.assertEqual(fetched.scheduling.min_node_cpus, 0)

def test_update_description_non_ascii(self):
self.insert_instance()
instance = self.get_instance()
self.assertEqual(instance.description, "test")
instance.description = "тест"
update_op = self.client.update(
project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE,
instance=self.name,
instance_resource=instance,
)
self.wait_for_zonal_operation(update_op.name)
fetched = self.get_instance()
self.assertEqual(fetched.description, "тест")

def get_instance(self):
return self.client.get(
project=self.DEFAULT_PROJECT, zone=self.DEFAULT_ZONE, instance=self.name
)

def assert_instance(self):
instance = self.get_instance()
self.assertEqual(getattr(instance, "name"), self.name)
self.assertEqual(len(getattr(instance, "network_interfaces")), 1)
self.assertEqual(len(getattr(instance, "disks")), 1)

def insert_instance(self):
initialize_params = AttachedDiskInitializeParams(source_image=self.DISK_IMAGE)
disk = AttachedDisk(
auto_delete=True,
boot=True,
type_=AttachedDisk.Type.PERSISTENT,
initialize_params=initialize_params,
)
network_interface = NetworkInterface(name="default")
instance = Instance(
name=self.name,
description="test",
disks=[disk],
machine_type=self.MACHINE_TYPE,
network_interfaces=[network_interface],
)

request = InsertInstanceRequest(
zone=self.DEFAULT_ZONE,
project=self.DEFAULT_PROJECT,
instance_resource=instance,
)
operation = self.client.insert(request=request)
self.wait_for_zonal_operation(operation.name)
self.instances.append(self.name)

0 comments on commit 10b2701

Please sign in to comment.