diff --git a/packages/google-cloud-compute/tests/system/base.py b/packages/google-cloud-compute/tests/system/base.py index 8c7e8f93c5b6..8967c491c8ea 100644 --- a/packages/google-cloud-compute/tests/system/base.py +++ b/packages/google-cloud-compute/tests/system/base.py @@ -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): @@ -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() diff --git a/packages/google-cloud-compute/tests/system/test_query_params.py b/packages/google-cloud-compute/tests/system/test_instance_group.py similarity index 53% rename from packages/google-cloud-compute/tests/system/test_query_params.py rename to packages/google-cloud-compute/tests/system/test_instance_group.py index 8f07410ebffc..3e6a37226f3f 100644 --- a/packages/google-cloud-compute/tests/system/test_query_params.py +++ b/packages/google-cloud-compute/tests/system/test_instance_group.py @@ -21,6 +21,8 @@ ) from google.cloud.compute_v1.types import ( InsertInstanceRequest, + InstanceGroupManager, + InstanceTemplate, Instance, AttachedDisk, NetworkInterface, @@ -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 @@ -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) diff --git a/packages/google-cloud-compute/tests/system/test_smoke.py b/packages/google-cloud-compute/tests/system/test_smoke.py index 926937c7da9d..36c6fbddf2dd 100644 --- a/packages/google-cloud-compute/tests/system/test_smoke.py +++ b/packages/google-cloud-compute/tests/system/test_smoke.py @@ -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() @@ -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() @@ -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)