File: test_ftp.py

package info (click to toggle)
junos-eznc 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,496 kB
  • sloc: xml: 50,219; python: 20,961; makefile: 155; sh: 44; ruby: 2
file content (147 lines) | stat: -rw-r--r-- 5,053 bytes parent folder | download | duplicates (2)
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
import unittest
import nose2
import ftplib
import sys
import os

from jnpr.junos import Device
import jnpr.junos.utils.ftp

from unittest.mock import patch

if sys.version < "3":
    builtin_string = "__builtin__"
else:
    builtin_string = "builtins"


@unittest.skipIf(sys.platform == "win32", "will work for windows in coming days")
class TestFtp(unittest.TestCase):
    @patch("ftplib.FTP.connect")
    @patch("ftplib.FTP.login")
    @patch("ftplib.FTP.close")
    @patch("ncclient.manager.connect")
    def setUp(self, mock_connect, mock_ftp_connect, mock_ftpconnect, mock_ftplogin):
        self.dev = Device(
            host="1.1.1.1", user="testuser", passwd="testpasswd", gather_facts=False
        )
        self.dev.open()
        self.dev._facts = {"hostname": "1.1.1.1"}
        self.dev_ftp = jnpr.junos.utils.ftp.FTP(self.dev)

    def test_ftp_open(self):
        assert isinstance(self.dev_ftp, ftplib.FTP)

    @patch("ftplib.FTP.login")
    @patch("ftplib.FTP.connect")
    def test_ftp_open_erors(self, mock_ftpconnect, mock_ftplogin):
        jnpr.junos.utils.ftp.FTP(self.dev)
        mock_ftplogin.assert_called_with("testuser", "testpasswd", "")

    #

    @patch("ftplib.FTP.close")
    def test_ftp_close(self, mock_close):
        self.dev_ftp.open()
        self.dev_ftp.close()
        mock_close.assert_called()

    @patch("ftplib.FTP.connect")
    @patch("ftplib.FTP.login")
    @patch("ftplib.FTP.close")
    def test_ftp_context(self, mock_ftpconnect, mock_ftplogin, mock_ftpclose):
        with jnpr.junos.utils.ftp.FTP(self.dev) as dev_ftp:
            assert isinstance(dev_ftp, ftplib.FTP)

    @patch(builtin_string + ".open")
    def test_ftp_upload_file_errors(self, mock_open):
        self.assertEqual(self.dev_ftp.put(local_file="testfile"), False)
        self.assertEqual(self.dev_ftp.put(local_file="/var/testfile"), False)

    @patch("ftplib.FTP.storbinary")
    @patch(builtin_string + ".open")
    def test_ftp_upload_file(self, mock_ftpstore, mock_open):
        self.assertEqual(self.dev_ftp.put(local_file="testfile"), True)

    @patch(builtin_string + ".open")
    def test_ftp_dnload_file_errors(self, mock_open):
        self.assertEqual(
            self.dev_ftp.get(local_path="testfile", remote_file="testfile"), False
        )

    @patch(builtin_string + ".open")
    def test_ftp_dnload_file_get(self, mock_open):
        self.assertEqual(self.dev_ftp.get(remote_file="/var/tmp/testfile"), False)

    @patch("ftplib.FTP.retrbinary")
    @patch(builtin_string + ".open")
    def test_ftp_dnload_file_get_retr(self, mock_open, mock_ftpretr):
        self.assertEqual(self.dev_ftp.get(remote_file="/var/tmp/testfile"), True)

    @patch("ftplib.FTP.retrbinary")
    @patch(builtin_string + ".open")
    def test_ftp_dnload_file_get_rf_filename(self, mock_open, mock_ftpretr):
        self.assertEqual(self.dev_ftp.get(remote_file="testfile.txt"), True)

    @patch("ftplib.FTP.retrbinary")
    @patch(builtin_string + ".open")
    def test_ftp_dnload_file(self, mock_ftpretr, mock_open):
        self.assertEqual(
            self.dev_ftp.get(local_path="testfile", remote_file="testfile"), True
        )

    @patch("ftplib.FTP.connect")
    @patch("ftplib.FTP.login")
    @patch("ftplib.FTP.retrbinary")
    def test_ftp_dnload_file_get_rf_filename_cb(
        self,
        mock_ftp_connect,
        mock_ftp_login,
        mock_ftp_retrbinary,
    ):
        def callback():
            pass

        kwargs = {"callback": callback}
        dev_ftp = jnpr.junos.utils.ftp.FTP(self.dev, **kwargs)
        self.assertEqual(dev_ftp.get(remote_file="/var/tmp/testfile"), True)

    @patch("ftplib.FTP.storbinary")
    @patch(builtin_string + ".open")
    def test_ftp_upload_file_rem_path(self, mock_open, mock_ftpstore):
        self.assertEqual(
            self.dev_ftp.put(local_file="/var/tmp/conf.txt", remote_path="/var/tmp"),
            True,
        )
        self.assertEqual(
            tuple(mock_ftpstore.call_args)[1]["cmd"], "STOR /var/tmp/conf.txt"
        )

    @patch("ftplib.FTP.storbinary")
    @patch(builtin_string + ".open")
    def test_ftp_upload_file_rem_full_path(self, mock_open, mock_ftpstore):
        self.assertEqual(
            self.dev_ftp.put(
                local_file=os.path.abspath("/var/tmp/conf.txt"),
                remote_path=os.path.abspath("/var/tmp/test.txt"),
            ),
            True,
        )
        self.assertEqual(
            tuple(mock_ftpstore.call_args)[1]["cmd"],
            "STOR " + os.path.abspath("/var/tmp/test.txt"),
        )

    @patch("ftplib.FTP.storbinary")
    @patch(builtin_string + ".open")
    def test_ftp_upload_file_rem_path_create(self, mock_open, mock_ftpstore):
        self.assertEqual(
            self.dev_ftp.put(
                local_file="conf.txt", remote_path=os.path.abspath("/var/tmp")
            ),
            True,
        )
        self.assertEqual(
            tuple(mock_ftpstore.call_args)[1]["cmd"],
            "STOR " + os.path.abspath("/var/tmp/conf.txt"),
        )