File: paramiko_tests.py

package info (click to toggle)
proxmoxer 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 188 kB
  • sloc: python: 761; makefile: 4
file content (66 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download | duplicates (4)
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
__author__ = 'Oleg Butovich'
__copyright__ = '(c) Oleg Butovich 2013-2017'
__licence__ = 'MIT'

import io
from mock import patch
from nose.tools import eq_
from proxmoxer import ProxmoxAPI
from .base.base_ssh_suite import BaseSSHSuite


@patch('paramiko.SSHClient')
def test_paramiko_connection(_):
    proxmox = ProxmoxAPI('proxmox', user='root', backend='ssh_paramiko', port=123)
    session = proxmox._store['session']
    eq_(session.ssh_client.connect.call_args[0], ('proxmox',))
    eq_(session.ssh_client.connect.call_args[1], {'username': 'root',
                                                  'allow_agent': True,
                                                  'key_filename': None,
                                                  'look_for_keys': True,
                                                  'timeout': 5,
                                                  'password': None,
                                                  'port': 123})


class TestParamikoSuite(BaseSSHSuite):

    # noinspection PyMethodOverriding
    @patch('paramiko.SSHClient')
    def setUp(self, _):
        self.proxmox = ProxmoxAPI('proxmox', user='root', backend='ssh_paramiko', port=123)
        self.client = self.proxmox._store['session'].ssh_client
        self.session = self.client.get_transport().open_session()
        self._set_stderr('200 OK')
        self._set_stdout('')

    def _get_called_cmd(self):
        return self.session.exec_command.call_args[0][0]

    def _set_stdout(self, stdout):
        self.session.makefile.return_value = io.BytesIO(stdout.encode('utf-8'))

    def _set_stderr(self, stderr):
        self.session.makefile_stderr.return_value = io.BytesIO(stderr.encode('utf-8'))


class TestParamikoSuiteWithSudo(BaseSSHSuite):

    # noinspection PyMethodOverriding
    @patch('paramiko.SSHClient')
    def setUp(self, _):
        super(TestParamikoSuiteWithSudo, self).__init__(sudo=True)
        self.proxmox = ProxmoxAPI('proxmox', user='root', backend='ssh_paramiko', port=123, sudo=True)
        self.client = self.proxmox._store['session'].ssh_client
        self.session = self.client.get_transport().open_session()
        self._set_stderr('200 OK')
        self._set_stdout('')

    def _get_called_cmd(self):
        return self.session.exec_command.call_args[0][0]

    def _set_stdout(self, stdout):
        self.session.makefile.return_value = io.BytesIO(stdout.encode('utf-8'))

    def _set_stderr(self, stderr):
        self.session.makefile_stderr.return_value = io.BytesIO(stderr.encode('utf-8'))