class TestsDispatchAction(Action): ACTION = "tests" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self._logger = LoggingHelper(__name__) self.tests_dump_action = None self.tests_list_action = None self.tests_show_action = None self.tests_generate_configuration = None Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.tests_list_action = TestsListAction(parsers) self.tests_show_action = TestsShowAction(parsers) self.tests_generate_configuration = TestsGenerateConfiguration(parsers) self.tests_dump_action = TestsDumpAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") { TestsListAction.ACTION: self.tests_list_action, TestsShowAction.ACTION: self.tests_show_action, TestsGenerateConfiguration.ACTION: self.tests_generate_configuration, TestsDumpAction.ACTION: self.tests_dump_action }[configuration.TESTS_CMD].process_action(configuration)
class StatusDispatchAction(Action): ACTION = "status" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self._logger = LoggingHelper(__name__) Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.status_list_action = StatusListAction(parsers) self.status_show_action = StatusShowAction(parsers) self.status_delete_action = StatusDeleteAction(parsers) self.status_dump_action = StatusDumpAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") { StatusListAction.ACTION: self.status_list_action, StatusShowAction.ACTION: self.status_show_action, StatusDeleteAction.ACTION: self.status_delete_action, StatusDumpAction.ACTION: self.status_dump_action }[configuration.STATUS_TYPE].process_action(configuration)
class TestsDispatchAction(Action): ACTION = "tests" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self._logger = LoggingHelper(__name__) self.tests_dump_action = None self.tests_list_action = None self.tests_show_action = None self.tests_generate_configuration = None Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.tests_list_action = TestsListAction(parsers) self.tests_show_action = TestsShowAction(parsers) self.tests_generate_configuration = TestsGenerateConfiguration(parsers) self.tests_dump_action = TestsDumpAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") {TestsListAction.ACTION: self.tests_list_action, TestsShowAction.ACTION: self.tests_show_action, TestsGenerateConfiguration.ACTION: self.tests_generate_configuration, TestsDumpAction.ACTION: self.tests_dump_action}[ configuration.TESTS_CMD].process_action(configuration)
def initialize_db_connection_handler(): logger = LoggingHelper(__name__) logger.log_debug("initializing database") try: DatabaseConnectionHandler._connection_string = ConfigurationFileReader().read_file()[GENERAL][ SQL_CONNECTION_STRING] DatabaseConnectionHandler.engine = create_engine(DatabaseConnectionHandler._connection_string, echo=False) Base.metadata.create_all(DatabaseConnectionHandler.engine) logger.log_debug("database initialization success") except KeyError: message = "Unable to read %s flag from section %s" % (SQL_CONNECTION_STRING, GENERAL) print message logger.log_critical(message) exit(1)
def initialize_db_connection_handler(): logger = LoggingHelper(__name__) logger.log_debug("initializing database") try: DatabaseConnectionHandler._connection_string = ConfigurationFileReader( ).read_file()[GENERAL][SQL_CONNECTION_STRING] DatabaseConnectionHandler.engine = create_engine( DatabaseConnectionHandler._connection_string, echo=False) Base.metadata.create_all(DatabaseConnectionHandler.engine) logger.log_debug("database initialization success") except KeyError: message = "Unable to read %s flag from section %s" % ( SQL_CONNECTION_STRING, GENERAL) print message logger.log_critical(message) exit(1)
class MetadataDispatchAction(Action): ACTION = "metadata" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self.metadata_diff_action = None self._logger = LoggingHelper(__name__) Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.metadata_diff_action = MetadataDiffAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") {MetadataDiffAction.ACTION: self.metadata_diff_action}[configuration.EXECUTE_TYPE]\ .process_action(configuration)
class ScenarioDispatchAction(Action): ACTION = "scenario" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self.scenario_list_action = None self.scenario_run_action = None self._logger = LoggingHelper(__name__) Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.scenario_list_action = ScenarioListAction(parsers) self.scenario_run_action = ScenarioAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") {ScenarioListAction.ACTION: self.scenario_list_action, ScenarioAction.ACTION: self.scenario_run_action}[configuration.EXECUTE_TYPE].process_action(configuration)
class MetadataDispatchAction(Action): ACTION = "metadata" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self.metadata_diff_action, self.metadata_report_action = None, None self._logger = LoggingHelper(__name__) Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.metadata_diff_action = MetadataDiffAction(parsers) self.metadata_report_action = MetadataReportAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") {MetadataDiffAction.ACTION: self.metadata_diff_action, MetadataReportAction.ACTION: self.metadata_report_action}[configuration.EXECUTE_TYPE]\ .process_action(configuration)
class StatusDispatchAction(Action): ACTION = "status" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self._logger = LoggingHelper(__name__) Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.status_list_action = StatusListAction(parsers) self.status_show_action = StatusShowAction(parsers) self.status_delete_action = StatusDeleteAction(parsers) self.status_dump_action = StatusDumpAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") {StatusListAction.ACTION: self.status_list_action, StatusShowAction.ACTION: self.status_show_action, StatusDeleteAction.ACTION: self.status_delete_action, StatusDumpAction.ACTION: self.status_dump_action}[ configuration.STATUS_TYPE].process_action(configuration)
class ExecuteDispatchAction(Action): ACTION = "execute" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self.execute_running_list_action = None self.execute_test_scripts_action = None self._logger = LoggingHelper(__name__) Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.execute_running_list_action = ExecuteRunningListAction(parsers) self.execute_test_scripts_action = ExecuteTestScriptsAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") {ExecuteRunningListAction.ACTION: self.execute_running_list_action, ExecuteTestScriptsAction.ACTION: self.execute_test_scripts_action}[configuration.EXECUTE_TYPE]\ .process_action(configuration)
class ExecuteDispatchAction(Action): ACTION = "execute" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self.execute_running_list_action = None self.execute_test_scripts_action = None self._logger = LoggingHelper(__name__) Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.execute_running_list_action = ExecuteRunningListAction(parsers) self.execute_test_scripts_action = ExecuteTestScriptsAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") {ExecuteRunningListAction.ACTION: self.execute_running_list_action, ExecuteTestScriptsAction.ACTION: self.execute_test_scripts_action}[configuration.EXECUTE_TYPE]\ .process_action(configuration)
class ScenarioDispatchAction(Action): ACTION = "scenario" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): self.scenario_list_action = None self.scenario_run_action = None self._logger = LoggingHelper(__name__) Action.__init__(self, *params, **kwargs) def fill_parser_arguments(self): parsers = self.parser.add_subparsers() self.scenario_list_action = ScenarioListAction(parsers) self.scenario_run_action = ScenarioAction(parsers) def process_action(self, configuration): self._logger.log_debug("Processing user request") { ScenarioListAction.ACTION: self.scenario_list_action, ScenarioAction.ACTION: self.scenario_run_action }[configuration.EXECUTE_TYPE].process_action(configuration)
class ExecuteTestScriptsAction(Action): ACTION = "tests" PARAM_NAME = "EXECUTE_TYPE" def __init__(self, *params, **kwargs): Action.__init__(self, *params, **kwargs) self._logger = LoggingHelper(__name__) def fill_parser_arguments(self): self.parser.add_argument("package", help="Test package to execute", type=str, nargs=1) self.parser.add_argument("-s", "--test_suites", type=str, nargs="+", help="Test suites to execute") self.parser.add_argument("-t", "--test_scripts", type=str, nargs="+", help="Test scripts to execute") self.parser.add_argument("-c", "--config_files", help="Configuration files for test scripts", required=True, nargs="+") self.parser.add_argument("-T", "--timeout", help="Timeout for scripts", type=int, nargs=1) self.parser.add_argument("-g", "--debug", default=False, action="store_true", help="Enables debugging mode which will stop tests execution after first failed test") self.parser.add_argument("-x", "--return_tests_status_as_exit_code", action="store_true", default=False, help="When flag set CTS framework will return calculate exit code basing on tests results.") # self.parser.add_argument("-m", "--metadata", help="Choose metadata", choices=['RSD', 'redfish'], default='RSD') def process_action(self, configuration): print "Using CTS in version %s" % (ColorPrinter.format_text(BuildInformation.BUILD_VERSION, bold=True)) tests_manager = TestsManager() configuration_files = configuration.config_files test_scripts_configuration = get_configuration_from_files(configuration_files) if test_scripts_configuration is None: sys.exit(-1) if not certificates_files_are_valid(test_scripts_configuration): sys.exit(-1) test_plan = TestsPackagesContainer() if configuration.test_suites or configuration.test_scripts: if configuration.test_suites: test_suites_found = tests_manager.get_packages() test_suites_found.filter(packages=configuration.package, suites=configuration.test_suites, remove_empty=True) test_plan += test_suites_found if configuration.test_scripts: test_scripts_found = tests_manager.get_packages() test_scripts_found.filter(packages=configuration.package, scripts=configuration.test_scripts, remove_empty=True) test_plan += test_scripts_found else: test_plan += tests_manager.get_packages() test_plan.filter(packages=configuration.package) if not test_plan.packages: print "No script selected to execute" exit(64) # EX_USAGE = 64 command line usage error message = "Executing " print "Executing:" for package in test_plan.packages: for suite in package.suites: for script in suite.scripts: print "\t* %s from suite %s from package %s" % (script.name, suite.name, package.name) message += "%s from suite %s from package %s, " % (script.name, suite.name, package.name) self._logger.log_debug(message) if configuration.timeout is not None: message = "setting timeout execution to %s" % configuration.timeout[0] self._logger.log_debug(message) print "\n%s" % message test_plan.set_scripts_timeout(configuration.timeout[0]) execution_feed = ExecutionFeed(test_plan, test_scripts_configuration) ExecuteTestScriptsAction.execute_configuration_group(execution_feed, test_status_as_exit_code=configuration.return_tests_status_as_exit_code) @staticmethod def execute_configuration_group(configuration_group, test_status_as_exit_code=None): """ :type configuration_group: cts_framework.tests_running.execution_feed.ExecutionFeed :type test_status_as_exit_code: bool """ test_run = TestRun() test_run.add_execution_feed(configuration_group) test_run.register_all() execution_status = test_run.run_all() if not execution_status and test_status_as_exit_code: print "Tests finished with status failed, exit code set to 4" exit(4)
class ReplayTestRunAction(Action): ACTION = "replay" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): Action.__init__(self, *params, **kwargs) self._logger = LoggingHelper(__name__) def fill_parser_arguments(self): self.parser.add_argument("replay_id", help="ID of the test script run to replay", type=str, nargs=1) def process_action(self, configuration): replay_id = configuration.replay_id[0] print "Using CTS in version %s to replay execution %s" \ % (ColorPrinter.format_text(BuildInformation.BUILD_VERSION, bold=True), replay_id) error, script_execution_id = split_replay_id(replay_id) if error: return # TODO: warn user when he tries to replay using newer CTS script_execution = ScriptDAO.get_script_execution_details( script_execution_id) if script_execution is None: cts_error( "Recording for script execution id={id:ignore} not found", id=script_execution_id) return script_path = script_execution.script_path configuration = self._configuration_from_string( script_execution.configuration) test_plan = self._prepare_test_plan(script_path) environ[ReplayController.CTS_REPLAY_SCRIPT_EXECUTION_ID] = str( script_execution_id) self._execute(configuration, test_plan) def _configuration_from_string(self, configuration_str): configuration = { b[0]: b[1] for b in (a.strip().split(' ', 1) for a in filter(None, configuration_str.split('--'))) } return configuration def _prepare_test_plan(self, script_path): test_plan = TestsPackagesContainer() tests_manager = TestsManager() test_scripts_found = tests_manager.get_packages() test_scripts_found.filter(script_paths=[script_path], remove_empty=True) test_plan += test_scripts_found if not test_plan.packages: print "Script to execute not found in any package" sys.exit(0) return test_plan def _execute(self, configuration, test_plan): """ :type configuration: dict :type test_plan: cts_framework.tests_managing.test_package.tests_packages_container.TestsPackagesContainer """ message = "Executing " print "Executing:" for package in test_plan.packages: for suite in package.suites: for script in suite.scripts: print "\t* %s from suite %s from package %s" % ( script.name, suite.name, package.name) message += "%s from suite %s from package %s, " % ( script.name, suite.name, package.name) self._logger.log_debug(message) execution_feed = ExecutionFeed(test_plan, configuration) ExecuteTestScriptsAction.execute_configuration_group(execution_feed)
class ScenarioAction(Action): ACTION = "scenario" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): Action.__init__(self, *params, **kwargs) self._logger = LoggingHelper(__name__) def fill_parser_arguments(self): self.parser.add_argument("-u", "--usecase", help="Use case to execute", type=str, nargs=1) self.parser.add_argument("-c", "--config_files", help="Configuration files for test scripts", required=True, nargs="+") self.parser.add_argument("-t", "--test_scripts", type=str, nargs="+", help="Test scripts to execute", default="validate_use_case") self.parser.add_argument("-T", "--timeout", help="Timeout for scripts", type=int, nargs=1) self.parser.add_argument( "-g", "--debug", default=False, action="store_true", help= "Enables debugging mode which will stop tests execution after first failed test" ) self.parser.add_argument( "-x", "--return_tests_status_as_exit_code", action="store_true", default=False, help= "When flag set CTS framework will return calculate exit code basing on tests results." ) def process_action(self, configuration): print "Using CTS in version %s" % (ColorPrinter.format_text( BuildInformation.BUILD_VERSION, bold=True)) tests_manager = TestsManager() configuration_files = configuration.config_files test_scripts_configuration = get_configuration_from_files( configuration_files) if not configuration.usecase: print('\nMissing --usecase argument\n') JsonLoader().print_available_actions() sys.exit(64) # EX_USAGE = 64 command line usage error test_scripts_configuration['UseCase'] = configuration.usecase[0] if test_scripts_configuration is None: sys.exit(-1) if not certificates_files_are_valid(test_scripts_configuration): sys.exit(-1) test_plan = TestsPackagesContainer() if configuration.test_scripts: test_scripts_found = tests_manager.get_packages() test_scripts_found.filter(scripts=configuration.test_scripts, remove_empty=True) test_plan += test_scripts_found else: test_plan += tests_manager.get_packages() if not test_plan.packages: print('No script selected to execute') exit(64) # EX_USAGE = 64 command line usage error message = "Executing " print('Executing:') for package in test_plan.packages: for suite in package.suites: for script in suite.scripts: print "\t* %s from suite %s from package %s" % ( script.name, suite.name, package.name) message += "%s from suite %s from package %s, " % ( script.name, suite.name, package.name) self._logger.log_debug(message) if configuration.timeout is not None: message = "setting timeout execution to %s" % configuration.timeout[ 0] self._logger.log_debug(message) print "\n%s" % message test_plan.set_scripts_timeout(configuration.timeout[0]) execution_feed = ExecutionFeed(test_plan, test_scripts_configuration) for case in configuration.usecase: if not self.check_use_case_exist(case): print('\nUse Case was not found: %s ' % case) exit(64) # EX_USAGE = 64 command line usage error ScenarioAction.execute_configuration_group( execution_feed, test_status_as_exit_code=configuration. return_tests_status_as_exit_code)
class ExecuteTestScriptsAction(Action): ACTION = "tests" PARAM_NAME = "EXECUTE_TYPE" def __init__(self, *params, **kwargs): Action.__init__(self, *params, **kwargs) self._logger = LoggingHelper(__name__) def fill_parser_arguments(self): self.parser.add_argument("package", help="Test package to execute", type=str, nargs=1) self.parser.add_argument("-s", "--test_suites", type=str, nargs="+", help="Test suites to execute") self.parser.add_argument("-t", "--test_scripts", type=str, nargs="+", help="Test scripts to execute") self.parser.add_argument("-c", "--config_files", help="Configuration files for test scripts", required=True, nargs="+") self.parser.add_argument("-T", "--timeout", help="Timeout for scripts", type=int, nargs=1) self.parser.add_argument( "-g", "--debug", default=False, action="store_true", help= "Enables debugging mode which will stop tests execution after first failed test" ) self.parser.add_argument( "-x", "--return_tests_status_as_exit_code", action="store_true", default=False, help= "When flag set CTS framework will return calculate exit code basing on tests results." ) # self.parser.add_argument("-m", "--metadata", help="Choose metadata", choices=['RSD', 'redfish'], default='RSD') def process_action(self, configuration): print "Using CTS in version %s" % (ColorPrinter.format_text( BuildInformation.BUILD_VERSION, bold=True)) tests_manager = TestsManager() configuration_files = configuration.config_files test_scripts_configuration = get_configuration_from_files( configuration_files) if test_scripts_configuration is None: sys.exit(-1) if not certificates_files_are_valid(test_scripts_configuration): sys.exit(-1) test_plan = TestsPackagesContainer() if configuration.test_suites or configuration.test_scripts: if configuration.test_suites: test_suites_found = tests_manager.get_packages() test_suites_found.filter(packages=configuration.package, suites=configuration.test_suites, remove_empty=True) test_plan += test_suites_found if configuration.test_scripts: test_scripts_found = tests_manager.get_packages() test_scripts_found.filter(packages=configuration.package, scripts=configuration.test_scripts, remove_empty=True) test_plan += test_scripts_found else: test_plan += tests_manager.get_packages() test_plan.filter(packages=configuration.package) if not test_plan.packages: print "No script selected to execute" exit(64) # EX_USAGE = 64 command line usage error message = "Executing " print "Executing:" for package in test_plan.packages: for suite in package.suites: for script in suite.scripts: print "\t* %s from suite %s from package %s" % ( script.name, suite.name, package.name) message += "%s from suite %s from package %s, " % ( script.name, suite.name, package.name) self._logger.log_debug(message) if configuration.timeout is not None: message = "setting timeout execution to %s" % configuration.timeout[ 0] self._logger.log_debug(message) print "\n%s" % message test_plan.set_scripts_timeout(configuration.timeout[0]) execution_feed = ExecutionFeed(test_plan, test_scripts_configuration) ExecuteTestScriptsAction.execute_configuration_group( execution_feed, test_status_as_exit_code=configuration. return_tests_status_as_exit_code) @staticmethod def execute_configuration_group(configuration_group, test_status_as_exit_code=None): """ :type configuration_group: cts_framework.tests_running.execution_feed.ExecutionFeed :type test_status_as_exit_code: bool """ test_run = TestRun() test_run.add_execution_feed(configuration_group) test_run.register_all() execution_status = test_run.run_all() if not execution_status and test_status_as_exit_code: print "Tests finished with status failed, exit code set to 4" exit(4)
class ScenarioAction(Action): ACTION = "scenario" PARAM_NAME = "ACTION" def __init__(self, *params, **kwargs): Action.__init__(self, *params, **kwargs) self._logger = LoggingHelper(__name__) def fill_parser_arguments(self): self.parser.add_argument("-u", "--usecase", help="Use case to execute", type=str, nargs=1) self.parser.add_argument("-c", "--config_files", help="Configuration files for test scripts", required=True, nargs="+") self.parser.add_argument("-t", "--test_scripts", type=str, nargs="+", help="Test scripts to execute", default="validate_use_case") self.parser.add_argument("-T", "--timeout", help="Timeout for scripts", type=int, nargs=1) self.parser.add_argument("-g", "--debug", default=False, action="store_true", help="Enables debugging mode which will stop tests execution after first failed test") self.parser.add_argument("-x", "--return_tests_status_as_exit_code", action="store_true", default=False, help="When flag set CTS framework will return calculate exit code basing on tests results.") def process_action(self, configuration): print "Using CTS in version %s" % (ColorPrinter.format_text(BuildInformation.BUILD_VERSION, bold=True)) tests_manager = TestsManager() configuration_files = configuration.config_files test_scripts_configuration = get_configuration_from_files(configuration_files) if not configuration.usecase: print('\nMissing --usecase argument\n') JsonLoader().print_available_actions() sys.exit(64) # EX_USAGE = 64 command line usage error test_scripts_configuration['UseCase'] = configuration.usecase[0] if test_scripts_configuration is None: sys.exit(-1) if not certificates_files_are_valid(test_scripts_configuration): sys.exit(-1) test_plan = TestsPackagesContainer() if configuration.test_scripts: test_scripts_found = tests_manager.get_packages() test_scripts_found.filter(scripts=configuration.test_scripts, remove_empty=True) test_plan += test_scripts_found else: test_plan += tests_manager.get_packages() if not test_plan.packages: print('No script selected to execute') exit(64) # EX_USAGE = 64 command line usage error message = "Executing " print('Executing:') for package in test_plan.packages: for suite in package.suites: for script in suite.scripts: print "\t* %s from suite %s from package %s" % (script.name, suite.name, package.name) message += "%s from suite %s from package %s, " % (script.name, suite.name, package.name) self._logger.log_debug(message) if configuration.timeout is not None: message = "setting timeout execution to %s" % configuration.timeout[0] self._logger.log_debug(message) print "\n%s" % message test_plan.set_scripts_timeout(configuration.timeout[0]) execution_feed = ExecutionFeed(test_plan, test_scripts_configuration) for case in configuration.usecase: if not self.check_use_case_exist(case): print('\nUse Case was not found: %s ' % case) exit(64) # EX_USAGE = 64 command line usage error ScenarioAction.execute_configuration_group(execution_feed, test_status_as_exit_code=configuration.return_tests_status_as_exit_code) @staticmethod def execute_configuration_group(configuration_group, test_status_as_exit_code=None): """ :type configuration_group: cts_framework.tests_running.execution_feed.ExecutionFeed :type test_status_as_exit_code: bool """ test_run = TestRun() test_run.add_execution_feed(configuration_group) test_run.register_all() execution_status = test_run.run_all() if not execution_status and test_status_as_exit_code: print "Tests finished with status failed, exit code set to 4" exit(4) @staticmethod def check_use_case_exist(sought_case): """ :param sought_case: use case name :return: bool """ use_cases, _ = JsonLoader().retrieve_available_actions() for case in use_cases: case_name = case['name'] if case_name == sought_case: return True elif case_name.lower() == sought_case.lower(): print('\nDo you mean use case: %s? (%s was not found)' % (case_name, sought_case)) exit(64) return False