Skip to content

Commit 5b33aea

Browse files
authored
Merge pull request #967 from NordSecurity/collect-tcpdumps-from-gateways
Collect tcpdumps from gateways
2 parents 49c1a34 + 6a3708c commit 5b33aea

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

.unreleased/collect-tcpdumps-from-gateways

Whitespace-only changes.

nat-lab/tests/conftest.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
from mesh_api import start_tcpdump, stop_tcpdump
1010
from utils.bindings import TelioAdapterType
1111
from utils.connection import DockerConnection
12-
from utils.connection_util import ConnectionTag, LAN_ADDR_MAP, new_connection_raw
12+
from utils.connection_util import (
13+
ConnectionTag,
14+
container_id,
15+
DOCKER_GW_MAP,
16+
LAN_ADDR_MAP,
17+
new_connection_raw,
18+
)
1319
from utils.process import ProcessExecError
1420
from utils.router import IPStack
1521
from utils.vm import windows_vm_util, mac_vm_util
@@ -321,12 +327,25 @@ def pytest_runtest_makereport(item, call):
321327
stop_tcpdump([f"nat-lab-dns-server-{i}-1" for i in range(1, 3)])
322328

323329

330+
# pylint: disable=unused-argument
331+
def pytest_sessionstart(session):
332+
if os.environ.get("NATLAB_SAVE_LOGS") is None:
333+
return
334+
335+
if not session.config.option.collectonly:
336+
start_tcpdump({container_id(gw_tag) for gw_tag in DOCKER_GW_MAP.values()})
337+
338+
324339
# pylint: disable=unused-argument
325340
def pytest_sessionfinish(session, exitstatus):
326341
if os.environ.get("NATLAB_SAVE_LOGS") is None:
327342
return
328343

329344
if not session.config.option.collectonly:
345+
stop_tcpdump(
346+
{container_id(gw_tag) for gw_tag in DOCKER_GW_MAP.values()}, "./logs"
347+
)
348+
330349
collect_nordderper_logs()
331350
collect_dns_server_logs()
332351
asyncio.run(collect_kernel_logs(session.items, "after_tests"))

nat-lab/tests/mesh_api.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import random
66
import time
77
import uuid
8+
from collections.abc import Iterable
9+
from concurrent.futures import ThreadPoolExecutor
810
from config import DERP_SERVERS, LIBTELIO_IPV6_WG_SUBNET, WG_SERVERS
911
from datetime import datetime
1012
from ipaddress import ip_address
@@ -444,10 +446,11 @@ def config_dynamic_nodes(
444446
return tuple(list(self.nodes.values())[current_node_list_len:])
445447

446448

447-
def start_tcpdump(container_names: List[str]):
449+
def start_tcpdump(container_names: Iterable[str]):
448450
if os.environ.get("NATLAB_SAVE_LOGS") is None:
449451
return
450-
for container_name in container_names:
452+
453+
def aux(container_name):
451454
# First make sure that no leftover processes/files will interfere
452455
cmd = f"docker exec --privileged {container_name} killall -w tcpdump"
453456
os.system(cmd)
@@ -456,22 +459,31 @@ def start_tcpdump(container_names: List[str]):
456459
cmd = f"docker exec -d --privileged {container_name} tcpdump -i any -U -w {PCAP_FILE_PATH}"
457460
os.system(cmd)
458461

462+
with ThreadPoolExecutor() as executor:
463+
executor.map(aux, container_names)
464+
459465

460-
def stop_tcpdump(container_names):
466+
def stop_tcpdump(container_names, store_in=None):
461467
if os.environ.get("NATLAB_SAVE_LOGS") is None:
462468
return
463469
log_dir = get_current_test_log_path()
464470
os.makedirs(log_dir, exist_ok=True)
465-
for container_name in container_names:
471+
472+
def aux(container_name):
466473
cmd = f"docker exec --privileged {container_name} killall -w tcpdump"
467474
os.system(cmd)
468-
path = find_unique_path_for_tcpdump(log_dir, container_name)
475+
path = find_unique_path_for_tcpdump(
476+
store_in if store_in else log_dir, container_name
477+
)
469478
cmd = f"docker container cp {container_name}:{PCAP_FILE_PATH} {path}"
470479
os.system(cmd)
471480

481+
with ThreadPoolExecutor() as executor:
482+
executor.map(aux, container_names)
483+
472484

473485
def find_unique_path_for_tcpdump(log_dir, container_name):
474-
candidate_path = f"./{log_dir}/{container_name}.pcap"
486+
candidate_path = f"{log_dir}/{container_name}.pcap"
475487
counter = 1
476488
# NOTE: counter starting from '1' means that the file will either have no suffix or
477489
# will have a suffix starting from '2'. This is to make it clear that it's not the

0 commit comments

Comments
 (0)