Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_tests(self) -> bool:
try:
self._compose.up()
if self.system_vars:
debug('Use system variables: ' + str(list(self.system_vars.keys())))
variables = self.system_vars
else:
variables = {}
if self.inventory is not None:
inv_vars = read_source_file(self.inventory)
inv_vars['INVENTORY'] = get_filename(self.inventory)
inv_vars['INVENTORY_FILE'] = self.inventory
variables = try_get_object(fill_template_str(inv_vars, variables)) # fill env vars
variables['CURRENT_DIR'] = self.path
variables['RESOURCES_DIR'] = self.resources or self.path + '/resources'
test_files = get_files(self.tests_path)
results = []
for file in test_files:
self.all_includes = None # each test has it's own include tree
variables['TEST_NAME'] = file
test, result = self._run_test(file, variables)
results.append(result)
self._run_finally(test, file, result)
return all(results)
finally:
logger.log_storage.write_report(join(self.path, 'reports'))
logger.log_storage.print_summary(self.tests_path)
self._compose.down()
def _prepare_include_vars(test, global_variables):
if test.include:
include_vars = try_get_object(fill_template_str(test.include.variables, global_variables))
else:
include_vars = {}
override_keys = report_override(test.variables, include_vars)
if override_keys:
debug('Include variables override these variables: ' + str(override_keys))
return include_vars
def get_messages(consumer: SimpleConsumer, where: Operator or None, variables, timeout) -> dict or None:
try:
while True:
consumer.fetch()
for message in consumer:
value = try_get_object(message.value.decode('utf-8'))
debug(value)
if Kafka.check_message(where, value, variables):
return value
if timeout > 0:
sleep(1)
timeout -= 1
else:
return None
finally:
consumer.commit_offsets()
def __init__(self,
path: str,
system_environment=None,
inventory_vars: dict = None,
cmd_env: Optional[dict] = None,
resources: Optional[dict] = None) -> None:
system_vars = system_environment or {}
if system_vars:
debug('Use system variables: ' + str(list(system_vars.keys())))
self._variables = system_vars
else:
self._variables = {}
inventory = try_get_object(fill_template_str(inventory_vars, self._variables)) # fill env vars
self._cmd_env = cmd_env or {}
self._variables.update(inventory)
self._variables['CURRENT_DIR'] = path
self._variables['RESOURCES_DIR'] = resources or os.path.join(path, 'resources')
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, '' if tag is None else tag))
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))
def _compose_variables(self, body_vars, variables, override_vars):
tests_variables = try_get_object(fill_template_str(body_vars,
merge_two_dicts(variables, self.environment)))
variables = merge_two_dicts(variables, tests_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 variables
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