Skip to content

Commit

Permalink
Convert seconds to nanoseconds for juju.unit.run (#237)
Browse files Browse the repository at this point in the history
* 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.

* Ensure the timeout is an integer
  • Loading branch information
Liam Young authored and johnsca committed Jun 5, 2018
1 parent 0f413e6 commit 462989b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion juju/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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 = int(timeout * 1000000000)

res = await action.Run(
[],
command,
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 462989b

Please sign in to comment.