Skip to content

Commit

Permalink
Fixed serveral trivial bugs or typos on CalledProcessError() usage (#726
Browse files Browse the repository at this point in the history
)

* Fixed serveral trivial bugs or typos on `subprocess.CalledProcessError()`.

* Improved CalledProcessError's output readablity.
  • Loading branch information
chanchiwai-ray authored Jul 21, 2022
1 parent 9b40e00 commit 8939617
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions charmhelpers/contrib/ansible/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,13 @@ def apply_playbook(playbook, tags=None, extra_vars=None):
try:
subprocess.check_output(call, env=env)
except subprocess.CalledProcessError as e:
charmhelpers.core.hookenv.log("Ansible playbook failed with {} "
"Stdout: {}".format(e, e.output),
err_msg = e.output.decode().strip()
charmhelpers.core.hookenv.log("Ansible playbook failed with "
"{}".format(e),
level="ERROR")
raise subprocess.CalledProcessError(e)
charmhelpers.core.hookenv.log("Stdout: {}".format(err_msg),
level="ERROR")
raise e


class AnsibleHooks(charmhelpers.core.hookenv.Hooks):
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/network/test_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def test_is_ipv6_disabled(self, mock_check_output):
# If the syscall returns an error, then return True

def fake_check_call(*args, **kwargs):
raise subprocess.CalledProcessError(['called'], 1)
raise subprocess.CalledProcessError(1, ['called'])
mock_check_output.side_effect = fake_check_call
self.assertTrue(net_ip.is_ipv6_disabled())

Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/storage/test_linux_storage_lvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_is_physical_volume(self):
def test_is_not_physical_volume(self):
"""It properly reports block dev is an LVM PV"""
with patch(STORAGE_LINUX_LVM + '.check_output') as check_output:
check_output.side_effect = subprocess.CalledProcessError('cmd', 2)
check_output.side_effect = subprocess.CalledProcessError(2, 'cmd')
self.assertFalse(lvm.is_lvm_physical_volume('/dev/loop0'))

def test_pvcreate(self):
Expand Down

0 comments on commit 8939617

Please sign in to comment.