4
4
import random
5
5
import shutil
6
6
import subprocess
7
+ from contextlib import AsyncExitStack
7
8
from datetime import datetime
8
9
from helpers import SetupParameters
9
10
from interderp_cli import InterDerpClient
10
11
from itertools import combinations
11
- from mesh_api import start_tcpdump , stop_tcpdump
12
12
from utils .bindings import TelioAdapterType
13
- from utils .connection import DockerConnection
14
- from utils .connection_util import (
15
- ConnectionTag ,
16
- container_id ,
17
- LAN_ADDR_MAP ,
18
- new_connection_raw ,
19
- )
13
+ from utils .connection_util import ConnectionTag , LAN_ADDR_MAP , new_connection_raw
20
14
from utils .process import ProcessExecError
21
15
from utils .router import IPStack
16
+ from utils .tcpdump import make_tcpdump
22
17
from utils .vm import windows_vm_util , mac_vm_util
23
18
24
19
DERP_SERVER_1_ADDR = "http://10.0.10.1:8765"
30
25
SETUP_CHECK_TIMEOUT_S = 30
31
26
SETUP_CHECK_RETRIES = 5
32
27
28
+ # pylint: disable=unnecessary-dunder-call
29
+ TEST_SCOPE_ASYNC_EXIT_STACK = asyncio .run (AsyncExitStack ().__aenter__ ())
30
+ # pylint: disable=unnecessary-dunder-call
31
+ SESSION_ASYNC_EXIT_STACK = asyncio .run (AsyncExitStack ().__aenter__ ())
32
+
33
33
34
34
def _cancel_all_tasks (loop : asyncio .AbstractEventLoop ):
35
35
to_cancel = asyncio .tasks .all_tasks (loop )
@@ -197,24 +197,25 @@ def pytest_make_parametrize_id(config, val):
197
197
198
198
199
199
async def setup_check_interderp ():
200
- async with new_connection_raw (ConnectionTag .DOCKER_CONE_CLIENT_1 ) as connection :
201
- if not isinstance (connection , DockerConnection ):
202
- raise Exception ("Not docker connection" )
203
- containers = [
204
- connection .container_name (),
205
- "nat-lab-derp-01-1" ,
206
- "nat-lab-derp-02-1" ,
207
- "nat-lab-derp-03-1" ,
200
+ async with AsyncExitStack () as exit_stack :
201
+ connections = [
202
+ await exit_stack .enter_async_context (new_connection_raw (conn_tag ))
203
+ for conn_tag in [
204
+ ConnectionTag .DOCKER_CONE_CLIENT_1 ,
205
+ ConnectionTag .DOCKER_DERP_1 ,
206
+ ConnectionTag .DOCKER_DERP_2 ,
207
+ ConnectionTag .DOCKER_DERP_3 ,
208
+ ]
208
209
]
209
- start_tcpdump ( containers )
210
- try :
210
+
211
+ async with make_tcpdump ( connections ) :
211
212
for idx , (server1 , server2 ) in enumerate (
212
213
combinations (
213
214
[DERP_SERVER_1_ADDR , DERP_SERVER_2_ADDR , DERP_SERVER_3_ADDR ], 2
214
215
)
215
216
):
216
217
derp_test = InterDerpClient (
217
- connection ,
218
+ connections [ 0 ] ,
218
219
server1 ,
219
220
server2 ,
220
221
DERP_SERVER_1_SECRET_KEY ,
@@ -223,8 +224,6 @@ async def setup_check_interderp():
223
224
)
224
225
await derp_test .execute ()
225
226
await derp_test .save_logs ()
226
- finally :
227
- stop_tcpdump (containers )
228
227
229
228
230
229
SETUP_CHECKS = [
@@ -405,24 +404,56 @@ def pytest_runtest_setup():
405
404
406
405
# pylint: disable=unused-argument
407
406
def pytest_runtest_call (item ):
408
- start_tcpdump ([f"nat-lab-dns-server-{ i } -1" for i in range (1 , 3 )])
407
+ if os .environ .get ("NATLAB_SAVE_LOGS" ) is None :
408
+ return
409
+
410
+ async def async_code ():
411
+ connections = [
412
+ await TEST_SCOPE_ASYNC_EXIT_STACK .enter_async_context (
413
+ new_connection_raw (conn_tag )
414
+ )
415
+ for conn_tag in [
416
+ ConnectionTag .DOCKER_DNS_SERVER_1 ,
417
+ ConnectionTag .DOCKER_DNS_SERVER_2 ,
418
+ ]
419
+ ]
420
+ await TEST_SCOPE_ASYNC_EXIT_STACK .enter_async_context (make_tcpdump (connections ))
421
+
422
+ asyncio .run (async_code ())
409
423
410
424
411
425
# pylint: disable=unused-argument
412
426
def pytest_runtest_makereport (item , call ):
427
+ if os .environ .get ("NATLAB_SAVE_LOGS" ) is None :
428
+ return
429
+
430
+ async def async_code ():
431
+ global TEST_SCOPE_ASYNC_EXIT_STACK
432
+ await TEST_SCOPE_ASYNC_EXIT_STACK .aclose ()
433
+ # pylint: disable=unnecessary-dunder-call
434
+ TEST_SCOPE_ASYNC_EXIT_STACK = await AsyncExitStack ().__aenter__ ()
435
+
413
436
if call .when == "call" :
414
- stop_tcpdump ([ f"nat-lab-dns-server- { i } -1" for i in range ( 1 , 3 )] )
437
+ asyncio . run ( async_code () )
415
438
416
439
417
440
# pylint: disable=unused-argument
418
441
def pytest_sessionstart (session ):
419
442
if os .environ .get ("NATLAB_SAVE_LOGS" ) is None :
420
443
return
421
444
445
+ async def async_code ():
446
+ connections = [
447
+ await SESSION_ASYNC_EXIT_STACK .enter_async_context (
448
+ new_connection_raw (gw_tag )
449
+ )
450
+ for gw_tag in ConnectionTag
451
+ if "_GW" in gw_tag .name
452
+ ]
453
+ await SESSION_ASYNC_EXIT_STACK .enter_async_context (make_tcpdump (connections ))
454
+
422
455
if not session .config .option .collectonly :
423
- start_tcpdump (
424
- {container_id (gw_tag ) for gw_tag in ConnectionTag if "_GW" in gw_tag .name }
425
- )
456
+ asyncio .run (async_code ())
426
457
427
458
428
459
# pylint: disable=unused-argument
@@ -431,11 +462,7 @@ def pytest_sessionfinish(session, exitstatus):
431
462
return
432
463
433
464
if not session .config .option .collectonly :
434
- stop_tcpdump (
435
- {container_id (gw_tag ) for gw_tag in ConnectionTag if "_GW" in gw_tag .name },
436
- "./logs" ,
437
- )
438
-
465
+ asyncio .run (SESSION_ASYNC_EXIT_STACK .aclose ())
439
466
collect_nordderper_logs ()
440
467
collect_dns_server_logs ()
441
468
asyncio .run (collect_kernel_logs (session .items , "after_tests" ))
0 commit comments