Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def prepare_test(self, file: str, variables: dict, override_vars: None or dict = None) -> Runnable:
body = read_source_file(file)
registered_includes = self.process_includes(file, body.get('include', []), variables)
variables = self._compose_variables(body.get('variables', {}), variables, override_vars)
ignore = body.get('ignore', False)
if ignore:
if not isinstance(ignore, bool):
ignore = Operator.find_operator(ignore).operation(variables)
if ignore:
return IgnoredTest(path=self.path, variables=deepcopy(variables))
return Test(self.path,
includes=registered_includes,
variables=deepcopy(variables), # each test has independent variables
config=body.get('config', {}),
steps=body.get('steps', []),
final=body.get('finally', []),
modules=self.modules,
override_vars=self.environment)
def action(self, includes: dict, variables: dict) -> dict:
output = variables
if self.type == 'while':
operator = Operator.find_operator(self.if_clause)
while operator.operation(output):
output = self.__run_actions(includes, output)
if self.max_cycle is not None:
if self.max_cycle == 0:
break
self.max_cycle = self.max_cycle - 1
return output
elif self.type == 'foreach':
loop_var = try_get_objects(fill_template_str(json.dumps(self.in_var), variables))
if not isinstance(loop_var, collections.Iterable):
raise ValueError(str(loop_var) + ' is not iterable')
for entry in loop_var:
output['ITEM'] = entry
output = self.__run_actions(includes, output)
return output
def action(self, includes: dict, variables: dict) -> dict:
operator = Operator.find_operator(self.end_if)
if operator.operation(variables):
raise StopException(str(self.end_if) + ' fired')
else:
return variables
def check_skip(self, variables: dict):
if self.skip_if is None:
return False
from catcher.steps.check import Operator
operator = Operator.find_operator(self.skip_if)
if operator.operation(variables):
raise SkipException('Skipped due to {}'.format(self.skip_if))
elements = source
elif isinstance(source, dict):
elements = source.items()
else:
debug(str(source) + ' not iterable')
return False
results = []
for element in elements:
oper_body = dict([(k, v) for (k, v) in body.items() if k != 'of'])
[next_operator] = oper_body.keys()
if not isinstance(oper_body[next_operator], dict): # terminator in short form
if next_operator == 'equals':
oper_body[next_operator] = Equals.to_long_form('{{ ITEM }}', oper_body[next_operator])
if next_operator == 'contains':
oper_body[next_operator] = Contains.to_long_form('{{ ITEM }}', oper_body[next_operator])
next_operation = Operator.find_operator(oper_body)
variables['ITEM'] = element
results.append(next_operation.operation(variables))
return self.operator(results)
def consume(self, topic, variables: dict) -> dict:
consumer = topic.get_simple_consumer(consumer_group=self.group_id.encode('utf-8'),
auto_offset_reset=OffsetType.EARLIEST,
reset_offset_on_start=False,
consumer_timeout_ms=self.timeout * 1000)
if self.where is not None:
operator = Operator.find_operator(self.where)
else:
operator = None
return Kafka.get_messages(consumer, operator, variables, self.timeout)
@staticmethod
def find_operator(source: dict or str) -> 'Operator':
if isinstance(source, str):
operator_str = 'equals'
else:
[operator_str] = source.keys()
operators = get_all_subclasses_of(Operator)
named = dict([(o.__name__.lower(), o) for o in operators])
if operator_str not in named:
raise RuntimeError('No ' + operator_str + ' available')
cls = named[operator_str]
return cls(source)
class Equals(Operator):
"""
Fail if elements are not equal
:Input:
:the: value
:is: variable to compare
:is_not: inverted `is`. Only one can be used at a time.
:Examples:
Check 'bar' equals variable 'foo'
::
check: {equals: {the: 'bar', is: '{{ foo }}'}}
Check list's third element is not greater than 2.
if not result:
debug(str(source) + ' is not equal to ' + str(subject))
return result
def determine_source(self, body: dict):
if 'is' in body:
return body['is']
self.negative = True
return body['is_not']
@staticmethod
def to_long_form(source: any, value: any):
return {'the': source, 'is': value}
class Contains(Operator):
"""
Fail if list of dictionary doesn't contain the value
:Input:
:the: value to contain
:in: variable to check
:not_in: inverted `in`. Only one can be used at a time.
:Examples:
Check 'a' not in variable 'list'
::
check:
contains: {the: 'a', not_in: '{{ list }}'}
if not result:
debug(str(subject) + ' is not in ' + str(source))
return result
def determine_source(self, body: dict):
if 'in' in body:
return body['in']
self.negative = True
return body['not_in']
@staticmethod
def to_long_form(source: any, value: any):
return {'the': source, 'in': value}
class And(Operator):
"""
Fail if any of the conditions fails.
:Input: The list of other checks.
:Examples:
This is the same as `1 in list and list[1] != 'b' and list[2] > 2`
::
check:
and:
- contains: {the: 1, in: '{{ list }}'}
- equals: {the: '{{ list[1] }}', is_not: 'b'}
- equals: {the: '{{ list[2] > 2 }}', is_not: true}
::
check:
or:
- contains: {the: 1, in: '{{ list }}'}
- equals: {the: '{{ list[1] }}', is_not: 'b'}
- equals: {the: '{{ list[2] > 2 }}', is_not: true}
"""
@property
def end(self):
return True
class All(Operator):
"""
Fail if any check on the iterable fail.
:Input:
:of: The source to check. Can be list or dictionary.
:: Check to perform on each element of the iterable.
:Examples:
Pass if all elements of `var` has `k` == `a`
::
check:
all:
of: '{{ var }}'
equals: {the: '{{ ITEM.k }}', is: 'a'}