Beispiel #1
0
    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#')
Beispiel #2
0
    def test_network_cli_open_shell(self):
        pc = PlayContext()
        new_stdin = StringIO()
        conn = network_cli.Connection(pc, new_stdin)

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

        mock_terminal = MagicMock()
        conn._terminal = mock_terminal

        mock__connect = MagicMock()
        conn._connect = mock__connect

        conn.open_shell()

        self.assertTrue(mock__connect.called)
        self.assertTrue(mock_terminal.on_open_shell.called)
        self.assertFalse(mock_terminal.on_authorize.called)

        mock_terminal.reset_mock()

        conn._play_context.become = True
        conn._play_context.become_pass = '******'

        conn.open_shell()

        self.assertTrue(mock__connect.called)
        mock_terminal.on_authorize.assert_called_with(passwd='password')
Beispiel #3
0
    def test_network_cli_exec_command(self):
        pc = PlayContext()
        new_stdin = StringIO()
        conn = network_cli.Connection(pc, new_stdin)

        mock_open_shell = MagicMock()
        conn.open_shell = mock_open_shell

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

        # test sending a single command and converting to dict
        rc, out, err = conn.exec_command('command')
        self.assertEqual(out, 'command response')
        self.assertTrue(mock_open_shell.called)
        mock_send.assert_called_with({'command': 'command'})

        mock_open_shell.reset_mock()

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

        mock_open_shell.reset_mock()
        conn._shell = MagicMock()

        # test _shell already open
        rc, out, err = conn.exec_command('command')
        self.assertEqual(out, 'command response')
        self.assertFalse(mock_open_shell.called)
        mock_send.assert_called_with({'command': 'command'})
Beispiel #4
0
    def test_network_cli_connection_module(self):
        play_context = PlayContext()
        play_context.prompt = (
            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '******'eos'
        in_stream = StringIO()

        self.assertIsInstance(network_cli.Connection(play_context, in_stream), network_cli.Connection)
Beispiel #5
0
    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)
Beispiel #6
0
    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)
Beispiel #7
0
    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')
Beispiel #8
0
    def test_network_cli__connect(self, mocked_super, mocked_terminal_loader):
        pc = PlayContext()
        new_stdin = StringIO()

        conn = network_cli.Connection(pc, new_stdin)
        conn.ssh = None

        self.assertRaises(AnsibleConnectionFailure, conn._connect)

        mocked_terminal_loader.reset_mock()
        mocked_terminal_loader.get.return_value = None

        pc.network_os = 'invalid'
        self.assertRaises(AnsibleConnectionFailure, conn._connect)
        self.assertFalse(mocked_terminal_loader.all.called)

        mocked_terminal_loader.reset_mock()
        mocked_terminal_loader.get.return_value = 'valid'

        conn._connect()
        self.assertEqual(conn._terminal, 'valid')
Beispiel #9
0
    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_become.called)

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

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