Merge lp:~andrewjbeach/juju-ci-tools/get-juju-dict into lp:juju-ci-tools

Proposed by Andrew James Beach
Status: Needs review
Proposed branch: lp:~andrewjbeach/juju-ci-tools/get-juju-dict
Merge into: lp:juju-ci-tools
Diff against target: 47 lines (+26/-0)
2 files modified
jujupy.py (+10/-0)
tests/test_jujupy.py (+16/-0)
To merge this branch: bzr merge lp:~andrewjbeach/juju-ci-tools/get-juju-dict
Reviewer Review Type Date Requested Status
Juju Release Engineering Pending
Review via email: mp+307191@code.launchpad.net

Description of the change

Added an method to EnvJujuClient, get_juju_dict. It is a thin operation, but
it wraps up get_juju_output with a yaml.safe_load. This pair of operations
(and adding '--format' 'yaml' to the arguments list) comes up in a number of
other methods as well as the general code base. Commonly enough it should be
worth wrapping up the idiom. It may also cut out some import yaml calls.

Added get_juju_dict to EnvJujuClient in jujupy.py.
Added test_get_juju_dict to TestJujuClient in test_jujupy.py.

To post a comment you must log in.

Unmerged revisions

1624. By Andrew James Beach

Added EnvJujuClient.get_juju_dict, which wraps the common get_juju_output and then parse to dict operation.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'jujupy.py'
2--- jujupy.py 2016-09-28 14:03:15 +0000
3+++ jujupy.py 2016-09-29 15:30:12 +0000
4@@ -1292,6 +1292,16 @@
5 command, args, self.used_feature_flags, self.env.juju_home,
6 model, user_name=self.env.user_name, **pass_kwargs)
7
8+ def get_juju_dict(self, command, *args, **kwargs):
9+ """Call a juju command and return the output as a dict.
10+
11+ As get_juju_output but with '--format yaml' appended to args and
12+ the results parsed into a python dict object.
13+ """
14+ format_args = args + ('--format', 'yaml')
15+ text = self.get_juju_output(command, *format_args, **kwargs)
16+ return yaml.safe_load(text)
17+
18 def show_status(self):
19 """Print the status to output."""
20 self.juju(self._show_status, ('--format', 'yaml'))
21
22=== modified file 'tests/test_jujupy.py'
23--- tests/test_jujupy.py 2016-09-28 14:03:15 +0000
24+++ tests/test_jujupy.py 2016-09-29 15:30:12 +0000
25@@ -1178,6 +1178,22 @@
26 (sys.executable, get_timeout_path(), '5.00', '--', 'juju',
27 '--show-log', 'bar', '-m', 'foo:foo'))
28
29+ def test_get_juju_dict(self):
30+ client = EnvJujuClient(JujuData('foo'), None, 'juju')
31+ with patch.object(client, 'get_juju_output', return_value=dedent("""\
32+ model:
33+ name: test
34+ machines:
35+ "0": example
36+ applications: {}
37+ """), autospec=True) as output_mock:
38+ data = client.get_juju_dict('show-machine', '0', timeout=200)
39+ output_mock.assert_called_once_with('show-machine', '0', '--format',
40+ 'yaml', timeout=200)
41+ self.assertEquals({'model': {'name': 'test'},
42+ 'machines': {"0": 'example'},
43+ 'applications': {}}, data)
44+
45 def test__shell_environ_juju_data(self):
46 client = EnvJujuClient(
47 JujuData('baz', {'type': 'ec2'}), '1.25-foobar', 'path', 'asdf')

Subscribers

People subscribed via source and target branches