Skip to content

Commit

Permalink
tests: add system tests for python-compute (#34)
Browse files Browse the repository at this point in the history
* tests: add system tests for python-compute

* tests: add more system tests

* tests: fix linter errors

* tests: fix lint issue

* tests: delete tests/test_system - dummy for mlts
  • Loading branch information
georgiyekkert authored Mar 30, 2021
1 parent 1555c4b commit d510b4c
Show file tree
Hide file tree
Showing 6 changed files with 483 additions and 33 deletions.
33 changes: 0 additions & 33 deletions packages/google-cloud-compute/tests/system.py

This file was deleted.

69 changes: 69 additions & 0 deletions packages/google-cloud-compute/tests/system/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import unittest
import uuid
import google.auth
from google.cloud.compute_v1.services.zone_operations.client import ZoneOperationsClient
from google.cloud.compute_v1.services.region_operations.client import (
RegionOperationsClient,
)
from google.cloud.compute_v1.services.global_operations.client import (
GlobalOperationsClient,
)


class TestBase(unittest.TestCase):
def setUp(self):
_, self.DEFAULT_PROJECT = google.auth.default()
if not self.DEFAULT_PROJECT:
self.skipTest("GCP project was not found, skipping system test")
self.DEFAULT_ZONE = "us-central1-a"
self.DEFAULT_REGION = "us-central1"
self.MACHINE_TYPE = (
"https://www.googleapis.com/compute/v1/projects/{}/"
"zones/us-central1-a/machineTypes/n1-standard-1".format(
self.DEFAULT_PROJECT
)
)
self.DISK_IMAGE = "projects/debian-cloud/global/images/family/debian-10"

@staticmethod
def get_unique_name(placeholder=""):
return "gapic" + placeholder + uuid.uuid4().hex

def wait_for_zonal_operation(self, operation):
client = ZoneOperationsClient()
result = client.wait(
operation=operation, zone=self.DEFAULT_ZONE, project=self.DEFAULT_PROJECT
)
if result.error:
self.fail("Zonal operation {} has errors".format(operation))

def wait_for_regional_operation(self, operation):
client = RegionOperationsClient()
result = client.wait(
operation=operation,
region=self.DEFAULT_REGION,
project=self.DEFAULT_PROJECT,
)
if result.error:
self.fail("Region operation {} has errors".format(operation))

def wait_for_global_operation(self, operation):
client = GlobalOperationsClient()
result = client.wait(operation=operation, project=self.DEFAULT_PROJECT)
if result.error:
self.fail("Global operation {} has errors".format(operation))
64 changes: 64 additions & 0 deletions packages/google-cloud-compute/tests/system/test_addresses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.cloud.compute_v1.types import Address, InsertAddressRequest
from google.cloud.compute_v1.services.addresses.client import AddressesClient
from tests.system.base import TestBase


class TestAddresses(TestBase):
def setUp(self) -> None:
super().setUp()
self.client = AddressesClient(transport="rest")
self.name = self.get_unique_name("address")
self.addresses = []

def tearDown(self) -> None:
for address in self.addresses:
self.client.delete(
project=self.DEFAULT_PROJECT,
region=self.DEFAULT_REGION,
address=address,
)

def insert_address(self):
address_res = Address()
address_res.name = self.name

request = InsertAddressRequest()
request.project = self.DEFAULT_PROJECT
request.region = self.DEFAULT_REGION
request.address_resource = address_res
operation = self.client.insert(request)
self.wait_for_regional_operation(operation.name)
self.addresses.append(self.name)

def test_create_read(self):
self.insert_address()
address = self.client.get(
project=self.DEFAULT_PROJECT, region=self.DEFAULT_REGION, address=self.name
)
self.assertEqual(getattr(address, "name"), self.name)

def test_list(self):
presented = False
self.insert_address()
result = self.client.list(
project=self.DEFAULT_PROJECT, region=self.DEFAULT_REGION
)
for item in result:
if getattr(item, "name") == self.name:
presented = True
break
self.assertTrue(presented)
67 changes: 67 additions & 0 deletions packages/google-cloud-compute/tests/system/test_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from google.cloud.compute_v1.services.zones.client import ZonesClient
from google.cloud.compute_v1.types import ListZonesRequest
from tests.system.base import TestBase


class TestComputePagination(TestBase):
def setUp(self) -> None:
super().setUp()
self.client = ZonesClient()

def test_max_results(self):
request = ListZonesRequest()
request.max_results = 1
request.project = self.DEFAULT_PROJECT
result = self.client.list(request=request)
self.assertEqual(len(getattr(result, "items")), 1)

def test_next_page_token(self):
request = ListZonesRequest()
request.max_results = 1
request.project = self.DEFAULT_PROJECT
result = self.client.list(request=request)

token_request = ListZonesRequest()
token_request.max_results = 1
token_request.project = self.DEFAULT_PROJECT
token_request.page_token = getattr(result, "next_page_token")
token_result = self.client.list(request=token_request)
self.assertNotEqual(getattr(result, "items"), getattr(token_result, "items"))

def test_filter(self):
request = ListZonesRequest()
request.project = self.DEFAULT_PROJECT
request.filter = "name = us-central1-a"
result = self.client.list(request=request)
description = getattr(getattr(result, "items")[0], "description")
self.assertEqual(len(getattr(result, "items")), 1)
self.assertEqual(description, "us-central1-a")

def test_auto_paging(self):
request = ListZonesRequest()
request.max_results = 1
request.project = self.DEFAULT_PROJECT
request.filter = "name = us-*"
result = self.client.list(request=request)
presented = False
for item in result:
desc = getattr(item, "description")
if desc == self.DEFAULT_ZONE:
presented = True
break
self.assertTrue(presented)
123 changes: 123 additions & 0 deletions packages/google-cloud-compute/tests/system/test_query_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.cloud.compute_v1.services.instances.client import InstancesClient
from google.cloud.compute_v1.services.instance_group_managers.client import (
InstanceGroupManagersClient,
)
from google.cloud.compute_v1.services.instance_templates.client import (
InstanceTemplatesClient,
)
from google.cloud.compute_v1.types import (
InsertInstanceRequest,
Instance,
AttachedDisk,
NetworkInterface,
AttachedDiskInitializeParams,
)
from tests.system.base import TestBase


class TestInstanceGroups(TestBase):
def setUp(self):
super().setUp()
self.instances = []
self.igms = []
self.templates = []
self.inst_client = InstancesClient(transport="rest")
self.name = self.get_unique_name("instance")
self.igm_client = InstanceGroupManagersClient()
self.template_client = InstanceTemplatesClient()

def tearDown(self) -> None:
for igm in self.igms:
op = self.igm_client.delete(
project=self.DEFAULT_PROJECT,
zone=self.DEFAULT_ZONE,
instance_group_manager=igm,
)
self.wait_for_zonal_operation(op.name)
for instance in self.instances:
op = self.inst_client.delete(
project=self.DEFAULT_PROJECT, zone=self.DEFAULT_ZONE, instance=instance
)
for template in self.templates:
op = self.template_client.delete(
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')
instance = self.insert_instance().target_link
template_resource = InstanceTemplate(
name=template_name,
source_instance=instance
)
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
igm_resource = InstanceGroupManager(
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)
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

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.inst_client.insert(request=request)
self.wait_for_zonal_operation(operation.name)
self.instances.append(self.name)
return operation
Loading

0 comments on commit d510b4c

Please sign in to comment.