summaryrefslogtreecommitdiff
path: root/test/units/plugins/connection/test_network_cli.py
blob: 899d1dc353ca6d61f1ead58d529ef14128923171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#
# (c) 2016 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import re
import json

from io import StringIO

from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch, MagicMock

from ansible.errors import AnsibleConnectionFailure
from ansible.playbook.play_context import PlayContext
from ansible.plugins.connection import network_cli


class TestConnectionClass(unittest.TestCase):

    @patch("ansible.plugins.connection.network_cli.ParamikoSshConnection._connect")
    def test_network_cli__connect_error(self, mocked_super):
        pc = PlayContext()
        new_stdin = StringIO()

        conn = network_cli.Connection(pc, new_stdin)
        conn.ssh = MagicMock()
        conn.receive = MagicMock()
        conn._terminal = MagicMock()
        pc.network_os = None
        self.assertRaises(AnsibleConnectionFailure, conn._connect)

    @patch("ansible.plugins.connection.network_cli.ParamikoSshConnection._connect")
    def test_network_cli__invalid_os(self, mocked_super):
        pc = PlayContext()
        new_stdin = StringIO()

        conn = network_cli.Connection(pc, new_stdin)
        conn.ssh = MagicMock()
        conn.receive = MagicMock()
        conn._terminal = MagicMock()
        pc.network_os = None
        self.assertRaises(AnsibleConnectionFailure, conn._connect)

    @patch("ansible.plugins.connection.network_cli.terminal_loader")
    @patch("ansible.plugins.connection.network_cli.ParamikoSshConnection._connect")
    def test_network_cli__connect(self, mocked_super, mocked_terminal_loader):
        pc = PlayContext()
        new_stdin = StringIO()

        conn = network_cli.Connection(pc, new_stdin)
        pc.network_os = 'ios'

        conn.ssh = MagicMock()
        conn.receive = MagicMock()
        conn._terminal = MagicMock()

        conn._connect()
        self.assertTrue(conn._terminal.on_open_shell.called)
        self.assertFalse(conn._terminal.on_authorize.called)

        conn._play_context.become = True
        conn._play_context.become_method = 'enable'
        conn._play_context.become_pass = 'password'
        conn._connected = False

        conn._connect()
        conn._terminal.on_authorize.assert_called_with(passwd='password')

    @patch("ansible.plugins.connection.network_cli.ParamikoSshConnection.close")
    def test_network_cli_close(self, mocked_super):
        pc = PlayContext()
        new_stdin = StringIO()
        conn = network_cli.Connection(pc, new_stdin)

        terminal = MagicMock(supports_multiplexing=False)
        conn._terminal = terminal
        conn._ssh_shell = MagicMock()
        conn._connected = True

        conn.close()
        self.assertTrue(terminal.on_close_shell.called)
        self.assertIsNone(conn._ssh_shell)

    @patch("ansible.plugins.connection.network_cli.ParamikoSshConnection._connect")
    def test_network_cli_exec_command(self, mocked_super):
        pc = PlayContext()
        new_stdin = StringIO()
        conn = network_cli.Connection(pc, new_stdin)

        mock_send = MagicMock(return_value=b'command response')
        conn.send = mock_send
        conn._ssh_shell = MagicMock()

        # test sending a single command and converting to dict
        out = conn.exec_command('command')
        self.assertEqual(out, b'command response')
        mock_send.assert_called_with(command=b'command')

        # test sending a json string
        out = conn.exec_command(json.dumps({'command': 'command'}))
        self.assertEqual(out, b'command response')
        mock_send.assert_called_with(command=b'command')

    def test_network_cli_send(self):
        pc = PlayContext()
        new_stdin = StringIO()
        conn = network_cli.Connection(pc, new_stdin)
        mock__terminal = MagicMock()
        mock__terminal.terminal_stdout_re = [re.compile(b'device#')]
        mock__terminal.terminal_stderr_re = [re.compile(b'^ERROR')]
        conn._terminal = mock__terminal

        mock__shell = MagicMock()
        conn._ssh_shell = mock__shell

        response = b"""device#command
        command response

        device#
        """

        mock__shell.recv.return_value = response

        output = conn.send(b'command', None, None, None)

        mock__shell.sendall.assert_called_with(b'command\r')
        self.assertEqual(output, 'command response')

        mock__shell.reset_mock()
        mock__shell.recv.return_value = b"ERROR: error message device#"

        with self.assertRaises(AnsibleConnectionFailure) as exc:
            conn.send(b'command', None, None, None)
        self.assertEqual(str(exc.exception), 'ERROR: error message device#')