Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the concept of atexit hooks to Zaza #459

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions zaza/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


__path__ = extend_path(__path__, __name__)
_ATEXIT = []


def run(*steps):
Expand Down Expand Up @@ -85,3 +86,30 @@ async def _run_it():
return await f(*args, **kwargs)
return run(_run_it())
return _wrapper


def atexit(func):
"""Queue the passed callable for the very end of execution.

:param func: A callable that will be run at the end of a Zaza run
:type func: Callable
:returns: None
:raises: RuntimeError if the argument is not callable
"""
global _ATEXIT
if not callable(func):
raise RuntimeError("_atexit must be passed a Callable")
_ATEXIT.append(func)


def run_atexit_hooks():
"""Run the queued atexit callables.

:returns: None
"""
global _ATEXIT
for func in _ATEXIT:
try:
func()
except Exception as e:
logging.error("Error in cleanup: {}".format(e))
2 changes: 2 additions & 0 deletions zaza/charm_lifecycle/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import unittest
import sys

import zaza
import zaza.model
import zaza.global_options as global_options
import zaza.charm_lifecycle.utils as utils
Expand Down Expand Up @@ -121,6 +122,7 @@ def test(model_name, tests, test_directory=None):
utils.set_base_test_dir(test_dir=test_directory)
zaza.model.set_juju_model(model_name)
run_test_list(tests)
zaza.run_atexit_hooks()


def parse_args(args):
Expand Down