From eb2f68f1cb3af9cb3562304f600e8b391f7e6b94 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Tue, 5 Jun 2018 10:12:49 +0100 Subject: [PATCH] Convert seconds to nanoseconds for juju.unit.run The juju.unit.run docstring does not specify what units the timeout value should be in. This led to issue #225. Having looked into it, juju is expecting it to be in nanoseconds. I don't think nanoseconds is particularly intuitive so I have updated the function to take the timeout in seconds and convert it internally to nanoseconds. I appreciate this is a breaking change for anyone currently specifying the timeout in nanoseconds so I would quite understand if this change is nack'd in which case I will submit a subsequent pr which simply updates the doc string. Closes #225. --- juju/unit.py | 6 +++++- tests/integration/test_unit.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/juju/unit.py b/juju/unit.py index ce33b083..732bb876 100644 --- a/juju/unit.py +++ b/juju/unit.py @@ -122,7 +122,7 @@ async def run(self, command, timeout=None): """Run command on this unit. :param str command: The command to run - :param int timeout: Time to wait before command is considered failed + :param int timeout: Time, in seconds, to wait before command is considered failed :returns: A :class:`juju.action.Action` instance. """ @@ -131,6 +131,10 @@ async def run(self, command, timeout=None): log.debug( 'Running `%s` on %s', command, self.name) + if timeout: + # Convert seconds to nanoseconds + timeout = timeout * 1000000000 + res = await action.Run( [], command, diff --git a/tests/integration/test_unit.py b/tests/integration/test_unit.py index 8b2251cc..bafe8233 100644 --- a/tests/integration/test_unit.py +++ b/tests/integration/test_unit.py @@ -25,6 +25,18 @@ async def test_run(event_loop): assert 'Stdout' in action.results break + for unit in app.units: + action = await unit.run('sleep 1', timeout=0.5) + assert isinstance(action, Action) + assert action.status == 'failed' + break + + for unit in app.units: + action = await unit.run('sleep 0.5', timeout=2) + assert isinstance(action, Action) + assert action.status == 'completed' + break + @base.bootstrapped @pytest.mark.asyncio