-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathupload_test.py
329 lines (277 loc) · 9.81 KB
/
upload_test.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Copyright 2023-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included
# in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
# in that file, in accordance with the Business Source License, use of this
# software will be governed by the Apache License, Version 2.0, included in
# the file licenses/APL2.txt.
import os
import pathlib
import ssl
import unittest.mock
import pytest
import sgcollect_info
import tasks
import trustme
ZIP_NAME = "foo.zip"
REDACTED_ZIP_NAME = "foo-redacted.zip"
@pytest.fixture
def main_norun(tmpdir):
workdir = pathlib.Path.cwd()
try:
os.chdir(tmpdir)
with unittest.mock.patch("tasks.TaskRunner.run"):
with open(ZIP_NAME, "w"):
pass
yield
finally:
os.chdir(workdir)
@pytest.fixture
def main_norun_redacted_zip(main_norun):
with open(REDACTED_ZIP_NAME, "w"):
pass
yield
class FakeResponse:
def __init__(self, status_code):
self.status_code = status_code
def getcode(self):
return self.status_code
class FakeSuccessUrlOpener:
def __init__(self, *args, **kwargs):
pass
def open(self, *args, **kwargs):
return FakeResponse(200)
class FakeFailureUrlOpener:
def __init__(self, *args, **kwargs):
pass
def open(self, *args, **kwargs):
return FakeResponse(500)
@pytest.mark.usefixtures("main_norun")
@pytest.mark.parametrize("args", [[], ["--log-redaction-level", "none"]])
def test_main_output_exists(args):
with unittest.mock.patch("sys.argv", ["sg_collect", *args, ZIP_NAME]):
sgcollect_info.main()
assert pathlib.Path(ZIP_NAME).exists()
assert not pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun_redacted_zip")
def test_main_output_exists_with_redacted():
with unittest.mock.patch(
"sys.argv", ["sg_collect", "--log-redaction-level", "partial", ZIP_NAME]
):
sgcollect_info.main()
assert pathlib.Path(ZIP_NAME).exists()
assert pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun")
@pytest.mark.parametrize("args", [[], ["--log-redaction-level", "none"]])
def test_main_zip_deleted_on_upload_success(args):
with unittest.mock.patch("tasks.urllib.request.build_opener", FakeSuccessUrlOpener):
with unittest.mock.patch(
"sys.argv",
[
"sg_collect",
*args,
"--upload-host",
"https://example.com",
"--customer",
"fakeCustomer",
ZIP_NAME,
],
):
with pytest.raises(SystemExit) as exc:
sgcollect_info.main()
assert exc.value.code == 0
assert not pathlib.Path(ZIP_NAME).exists()
assert not pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun")
@pytest.mark.parametrize("args", [[], ["--log-redaction-level", "none"]])
def test_main_zip_deleted_on_upload_failure(args):
with unittest.mock.patch("tasks.urllib.request.build_opener", FakeFailureUrlOpener):
with unittest.mock.patch(
"sys.argv",
[
"sg_collect",
*args,
"--upload-host",
"https://example.com",
"--customer",
"fakeCustomer",
ZIP_NAME,
],
):
with pytest.raises(SystemExit) as exc:
sgcollect_info.main()
assert exc.value.code == 1
assert not pathlib.Path(ZIP_NAME).exists()
assert not pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun_redacted_zip")
def test_main_redacted_zip_deleted_on_upload_success():
with unittest.mock.patch("tasks.urllib.request.build_opener", FakeSuccessUrlOpener):
with unittest.mock.patch(
"sys.argv",
[
"sg_collect",
"--log-redaction-level",
"partial",
"--upload-host",
"https://example.com",
"--customer",
"fakeCustomer",
ZIP_NAME,
],
):
with pytest.raises(SystemExit) as exc:
sgcollect_info.main()
assert exc.value.code == 0
assert not pathlib.Path(ZIP_NAME).exists()
assert not pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun_redacted_zip")
def test_main_redacted_zip_deleted_on_upload_failure():
with unittest.mock.patch("tasks.urllib.request.build_opener", FakeFailureUrlOpener):
with unittest.mock.patch(
"sys.argv",
[
"sg_collect",
"--log-redaction-level",
"partial",
"--upload-host",
"https://example.com",
"--customer",
"fakeCustomer",
ZIP_NAME,
],
):
with pytest.raises(SystemExit) as exc:
sgcollect_info.main()
assert exc.value.code == 1
assert not pathlib.Path(ZIP_NAME).exists()
assert not pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun")
@pytest.mark.parametrize("args", [[], ["--log-redaction-level", "none"]])
def test_main_keep_zip_on_upload_success(args):
with unittest.mock.patch("tasks.urllib.request.build_opener", FakeSuccessUrlOpener):
with unittest.mock.patch(
"sys.argv",
[
"sg_collect",
*args,
"--upload-host",
"https://example.com",
"--customer",
"fakeCustomer",
"--keep-zip",
ZIP_NAME,
],
):
with pytest.raises(SystemExit) as exc:
sgcollect_info.main()
assert exc.value.code == 0
assert pathlib.Path(ZIP_NAME).exists()
assert not pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun")
@pytest.mark.parametrize("args", [[], ["--log-redaction-level", "none"]])
def test_main_keep_zip_on_upload_failure(args):
with unittest.mock.patch("tasks.urllib.request.build_opener", FakeFailureUrlOpener):
with unittest.mock.patch(
"sys.argv",
[
"sg_collect",
*args,
"--upload-host",
"https://example.com",
"--customer",
"fakeCustomer",
"--keep-zip",
ZIP_NAME,
],
):
with pytest.raises(SystemExit) as exc:
sgcollect_info.main()
assert exc.value.code == 1
assert pathlib.Path(ZIP_NAME).exists()
assert not pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun_redacted_zip")
def test_main_keep_zip_deleted_on_upload_success():
with unittest.mock.patch("tasks.urllib.request.build_opener", FakeSuccessUrlOpener):
with unittest.mock.patch(
"sys.argv",
[
"sg_collect",
"--log-redaction-level",
"partial",
"--upload-host",
"https://example.com",
"--customer",
"fakeCustomer",
"--keep-zip",
ZIP_NAME,
],
):
with pytest.raises(SystemExit) as exc:
sgcollect_info.main()
assert exc.value.code == 0
assert pathlib.Path(ZIP_NAME).exists()
assert pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.mark.usefixtures("main_norun_redacted_zip")
def test_main_keep_zip_deleted_on_upload_failure():
with unittest.mock.patch("tasks.urllib.request.build_opener", FakeFailureUrlOpener):
with unittest.mock.patch(
"sys.argv",
[
"sg_collect",
"--log-redaction-level",
"partial",
"--upload-host",
"https://example.com",
"--customer",
"fakeCustomer",
"--keep-zip",
ZIP_NAME,
],
):
with pytest.raises(SystemExit) as exc:
sgcollect_info.main()
assert exc.value.code == 1
assert pathlib.Path(ZIP_NAME).exists()
assert pathlib.Path(REDACTED_ZIP_NAME).exists()
@pytest.fixture(scope="session")
def httpserver_ssl_context():
ca = trustme.CA()
client_context = ssl.SSLContext()
server_context = ssl.SSLContext()
server_cert = ca.issue_cert("test-host.example.org")
ca.configure_trust(client_context)
server_cert.configure_cert(server_context)
def default_context():
return client_context
ssl._create_default_https_context = default_context
return server_context
def test_stream_large_file(tmpdir, httpserver):
"""
Write a file greater than 2GB to make sure it does not throw an exception.
"""
p = tmpdir.join("testfile.txt")
with open(p, "wb") as f:
for i in range(2200):
f.write(os.urandom(1_000_000))
def handler(request):
pass
httpserver.expect_request("/").respond_with_handler(handler)
assert tasks.do_upload(p, httpserver.url_for("/"), "") == 0
httpserver.check()
def test_stream_file(tmpdir, httpserver):
"""
Make sure that streaming the contents of a file show up when streaming.
"""
p = tmpdir.join("testfile.txt")
body = "foobar"
p.write(body)
r = None
def handler(request):
nonlocal r
r = request
httpserver.expect_request("/").respond_with_handler(handler)
assert tasks.do_upload(p, httpserver.url_for("/"), "") == 0
httpserver.check()
assert r.headers.get("Content-Length") == "6"
assert r.headers.get("Transfer-Encoding") is None
assert r.data == body.encode()