Merge lp:~abentley/juju-ci-tools/wait-for-py into lp:juju-ci-tools

Proposed by Aaron Bentley
Status: Merged
Merged at revision: 1991
Proposed branch: lp:~abentley/juju-ci-tools/wait-for-py
Merge into: lp:juju-ci-tools
Diff against target: 122 lines (+61/-3)
4 files modified
jujupy/client.py (+13/-2)
jujupy/tests/test_client.py (+9/-0)
tests/test_deploy_stack.py (+0/-1)
wait_for.py (+39/-0)
To merge this branch: bzr merge lp:~abentley/juju-ci-tools/wait-for-py
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Review via email: mp+322235@code.launchpad.net

Commit message

Add wait_for.py

Description of the change

This branch provides ModelClient.wait_for as a script.

This is mainly useful as a way to monitor interative tests.

Right now, it supports WaitAgentsStarted, which is roughly equivalent to ModelCient.wait_for_started.

It should be trivial to add additional conditions, though ConditionList would be awkward to specify at the commandline.

It also improves error handling when there's no active model, and fixes some lint.

To post a comment you must log in.
1986. By Aaron Bentley

Fix python3 compatibility.

Revision history for this message
Curtis Hovey (sinzui) wrote :

Thank you.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'jujupy/client.py'
2--- jujupy/client.py 2017-04-07 20:20:17 +0000
3+++ jujupy/client.py 2017-04-07 20:33:23 +0000
4@@ -138,6 +138,10 @@
5 """Raised when the provided auth was not accepted."""
6
7
8+class NoActiveModel(Exception):
9+ """Raised when no active model could be found."""
10+
11+
12 def get_timeout_prefix(duration, timeout_path=None):
13 """Return extra arguments to run a command with a timeout."""
14 if timeout_path is None:
15@@ -1286,8 +1290,14 @@
16
17 def get_active_model(self, juju_data_dir):
18 """Determine the active model in a juju data dir."""
19- current = self.get_juju_output(
20- 'switch', (), set(), juju_data_dir, model=None).decode('ascii')
21+ try:
22+ current = self.get_juju_output(
23+ 'switch', (), set(), juju_data_dir, model=None).decode('ascii')
24+ except subprocess.CalledProcessError as e:
25+ if b'no currently specified model' not in e.stderr:
26+ raise
27+ raise NoActiveModel(
28+ 'No active model for {}'.format(juju_data_dir))
29 controller_name, user_model = current.split(':', 1)
30 user_name, model_name = user_model.split('/', 1)
31 return controller_name, user_name, model_name.rstrip('\n')
32@@ -1452,6 +1462,7 @@
33
34
35 class WaitAgentsStarted(BaseCondition):
36+ """Wait until all agents are idle or started."""
37
38 def __init__(self, timeout=1200):
39 super(WaitAgentsStarted, self).__init__(timeout)
40
41=== modified file 'jujupy/tests/test_client.py'
42--- jujupy/tests/test_client.py 2017-04-07 20:20:17 +0000
43+++ jujupy/tests/test_client.py 2017-04-07 20:33:23 +0000
44@@ -70,6 +70,7 @@
45 make_safe_config,
46 ModelClient,
47 NameNotAccepted,
48+ NoActiveModel,
49 NoopCondition,
50 NoProvider,
51 parse_new_state_server_from_error,
52@@ -321,6 +322,14 @@
53 result = backend.get_active_model('/foo/bar')
54 self.assertEqual(('ctrl1', 'user1', 'model1'), result)
55
56+ def test_get_active_model_none(self):
57+ backend = Juju2Backend('/bin/path', '2.0', set(), debug=False,
58+ soft_deadline=None)
59+ with patch('subprocess.Popen', autospec=True, return_value=FakePopen(
60+ '', 'ERROR no currently specified model', 1)):
61+ with self.assertRaises(NoActiveModel):
62+ backend.get_active_model('/foo/bar')
63+
64
65 def backend_call(client, cmd, args, model=None, check=True, timeout=None,
66 extra_env=None):
67
68=== modified file 'tests/test_deploy_stack.py'
69--- tests/test_deploy_stack.py 2017-04-05 23:07:51 +0000
70+++ tests/test_deploy_stack.py 2017-04-07 20:33:23 +0000
71@@ -71,7 +71,6 @@
72 )
73
74 from jujupy.client import (
75- NoopCondition,
76 CommandTime,
77 )
78 from jujupy.configuration import (
79
80=== added file 'wait_for.py'
81--- wait_for.py 1970-01-01 00:00:00 +0000
82+++ wait_for.py 2017-04-07 20:33:23 +0000
83@@ -0,0 +1,39 @@
84+#!/usr/bin/env python
85+
86+from __future__ import print_function
87+
88+
89+from argparse import ArgumentParser
90+import sys
91+
92+from jujupy import (
93+ client_for_existing,
94+ get_juju_data,
95+ )
96+from jujupy.client import (
97+ NoActiveModel,
98+ WaitAgentsStarted,
99+ )
100+
101+
102+def main():
103+ parser = ArgumentParser()
104+ subparsers = parser.add_subparsers(dest='cmd')
105+ started_parser = subparsers.add_parser(
106+ 'agents-started', description=WaitAgentsStarted.__doc__,
107+ )
108+ started_parser.set_defaults(factory=WaitAgentsStarted)
109+ started_parser.add_argument('--timeout', type=int)
110+ args = parser.parse_args()
111+ try:
112+ client = client_for_existing(None, get_juju_data())
113+ except NoActiveModel as e:
114+ print(e, file=sys.stderr)
115+ sys.exit(1)
116+ kwargs = dict((k, v) for k, v in vars(args).items()
117+ if k not in ('factory', 'cmd'))
118+ client.wait_for(args.factory(**kwargs))
119+
120+
121+if __name__ == '__main__':
122+ main()

Subscribers

People subscribed via source and target branches