catch the case that cowsay is broken (#76326)

* catch the case that cowsay is broken

fixes https://github.com/ansible/ansible/issues/72582

  add changelog
  raise Exception for broken cowsay
  add test for broken cowsay

Co-authored-by: Matthias Bernt <m.bernt@ufz.de>
pull/76494/head
Brian Coca 3 years ago committed by GitHub
parent 60d094db73
commit 472028c869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- catch the case that cowsay is broken which would lead to missing output

@ -219,7 +219,9 @@ class Display(metaclass=Singleton):
try:
cmd = subprocess.Popen([self.b_cowsay, "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate()
self.cows_available = {to_text(c) for c in out.split()}
if cmd.returncode:
raise Exception
self.cows_available = {to_text(c) for c in out.split()} # set comprehension
if C.ANSIBLE_COW_ACCEPTLIST and any(C.ANSIBLE_COW_ACCEPTLIST):
self.cows_available = set(C.ANSIBLE_COW_ACCEPTLIST).intersection(self.cows_available)
except Exception:

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from ansible.utils.display import Display
from mock import MagicMock
def test_display_with_fake_cowsay_binary(capsys, mocker):
mocker.patch("ansible.constants.ANSIBLE_COW_PATH", "./cowsay.sh")
def mock_communicate(input=None, timeout=None):
return b"", b""
mock_popen = MagicMock()
mock_popen.return_value.communicate = mock_communicate
mock_popen.return_value.returncode = 1
mocker.patch("subprocess.Popen", mock_popen)
display = Display()
assert not hasattr(display, "cows_available")
assert display.b_cowsay is None
Loading…
Cancel
Save