1 | """ |
---|
2 | These tests were originally written to achieve some level of |
---|
3 | coverage for the WebAPI functionality during Python3 porting (there |
---|
4 | aren't many tests of the Web API period). |
---|
5 | |
---|
6 | Most of the tests have cursory asserts and encode 'what the WebAPI did |
---|
7 | at the time of testing' -- not necessarily a cohesive idea of what the |
---|
8 | WebAPI *should* do in every situation. It's not clear the latter |
---|
9 | exists anywhere, however. |
---|
10 | """ |
---|
11 | |
---|
12 | from __future__ import annotations |
---|
13 | |
---|
14 | import time |
---|
15 | from urllib.parse import unquote as url_unquote, quote as url_quote |
---|
16 | |
---|
17 | from twisted.internet.threads import deferToThread |
---|
18 | |
---|
19 | import allmydata.uri |
---|
20 | from allmydata.util import jsonbytes as json |
---|
21 | |
---|
22 | from . import util |
---|
23 | from .util import run_in_thread |
---|
24 | |
---|
25 | import requests |
---|
26 | import html5lib |
---|
27 | from bs4 import BeautifulSoup |
---|
28 | |
---|
29 | import pytest_twisted |
---|
30 | |
---|
31 | @run_in_thread |
---|
32 | def test_index(alice): |
---|
33 | """ |
---|
34 | we can download the index file |
---|
35 | """ |
---|
36 | util.web_get(alice.process, u"") |
---|
37 | |
---|
38 | |
---|
39 | @run_in_thread |
---|
40 | def test_index_json(alice): |
---|
41 | """ |
---|
42 | we can download the index file as json |
---|
43 | """ |
---|
44 | data = util.web_get(alice.process, u"", params={u"t": u"json"}) |
---|
45 | # it should be valid json |
---|
46 | json.loads(data) |
---|
47 | |
---|
48 | |
---|
49 | @run_in_thread |
---|
50 | def test_upload_download(alice): |
---|
51 | """ |
---|
52 | upload a file, then download it via readcap |
---|
53 | """ |
---|
54 | |
---|
55 | FILE_CONTENTS = u"some contents" |
---|
56 | |
---|
57 | readcap = util.web_post( |
---|
58 | alice.process, u"uri", |
---|
59 | data={ |
---|
60 | u"t": u"upload", |
---|
61 | u"format": u"mdmf", |
---|
62 | }, |
---|
63 | files={ |
---|
64 | u"file": FILE_CONTENTS, |
---|
65 | }, |
---|
66 | ) |
---|
67 | readcap = readcap.strip() |
---|
68 | |
---|
69 | data = util.web_get( |
---|
70 | alice.process, u"uri", |
---|
71 | params={ |
---|
72 | u"uri": readcap, |
---|
73 | u"filename": u"boom", |
---|
74 | } |
---|
75 | ) |
---|
76 | assert str(data, "utf-8") == FILE_CONTENTS |
---|
77 | |
---|
78 | |
---|
79 | @run_in_thread |
---|
80 | def test_put(alice): |
---|
81 | """ |
---|
82 | use PUT to create a file |
---|
83 | """ |
---|
84 | |
---|
85 | FILE_CONTENTS = b"added via PUT" * 20 |
---|
86 | |
---|
87 | resp = requests.put( |
---|
88 | util.node_url(alice.process.node_dir, u"uri"), |
---|
89 | data=FILE_CONTENTS, |
---|
90 | ) |
---|
91 | cap = allmydata.uri.from_string(resp.text.strip().encode('ascii')) |
---|
92 | cfg = alice.process.get_config() |
---|
93 | assert isinstance(cap, allmydata.uri.CHKFileURI) |
---|
94 | assert cap.size == len(FILE_CONTENTS) |
---|
95 | assert cap.total_shares == int(cfg.get_config("client", "shares.total")) |
---|
96 | assert cap.needed_shares == int(cfg.get_config("client", "shares.needed")) |
---|
97 | |
---|
98 | |
---|
99 | @run_in_thread |
---|
100 | def test_helper_status(storage_nodes): |
---|
101 | """ |
---|
102 | successfully GET the /helper_status page |
---|
103 | """ |
---|
104 | |
---|
105 | url = util.node_url(storage_nodes[0].process.node_dir, "helper_status") |
---|
106 | resp = requests.get(url) |
---|
107 | assert resp.status_code >= 200 and resp.status_code < 300 |
---|
108 | dom = BeautifulSoup(resp.content, "html5lib") |
---|
109 | assert str(dom.h1.string) == u"Helper Status" |
---|
110 | |
---|
111 | |
---|
112 | @run_in_thread |
---|
113 | def test_deep_stats(alice): |
---|
114 | """ |
---|
115 | create a directory, do deep-stats on it and prove the /operations/ |
---|
116 | URIs work |
---|
117 | """ |
---|
118 | resp = requests.post( |
---|
119 | util.node_url(alice.process.node_dir, "uri"), |
---|
120 | params={ |
---|
121 | "format": "sdmf", |
---|
122 | "t": "mkdir", |
---|
123 | "redirect_to_result": "true", |
---|
124 | }, |
---|
125 | ) |
---|
126 | assert resp.status_code >= 200 and resp.status_code < 300 |
---|
127 | |
---|
128 | # when creating a directory, we'll be re-directed to a URL |
---|
129 | # containing our writecap.. |
---|
130 | uri = url_unquote(resp.url) |
---|
131 | assert 'URI:DIR2:' in uri |
---|
132 | dircap = uri[uri.find("URI:DIR2:"):].rstrip('/') |
---|
133 | dircap_uri = util.node_url(alice.process.node_dir, "uri/{}".format(url_quote(dircap))) |
---|
134 | |
---|
135 | # POST a file into this directory |
---|
136 | FILE_CONTENTS = u"a file in a directory" |
---|
137 | |
---|
138 | resp = requests.post( |
---|
139 | dircap_uri, |
---|
140 | data={ |
---|
141 | u"t": u"upload", |
---|
142 | }, |
---|
143 | files={ |
---|
144 | u"file": FILE_CONTENTS, |
---|
145 | }, |
---|
146 | ) |
---|
147 | resp.raise_for_status() |
---|
148 | |
---|
149 | # confirm the file is in the directory |
---|
150 | resp = requests.get( |
---|
151 | dircap_uri, |
---|
152 | params={ |
---|
153 | u"t": u"json", |
---|
154 | }, |
---|
155 | ) |
---|
156 | d = json.loads(resp.content) |
---|
157 | k, data = d |
---|
158 | assert k == u"dirnode" |
---|
159 | assert len(data['children']) == 1 |
---|
160 | k, child = list(data['children'].values())[0] |
---|
161 | assert k == u"filenode" |
---|
162 | assert child['size'] == len(FILE_CONTENTS) |
---|
163 | |
---|
164 | # perform deep-stats on it... |
---|
165 | resp = requests.post( |
---|
166 | dircap_uri, |
---|
167 | data={ |
---|
168 | u"t": u"start-deep-stats", |
---|
169 | u"ophandle": u"something_random", |
---|
170 | }, |
---|
171 | ) |
---|
172 | assert resp.status_code >= 200 and resp.status_code < 300 |
---|
173 | |
---|
174 | # confirm we get information from the op .. after its done |
---|
175 | tries = 10 |
---|
176 | while tries > 0: |
---|
177 | tries -= 1 |
---|
178 | resp = requests.get( |
---|
179 | util.node_url(alice.process.node_dir, u"operations/something_random"), |
---|
180 | ) |
---|
181 | d = json.loads(resp.content) |
---|
182 | if d['size-literal-files'] == len(FILE_CONTENTS): |
---|
183 | print("stats completed successfully") |
---|
184 | break |
---|
185 | else: |
---|
186 | print("{} != {}; waiting".format(d['size-literal-files'], len(FILE_CONTENTS))) |
---|
187 | time.sleep(.5) |
---|
188 | |
---|
189 | |
---|
190 | @run_in_thread |
---|
191 | def test_status(alice): |
---|
192 | """ |
---|
193 | confirm we get something sensible from /status and the various sub-types |
---|
194 | """ |
---|
195 | |
---|
196 | # upload a file |
---|
197 | # (because of the nature of the integration-tests, we can only |
---|
198 | # assert things about "our" file because we don't know what other |
---|
199 | # operations may have happened in the grid before our test runs). |
---|
200 | |
---|
201 | FILE_CONTENTS = u"all the Important Data of alice\n" * 1200 |
---|
202 | |
---|
203 | resp = requests.put( |
---|
204 | util.node_url(alice.process.node_dir, u"uri"), |
---|
205 | data=FILE_CONTENTS, |
---|
206 | ) |
---|
207 | cap = resp.text.strip() |
---|
208 | |
---|
209 | print("Uploaded data, cap={}".format(cap)) |
---|
210 | resp = requests.get( |
---|
211 | util.node_url(alice.process.node_dir, u"uri/{}".format(url_quote(cap))), |
---|
212 | ) |
---|
213 | |
---|
214 | print("Downloaded {} bytes of data".format(len(resp.content))) |
---|
215 | assert str(resp.content, "ascii") == FILE_CONTENTS |
---|
216 | |
---|
217 | resp = requests.get( |
---|
218 | util.node_url(alice.process.node_dir, "status"), |
---|
219 | ) |
---|
220 | dom = html5lib.parse(resp.content) |
---|
221 | |
---|
222 | hrefs = [ |
---|
223 | a.get('href') |
---|
224 | for a in dom.iter(u'{http://www.w3.org/1999/xhtml}a') |
---|
225 | ] |
---|
226 | |
---|
227 | found_upload = False |
---|
228 | found_download = False |
---|
229 | for href in hrefs: |
---|
230 | if href == u"/" or not href: |
---|
231 | continue |
---|
232 | resp = requests.get(util.node_url(alice.process.node_dir, href)) |
---|
233 | if href.startswith(u"/status/up"): |
---|
234 | assert b"File Upload Status" in resp.content |
---|
235 | if b"Total Size: %d" % (len(FILE_CONTENTS),) in resp.content: |
---|
236 | found_upload = True |
---|
237 | elif href.startswith(u"/status/down"): |
---|
238 | assert b"File Download Status" in resp.content |
---|
239 | if b"Total Size: %d" % (len(FILE_CONTENTS),) in resp.content: |
---|
240 | found_download = True |
---|
241 | |
---|
242 | # download the specialized event information |
---|
243 | resp = requests.get( |
---|
244 | util.node_url(alice.process.node_dir, u"{}/event_json".format(href)), |
---|
245 | ) |
---|
246 | js = json.loads(resp.content) |
---|
247 | # there's usually just one "read" operation, but this can handle many .. |
---|
248 | total_bytes = sum([st['bytes_returned'] for st in js['read']], 0) |
---|
249 | assert total_bytes == len(FILE_CONTENTS) |
---|
250 | |
---|
251 | |
---|
252 | assert found_upload, "Failed to find the file we uploaded in the status-page" |
---|
253 | assert found_download, "Failed to find the file we downloaded in the status-page" |
---|
254 | |
---|
255 | |
---|
256 | @pytest_twisted.ensureDeferred |
---|
257 | async def test_directory_deep_check(reactor, request, alice): |
---|
258 | """ |
---|
259 | use deep-check and confirm the result pages work |
---|
260 | """ |
---|
261 | # Make sure the node is configured compatibly with expectations of this |
---|
262 | # test. |
---|
263 | happy = 3 |
---|
264 | required = 2 |
---|
265 | total = 4 |
---|
266 | |
---|
267 | await alice.reconfigure_zfec(reactor, (happy, required, total), convergence=None) |
---|
268 | await deferToThread(_test_directory_deep_check_blocking, alice) |
---|
269 | |
---|
270 | |
---|
271 | def _test_directory_deep_check_blocking(alice): |
---|
272 | # create a directory |
---|
273 | resp = requests.post( |
---|
274 | util.node_url(alice.process.node_dir, u"uri"), |
---|
275 | params={ |
---|
276 | u"t": u"mkdir", |
---|
277 | u"redirect_to_result": u"true", |
---|
278 | } |
---|
279 | ) |
---|
280 | |
---|
281 | # get json information about our directory |
---|
282 | dircap_url = resp.url |
---|
283 | resp = requests.get( |
---|
284 | dircap_url, |
---|
285 | params={u"t": u"json"}, |
---|
286 | ) |
---|
287 | # Just verify it is valid JSON. |
---|
288 | json.loads(resp.content) |
---|
289 | |
---|
290 | # upload a file of pangrams into the directory |
---|
291 | FILE_CONTENTS = u"Sphinx of black quartz, judge my vow.\n" * (2048*10) |
---|
292 | |
---|
293 | resp = requests.post( |
---|
294 | dircap_url, |
---|
295 | params={ |
---|
296 | u"t": u"upload", |
---|
297 | u"upload-chk": u"upload-chk", |
---|
298 | }, |
---|
299 | files={ |
---|
300 | u"file": FILE_CONTENTS, |
---|
301 | } |
---|
302 | ) |
---|
303 | cap0 = resp.content |
---|
304 | print("Uploaded data0, cap={}".format(cap0)) |
---|
305 | |
---|
306 | # a different pangram |
---|
307 | FILE_CONTENTS = u"The five boxing wizards jump quickly.\n" * (2048*10) |
---|
308 | |
---|
309 | resp = requests.post( |
---|
310 | dircap_url, |
---|
311 | params={ |
---|
312 | u"t": u"upload", |
---|
313 | u"upload-chk": u"upload-chk", |
---|
314 | }, |
---|
315 | files={ |
---|
316 | u"file": FILE_CONTENTS, |
---|
317 | } |
---|
318 | ) |
---|
319 | cap1 = resp.content |
---|
320 | print("Uploaded data1, cap={}".format(cap1)) |
---|
321 | |
---|
322 | resp = requests.get( |
---|
323 | util.node_url(alice.process.node_dir, u"uri/{}".format(url_quote(cap0))), |
---|
324 | params={u"t": u"info"}, |
---|
325 | ) |
---|
326 | |
---|
327 | def check_repair_data(checkdata): |
---|
328 | assert checkdata["healthy"] |
---|
329 | assert checkdata["count-happiness"] == 4 |
---|
330 | assert checkdata["count-good-share-hosts"] == 4 |
---|
331 | assert checkdata["count-shares-good"] == 4 |
---|
332 | assert checkdata["count-corrupt-shares"] == 0 |
---|
333 | assert checkdata["list-corrupt-shares"] == [] |
---|
334 | |
---|
335 | # do a "check" (once for HTML, then with JSON for easier asserts) |
---|
336 | resp = requests.post( |
---|
337 | dircap_url, |
---|
338 | params={ |
---|
339 | u"t": u"check", |
---|
340 | u"return_to": u".", |
---|
341 | u"verify": u"true", |
---|
342 | } |
---|
343 | ) |
---|
344 | resp = requests.post( |
---|
345 | dircap_url, |
---|
346 | params={ |
---|
347 | u"t": u"check", |
---|
348 | u"return_to": u".", |
---|
349 | u"verify": u"true", |
---|
350 | u"output": u"JSON", |
---|
351 | } |
---|
352 | ) |
---|
353 | check_repair_data(json.loads(resp.content)["results"]) |
---|
354 | |
---|
355 | # "check and repair" |
---|
356 | resp = requests.post( |
---|
357 | dircap_url, |
---|
358 | params={ |
---|
359 | u"t": u"check", |
---|
360 | u"return_to": u".", |
---|
361 | u"verify": u"true", |
---|
362 | u"repair": u"true", |
---|
363 | } |
---|
364 | ) |
---|
365 | resp = requests.post( |
---|
366 | dircap_url, |
---|
367 | params={ |
---|
368 | u"t": u"check", |
---|
369 | u"return_to": u".", |
---|
370 | u"verify": u"true", |
---|
371 | u"repair": u"true", |
---|
372 | u"output": u"JSON", |
---|
373 | } |
---|
374 | ) |
---|
375 | check_repair_data(json.loads(resp.content)["post-repair-results"]["results"]) |
---|
376 | |
---|
377 | # start a "deep check and repair" |
---|
378 | resp = requests.post( |
---|
379 | dircap_url, |
---|
380 | params={ |
---|
381 | u"t": u"start-deep-check", |
---|
382 | u"return_to": u".", |
---|
383 | u"verify": u"on", |
---|
384 | u"repair": u"on", |
---|
385 | u"output": u"JSON", |
---|
386 | u"ophandle": u"deadbeef", |
---|
387 | } |
---|
388 | ) |
---|
389 | deepcheck_uri = resp.url |
---|
390 | |
---|
391 | data = json.loads(resp.content) |
---|
392 | tries = 10 |
---|
393 | while not data['finished'] and tries > 0: |
---|
394 | tries -= 1 |
---|
395 | time.sleep(0.5) |
---|
396 | print("deep-check not finished, reloading") |
---|
397 | resp = requests.get(deepcheck_uri, params={u"output": "JSON"}) |
---|
398 | data = json.loads(resp.content) |
---|
399 | print("deep-check finished") |
---|
400 | assert data[u"stats"][u"count-immutable-files"] == 1 |
---|
401 | assert data[u"stats"][u"count-literal-files"] == 0 |
---|
402 | assert data[u"stats"][u"largest-immutable-file"] == 778240 |
---|
403 | assert data[u"count-objects-checked"] == 2 |
---|
404 | |
---|
405 | # also get the HTML version |
---|
406 | resp = requests.post( |
---|
407 | dircap_url, |
---|
408 | params={ |
---|
409 | u"t": u"start-deep-check", |
---|
410 | u"return_to": u".", |
---|
411 | u"verify": u"on", |
---|
412 | u"repair": u"on", |
---|
413 | u"ophandle": u"definitely_random", |
---|
414 | } |
---|
415 | ) |
---|
416 | deepcheck_uri = resp.url |
---|
417 | |
---|
418 | # if the operations isn't done, there's an <H2> tag with the |
---|
419 | # reload link; otherwise there's only an <H1> tag..wait up to 5 |
---|
420 | # seconds for this to respond properly. |
---|
421 | for _ in range(5): |
---|
422 | resp = requests.get(deepcheck_uri) |
---|
423 | dom = BeautifulSoup(resp.content, "html5lib") |
---|
424 | if dom.h1 and u'Results' in str(dom.h1.string): |
---|
425 | break |
---|
426 | if dom.h2 and dom.h2.a and u"Reload" in str(dom.h2.a.string): |
---|
427 | dom = None |
---|
428 | time.sleep(1) |
---|
429 | assert dom is not None, "Operation never completed" |
---|
430 | |
---|
431 | |
---|
432 | @run_in_thread |
---|
433 | def test_storage_info(storage_nodes): |
---|
434 | """ |
---|
435 | retrieve and confirm /storage URI for one storage node |
---|
436 | """ |
---|
437 | storage0 = storage_nodes[0] |
---|
438 | |
---|
439 | requests.get( |
---|
440 | util.node_url(storage0.process.node_dir, u"storage"), |
---|
441 | ) |
---|
442 | |
---|
443 | |
---|
444 | @run_in_thread |
---|
445 | def test_storage_info_json(storage_nodes): |
---|
446 | """ |
---|
447 | retrieve and confirm /storage?t=json URI for one storage node |
---|
448 | """ |
---|
449 | storage0 = storage_nodes[0] |
---|
450 | |
---|
451 | resp = requests.get( |
---|
452 | util.node_url(storage0.process.node_dir, u"storage"), |
---|
453 | params={u"t": u"json"}, |
---|
454 | ) |
---|
455 | data = json.loads(resp.content) |
---|
456 | assert data[u"stats"][u"storage_server.reserved_space"] == 1000000000 |
---|
457 | |
---|
458 | |
---|
459 | @run_in_thread |
---|
460 | def test_introducer_info(introducer): |
---|
461 | """ |
---|
462 | retrieve and confirm /introducer URI for the introducer |
---|
463 | """ |
---|
464 | resp = requests.get( |
---|
465 | util.node_url(introducer.process.node_dir, u""), |
---|
466 | ) |
---|
467 | assert b"Introducer" in resp.content |
---|
468 | |
---|
469 | resp = requests.get( |
---|
470 | util.node_url(introducer.process.node_dir, u""), |
---|
471 | params={u"t": u"json"}, |
---|
472 | ) |
---|
473 | data = json.loads(resp.content) |
---|
474 | assert "announcement_summary" in data |
---|
475 | assert "subscription_summary" in data |
---|
476 | |
---|
477 | |
---|
478 | @run_in_thread |
---|
479 | def test_mkdir_with_children(alice): |
---|
480 | """ |
---|
481 | create a directory using ?t=mkdir-with-children |
---|
482 | """ |
---|
483 | |
---|
484 | # create a file to put in our directory |
---|
485 | FILE_CONTENTS = u"some file contents\n" * 500 |
---|
486 | resp = requests.put( |
---|
487 | util.node_url(alice.process.node_dir, u"uri"), |
---|
488 | data=FILE_CONTENTS, |
---|
489 | ) |
---|
490 | filecap = resp.content.strip() |
---|
491 | |
---|
492 | # create a (sub) directory to put in our directory |
---|
493 | resp = requests.post( |
---|
494 | util.node_url(alice.process.node_dir, u"uri"), |
---|
495 | params={ |
---|
496 | u"t": u"mkdir", |
---|
497 | } |
---|
498 | ) |
---|
499 | # (we need both the read-write and read-only URIs I guess) |
---|
500 | dircap = resp.content |
---|
501 | dircap_obj = allmydata.uri.from_string(dircap) |
---|
502 | dircap_ro = dircap_obj.get_readonly().to_string() |
---|
503 | |
---|
504 | # create json information about our directory |
---|
505 | meta = { |
---|
506 | "a_file": [ |
---|
507 | "filenode", { |
---|
508 | "ro_uri": filecap, |
---|
509 | "metadata": { |
---|
510 | "ctime": 1202777696.7564139, |
---|
511 | "mtime": 1202777696.7564139, |
---|
512 | "tahoe": { |
---|
513 | "linkcrtime": 1202777696.7564139, |
---|
514 | "linkmotime": 1202777696.7564139 |
---|
515 | } |
---|
516 | } |
---|
517 | } |
---|
518 | ], |
---|
519 | "some_subdir": [ |
---|
520 | "dirnode", { |
---|
521 | "rw_uri": dircap, |
---|
522 | "ro_uri": dircap_ro, |
---|
523 | "metadata": { |
---|
524 | "ctime": 1202778102.7589991, |
---|
525 | "mtime": 1202778111.2160511, |
---|
526 | "tahoe": { |
---|
527 | "linkcrtime": 1202777696.7564139, |
---|
528 | "linkmotime": 1202777696.7564139 |
---|
529 | } |
---|
530 | } |
---|
531 | } |
---|
532 | ] |
---|
533 | } |
---|
534 | |
---|
535 | # create a new directory with one file and one sub-dir (all-at-once) |
---|
536 | resp = util.web_post( |
---|
537 | alice.process, u"uri", |
---|
538 | params={u"t": "mkdir-with-children"}, |
---|
539 | data=json.dumps(meta), |
---|
540 | ) |
---|
541 | assert resp.startswith(b"URI:DIR2") |
---|
542 | cap = allmydata.uri.from_string(resp) |
---|
543 | assert isinstance(cap, allmydata.uri.DirectoryURI) |
---|