From 7741347041b078ca4d687597897194d7797d202d Mon Sep 17 00:00:00 2001 From: Glenn Van de Water Date: Fri, 8 Mar 2019 16:31:23 +0100 Subject: [PATCH] Fix service discovery in functional tests If a required service is not enabled then we skip the test. The discovery is done by tests/functional/base.py:is_service_enabled but this method is broken, credentials are not passed to the 'openstack service show' command so every call will fail and every test that relies on it will be skipped. This commit fixed that method and the issues that popped up when re-enabling tests. Network segment range: - issue where we assumed network-segment-range extension is always present - issue where we compare integers and string representations of numbers Subnet: - issue where we try to deepcopy an uncopyable object in UnsetSubnet Change-Id: Id3cc907c1ed2a25b49cf6f4a7233e0401a02383a Story: 2005169 Task: 29908 --- openstackclient/network/v2/subnet.py | 19 +++++----- openstackclient/tests/functional/base.py | 35 ++++++++++--------- .../tests/functional/common/test_extension.py | 2 +- .../tests/functional/common/test_quota.py | 2 +- .../functional/compute/v2/test_server.py | 3 +- .../tests/functional/network/v2/common.py | 2 +- .../network/v2/test_network_segment_range.py | 14 ++++---- .../tests/functional/object/v1/common.py | 2 +- .../tests/functional/volume/v1/common.py | 3 +- 9 files changed, 41 insertions(+), 41 deletions(-) diff --git a/openstackclient/network/v2/subnet.py b/openstackclient/network/v2/subnet.py index 5f8113bb8..0733f37c8 100644 --- a/openstackclient/network/v2/subnet.py +++ b/openstackclient/network/v2/subnet.py @@ -681,29 +681,30 @@ class UnsetSubnet(command.Command): def take_action(self, parsed_args): client = self.app.client_manager.network obj = client.find_subnet(parsed_args.subnet, ignore_missing=False) - tmp_obj = copy.deepcopy(obj) + attrs = {} if parsed_args.dns_nameservers: - _update_arguments(tmp_obj.dns_nameservers, + attrs['dns_nameservers'] = copy.deepcopy(obj.dns_nameservers) + _update_arguments(attrs['dns_nameservers'], parsed_args.dns_nameservers, 'dns-nameserver') - attrs['dns_nameservers'] = tmp_obj.dns_nameservers if parsed_args.host_routes: + attrs['host_routes'] = copy.deepcopy(obj.host_routes) _update_arguments( - tmp_obj.host_routes, + attrs['host_routes'], convert_entries_to_nexthop(parsed_args.host_routes), 'host-route') - attrs['host_routes'] = tmp_obj.host_routes if parsed_args.allocation_pools: - _update_arguments(tmp_obj.allocation_pools, + attrs['allocation_pools'] = copy.deepcopy(obj.allocation_pools) + _update_arguments(attrs['allocation_pools'], parsed_args.allocation_pools, 'allocation-pool') - attrs['allocation_pools'] = tmp_obj.allocation_pools + if parsed_args.service_types: - _update_arguments(tmp_obj.service_types, + attrs['service_types'] = copy.deepcopy(obj.service_types) + _update_arguments(attrs['service_types'], parsed_args.service_types, 'service-type') - attrs['service_types'] = tmp_obj.service_types if attrs: client.update_subnet(obj, **attrs) diff --git a/openstackclient/tests/functional/base.py b/openstackclient/tests/functional/base.py index 7705c6553..1414e6bbb 100644 --- a/openstackclient/tests/functional/base.py +++ b/openstackclient/tests/functional/base.py @@ -11,7 +11,6 @@ # under the License. import os -import re import shlex import subprocess @@ -30,8 +29,6 @@ ADMIN_CLOUD = os.environ.get('OS_ADMIN_CLOUD', 'devstack-admin') def execute(cmd, fail_ok=False, merge_stderr=False): """Executes specified command for the given action.""" cmdlist = shlex.split(cmd) - result = '' - result_err = '' stdout = subprocess.PIPE stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE proc = subprocess.Popen(cmdlist, stdout=stdout, stderr=stderr) @@ -43,22 +40,8 @@ def execute(cmd, fail_ok=False, merge_stderr=False): return result -def is_service_enabled(service): - """Ask client cloud if service is available""" - try: - ret = execute('openstack service show -f value -c enabled ' + service) - except exceptions.CommandFailed: - # We get here for multiple reasons, all of them mean that a working - # service is not available - return False - - return "True" in ret - - class TestCase(testtools.TestCase): - delimiter_line = re.compile('^\+\-[\+\-]+\-\+$') - @classmethod def openstack(cls, cmd, cloud=ADMIN_CLOUD, fail_ok=False): """Executes openstackclient command for the given action.""" @@ -66,6 +49,24 @@ class TestCase(testtools.TestCase): 'openstack --os-cloud={cloud} '.format(cloud=cloud) + cmd, fail_ok=fail_ok) + @classmethod + def is_service_enabled(cls, service): + """Ask client cloud if service is available""" + cmd = ('service show -f value -c enabled {service}' + .format(service=service)) + try: + return "True" in cls.openstack(cmd) + except exceptions.CommandFailed as e: + if "No service with a type, name or ID of" in str(e): + return False + else: + raise # Unable to determine if service is enabled + + @classmethod + def is_extension_enabled(cls, alias): + """Ask client cloud if extension is enabled""" + return alias in cls.openstack('extension list -f value -c Alias') + @classmethod def get_openstack_configuration_value(cls, configuration): opts = cls.get_opts([configuration]) diff --git a/openstackclient/tests/functional/common/test_extension.py b/openstackclient/tests/functional/common/test_extension.py index db50855f7..92efabefe 100644 --- a/openstackclient/tests/functional/common/test_extension.py +++ b/openstackclient/tests/functional/common/test_extension.py @@ -26,7 +26,7 @@ class ExtensionTests(base.TestCase): @classmethod def setUpClass(cls): super(ExtensionTests, cls).setUpClass() - cls.haz_network = base.is_service_enabled('network') + cls.haz_network = cls.is_service_enabled('network') def test_extension_list_compute(self): """Test compute extension list""" diff --git a/openstackclient/tests/functional/common/test_quota.py b/openstackclient/tests/functional/common/test_quota.py index 859422812..9c0574607 100644 --- a/openstackclient/tests/functional/common/test_quota.py +++ b/openstackclient/tests/functional/common/test_quota.py @@ -27,7 +27,7 @@ class QuotaTests(base.TestCase): @classmethod def setUpClass(cls): super(QuotaTests, cls).setUpClass() - cls.haz_network = base.is_service_enabled('network') + cls.haz_network = cls.is_service_enabled('network') cls.PROJECT_NAME =\ cls.get_openstack_configuration_value('auth.project_name') diff --git a/openstackclient/tests/functional/compute/v2/test_server.py b/openstackclient/tests/functional/compute/v2/test_server.py index 3cb72d9f9..c8fb44d38 100644 --- a/openstackclient/tests/functional/compute/v2/test_server.py +++ b/openstackclient/tests/functional/compute/v2/test_server.py @@ -16,7 +16,6 @@ import uuid from tempest.lib import exceptions -from openstackclient.tests.functional import base from openstackclient.tests.functional.compute.v2 import common from openstackclient.tests.functional.volume.v2 import common as volume_common @@ -27,7 +26,7 @@ class ServerTests(common.ComputeTestCase): @classmethod def setUpClass(cls): super(ServerTests, cls).setUpClass() - cls.haz_network = base.is_service_enabled('network') + cls.haz_network = cls.is_service_enabled('network') def test_server_list(self): """Test server list, set""" diff --git a/openstackclient/tests/functional/network/v2/common.py b/openstackclient/tests/functional/network/v2/common.py index a18bc48fa..5243ecd0b 100644 --- a/openstackclient/tests/functional/network/v2/common.py +++ b/openstackclient/tests/functional/network/v2/common.py @@ -22,7 +22,7 @@ class NetworkTests(base.TestCase): @classmethod def setUpClass(cls): super(NetworkTests, cls).setUpClass() - cls.haz_network = base.is_service_enabled('network') + cls.haz_network = cls.is_service_enabled('network') class NetworkTagTests(NetworkTests): diff --git a/openstackclient/tests/functional/network/v2/test_network_segment_range.py b/openstackclient/tests/functional/network/v2/test_network_segment_range.py index 95402dcb3..c42f8744c 100644 --- a/openstackclient/tests/functional/network/v2/test_network_segment_range.py +++ b/openstackclient/tests/functional/network/v2/test_network_segment_range.py @@ -28,6 +28,8 @@ class NetworkSegmentRangeTests(common.NetworkTests): # Nothing in this class works with Nova Network if not self.haz_network: self.skipTest("No Network service present") + if not self.is_extension_enabled('network-segment-range'): + self.skipTest("No network-segment-range extension present") self.PROJECT_NAME = uuid.uuid4().hex def test_network_segment_range_create_delete(self): @@ -83,7 +85,7 @@ class NetworkSegmentRangeTests(common.NetworkTests): ) json_output = json.loads(self.openstack( - 'network segment list -f json' + 'network segment range list -f json' )) item_map = { item.get('ID'): item.get('Name') for item in json_output @@ -117,13 +119,11 @@ class NetworkSegmentRangeTests(common.NetworkTests): json_output["project_id"], ) - new_minimum = '2010' - new_maximum = '2060' + new_minimum = 2010 + new_maximum = 2060 cmd_output = self.openstack( - 'network segment range set ' + - '--minimum ' + new_minimum + ' ' + - '--maximum ' + new_maximum + ' ' + - name + 'network segment range set --minimum {min} --maximum {max} {name}' + .format(min=new_minimum, max=new_maximum, name=name) ) self.assertOutput('', cmd_output) diff --git a/openstackclient/tests/functional/object/v1/common.py b/openstackclient/tests/functional/object/v1/common.py index 44771aaab..b01334302 100644 --- a/openstackclient/tests/functional/object/v1/common.py +++ b/openstackclient/tests/functional/object/v1/common.py @@ -19,4 +19,4 @@ class ObjectStoreTests(base.TestCase): @classmethod def setUpClass(cls): super(ObjectStoreTests, cls).setUpClass() - cls.haz_object_store = base.is_service_enabled('object-store') + cls.haz_object_store = cls.is_service_enabled('object-store') diff --git a/openstackclient/tests/functional/volume/v1/common.py b/openstackclient/tests/functional/volume/v1/common.py index bb3c674ea..04eb1f48a 100644 --- a/openstackclient/tests/functional/volume/v1/common.py +++ b/openstackclient/tests/functional/volume/v1/common.py @@ -12,7 +12,6 @@ import fixtures -from openstackclient.tests.functional import base from openstackclient.tests.functional.volume import base as volume_base @@ -25,7 +24,7 @@ class BaseVolumeTests(volume_base.BaseVolumeTests): # TODO(dtroyer): This needs to be updated to specifically check for # Volume v1 rather than just 'volume', but for now # that is enough until we get proper version negotiation - cls.haz_volume_v1 = base.is_service_enabled('volume') + cls.haz_volume_v1 = cls.is_service_enabled('volume') def setUp(self): super(BaseVolumeTests, self).setUp()