SafeOptionParser for Configuration Management
Change-Id: I1a32f116b851f4ba9fb445f7f02176c9ab6fa0e4 Signed-off-by: croy <Christian.Roy@windriver.com>
This commit is contained in:
@@ -2,6 +2,8 @@ from optparse import OptionParser
|
|||||||
|
|
||||||
from _pytest.main import Session
|
from _pytest.main import Session
|
||||||
|
|
||||||
|
from framework.options.safe_option_parser import SafeOptionParser
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationFileLocationsManager:
|
class ConfigurationFileLocationsManager:
|
||||||
"""
|
"""
|
||||||
@@ -30,8 +32,6 @@ class ConfigurationFileLocationsManager:
|
|||||||
Args:
|
Args:
|
||||||
session (Session): the pytest session
|
session (Session): the pytest session
|
||||||
|
|
||||||
Returns: None
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
lab_config_file = session.config.getoption("--lab_config_file")
|
lab_config_file = session.config.getoption("--lab_config_file")
|
||||||
if lab_config_file:
|
if lab_config_file:
|
||||||
@@ -97,7 +97,9 @@ class ConfigurationFileLocationsManager:
|
|||||||
Returns: None
|
Returns: None
|
||||||
|
|
||||||
"""
|
"""
|
||||||
options = self._add_options(parser)
|
safe_option_parser = SafeOptionParser(parser)
|
||||||
|
self.add_options(safe_option_parser)
|
||||||
|
options, args = parser.parse_args()
|
||||||
|
|
||||||
lab_config_file = options.lab_config_file
|
lab_config_file = options.lab_config_file
|
||||||
if lab_config_file:
|
if lab_config_file:
|
||||||
@@ -151,123 +153,28 @@ class ConfigurationFileLocationsManager:
|
|||||||
if openstack_config_file:
|
if openstack_config_file:
|
||||||
self.set_openstack_config_file(openstack_config_file)
|
self.set_openstack_config_file(openstack_config_file)
|
||||||
|
|
||||||
def _add_options(self, parser: OptionParser):
|
@staticmethod
|
||||||
|
def add_options(safe_parser: SafeOptionParser):
|
||||||
"""
|
"""
|
||||||
Adds the command line options we can expect.
|
This function will add the configuration file locations options to the safe_parser passed in.
|
||||||
|
|
||||||
Returns: None
|
Args:
|
||||||
|
safe_parser (SafeOptionParser): The SafeOptionParser
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not parser:
|
safe_parser.add_option("--lab_config_file", action="store", dest="lab_config_file", help="the lab file used for scanning")
|
||||||
parser = OptionParser()
|
safe_parser.add_option("--deployment_assets_config_file", action="store", dest="deployment_assets_config_file", help="The location of the files used to deploy the lab")
|
||||||
|
safe_parser.add_option("--k8s_config_file", action="store", dest="k8s_config_file", help="the k8s config file")
|
||||||
parser.add_option(
|
safe_parser.add_option("--ptp_config_file", action="store", dest="ptp_config_file", help="the PTP config file")
|
||||||
"--lab_config_file",
|
safe_parser.add_option("--logger_config_file", action="store", dest="logger_config_file", help="the logger config file")
|
||||||
action="store",
|
safe_parser.add_option("--docker_config_file", action="store", dest="docker_config_file", help="the docker config file")
|
||||||
type="str",
|
safe_parser.add_option("--web_config_file", action="store", dest="web_config_file", help="The Web config file")
|
||||||
dest="lab_config_file",
|
safe_parser.add_option("--database_config_file", action="store", dest="database_config_file", help="The database config file")
|
||||||
help="the lab file used for scanning",
|
safe_parser.add_option("--rest_api_config_file", action="store", dest="rest_api_config_file", help="The rest api config file")
|
||||||
)
|
safe_parser.add_option("--security_config_file", action="store", dest="security_config_file", help="The security config file")
|
||||||
|
safe_parser.add_option("--usm_config_file", action="store", dest="usm_config_file", help="The USM config file")
|
||||||
parser.add_option(
|
safe_parser.add_option("--app_config_file", action="store", dest="app_config_file", help="The app config file")
|
||||||
"--deployment_assets_config_file",
|
safe_parser.add_option("--openstack_config_file", action="store", dest="openstack_config_file", help="The openstack config file")
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="deployment_assets_config_file",
|
|
||||||
help="The location of the files used to deploy the lab",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--k8s_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="k8s_config_file",
|
|
||||||
help="the k8s config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--ptp_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="ptp_config_file",
|
|
||||||
help="the PTP config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--logger_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="logger_config_file",
|
|
||||||
help="the logger config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--docker_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="docker_config_file",
|
|
||||||
help="the docker config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--web_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="web_config_file",
|
|
||||||
help="The Web config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--database_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="database_config_file",
|
|
||||||
help="The database config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--rest_api_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="rest_api_config_file",
|
|
||||||
help="The rest api config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--security_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="security_config_file",
|
|
||||||
help="The security config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--usm_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="usm_config_file",
|
|
||||||
help="The USM config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--app_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="app_config_file",
|
|
||||||
help="The app config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"--openstack_config_file",
|
|
||||||
action="store",
|
|
||||||
type="str",
|
|
||||||
dest="openstack_config_file",
|
|
||||||
help="The openstack config file",
|
|
||||||
)
|
|
||||||
|
|
||||||
options, args = parser.parse_args()
|
|
||||||
|
|
||||||
return options
|
|
||||||
|
|
||||||
def set_lab_config_file(self, lab_config_file: str):
|
def set_lab_config_file(self, lab_config_file: str):
|
||||||
"""
|
"""
|
||||||
@@ -534,4 +441,4 @@ class ConfigurationFileLocationsManager:
|
|||||||
openstack_config_file (str): the app config file
|
openstack_config_file (str): the app config file
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.openstack_config_file = openstack_config_file
|
self.openstack_config_file = openstack_config_file
|
||||||
|
56
framework/options/safe_option_parser.py
Normal file
56
framework/options/safe_option_parser.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from optparse import OptionConflictError, OptionParser
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from pytest import Parser
|
||||||
|
|
||||||
|
|
||||||
|
class SafeOptionParser:
|
||||||
|
"""Class that abstracts out the differences in setting Pytest options and OptionParser options."""
|
||||||
|
|
||||||
|
def __init__(self, parser: Any = None):
|
||||||
|
"""Initialize SafeOptionParser with an optional OptionParser or Pytest Parser instance.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
parser (Any): An existing OptionParser or Parser instance to wrap. If None, a new OptionParser will be created. Defaults to None.
|
||||||
|
"""
|
||||||
|
if not parser:
|
||||||
|
parser = OptionParser()
|
||||||
|
|
||||||
|
options_parser = None
|
||||||
|
if isinstance(parser, OptionParser):
|
||||||
|
options_parser = parser
|
||||||
|
|
||||||
|
pytest_parser = None
|
||||||
|
if isinstance(parser, Parser):
|
||||||
|
pytest_parser = parser
|
||||||
|
|
||||||
|
# These implementations are mutually exclusive. Only one should be set at a time.
|
||||||
|
self.options_parser = options_parser
|
||||||
|
self.pytest_parser = pytest_parser
|
||||||
|
|
||||||
|
def add_option(self, *args: Dict, **kwargs: Dict):
|
||||||
|
"""Add an option to the underlying OptionParser.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
*args (Dict): Positional arguments to pass to OptionParser.add_option
|
||||||
|
**kwargs (Dict): Keyword arguments to pass to OptionParser.add_option
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if self.options_parser:
|
||||||
|
self.options_parser.add_option(*args, **kwargs)
|
||||||
|
else:
|
||||||
|
self.pytest_parser.addoption(*args, **kwargs)
|
||||||
|
except (OptionConflictError, ValueError):
|
||||||
|
pass # We know that sometimes options get added twice, but we only need them once.
|
||||||
|
|
||||||
|
def get_option_parser(self) -> Any:
|
||||||
|
"""Get the underlying OptionParser instance.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Any: The wrapped OptionParser instance.
|
||||||
|
"""
|
||||||
|
if self.options_parser:
|
||||||
|
return self.options_parser
|
||||||
|
else:
|
||||||
|
return self.pytest_parser
|
@@ -1,46 +1,36 @@
|
|||||||
import os
|
import os
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from pytest import Parser
|
||||||
|
|
||||||
from config.configuration_file_locations_manager import ConfigurationFileLocationsManager
|
from config.configuration_file_locations_manager import ConfigurationFileLocationsManager
|
||||||
from config.configuration_manager import ConfigurationManager
|
from config.configuration_manager import ConfigurationManager
|
||||||
from framework.logging import log_banners
|
from framework.logging import log_banners
|
||||||
from framework.logging.automation_logger import configure_testcase_log_handler, get_logger, remove_testcase_handler
|
from framework.logging.automation_logger import configure_testcase_log_handler, get_logger, remove_testcase_handler
|
||||||
|
from framework.options.safe_option_parser import SafeOptionParser
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser: Any):
|
def pytest_addoption(parser: Parser):
|
||||||
"""
|
"""
|
||||||
Adds the pytest options
|
Adds the pytest options
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
parser (Any): the parser
|
parser (Parser): the parser
|
||||||
|
|
||||||
"""
|
"""
|
||||||
parser.addoption("--lab_config_file", action="store")
|
safe_parser = SafeOptionParser(parser)
|
||||||
parser.addoption("--deployment_assets_config_file", action="store")
|
ConfigurationFileLocationsManager.add_options(safe_parser)
|
||||||
parser.addoption("--k8s_config_file", action="store")
|
|
||||||
parser.addoption("--ptp_config_file", action="store")
|
|
||||||
parser.addoption("--logger_config_file", action="store")
|
|
||||||
parser.addoption("--docker_config_file", action="store")
|
|
||||||
parser.addoption("--web_config_file", action="store")
|
|
||||||
parser.addoption("--database_config_file", action="store")
|
|
||||||
parser.addoption("--rest_api_config_file", action="store")
|
|
||||||
parser.addoption("--security_config_file", action="store")
|
|
||||||
parser.addoption("--usm_config_file", action="store")
|
|
||||||
parser.addoption("--app_config_file", action="store")
|
|
||||||
parser.addoption("--openstack_config_file", action="store")
|
|
||||||
|
|
||||||
|
|
||||||
def pytest_sessionstart(session: Any):
|
def pytest_sessionstart(session: Any):
|
||||||
"""
|
"""
|
||||||
This is run once at test start up.
|
This is run once at test start up.`
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session (Any): the session
|
session (Any): the session
|
||||||
"""
|
"""
|
||||||
configuration_locations_manager = ConfigurationFileLocationsManager()
|
configuration_locations_manager = ConfigurationFileLocationsManager()
|
||||||
|
|
||||||
configuration_locations_manager.set_configs_from_pytest_args(session)
|
configuration_locations_manager.set_configs_from_pytest_args(session)
|
||||||
|
|
||||||
ConfigurationManager.load_configs(configuration_locations_manager)
|
ConfigurationManager.load_configs(configuration_locations_manager)
|
||||||
log_configuration()
|
log_configuration()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user