Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Adding configurable test state #15

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions idem/states/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,60 @@ def mod_watch(hub, ctx, name, **kwargs):
'comment': 'Watch ran!'
}
return ret


def configurable_test_state(hub, ctx, name, changes=True, result=True, comment='', **kwargs):
'''
A configurable test state which determines its output based on the inputs.

name:
A unique string.
changes:
Do we return anything in the changes field?
Accepts True, False, and 'Random'
Default is True
result:
Do we return successfully or not?
Accepts True, False, and 'Random'
Default is True
If test is True and changes is True, this will be None. If test is
True and and changes is False, this will be True.
comment:
String to fill the comment field with.
Default is ''
'''
ret = {
'name': name,
'changes': {},
'result': False,
'comment': comment
}

change_data = {
'testing': {
'old': 'Unchanged',
'new': 'Something pretended to change'
}
}

# If changes is True, then we place our dummy change dictionary into it
if changes == 'Random':
if random.choice([True, False]):
ret['changes'] = change_data
elif changes is True:
ret['changes'] = change_data
elif changes is False:
ret['changes'] = {}

if result == 'Random':
ret['result'] = random.choice([True, False])
elif result is True:
ret['result'] = True
elif result is False:
ret['result'] = False

if ctx['test']:
ret['result'] = True if changes is False else None
ret['comment'] = 'This is a test' if not comment else comment

return ret