def action(self, includes: dict, variables: dict) -> dict: filled_vars = dict([(k, fill_template_str(v, variables)) for (k, v) in self.variables.items()]) out = fill_template_str(self.include, variables) test, tag = get_tag(out) if test not in includes: error('No include registered for name ' + test) raise Exception('No include registered for name ' + test) include = includes[test] variables = merge_two_dicts(include.variables, merge_two_dicts(variables, filled_vars)) include.variables = try_get_object(fill_template_str(variables, variables)) try: info('Running {}'.format(test)) logger.log_storage.nested_test_in() variables = include.run(tag=tag, raise_stop=True) logger.log_storage.nested_test_out() except SkipException: logger.log_storage.nested_test_out() debug('Include ignored') return variables except StopException as e: logger.log_storage.nested_test_out() raise e except Exception as e: logger.log_storage.nested_test_out() if not self.ignore_errors: raise Exception('Step run ' + test + ' failed: ' + str(e)) return variables
def prepare_test(self, file: str, variables: dict, override_vars: None or dict = None) -> Test: body = read_source_file(file) registered_includes = self.process_includes(body.get('include', []), variables) variables = merge_two_dicts(variables, body.get( 'variables', {})) # override variables with test's variables if override_vars: # TODO warn when overriding inventory vars? variables = merge_two_dicts(variables, override_vars) return Test(self.path, registered_includes, variables, body.get('config', {}), body.get('steps', []), self.modules, self.environment)
def run(self, tag=None, raise_stop=False) -> dict: for step in self.steps: [action] = step.keys() ignore_errors = get_or_default('ignore_errors', step[action], False) if tag is not None: step_tag = get_or_default('tag', step[action], None) if step_tag != tag: continue actions = step_factory.get_actions(self.path, step, self.modules) for action_object in actions: # override all variables with cmd variables variables = merge_two_dicts(self.variables, self.override_vars) action_name = get_action_name(action, action_object, variables) try: self.variables = action_object.action( self.includes, variables) info('Step ' + action_name + ' OK') except StopException as e: if raise_stop: raise e debug('Skip ' + action_name + ' due to ' + str(e)) info('Step ' + action_name + ' OK') return self.variables # stop current test except Exception as e: if ignore_errors: debug('Step ' + action_name + ' failed, but we ignore it') continue info('Step ' + action_name + ' failed: ' + str(e)) raise e return self.variables
def action(self, includes: dict, variables: dict) -> dict: filled_vars = dict([(k, fill_template_str(v, variables)) for (k, v) in self.variables.items()]) out = fill_template_str(self.include, variables) test, tag = get_tag(out, self.tag) if test not in includes: error('No include registered for name ' + test) raise Exception('No include registered for name ' + test) include = includes[test] variables = merge_two_dicts(include.variables, merge_two_dicts(variables, filled_vars)) include.variables = variables try: variables = include.run(tag=tag, raise_stop=True) except StopException as e: raise e except Exception as e: if not self.ignore_errors: raise Exception('Step run ' + test + ' failed: ' + str(e)) return variables
def prepare_test(self, file: str, variables: dict, override_vars: None or dict = None) -> Test: body = read_source_file(file) registered_includes = self.process_includes(body.get('include', []), variables) variables = merge_two_dicts(variables, body.get( 'variables', {})) # override variables with test's variables if override_vars: override_keys = report_override(variables, override_vars) if override_keys: debug('Overriding these variables: ' + str(override_keys)) variables = merge_two_dicts(variables, override_vars) return Test( self.path, registered_includes, deepcopy(variables), # each test has independent variables body.get('config', {}), body.get('steps', []), self.modules, self.environment)
def process_register(self, variables, output: dict or list or str or None = None) -> dict: if self.register is not None: for key in self.register.keys(): if output is not None: out = fill_template( self.register[key], merge_two_dicts(variables, {'OUTPUT': try_get_object(output)})) else: out = fill_template(self.register[key], variables) variables[key] = out return variables
def run(self, tag=None, raise_stop=False) -> dict: for step in self.steps: [action] = step.keys() ignore_errors = get_or_default('ignore_errors', step[action], False) if tag is not None: step_tag = get_or_default('tag', step[action], None) if step_tag != tag: continue actions = step_factory.get_actions(self.path, step, self.modules) for action_object in actions: # override all variables with cmd variables variables = merge_two_dicts(self.variables, self.override_vars) action_name = get_action_name(action, action_object, variables) try: logger.log_storage.new_step(step, variables) self.variables = action_object.action( self.includes, variables) info('Step ' + action_name + ' OK') logger.log_storage.step_end(step, self.variables) except StopException as e: # stop a test without error if raise_stop: # or raise error if configured logger.log_storage.step_end(step, variables, success=False, output=str(e)) raise e debug('Skip ' + action_name + ' due to ' + str(e)) info('Step ' + action_name + ' OK') logger.log_storage.step_end(step, self.variables, success=True, output=str(e)) return self.variables # stop current test except Exception as e: if ignore_errors: debug('Step ' + action_name + ' failed, but we ignore it') logger.log_storage.step_end(step, variables, success=True) continue info('Step ' + action_name + ' failed: ' + str(e)) debug(traceback.format_exc()) logger.log_storage.step_end(step, variables, success=False, output=str(e)) raise e return self.variables
def action(self, includes: dict, variables: dict) -> dict or tuple: input_data = self.simple_input(variables) variables_override = misc.merge_two_dicts( variables, input_data['populate'].get('variables')) db_modules = module_utils.list_modules_in_package( catcher_modules.database) for service, data in input_data['populate'].items(): if service == 'variables': continue if service in db_modules: # database found = module_utils.find_class_in_module( 'catcher_modules.database.' + service, service) found(**{service: data}).populate(variables_override, **data) # TODO cache # TODO s3 # TODO http mock return variables
def __init__(self, path: str, tests_path: str, inventory: str or None, modules=None, environment=None, resources=None) -> None: if modules is None: modules = [] self.environment = environment if environment is not None else {} self.tests_path = tests_path self.path = path self.inventory = inventory self.all_includes = [] self.modules = merge_two_dicts( prepare_modules(modules, step.registered_steps), step.registered_steps) self._compose = DockerCompose(resources)
def prepare_variables(self, test, global_variables: dict): """ Create variables for test :param global_variables: :param test: test with test variables """ # system env + inventory # (test template is filled in based on system, inventory & cmd vars) test.variables = try_get_object( fill_template_str(test.variables, merge_two_dicts(global_variables, self._cmd_env))) # test local global_variables.update(test.variables) # include vars override test local global_variables.update( self._prepare_include_vars(test, global_variables)) # cmd_env override everything global_variables.update(self._cmd_env) test.variables = global_variables
def __init__(self, path: str, tests_path: str, inventory: str or None, modules=None, environment=None, system_environment=None, resources=None, output_format=None) -> None: if modules is None: modules = [] self.environment = environment if environment is not None else {} self.system_vars = system_environment if system_environment is not None else {} self.tests_path = tests_path self.path = path self.inventory = inventory self.all_includes = [] self.modules = merge_two_dicts( prepare_modules(modules, step.registered_steps), step.registered_steps) self._compose = DockerCompose(resources) self.resources = resources if output_format: logger.log_storage = LogStorage(output_format)
def __init__(self, modules: Optional[List[str]] = None) -> None: if modules is None: modules = [] self.modules = merge_two_dicts( prepare_modules(modules, step.registered_steps), step.registered_steps)