Permalink
Browse files

Catch JSON errors when decoding "ohai ipaddress" output

Fixes #107
  • Loading branch information...
1 parent dab528b commit f3041d66b08e83fbd6d3e9e0bede51cdbf0dcfce @tobami committed Oct 12, 2012
Showing with 6 additions and 2 deletions.
  1. +5 −1 littlechef/chef.py
  2. +1 −1 tests/test_lib.py
View
6 littlechef/chef.py
@@ -62,7 +62,11 @@ def _get_ipaddress(node):
with settings(hide('stdout'), warn_only=True):
output = sudo('ohai ipaddress')
if output.succeeded:
- node['ipaddress'] = json.loads(output)[0]
+ try:
+ node['ipaddress'] = json.loads(output)[0]
+ except json.JSONDecodeError:
+ abort("Could not parse ohai's output for ipaddress"
+ ":\n {0}".format(output))
return True
return False
View
2 tests/test_lib.py
@@ -308,7 +308,7 @@ class MockSudoReturnValue(str):
mocked_ohai_response = ('Invalid gemspec '
'["{0}"]'.format("1.1.1.2"))
mock_method.return_value = MockSudoReturnValue(mocked_ohai_response)
- self.assertRaises(SystemExit, chef._get_ipaddress({}))
+ self.assertRaises(SystemExit, chef._get_ipaddress, {})
def test_build_node_data_bag(self):
"""Should create a node data bag with one item per node"""

0 comments on commit f3041d6

Please sign in to comment.