From ab8ed2f84dec38f8975022b9637aceed25166408 Mon Sep 17 00:00:00 2001 From: Fran Fitzpatrick Date: Tue, 16 Oct 2018 04:44:38 -0500 Subject: [PATCH] Add unit tests for junos terminal (#47103) --- lib/ansible/plugins/terminal/junos.py | 2 +- test/units/plugins/terminal/test_junos.py | 55 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test/units/plugins/terminal/test_junos.py diff --git a/lib/ansible/plugins/terminal/junos.py b/lib/ansible/plugins/terminal/junos.py index 63d1059025f..9d7f73f2e0d 100644 --- a/lib/ansible/plugins/terminal/junos.py +++ b/lib/ansible/plugins/terminal/junos.py @@ -49,7 +49,7 @@ class TerminalModule(TerminalBase): prompt = self._get_prompt() if prompt.strip().endswith(b'%'): display.vvv('starting cli', self._connection._play_context.remote_addr) - self._exec_cli_command('cli') + self._exec_cli_command(b'cli') for c in (b'set cli timestamp disable', b'set cli screen-length 0', b'set cli screen-width 1024'): self._exec_cli_command(c) except AnsibleConnectionFailure: diff --git a/test/units/plugins/terminal/test_junos.py b/test/units/plugins/terminal/test_junos.py new file mode 100644 index 00000000000..d5ba4709e50 --- /dev/null +++ b/test/units/plugins/terminal/test_junos.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# Copyright: (c) 2018, Fran Fitzpatrick fxfitz +# 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 mock import call, MagicMock +import pytest + +from ansible.errors import AnsibleConnectionFailure +from ansible.plugins.terminal import junos + + +@pytest.fixture +def junos_terminal(): + mock_connection = MagicMock() + return junos.TerminalModule(mock_connection) + + +def test_on_open_shell_sets_terminal_parameters(junos_terminal): + expected_calls = [ + call(b'set cli timestamp disable'), + call(b'set cli screen-length 0'), + call(b'set cli screen-width 1024'), + ] + junos_terminal._exec_cli_command = MagicMock() + junos_terminal._get_prompt = MagicMock() + + junos_terminal._get_prompt.return_value = b'user@localhost >' + junos_terminal.on_open_shell() + junos_terminal._exec_cli_command.assert_has_calls(expected_calls) + + +def test_on_open_shell_enters_cli_if_root_prompt(junos_terminal): + expected_calls = [ + call(b'cli'), + call(b'set cli timestamp disable'), + call(b'set cli screen-length 0'), + call(b'set cli screen-width 1024'), + ] + junos_terminal._exec_cli_command = MagicMock() + junos_terminal._get_prompt = MagicMock() + + junos_terminal._connection.get_prompt.return_value = b'root@localhost%' + junos_terminal.on_open_shell() + junos_terminal._exec_cli_command.assert_has_calls(expected_calls) + + +def test_on_open_shell_raises_problem_setting_terminal_config(junos_terminal): + junos_terminal._connection.exec_command.side_effect = AnsibleConnectionFailure + with pytest.raises(AnsibleConnectionFailure) as exc: + junos_terminal.on_open_shell() + + assert 'unable to set terminal parameters' in str(exc)