|
1 | 1 | import os |
2 | 2 | import unittest |
3 | 3 | from io import BytesIO |
4 | | -from time import sleep |
| 4 | + |
| 5 | +import aiofiles |
5 | 6 |
|
6 | 7 | import pyunicore.aio.client as uc_client |
7 | 8 | import pyunicore.credentials as uc_credentials |
8 | 9 |
|
9 | 10 |
|
10 | | -class TestBasic(unittest.IsolatedAsyncioTestCase): |
| 11 | +class TestAsyncStorage(unittest.IsolatedAsyncioTestCase): |
11 | 12 | def setUp(self): |
12 | 13 | pass |
13 | 14 |
|
@@ -36,57 +37,66 @@ async def test_list_storages(self): |
36 | 37 | home = s |
37 | 38 | break |
38 | 39 | self.assertIsNotNone(home) |
39 | | - home.listdir() |
40 | | - home.listdir(".") |
41 | | - home.listdir("/") |
| 40 | + await home.listdir() |
| 41 | + await home.listdir(".") |
| 42 | + await home.listdir("/") |
42 | 43 |
|
43 | | - def x_test_upload_download(self): |
| 44 | + async def test_upload_download(self): |
44 | 45 | print("*** test_upload_download") |
45 | | - home = self.get_home_storage() |
46 | | - _path = "tests/integration/files/script.sh" |
47 | | - _length = os.stat(_path).st_size |
48 | | - with open(_path, "rb") as f: |
49 | | - home.put(f, "script.sh") |
50 | | - remote_file = home.stat("script.sh") |
51 | | - self.assertEqual(_length, int(remote_file.properties["size"])) |
52 | | - _out = BytesIO() |
53 | | - remote_file.download(_out) |
54 | | - self.assertEqual(_length, len(str(_out.getvalue(), "UTF-8"))) |
| 46 | + async with self.get_home_storage() as home: |
| 47 | + _path = "tests/integration/files/script.sh" |
| 48 | + _length = os.stat(_path).st_size |
| 49 | + async with aiofiles.open(_path, "rb") as f: |
| 50 | + await home.put(f, "script.sh") |
| 51 | + remote_file = await home.stat("script.sh") |
| 52 | + self.assertEqual(_length, await remote_file.size) |
| 53 | + _out = BytesIO() |
| 54 | + await remote_file.download(_out) |
| 55 | + self.assertEqual(_length, len(str(_out.getvalue(), "UTF-8"))) |
55 | 56 |
|
56 | | - def x_test_upload_download_data(self): |
| 57 | + async def test_upload_download_data(self): |
57 | 58 | print("*** test_upload_download_data") |
58 | | - home = self.get_home_storage() |
59 | | - _data = "this is some test data" |
60 | | - _length = len(_data) |
61 | | - home.put(_data, "test.txt") |
62 | | - remote_file = home.stat("test.txt") |
63 | | - self.assertEqual(_length, int(remote_file.properties["size"])) |
64 | | - _out = BytesIO() |
65 | | - remote_file.download(_out) |
66 | | - self.assertEqual(_length, len(str(_out.getvalue(), "UTF-8"))) |
| 59 | + async with self.get_home_storage() as home: |
| 60 | + _data = "this is some test data" |
| 61 | + _length = len(_data) |
| 62 | + await home.put(_data, "test.txt") |
| 63 | + remote_file = await home.stat("test.txt") |
| 64 | + self.assertEqual(_length, await remote_file.size) |
| 65 | + _out = BytesIO() |
| 66 | + await remote_file.download(_out) |
| 67 | + self.assertEqual(_length, len(str(_out.getvalue(), "UTF-8"))) |
67 | 68 |
|
68 | | - def x_test_transfer(self): |
| 69 | + async def test_transfer(self): |
69 | 70 | print("*** test_transfer") |
70 | | - storage1 = self.get_home_storage() |
71 | | - _path = "tests/integration/files/script.sh" |
72 | | - _length = os.stat(_path).st_size |
73 | | - with open(_path, "rb") as f: |
74 | | - storage1.put(f, "script.sh") |
75 | | - site_client = self.get_client() |
76 | | - storage2 = site_client.new_job({}).working_dir |
77 | | - transfer = storage2.receive_file(storage1.resource_url + "/files/script.sh", "script.sh") |
78 | | - print(transfer) |
79 | | - while transfer.is_running(): |
80 | | - sleep(2) |
81 | | - print("Transferred bytes: %s" % transfer.properties["transferredBytes"]) |
82 | | - self.assertEqual(_length, int(transfer.properties["transferredBytes"])) |
83 | | - transfer2 = storage1.send_file("script.sh", storage2.resource_url + "/files/script2.sh") |
84 | | - print(transfer2) |
85 | | - transfer2.poll() |
86 | | - print("Transferred bytes: %s" % transfer2.properties["transferredBytes"]) |
87 | | - self.assertEqual(_length, int(transfer2.properties["transferredBytes"])) |
88 | | - for t in site_client.get_transfers(): |
89 | | - print(t) |
| 71 | + async with self.get_home_storage() as storage1: |
| 72 | + _path = "tests/integration/files/script.sh" |
| 73 | + _length = os.stat(_path).st_size |
| 74 | + async with aiofiles.open(_path, "rb") as f: |
| 75 | + await storage1.put(f, "script.sh") |
| 76 | + async with self.get_client() as site_client: |
| 77 | + j = await site_client.new_job({}) |
| 78 | + storage2 = await j.working_dir |
| 79 | + await storage2._wait_until_ready() |
| 80 | + transfer = await storage2.receive_file( |
| 81 | + storage1.resource_url + "/files/script.sh", "script.sh" |
| 82 | + ) |
| 83 | + print(transfer) |
| 84 | + await transfer.poll() |
| 85 | + self.assertFalse(await transfer.is_running) |
| 86 | + n = await transfer.transferred_bytes |
| 87 | + print("Transferred bytes: %s" % n) |
| 88 | + self.assertEqual(_length, n) |
| 89 | + |
| 90 | + transfer2 = await storage1.send_file( |
| 91 | + "script.sh", storage2.resource_url + "/files/script2.sh" |
| 92 | + ) |
| 93 | + print(transfer2) |
| 94 | + await transfer2.poll() |
| 95 | + n2 = await transfer2.transferred_bytes |
| 96 | + print("Transferred bytes: %s" % n2) |
| 97 | + self.assertEqual(_length, n2) |
| 98 | + for t in await site_client.get_transfers(): |
| 99 | + print(t) |
90 | 100 |
|
91 | 101 |
|
92 | 102 | if __name__ == "__main__": |
|
0 commit comments