| 1 |
import unittest |
| 2 |
import nose2 |
| 3 |
import ftplib |
| 4 |
import sys |
| 5 |
import os |
| 6 |
|
| 7 |
from jnpr.junos import Device |
| 8 |
import jnpr.junos.utils.ftp |
| 9 |
|
| 10 |
from unittest.mock import patch |
| 11 |
|
| 12 |
if sys.version < "3": |
| 13 |
builtin_string = "__builtin__" |
| 14 |
else: |
| 15 |
builtin_string = "builtins" |
| 16 |
|
| 17 |
|
| 18 |
@unittest.skipIf(sys.platform == "win32", "will work for windows in coming days") |
| 19 |
class TestFtp(unittest.TestCase): |
| 20 |
@patch("ftplib.FTP.connect") |
| 21 |
@patch("ftplib.FTP.login") |
| 22 |
@patch("ftplib.FTP.close") |
| 23 |
@patch("ncclient.manager.connect") |
| 24 |
def setUp(self, mock_connect, mock_ftp_connect, mock_ftpconnect, mock_ftplogin): |
| 25 |
self.dev = Device( |
| 26 |
host="1.1.1.1", user="testuser", passwd="testpasswd", gather_facts=False |
| 27 |
) |
| 28 |
self.dev.open() |
| 29 |
self.dev._facts = {"hostname": "1.1.1.1"} |
| 30 |
self.dev_ftp = jnpr.junos.utils.ftp.FTP(self.dev) |
| 31 |
|
| 32 |
def test_ftp_open(self): |
| 33 |
assert isinstance(self.dev_ftp, ftplib.FTP) |
| 34 |
|
| 35 |
@patch("ftplib.FTP.login") |
| 36 |
@patch("ftplib.FTP.connect") |
| 37 |
def test_ftp_open_erors(self, mock_ftpconnect, mock_ftplogin): |
| 38 |
jnpr.junos.utils.ftp.FTP(self.dev) |
| 39 |
mock_ftplogin.assert_called_with("testuser", "testpasswd", "") |
| 40 |
|
| 41 |
# |
| 42 |
|
| 43 |
@patch("ftplib.FTP.close") |
| 44 |
def test_ftp_close(self, mock_close): |
| 45 |
self.dev_ftp.open() |
| 46 |
self.dev_ftp.close() |
| 47 |
mock_close.assert_called() |
| 48 |
|
| 49 |
@patch("ftplib.FTP.connect") |
| 50 |
@patch("ftplib.FTP.login") |
| 51 |
@patch("ftplib.FTP.close") |
| 52 |
def test_ftp_context(self, mock_ftpconnect, mock_ftplogin, mock_ftpclose): |
| 53 |
with jnpr.junos.utils.ftp.FTP(self.dev) as dev_ftp: |
| 54 |
assert isinstance(dev_ftp, ftplib.FTP) |
| 55 |
|
| 56 |
@patch(builtin_string + ".open") |
| 57 |
def test_ftp_upload_file_errors(self, mock_open): |
| 58 |
self.assertEqual(self.dev_ftp.put(local_file="testfile"), False) |
| 59 |
self.assertEqual(self.dev_ftp.put(local_file="/var/testfile"), False) |
| 60 |
|
| 61 |
@patch("ftplib.FTP.storbinary") |
| 62 |
@patch(builtin_string + ".open") |
| 63 |
def test_ftp_upload_file(self, mock_ftpstore, mock_open): |
| 64 |
self.assertEqual(self.dev_ftp.put(local_file="testfile"), True) |
| 65 |
|
| 66 |
@patch(builtin_string + ".open") |
| 67 |
def test_ftp_dnload_file_errors(self, mock_open): |
| 68 |
self.assertEqual( |
| 69 |
self.dev_ftp.get(local_path="testfile", remote_file="testfile"), False |
| 70 |
) |
| 71 |
|
| 72 |
@patch(builtin_string + ".open") |
| 73 |
def test_ftp_dnload_file_get(self, mock_open): |
| 74 |
self.assertEqual(self.dev_ftp.get(remote_file="/var/tmp/testfile"), False) |
| 75 |
|
| 76 |
@patch("ftplib.FTP.retrbinary") |
| 77 |
@patch(builtin_string + ".open") |
| 78 |
def test_ftp_dnload_file_get_retr(self, mock_open, mock_ftpretr): |
| 79 |
self.assertEqual(self.dev_ftp.get(remote_file="/var/tmp/testfile"), True) |
| 80 |
|
| 81 |
@patch("ftplib.FTP.retrbinary") |
| 82 |
@patch(builtin_string + ".open") |
| 83 |
def test_ftp_dnload_file_get_rf_filename(self, mock_open, mock_ftpretr): |
| 84 |
self.assertEqual(self.dev_ftp.get(remote_file="testfile.txt"), True) |
| 85 |
|
| 86 |
@patch("ftplib.FTP.retrbinary") |
| 87 |
@patch(builtin_string + ".open") |
| 88 |
def test_ftp_dnload_file(self, mock_ftpretr, mock_open): |
| 89 |
self.assertEqual( |
| 90 |
self.dev_ftp.get(local_path="testfile", remote_file="testfile"), True |
| 91 |
) |
| 92 |
|
| 93 |
@patch("ftplib.FTP.connect") |
| 94 |
@patch("ftplib.FTP.login") |
| 95 |
@patch("ftplib.FTP.retrbinary") |
| 96 |
def test_ftp_dnload_file_get_rf_filename_cb( |
| 97 |
self, |
| 98 |
mock_ftp_connect, |
| 99 |
mock_ftp_login, |
| 100 |
mock_ftp_retrbinary, |
| 101 |
): |
| 102 |
def callback(): |
| 103 |
pass |
| 104 |
|
| 105 |
kwargs = {"callback": callback} |
| 106 |
dev_ftp = jnpr.junos.utils.ftp.FTP(self.dev, **kwargs) |
| 107 |
self.assertEqual(dev_ftp.get(remote_file="/var/tmp/testfile"), True) |
| 108 |
|
| 109 |
@patch("ftplib.FTP.storbinary") |
| 110 |
@patch(builtin_string + ".open") |
| 111 |
def test_ftp_upload_file_rem_path(self, mock_open, mock_ftpstore): |
| 112 |
self.assertEqual( |
| 113 |
self.dev_ftp.put(local_file="/var/tmp/conf.txt", remote_path="/var/tmp"), |
| 114 |
True, |
| 115 |
) |
| 116 |
self.assertEqual( |
| 117 |
tuple(mock_ftpstore.call_args)[1]["cmd"], "STOR /var/tmp/conf.txt" |
| 118 |
) |
| 119 |
|
| 120 |
@patch("ftplib.FTP.storbinary") |
| 121 |
@patch(builtin_string + ".open") |
| 122 |
def test_ftp_upload_file_rem_full_path(self, mock_open, mock_ftpstore): |
| 123 |
self.assertEqual( |
| 124 |
self.dev_ftp.put( |
| 125 |
local_file=os.path.abspath("/var/tmp/conf.txt"), |
| 126 |
remote_path=os.path.abspath("/var/tmp/test.txt"), |
| 127 |
), |
| 128 |
True, |
| 129 |
) |
| 130 |
self.assertEqual( |
| 131 |
tuple(mock_ftpstore.call_args)[1]["cmd"], |
| 132 |
"STOR " + os.path.abspath("/var/tmp/test.txt"), |
| 133 |
) |
| 134 |
|
| 135 |
@patch("ftplib.FTP.storbinary") |
| 136 |
@patch(builtin_string + ".open") |
| 137 |
def test_ftp_upload_file_rem_path_create(self, mock_open, mock_ftpstore): |
| 138 |
self.assertEqual( |
| 139 |
self.dev_ftp.put( |
| 140 |
local_file="conf.txt", remote_path=os.path.abspath("/var/tmp") |
| 141 |
), |
| 142 |
True, |
| 143 |
) |
| 144 |
self.assertEqual( |
| 145 |
tuple(mock_ftpstore.call_args)[1]["cmd"], |
| 146 |
"STOR " + os.path.abspath("/var/tmp/conf.txt"), |
| 147 |
) |