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
=== modified file 'jujupy.py'
--- jujupy.py 2016-09-28 14:03:15 +0000
+++ jujupy.py 2016-09-29 15:30:12 +0000
@@ -1292,6 +1292,16 @@
1292 command, args, self.used_feature_flags, self.env.juju_home,1292 command, args, self.used_feature_flags, self.env.juju_home,
1293 model, user_name=self.env.user_name, **pass_kwargs)1293 model, user_name=self.env.user_name, **pass_kwargs)
12941294
1295 def get_juju_dict(self, command, *args, **kwargs):
1296 """Call a juju command and return the output as a dict.
1297
1298 As get_juju_output but with '--format yaml' appended to args and
1299 the results parsed into a python dict object.
1300 """
1301 format_args = args + ('--format', 'yaml')
1302 text = self.get_juju_output(command, *format_args, **kwargs)
1303 return yaml.safe_load(text)
1304
1295 def show_status(self):1305 def show_status(self):
1296 """Print the status to output."""1306 """Print the status to output."""
1297 self.juju(self._show_status, ('--format', 'yaml'))1307 self.juju(self._show_status, ('--format', 'yaml'))
12981308
=== modified file 'tests/test_jujupy.py'
--- tests/test_jujupy.py 2016-09-28 14:03:15 +0000
+++ tests/test_jujupy.py 2016-09-29 15:30:12 +0000
@@ -1178,6 +1178,22 @@
1178 (sys.executable, get_timeout_path(), '5.00', '--', 'juju',1178 (sys.executable, get_timeout_path(), '5.00', '--', 'juju',
1179 '--show-log', 'bar', '-m', 'foo:foo'))1179 '--show-log', 'bar', '-m', 'foo:foo'))
11801180
1181 def test_get_juju_dict(self):
1182 client = EnvJujuClient(JujuData('foo'), None, 'juju')
1183 with patch.object(client, 'get_juju_output', return_value=dedent("""\
1184 model:
1185 name: test
1186 machines:
1187 "0": example
1188 applications: {}
1189 """), autospec=True) as output_mock:
1190 data = client.get_juju_dict('show-machine', '0', timeout=200)
1191 output_mock.assert_called_once_with('show-machine', '0', '--format',
1192 'yaml', timeout=200)
1193 self.assertEquals({'model': {'name': 'test'},
1194 'machines': {"0": 'example'},
1195 'applications': {}}, data)
1196
1181 def test__shell_environ_juju_data(self):1197 def test__shell_environ_juju_data(self):
1182 client = EnvJujuClient(1198 client = EnvJujuClient(
1183 JujuData('baz', {'type': 'ec2'}), '1.25-foobar', 'path', 'asdf')1199 JujuData('baz', {'type': 'ec2'}), '1.25-foobar', 'path', 'asdf')

Subscribers

People subscribed via source and target branches