Skip to content

Commit

Permalink
Add test for print_result function (#126)
Browse files Browse the repository at this point in the history
Change severity to severity_level on tasks, add tests to print_output
  • Loading branch information
ogenstad authored May 1, 2018
1 parent bd26fef commit 6a667c7
Show file tree
Hide file tree
Showing 37 changed files with 376 additions and 27 deletions.
22 changes: 11 additions & 11 deletions brigade/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Task(object):
Arguments:
task (callable): function or callable we will be calling
name (``string``): name of task, defaults to ``task.__name__``
severity (logging.LEVEL): Severity level associated to the task
severity_level (logging.LEVEL): Severity level associated to the task
**kwargs: Parameters that will be passed to the ``task``
Attributes:
Expand All @@ -26,15 +26,15 @@ class Task(object):
before calling the ``task``
brigade(:obj:`brigade.core.Brigade`): Populated right before calling
the ``task``
severity (logging.LEVEL): Severity level associated to the task
severity_level (logging.LEVEL): Severity level associated to the task
"""

def __init__(self, task, name=None, severity=logging.INFO, **kwargs):
def __init__(self, task, name=None, severity_level=logging.INFO, **kwargs):
self.name = name or task.__name__
self.task = task
self.params = kwargs
self.results = MultiResult(self.name)
self.severity = severity
self.severity_level = severity_level

def __repr__(self):
return self.name
Expand Down Expand Up @@ -66,7 +66,7 @@ def start(self, host, brigade):
logger.error("{}: {}".format(self.host, tb))
r = Result(host, exception=e, result=tb, failed=True)
r.name = self.name
r.severity = logging.ERROR if r.failed else self.severity
r.severity_level = logging.ERROR if r.failed else self.severity_level

self.results.insert(0, r)
return self.results
Expand All @@ -90,8 +90,8 @@ def grouped_tasks(task):
)
raise Exception(msg)

if "severity" not in kwargs:
kwargs["severity"] = self.severity
if "severity_level" not in kwargs:
kwargs["severity_level"] = self.severity_level
r = Task(task, **kwargs).start(self.host, self.brigade)
if r.failed:
# Without this we will keep running the grouped task
Expand Down Expand Up @@ -120,7 +120,7 @@ class Result(object):
result (obj): Result of the task execution, see task's documentation for details
host (:obj:`brigade.core.inventory.Host`): Reference to the host that lead ot this result
failed (bool): Whether the execution failed or not
severity (logging.LEVEL): Severity level associated to the result of the excecution
severity_level (logging.LEVEL): Severity level associated to the result of the excecution
exception (Exception): uncaught exception thrown during the exection of the task (if any)
Attributes:
Expand All @@ -129,7 +129,7 @@ class Result(object):
result (obj): Result of the task execution, see task's documentation for details
host (:obj:`brigade.core.inventory.Host`): Reference to the host that lead ot this result
failed (bool): Whether the execution failed or not
severity (logging.LEVEL): Severity level associated to the result of the excecution
severity_level (logging.LEVEL): Severity level associated to the result of the excecution
exception (Exception): uncaught exception thrown during the exection of the task (if any)
"""

Expand All @@ -141,7 +141,7 @@ def __init__(
diff="",
failed=False,
exception=None,
severity=logging.INFO,
severity_level=logging.INFO,
**kwargs
):
self.result = result
Expand All @@ -151,7 +151,7 @@ def __init__(
self.failed = failed
self.exception = exception
self.name = None
self.severity = severity
self.severity_level = severity_level

for k, v in kwargs.items():
setattr(self, k, v)
Expand Down
6 changes: 3 additions & 3 deletions brigade/plugins/functions/text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def _get_color(result, failed):
def _print_individual_result(
result, host, vars, failed, severity_level, task_group=False
):
if result.severity < severity_level:
if result.severity_level < severity_level:
return

color = _get_color(result, failed)
subtitle = "" if result.changed is None else " ** changed : {} ".format(
result.changed
)
level_name = logging.getLevelName(result.severity)
level_name = logging.getLevelName(result.severity_level)
symbol = "v" if task_group else "-"
msg = "{} {}{}".format(symbol * 4, result.name, subtitle)
print(
Expand Down Expand Up @@ -77,7 +77,7 @@ def print_result(
if isinstance(result, AggregatedResult):
msg = result.name
print("{}{}{}{}".format(Style.BRIGHT, Fore.CYAN, msg, "*" * (80 - len(msg))))
for host, host_data in result.items():
for host, host_data in sorted(result.items()):
title = "" if host_data.changed is None else " ** changed : {} ".format(
host_data.changed
)
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/intro/task_results.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
" name=\"Base Configuration\",\n",
" template=\"base.j2\",\n",
" path=f\"templates/{task.host.nos}\",\n",
" severity=logging.DEBUG)\n",
" severity_level=logging.DEBUG)\n",
"\n",
" # Save the compiled configuration into a host variable\n",
" task.host[\"config\"] = r.result\n",
Expand All @@ -41,7 +41,7 @@
" name=\"Loading Configuration on the device\",\n",
" replace=False,\n",
" configuration=task.host[\"config\"],\n",
" severity=logging.INFO)"
" severity_level=logging.INFO)"
]
},
{
Expand Down Expand Up @@ -209,7 +209,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As you probably noticed, not all the tasks where printed. If you check the tests they all got a new argument `severity`. This let's us flag tasks with any of the logging levels. Then `print_result` is able to following logging rules to print the results. By default only tasks marked as `INFO` will be printed (this is also the default for the tasks if none is specified). Note that a failed task will have its severity level changed to `ERROR` regardless of the one specified by the user.\n",
"As you probably noticed, not all the tasks where printed. If you check the tests they all got a new argument `severity_level`. This let's us flag tasks with any of the logging levels. Then `print_result` is able to following logging rules to print the results. By default only tasks marked as `INFO` will be printed (this is also the default for the tasks if none is specified). Note that a failed task will have its severity level changed to `ERROR` regardless of the one specified by the user.\n",
"\n",
"Now let's tell `print_result` to print tasks marked as `DEBUG`."
]
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
decorator
pytest
pytest-cov
pylama
Expand Down
22 changes: 12 additions & 10 deletions tests/core/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def task_fails_for_some(task):
task.run(
commands.command,
command="echo {}".format(task.host),
severity=logging.DEBUG,
severity_level=logging.DEBUG,
)


Expand Down Expand Up @@ -79,23 +79,25 @@ def test_run_on(self, brigade):
def test_severity(self, brigade):
r = brigade.run(commands.command, command="echo blah")
for host, result in r.items():
assert result[0].severity == logging.INFO
assert result[0].severity_level == logging.INFO

r = brigade.run(commands.command, command="echo blah", severity=logging.WARN)
r = brigade.run(
commands.command, command="echo blah", severity_level=logging.WARN
)
for host, result in r.items():
assert result[0].severity == logging.WARN
assert result[0].severity_level == logging.WARN

r = brigade.run(sub_task, severity=logging.WARN)
r = brigade.run(sub_task, severity_level=logging.WARN)
for host, result in r.items():
for sr in result:
assert sr.severity == logging.WARN
assert sr.severity_level == logging.WARN

r = brigade.run(task_fails_for_some, severity=logging.WARN, num_workers=1)
r = brigade.run(task_fails_for_some, severity_level=logging.WARN, num_workers=1)
for host, result in r.items():
if host == "dev3.group_2":
assert result[0].severity == logging.ERROR
assert result[0].severity_level == logging.ERROR
else:
assert result[0].severity == logging.WARN
assert result[1].severity == logging.DEBUG
assert result[0].severity_level == logging.WARN
assert result[1].severity_level == logging.DEBUG

brigade.data.reset_failed_hosts()
Empty file.
Empty file.
Empty file.
17 changes: 17 additions & 0 deletions tests/plugins/functions/text/output_data/basic_inventory.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
echo_task***********************************************************************
* dev1.group_1 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vvvv data_with_greeting ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- echo_task ** changed : False ---------------------------------------------- INFO
Hello from Brigade
---- load_data ** changed : False ---------------------------------------------- INFO
{'os': 'Linux', 'services': ['http', 'smtp', 'dns']}
^^^^ END data_with_greeting ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vvvv data_with_greeting ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- echo_task ** changed : False ---------------------------------------------- INFO
Hello from Brigade
---- load_data ** changed : False ---------------------------------------------- INFO
{ 'os': 'Linux', 'services': ['http', 'smtp', 'dns']}
^^^^ END data_with_greeting ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- echo_task ** changed : False ---------------------------------------------- INFO
Hello from Brigade
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- echo_task ** changed : False ---------------------------------------------- INFO
Hello from Brigade
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
echo_task***********************************************************************
* dev1.group_1 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
5 changes: 5 additions & 0 deletions tests/plugins/functions/text/output_data/basic_single.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
echo_task***********************************************************************
* dev1.group_1 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
echo_task***********************************************************************
* dev1.group_1 ** changed : False **********************************************
vvvv echo_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Hello from Brigade
^^^^ END echo_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
15 changes: 15 additions & 0 deletions tests/plugins/functions/text/output_data/changed_host.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
read_data***********************************************************************
* dev1.group_1 ** changed : True ***********************************************
vvvv read_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv WARNING
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
---- parse_data ** changed : True ---------------------------------------------- WARNING
[1, 2, 3]
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
vvvv read_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv WARNING
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
---- parse_data ** changed : False --------------------------------------------- WARNING
[4, 5, 6]
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
read_data***********************************************************************
* dev1.group_1 ** changed : True ***********************************************
vvvv read_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv WARNING
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
---- parse_data ** changed : True ---------------------------------------------- WARNING
[1, 2, 3]
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
vvvv read_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv WARNING
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
---- parse_data ** changed : False --------------------------------------------- WARNING
[4, 5, 6]
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
read_data***********************************************************************
* dev1.group_1 ** changed : True ***********************************************
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : False **********************************************
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv read_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Exception('Unknown Error -> Contact your system administrator',)
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
read_data***********************************************************************
* dev1.group_1 ** changed : True ***********************************************
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : False **********************************************
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv read_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Exception('Unknown Error -> Contact your system administrator',)
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Empty file.
Loading

0 comments on commit 6a667c7

Please sign in to comment.