Fix client usage in validation resources

Validation resources implicitly depends on tempest/clients.py as
it uses client aliases defined there. This is not ok since that
module does not live under tempest/lib.

Change validation resources to use clients as defined in
tempest/lib/services/clients.py instead. The ServiceClients class
defined in there is the superclass of the Manager class that was
assumed before, so it will just work with any further change.

Clients were also not used consistently with the use_neutron
flag, so making sure that only neutron clients are used if neutron
is selected and viceversa.

Change-Id: If83610b06af13aa736b03d463a94ac6ed02296bd
This commit is contained in:
Andrea Frittoli 2017-08-09 16:55:33 +01:00
parent 1fa7a60e26
commit 463a8a64ef
4 changed files with 54 additions and 31 deletions

View File

@ -0,0 +1,5 @@
---
features:
- |
Implement the `rest_client` method `is_resource_deleted` in the network
security group client.

View File

@ -19,35 +19,39 @@ from tempest.lib import exceptions as lib_exc
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
def _create_neutron_sec_group_rules(os, sec_group, ethertype='IPv4'): def _network_service(os, use_neutron):
sec_group_rules_client = os.security_group_rules_client if use_neutron:
return os.network
sec_group_rules_client.create_security_group_rule( else:
security_group_id=sec_group['id'], return os.compute
protocol='tcp',
ethertype=ethertype,
port_range_min=22,
port_range_max=22,
direction='ingress')
sec_group_rules_client.create_security_group_rule(
security_group_id=sec_group['id'],
protocol='icmp',
ethertype=ethertype,
direction='ingress')
def create_ssh_security_group(os, add_rule=False, ethertype='IPv4', def create_ssh_security_group(os, add_rule=False, ethertype='IPv4',
use_neutron=True): use_neutron=True):
security_groups_client = os.compute_security_groups_client network_service = _network_service(os, use_neutron)
security_group_rules_client = os.compute_security_group_rules_client security_groups_client = network_service.SecurityGroupsClient()
security_group_rules_client = network_service.SecurityGroupRulesClient()
# Security Group clients for nova and neutron behave the same
sg_name = data_utils.rand_name('securitygroup-') sg_name = data_utils.rand_name('securitygroup-')
sg_description = data_utils.rand_name('description-') sg_description = data_utils.rand_name('description-')
security_group = security_groups_client.create_security_group( security_group = security_groups_client.create_security_group(
name=sg_name, description=sg_description)['security_group'] name=sg_name, description=sg_description)['security_group']
# Security Group Rules clients require different parameters depending on
# the network service in use
if add_rule: if add_rule:
if use_neutron: if use_neutron:
_create_neutron_sec_group_rules(os, security_group, security_group_rules_client.create_security_group_rule(
ethertype=ethertype) security_group_id=security_group['id'],
protocol='tcp',
ethertype=ethertype,
port_range_min=22,
port_range_max=22,
direction='ingress')
security_group_rules_client.create_security_group_rule(
security_group_id=security_group['id'],
protocol='icmp',
ethertype=ethertype,
direction='ingress')
else: else:
security_group_rules_client.create_security_group_rule( security_group_rules_client.create_security_group_rule(
parent_group_id=security_group['id'], ip_protocol='tcp', parent_group_id=security_group['id'], ip_protocol='tcp',
@ -69,7 +73,7 @@ def create_validation_resources(os, validation_resources=None,
if validation_resources: if validation_resources:
if validation_resources['keypair']: if validation_resources['keypair']:
keypair_name = data_utils.rand_name('keypair') keypair_name = data_utils.rand_name('keypair')
validation_data.update(os.keypairs_client.create_keypair( validation_data.update(os.compute.KeyPairsClient().create_keypair(
name=keypair_name)) name=keypair_name))
LOG.debug("Validation resource key %s created", keypair_name) LOG.debug("Validation resource key %s created", keypair_name)
add_rule = False add_rule = False
@ -80,8 +84,10 @@ def create_validation_resources(os, validation_resources=None,
create_ssh_security_group( create_ssh_security_group(
os, add_rule, use_neutron=use_neutron, ethertype=ethertype) os, add_rule, use_neutron=use_neutron, ethertype=ethertype)
if validation_resources['floating_ip']: if validation_resources['floating_ip']:
floating_ip_client = _network_service(
os, use_neutron).FloatingIPsClient()
if use_neutron: if use_neutron:
floatingip = os.floating_ips_client.create_floatingip( floatingip = floating_ip_client.create_floatingip(
floating_network_id=floating_network_id) floating_network_id=floating_network_id)
# validation_resources['floating_ip'] has historically looked # validation_resources['floating_ip'] has historically looked
# like a compute API POST /os-floating-ips response, so we need # like a compute API POST /os-floating-ips response, so we need
@ -94,18 +100,17 @@ def create_validation_resources(os, validation_resources=None,
# NOTE(mriedem): The os-floating-ips compute API was deprecated # NOTE(mriedem): The os-floating-ips compute API was deprecated
# in the 2.36 microversion. Any tests for CRUD operations on # in the 2.36 microversion. Any tests for CRUD operations on
# floating IPs using the compute API should be capped at 2.35. # floating IPs using the compute API should be capped at 2.35.
validation_data.update( validation_data.update(floating_ip_client.create_floating_ip(
os.compute_floating_ips_client.create_floating_ip( pool=floating_network_name))
pool=floating_network_name))
return validation_data return validation_data
def clear_validation_resources(os, validation_data=None): def clear_validation_resources(os, validation_data=None, use_neutron=True):
# Cleanup the vm validation resources # Cleanup the vm validation resources
has_exception = None has_exception = None
if validation_data: if validation_data:
if 'keypair' in validation_data: if 'keypair' in validation_data:
keypair_client = os.keypairs_client keypair_client = os.compute.KeyPairsClient()
keypair_name = validation_data['keypair']['name'] keypair_name = validation_data['keypair']['name']
try: try:
keypair_client.delete_keypair(keypair_name) keypair_client.delete_keypair(keypair_name)
@ -119,8 +124,9 @@ def clear_validation_resources(os, validation_data=None):
keypair_name) keypair_name)
if not has_exception: if not has_exception:
has_exception = exc has_exception = exc
network_service = _network_service(os, use_neutron)
if 'security_group' in validation_data: if 'security_group' in validation_data:
security_group_client = os.compute_security_groups_client security_group_client = network_service.SecurityGroupsClient()
sec_id = validation_data['security_group']['id'] sec_id = validation_data['security_group']['id']
try: try:
security_group_client.delete_security_group(sec_id) security_group_client.delete_security_group(sec_id)
@ -139,10 +145,13 @@ def clear_validation_resources(os, validation_data=None):
if not has_exception: if not has_exception:
has_exception = exc has_exception = exc
if 'floating_ip' in validation_data: if 'floating_ip' in validation_data:
floating_client = os.compute_floating_ips_client floating_ip_client = network_service.FloatingIPsClient()
fip_id = validation_data['floating_ip']['id'] fip_id = validation_data['floating_ip']['id']
try: try:
floating_client.delete_floating_ip(fip_id) if use_neutron:
floating_ip_client.delete_floatingip(fip_id)
else:
floating_ip_client.delete_floating_ip(fip_id)
except lib_exc.NotFound: except lib_exc.NotFound:
LOG.warning('Floating ip %s not found while attempting to ' LOG.warning('Floating ip %s not found while attempting to '
'delete', fip_id) 'delete', fip_id)

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from tempest.lib import exceptions as lib_exc
from tempest.lib.services.network import base from tempest.lib.services.network import base
@ -66,3 +67,10 @@ class SecurityGroupsClient(base.BaseNetworkClient):
""" """
uri = '/security-groups' uri = '/security-groups'
return self.list_resources(uri, **filters) return self.list_resources(uri, **filters)
def is_resource_deleted(self, id):
try:
self.show_security_group(id)
except lib_exc.NotFound:
return True
return False

View File

@ -403,8 +403,9 @@ class BaseTestCase(testtools.testcase.WithAttributes,
""" """
if cls.validation_resources: if cls.validation_resources:
if hasattr(cls, "os_primary"): if hasattr(cls, "os_primary"):
vresources.clear_validation_resources(cls.os_primary, vresources.clear_validation_resources(
cls.validation_resources) cls.os_primary, cls.validation_resources,
use_neutron=CONF.service_available.neutron)
cls.validation_resources = {} cls.validation_resources = {}
else: else:
LOG.warning("Client manager not found, validation resources " LOG.warning("Client manager not found, validation resources "