summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Doran <sdoran@redhat.com>2021-05-17 05:09:22 -0400
committerGitHub <noreply@github.com>2021-05-17 04:09:22 -0500
commitd10195631ec60c320e5cf74943d3aac9ed42e806 (patch)
tree0d63fa2a2b8a0dc34e4f4bfdd885c8af6395b6ec
parent66a9ea2f23d673a92c007a6f7ef6c793b407dd10 (diff)
downloadansible-d10195631ec60c320e5cf74943d3aac9ed42e806.tar.gz
[stable-2.11] pause - ensure control characters are always set appropriately (#74568) (#74600)
On some systems, curses.tigetstr() returns None, which does not work as a control character. * Add unit tests * Sort imports * Skip on older Python This is an action plugin and only runs on the controller, so no need to test of Python 2. Making the import hackery work on Python 2 would required some more work which I am not sure is worth it since we are moving away from Python 2 support on the controller. * Make the tests work on Python 2 and 3 (cherry picked from commit 55b401a3e7) Co-authored-by: Sam Doran <sdoran@redhat.com>
-rw-r--r--changelogs/fragments/73264-pause-emacs.yml2
-rw-r--r--lib/ansible/plugins/action/pause.py10
-rw-r--r--test/units/plugins/action/test_pause.py89
3 files changed, 96 insertions, 5 deletions
diff --git a/changelogs/fragments/73264-pause-emacs.yml b/changelogs/fragments/73264-pause-emacs.yml
new file mode 100644
index 0000000000..41b49fe470
--- /dev/null
+++ b/changelogs/fragments/73264-pause-emacs.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - pause - ensure control characters are always set to an appropriate value (https://github.com/ansible/ansible/issues/73264)
diff --git a/lib/ansible/plugins/action/pause.py b/lib/ansible/plugins/action/pause.py
index 728552ad7b..2bc7d6f6f3 100644
--- a/lib/ansible/plugins/action/pause.py
+++ b/lib/ansible/plugins/action/pause.py
@@ -51,12 +51,12 @@ try:
except ImportError:
HAS_CURSES = False
+MOVE_TO_BOL = b'\r'
+CLEAR_TO_EOL = b'\x1b[K'
if HAS_CURSES:
- MOVE_TO_BOL = curses.tigetstr('cr')
- CLEAR_TO_EOL = curses.tigetstr('el')
-else:
- MOVE_TO_BOL = b'\r'
- CLEAR_TO_EOL = b'\x1b[K'
+ # curses.tigetstr() returns None in some circumstances
+ MOVE_TO_BOL = curses.tigetstr('cr') or MOVE_TO_BOL
+ CLEAR_TO_EOL = curses.tigetstr('el') or CLEAR_TO_EOL
class AnsibleTimeoutExceeded(Exception):
diff --git a/test/units/plugins/action/test_pause.py b/test/units/plugins/action/test_pause.py
new file mode 100644
index 0000000000..8ad6db7208
--- /dev/null
+++ b/test/units/plugins/action/test_pause.py
@@ -0,0 +1,89 @@
+# -*- 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
+
+import curses
+import importlib
+import io
+import pytest
+import sys
+
+from ansible.plugins.action import pause # noqa: F401
+from ansible.module_utils.six import PY2
+
+builtin_import = 'builtins.__import__'
+if PY2:
+ builtin_import = '__builtin__.__import__'
+
+
+def test_pause_curses_tigetstr_none(mocker, monkeypatch):
+ monkeypatch.delitem(sys.modules, 'ansible.plugins.action.pause')
+
+ dunder_import = __import__
+
+ def _import(*args, **kwargs):
+ if args[0] == 'curses':
+ mock_curses = mocker.Mock()
+ mock_curses.setupterm = mocker.Mock(return_value=True)
+ mock_curses.tigetstr = mocker.Mock(return_value=None)
+ return mock_curses
+ else:
+ return dunder_import(*args, **kwargs)
+
+ mocker.patch(builtin_import, _import)
+
+ mod = importlib.import_module('ansible.plugins.action.pause')
+
+ assert mod.HAS_CURSES is True
+ assert mod.MOVE_TO_BOL == b'\r'
+ assert mod.CLEAR_TO_EOL == b'\x1b[K'
+
+
+def test_pause_missing_curses(mocker, monkeypatch):
+ monkeypatch.delitem(sys.modules, 'ansible.plugins.action.pause')
+
+ dunder_import = __import__
+
+ def _import(*args, **kwargs):
+ if args[0] == 'curses':
+ raise ImportError
+ else:
+ return dunder_import(*args, **kwargs)
+
+ mocker.patch(builtin_import, _import)
+
+ mod = importlib.import_module('ansible.plugins.action.pause')
+
+ with pytest.raises(AttributeError):
+ mod.curses
+
+ assert mod.HAS_CURSES is False
+ assert mod.MOVE_TO_BOL == b'\r'
+ assert mod.CLEAR_TO_EOL == b'\x1b[K'
+
+
+@pytest.mark.parametrize('exc', (curses.error, TypeError, io.UnsupportedOperation))
+def test_pause_curses_setupterm_error(mocker, monkeypatch, exc):
+ monkeypatch.delitem(sys.modules, 'ansible.plugins.action.pause')
+
+ dunder_import = __import__
+
+ def _import(*args, **kwargs):
+ if args[0] == 'curses':
+ mock_curses = mocker.Mock()
+ mock_curses.setupterm = mocker.Mock(side_effect=exc)
+ mock_curses.error = curses.error
+ return mock_curses
+ else:
+ return dunder_import(*args, **kwargs)
+
+ mocker.patch(builtin_import, _import)
+
+ mod = importlib.import_module('ansible.plugins.action.pause')
+
+ assert mod.HAS_CURSES is False
+ assert mod.MOVE_TO_BOL == b'\r'
+ assert mod.CLEAR_TO_EOL == b'\x1b[K'