test/automated-pytest-suite/testfixtures/fixture_resources.py
Yang Liu 33756ac899 Initial submission for starlingx pytest framework.
Include:
- util modules. such as table_parser, ssh/localhost clients, cli module,
exception, logger, etc. Util modules are mostly used by keywords.
- keywords modules. These are helper functions that are used directly by
test functions.
- platform (with platform or platform_sanity marker) and stx-openstack
(with sanity, sx_sanity, cpe_sanity, or storage_sanity marker) sanity
testcases
- pytest config conftest, and test fixture modules
- test config file template/example

Required packages:
- python3.4 or python3.5
- pytest >=3.10,<4.0
- pexpect
- requests
- pyyaml
- selenium (firefox, ffmpeg, pyvirtualdisplay, Xvfb or Xephyr or Xvnc)

Limitations:
- Anything that requires copying from Test File Server will not work until
a public share is configured to shared test files. Tests skipped for now.

Co-Authored-By: Maria Yousaf <maria.yousaf@windriver.com>
Co-Authored-By: Marvin Huang <marvin.huang@windriver.com>
Co-Authored-By: Yosief Gebremariam <yosief.gebremariam@windriver.com>
Co-Authored-By: Paul Warner <paul.warner@windriver.com>
Co-Authored-By: Xueguang Ma <Xueguang.Ma@windriver.com>
Co-Authored-By: Charles Chen <charles.chen@windriver.com>
Co-Authored-By: Daniel Graziano <Daniel.Graziano@windriver.com>
Co-Authored-By: Jordan Li <jordan.li@windriver.com>
Co-Authored-By: Nimalini Rasa <nimalini.rasa@windriver.com>
Co-Authored-By: Senthil Mukundakumar <senthil.mukundakumar@windriver.com>
Co-Authored-By: Anuejyan Manokeran <anujeyan.manokeran@windriver.com>
Co-Authored-By: Peng Peng <peng.peng@windriver.com>
Co-Authored-By: Chris Winnicki <chris.winnicki@windriver.com>
Co-Authored-By: Joe Vimar <Joe.Vimar@windriver.com>
Co-Authored-By: Alex Kozyrev <alex.kozyrev@windriver.com>
Co-Authored-By: Jack Ding <jack.ding@windriver.com>
Co-Authored-By: Ming Lei <ming.lei@windriver.com>
Co-Authored-By: Ankit Jain <ankit.jain@windriver.com>
Co-Authored-By: Eric Barrett <eric.barrett@windriver.com>
Co-Authored-By: William Jia <william.jia@windriver.com>
Co-Authored-By: Joseph Richard <Joseph.Richard@windriver.com>
Co-Authored-By: Aldo Mcfarlane <aldo.mcfarlane@windriver.com>

Story: 2005892
Task: 33750
Signed-off-by: Yang Liu <yang.liu@windriver.com>

Change-Id: I7a88a47e09733d39f024144530f5abb9aee8cad2
2019-07-15 15:30:00 -04:00

185 lines
5.7 KiB
Python
Executable File

#
# Copyright (c) 2019 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
from copy import deepcopy
VALID_SCOPES = ['function', 'class', 'module', 'session']
_RESOURCE_TYPES = ['vm', 'volume', 'volume_type', 'volume_qos',
'flavor', 'image', 'server_group', 'router',
'subnet', 'floating_ip', 'heat_stack', 'port',
'trunk', 'network', 'security_group_rule'
'security_group', 'network_qos', 'vol_snapshot', 'aggregate',
'port_pair', 'port_pair_group', 'flow_classifier',
'port_chain', 'datanetwork', 'providernet']
_RESOURCE_DICT = {key: [] for key in _RESOURCE_TYPES+['vm_with_vol']}
def _check_values(value, val_type='scope', valid_vals=None):
value = value.lower()
if not valid_vals:
valid_vals = VALID_SCOPES
if value not in valid_vals:
raise ValueError("'{}' param value has to be one of the: "
"{}".format(val_type, valid_vals))
class ResourceCleanup:
"""
Class to hold the cleanup list and related functions.
"""
__resources_to_cleanup = \
{key_: deepcopy(_RESOURCE_DICT) for key_ in VALID_SCOPES}
@classmethod
def _get_resources(cls, scope):
return cls.__resources_to_cleanup[scope]
@classmethod
def _reset(cls, scope):
for key in cls.__resources_to_cleanup[scope]:
cls.__resources_to_cleanup[scope][key] = []
@classmethod
def add(cls, resource_type, resource_id, scope='function',
del_vm_vols=True):
"""
Add resource to cleanup list.
Args:
resource_type (str): one of these: 'vm', 'volume', 'flavor
resource_id (str|list): id(s) of the resource to add to cleanup list
scope (str): when the cleanup should be done. Valid value is one of
these: 'function', 'class', 'module'
del_vm_vols (bool): whether to delete attached volume(s) if given
resource is vm.
"""
_check_values(scope)
_check_values(resource_type, val_type='resource_type',
valid_vals=_RESOURCE_TYPES)
if resource_type == 'vm' and del_vm_vols:
resource_type = 'vm_with_vol'
if not isinstance(resource_id, (list, tuple)):
resource_id = [resource_id]
for res_id in resource_id:
cls.__resources_to_cleanup[scope][resource_type].append(res_id)
@classmethod
def remove(cls, resource_type, resource_id, scope='function',
del_vm_vols=True):
"""
Add resource to cleanup list.
Args:
resource_type (str): one of these: 'vm', 'volume', 'flavor
resource_id (str|list): id(s) of the resource to add to cleanup list
scope (str): when the cleanup should be done. Valid value is one of
these: 'function', 'class', 'module'
del_vm_vols (bool): whether to delete attached volume(s) if given
resource is vm.
"""
if scope is None:
return
_check_values(scope)
_check_values(resource_type, val_type='resource_type',
valid_vals=_RESOURCE_TYPES)
if resource_type == 'vm' and del_vm_vols:
resource_type = 'vm_with_vol'
if not isinstance(resource_id, (list, tuple)):
resource_id = [resource_id]
existing_list = cls.__resources_to_cleanup[scope][resource_type]
for res_id in resource_id:
if res_id in existing_list:
existing_list.remove(res_id)
class VlmHostsReserved:
__hosts_reserved_dict = {key: [] for key in VALID_SCOPES}
@classmethod
def _reset(cls, scope):
cls.__hosts_reserved_dict[scope] = []
@classmethod
def _get_hosts_reserved(cls, scope):
return list(cls.__hosts_reserved_dict[scope])
@classmethod
def add(cls, hosts, scope='session'):
"""
Add resource to cleanup list.
Args:
hosts (str|list): hostname(s)
scope (str): one of these: 'function', 'class', 'module', 'session'
"""
_check_values(scope)
if not isinstance(hosts, (list, tuple)):
hosts = [hosts]
for host in hosts:
cls.__hosts_reserved_dict[scope].append(host)
class GuestLogs:
__guests_to_collect = {key: [] for key in VALID_SCOPES}
@classmethod
def _reset(cls, scope):
cls.__guests_to_collect[scope] = []
@classmethod
def remove(cls, vm_id):
"""
Remove a guest from collect log list. Call this if test passed.
Args:
vm_id (str): vm to remove from collection list
"""
for scope in VALID_SCOPES:
try:
cls.__guests_to_collect[scope].remove(vm_id)
except ValueError:
continue
@classmethod
def _get_guests(cls, scope):
return list(cls.__guests_to_collect[scope])
@classmethod
def add(cls, vm_id, scope='function'):
"""
Add a guest to collect log list. Applicable to guest heartbeat,
server group, vm scaling test cases.
- Use fixture_resources.GuestLogs.add() to add a guest to collect
list
- Use fixture_resources.GuestLogs.remove() to remove a guest
from collect list if test passed
Args:
vm_id (str): vm to add to collection list
scope (str): one of these: 'function', 'class', 'module', 'session'
"""
_check_values(scope)
if vm_id not in cls.__guests_to_collect[scope]:
cls.__guests_to_collect[scope].append(vm_id)