Debian Codegraph @

tests/unit/utils/test_scp.py

raw
1 import sys
2 from six import StringIO
3 from contextlib import contextmanager
4
5 import unittest
6 import nose2
7
8 from jnpr.junos import Device
9 from jnpr.junos.utils.scp import SCP
10
11 from unittest.mock import patch
12
13 __author__ = "Rick Sherman, Nitin Kumar"
14 __credits__ = "Jeremy Schulman"
15
16 if sys.version < "3":
17 builtin_string = "__builtin__"
18 else:
19 builtin_string = "builtins"
20
21
22 class TestScp(unittest.TestCase):
23 def setUp(self):
24 self.dev = Device(host="1.1.1.1")
25
26 @patch("paramiko.SSHClient")
27 def test_scp_open(self, mock_connect):
28 from scp import SCPClient
29
30 self.dev.bind(scp=SCP)
31 assert isinstance(self.dev.scp.open(), SCPClient)
32
33 @patch("paramiko.SSHClient")
34 def test_scp_close(self, mock_connect):
35 self.dev.bind(scp=SCP)
36 self.dev.scp.open()
37 self.assertEqual(self.dev.scp.close(), None)
38
39 @patch("paramiko.SSHClient")
40 def test_scp_context(self, mock_connect):
41 with SCP(self.dev) as scp:
42 scp.get("addrbook.conf")
43
44 def test_scp_console(self):
45 dev = Device(host="1.1.1.1", mode="telnet")
46 self.assertRaises(RuntimeError, SCP, dev)
47
48 @patch("jnpr.junos.device.os")
49 @patch(builtin_string + ".open")
50 @patch("paramiko.config.SSHConfig.lookup")
51 @patch("paramiko.SSHClient")
52 @patch("paramiko.proxy.ProxyCommand")
53 def test_scp_proxycommand(
54 self, mock_proxy, mock_paramiko, mock_connect, open_mock, os_mock
55 ):
56 os_mock.path.exists.return_value = True
57 # self.dev._sshconf_path = '/home/rsherman/.ssh/config'
58 with SCP(self.dev) as scp:
59 scp.get("addrbook.conf")
60 mock_proxy.assert_called_once()
61
62 def test_scp_progress(self):
63 scp = SCP(self.dev)
64 print(scp._scp_progress("test", 100, 50))
65
66 @patch("paramiko.SSHClient")
67 @patch("scp.SCPClient.put")
68 @patch("scp.SCPClient.__init__")
69 def test_scp_user_def_progress(self, mock_scpclient, mock_put, mock_ssh):
70 mock_scpclient.return_value = None
71
72 def fn(file, total, tfd):
73 pass
74
75 package = "test.tgz"
76 with SCP(self.dev, progress=fn) as scp:
77 scp.put(package)
78 self.assertEqual(mock_scpclient.mock_calls[0][2]["progress"].__name__, "fn")
79
80 @patch("paramiko.SSHClient")
81 @patch("scp.SCPClient.put")
82 @patch("scp.SCPClient.__init__")
83 def test_scp_user_def_progress_args_2(self, mock_scpclient, mock_put, mock_ssh):
84 mock_scpclient.return_value = None
85
86 def myprogress(dev, report):
87 print("host: %s, report: %s" % (dev.hostname, report))
88
89 package = "test.tgz"
90 with SCP(self.dev, progress=myprogress) as scp:
91 scp.put(package)
92 self.assertEqual(
93 mock_scpclient.mock_calls[0][2]["progress"].__name__, "_scp_progress"
94 )
95
96 @patch("paramiko.SSHClient")
97 @patch("scp.SCPClient.put")
98 @patch("scp.SCPClient.__init__")
99 def test_scp_progress_true(self, mock_scpclient, mock_put, mock_sshclient):
100 mock_scpclient.return_value = None
101 package = "test.tgz"
102 with SCP(self.dev, progress=True) as scp:
103 scp.put(package)
104 self.assertEqual(
105 mock_scpclient.mock_calls[0][2]["progress"].__name__, "_scp_progress"
106 )
107
108 @patch("ncclient.manager.connect")
109 @patch("paramiko.SSHClient.connect")
110 @patch("scp.SCPClient.put")
111 @patch("scp.SCPClient.__init__")
112 def test_ssh_private_key_file(
113 self, mock_scpclient, mock_put, mock_sshclient, mock_ncclient
114 ):
115 mock_scpclient.return_value = None
116 package = "test.tgz"
117 dev = Device(
118 host="1.1.1.1", user="user", ssh_private_key_file="/Users/test/testkey"
119 )
120 dev.open(gather_facts=False)
121 with SCP(dev) as scp:
122 scp.put(package)
123 self.assertEqual(
124 mock_sshclient.mock_calls[0][2]["key_filename"], "/Users/test/testkey"
125 )
126
127 @contextmanager
128 def capture(self, command, *args, **kwargs):
129 out, sys.stdout = sys.stdout, StringIO()
130 command(*args, **kwargs)
131 sys.stdout.seek(0)
132 yield sys.stdout.read()
133 sys.stdout = out