11import aiohttp
22import pytest
3+ from aiohttp import web
34from aioresponses import aioresponses
45from aleph_message .models import ItemHash
56
@@ -23,6 +24,7 @@ async def test_notify_allocation():
2324 assert m .requests
2425 await vm_client .session .close ()
2526
27+
2628@pytest .mark .asyncio
2729async def test_perform_operation ():
2830 account = ETHAccount (private_key = b"0x" + b"1" * 30 )
@@ -35,13 +37,18 @@ async def test_perform_operation():
3537 node_url = "http://localhost" ,
3638 session = aiohttp .ClientSession (),
3739 )
38- m .post (f"http://localhost/control/machine/{ vm_id } /{ operation } " , status = 200 , payload = "mock_response_text" )
40+ m .post (
41+ f"http://localhost/control/machine/{ vm_id } /{ operation } " ,
42+ status = 200 ,
43+ payload = "mock_response_text" ,
44+ )
3945
4046 status , response_text = await vm_client .perform_operation (vm_id , operation )
4147 assert status == 200
42- assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
48+ assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
4349 await vm_client .session .close ()
4450
51+
4552@pytest .mark .asyncio
4653async def test_stop_instance ():
4754 account = ETHAccount (private_key = b"0x" + b"1" * 30 )
@@ -53,13 +60,18 @@ async def test_stop_instance():
5360 node_url = "http://localhost" ,
5461 session = aiohttp .ClientSession (),
5562 )
56- m .post (f"http://localhost/control/machine/{ vm_id } /stop" , status = 200 , payload = "mock_response_text" )
63+ m .post (
64+ f"http://localhost/control/machine/{ vm_id } /stop" ,
65+ status = 200 ,
66+ payload = "mock_response_text" ,
67+ )
5768
5869 status , response_text = await vm_client .stop_instance (vm_id )
5970 assert status == 200
60- assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
71+ assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
6172 await vm_client .session .close ()
6273
74+
6375@pytest .mark .asyncio
6476async def test_reboot_instance ():
6577 account = ETHAccount (private_key = b"0x" + b"1" * 30 )
@@ -71,13 +83,18 @@ async def test_reboot_instance():
7183 node_url = "http://localhost" ,
7284 session = aiohttp .ClientSession (),
7385 )
74- m .post (f"http://localhost/control/machine/{ vm_id } /reboot" , status = 200 , payload = "mock_response_text" )
86+ m .post (
87+ f"http://localhost/control/machine/{ vm_id } /reboot" ,
88+ status = 200 ,
89+ payload = "mock_response_text" ,
90+ )
7591
7692 status , response_text = await vm_client .reboot_instance (vm_id )
7793 assert status == 200
78- assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
94+ assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
7995 await vm_client .session .close ()
8096
97+
8198@pytest .mark .asyncio
8299async def test_erase_instance ():
83100 account = ETHAccount (private_key = b"0x" + b"1" * 30 )
@@ -89,13 +106,18 @@ async def test_erase_instance():
89106 node_url = "http://localhost" ,
90107 session = aiohttp .ClientSession (),
91108 )
92- m .post (f"http://localhost/control/machine/{ vm_id } /erase" , status = 200 , payload = "mock_response_text" )
109+ m .post (
110+ f"http://localhost/control/machine/{ vm_id } /erase" ,
111+ status = 200 ,
112+ payload = "mock_response_text" ,
113+ )
93114
94115 status , response_text = await vm_client .erase_instance (vm_id )
95116 assert status == 200
96- assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
117+ assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
97118 await vm_client .session .close ()
98119
120+
99121@pytest .mark .asyncio
100122async def test_expire_instance ():
101123 account = ETHAccount (private_key = b"0x" + b"1" * 30 )
@@ -107,9 +129,51 @@ async def test_expire_instance():
107129 node_url = "http://localhost" ,
108130 session = aiohttp .ClientSession (),
109131 )
110- m .post (f"http://localhost/control/machine/{ vm_id } /expire" , status = 200 , payload = "mock_response_text" )
132+ m .post (
133+ f"http://localhost/control/machine/{ vm_id } /expire" ,
134+ status = 200 ,
135+ payload = "mock_response_text" ,
136+ )
111137
112138 status , response_text = await vm_client .expire_instance (vm_id )
113139 assert status == 200
114- assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
115- await vm_client .session .close ()
140+ assert response_text == '"mock_response_text"' # ' ' cause by aioresponses
141+ await vm_client .session .close ()
142+
143+
144+ @pytest .mark .asyncio
145+ async def test_get_logs (aiohttp_client ):
146+ account = ETHAccount (private_key = b"0x" + b"1" * 30 )
147+ vm_id = ItemHash ("cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe" )
148+
149+ async def websocket_handler (request ):
150+ ws = web .WebSocketResponse ()
151+ await ws .prepare (request )
152+
153+ async for msg in ws :
154+ if msg .type == aiohttp .WSMsgType .TEXT :
155+ await ws .send_str ("mock_log_entry" )
156+ elif msg .type == aiohttp .WSMsgType .ERROR :
157+ break
158+
159+ return ws
160+
161+ app = web .Application ()
162+ app .router .add_route ("GET" , "/logs/{vm_id}" , websocket_handler )
163+
164+ client = await aiohttp_client (app )
165+
166+ vm_client = VmClient (
167+ account = account ,
168+ node_url = str (client .make_url ("/" )).rstrip ("/" ),
169+ session = client .session ,
170+ )
171+
172+ logs = []
173+ async for log in vm_client .get_logs (vm_id ):
174+ logs .append (log )
175+ if log == "mock_log_entry" :
176+ break
177+
178+ assert logs == ["mock_log_entry" ]
179+ await vm_client .session .close ()
0 commit comments