Add unit tests for junos terminal (#47103)

This commit is contained in:
Fran Fitzpatrick 2018-10-16 04:44:38 -05:00 committed by Ganesh Nalawade
parent c3cc2ecc5b
commit ab8ed2f84d
2 changed files with 56 additions and 1 deletions

View file

@ -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:

View file

@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Fran Fitzpatrick <francis.x.fitzpatrick@gmail.com> 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)