-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
255 lines (203 loc) · 9.58 KB
/
Copy pathtests.py
File metadata and controls
255 lines (203 loc) · 9.58 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from io import BytesIO, StringIO
import socket
from unittest import TestCase
import paramiko
from sftpmock import SFTPMock, with_sftpmock
from unittest import mock
class SFTPMockerTest(TestCase):
'''
This tests core functionality of the "with_sftpmock" decorator
'''
@with_sftpmock({
"test.com": {"Outbound": {"file.txt": "some text"}},
"otherdomain.com": {"Outbound": {"another.TXT": "another text"}}
})
def test_fake_server_port_set_correctly(self):
'''
Tests if Connection was mocked to include the hostname -> port attribute (_fake_server_port)
'''
# Import needs to happen here because we want to use mocked Transport
from paramiko import Transport
assert hasattr(
Transport, "_fake_server_port"), "Transport class should have mocked _fake_server_port attribute"
assert isinstance(Transport._fake_server_port, dict)
assert "test.com" in Transport._fake_server_port, "Transport should have 'test.com' as _fake_server_port key"
assert "otherdomain.com" in Transport._fake_server_port, "Transport should have 'otherdomain.com' as _fake_server_port key"
assert Transport._fake_server_port["test.com"] is not None, "Transport should have a port for 'test.com'"
assert Transport._fake_server_port["otherdomain.com"] is not None, "Transport should have a port for 'otherdomain.com'"
assert Transport._fake_server_port["test.com"] != Transport._fake_server_port[
"otherdomain.com"], "Transport should have different ports for different hosts"
@with_sftpmock({
"test.com": {"a_folder": {"file.txt": "some text"}},
})
def test_getfo_operation(self):
'''
Test if SFTPClient.getfo works as expected
'''
# Import needs to happen here because we need to use mocked Transport
from paramiko import Transport
with Transport(("test.com", 22)) as transport:
transport.connect(None, "user", "pass")
client = paramiko.SFTPClient.from_transport(transport)
outfile = BytesIO()
client.getfo("/a_folder/file.txt", outfile)
assert outfile.getvalue().decode('utf-8') == "some text"
@with_sftpmock({
"test.com": {"a_folder": {"file.txt": "some text"}},
})
def test_putfo_operation(self):
'''
Test if Connection.putfo works as expected
'''
# Import needs to happen here because we need to use mocked Transport
from paramiko import Transport
with Transport(("test.com", 22)) as transport:
transport.connect(None, "user", "pass")
client = paramiko.SFTPClient.from_transport(transport)
client.putfo(StringIO("texto aleatorio"),
"/a_folder/outro_arquivo")
outfile = BytesIO()
client.getfo("/a_folder/outro_arquivo", outfile)
assert outfile.getvalue().decode('utf-8') == "texto aleatorio"
@with_sftpmock({
"test.com": {"a_folder": {"file.txt": "some text"}, "other_folder": {}},
})
def test_listdir_operation(self):
'''
Test if Connection.putfo works as expected
'''
# Import needs to happen here because we need to use mocked Transport
from paramiko import Transport
with Transport(("test.com", 22)) as transport:
transport.connect(None, "user", "pass")
client = paramiko.SFTPClient.from_transport(transport)
records = client.listdir()
assert records == ["a_folder", "other_folder"], \
f"Expected '[a_folder, other_folder]', found {records}"
records = client.listdir("/")
assert records == ["a_folder", "other_folder"], \
f"Expected '[a_folder, other_folder]', found {records}"
records = client.listdir("/a_folder")
assert records == ["file.txt"], \
f"Expected '[file.txt]', found {records}"
records = client.listdir("/other_folder")
assert records == [], \
f"Expected '[]', found {records}"
@with_sftpmock({
"test.com": {"a_folder": {"coisa.txt": "some text"}},
"otherdomain.com": {"a_folder": {"another.txt": "another text"}}
})
def test_both_connections_work_independently(self):
'''
Test if nested connections work correctly as two separate servers
'''
# Import needs to happen here because we need to use mocked Transport
from paramiko import Transport
with Transport(("test.com", 22)) as transport:
transport.connect(None, "user", "pass")
client = paramiko.SFTPClient.from_transport(transport)
records = client.listdir("/a_folder")
assert records == ["coisa.txt"], \
f"Expected '[coisa.txt]', found {records}"
with Transport(("otherdomain.com", 22)) as transport:
transport.connect(None, "user", "pass")
other_client = paramiko.SFTPClient.from_transport(transport)
records = other_client.listdir("/a_folder")
assert records == ["another.txt"], \
f"Expected '[another.txt]', found {records}"
def test_servers_are_shutdown_normally(self):
'''
Test if servers are shutdown when execution ends normally.
We test SFTPMock directly because we need to check its variables
'''
servers_context = SFTPMock({
"test.com": {"Outbound": {"file.TXT": "some text"}},
"otherdomain.com": {"Outbound": {"another.TXT": "another text"}}
})
assert not servers_context.host_servers, "Servers should not be started yet"
with servers_context:
assert all(server.port for server in servers_context.host_servers.values(
)), "Servers should be started"
assert all(not server.is_alive() for server in servers_context.host_servers.values()), \
"Servers should be stopped"
assert all(server.socket._closed for server in servers_context.host_servers.values()), \
"All sockets should be closed"
def test_servers_are_shutdown_exception(self):
'''
Test if servers are shutdown when an exception is raised
We test SFTPMock directly because we need to check its variables
'''
servers_context = SFTPMock({
"test.com": {"Outbound": {"file.TXT": "some text"}},
"otherdomain.com": {"Outbound": {"another.TXT": "another text"}}
})
assert not servers_context.host_servers, "Servers should not be started yet"
try:
with servers_context:
assert all(server.port for server in servers_context.host_servers.values(
)), "Servers should be started"
raise Exception("Some error")
except Exception as e:
pass
assert all(not server.is_alive() for server in servers_context.host_servers.values()), \
"Servers should be stopped"
assert all(server.socket._closed for server in servers_context.host_servers.values()), \
"All sockets should be closed"
@with_sftpmock({
"test.com": {},
})
def test_init_with_string(self):
'''
Test if Transport can be initialized with a string as sock argument
'''
# Import needs to happen here because we need to use mocked Transport
from paramiko import Transport
with Transport("test.com:22") as transport:
assert transport.hostname == "localhost"
@with_sftpmock({
"test.com": {},
})
def test_init_with_tuple(self):
'''
Test if Transport can be initialized with a tuple as sock argument
'''
# Import needs to happen here because we need to use mocked Transport
from paramiko import Transport
with Transport(("test.com", 22)) as transport:
assert transport.hostname == "localhost"
@with_sftpmock({
"test.com": {},
})
@mock.patch('socket.socket', spec=socket.socket)
def test_init_with_socket(self, mock_socket):
'''
Test if Transport can be initialized with a socket as sock argument
'''
# Import needs to happen here because we need to use mocked Transport
from paramiko import Transport
mock_socket.return_value.getsockname.return_value = ("test.com", 22)
serversocket = socket.socket()
serversocket.connect(("test.com", 22))
with Transport(serversocket) as transport:
assert transport.hostname == "localhost"
@with_sftpmock({
"test.com": {},
})
def test_other_connections(self):
'''
Test if a non-mocked server is unnafected by the decorator
'''
# NOTE this test does an actual connection and tests if it fails, which it should
# This also means this test is slighly slower, due to timeout not being changeable in this case
# Import needs to happen here because we need to use mocked Transport
from paramiko import Transport
with mock.patch('socket.getaddrinfo') as mock_getaddrinfo:
try:
with Transport("example.com:22") as transport:
pass
except:
# This raises and exception because of the mocked getaddrinfo,
# but we only want to know if the connection was attempted, meaning it was not mocked
pass
mock_getaddrinfo.assert_called_once_with(
"example.com", 22, socket.AF_UNSPEC, socket.SOCK_STREAM)