Skip to content

Commit

Permalink
natlab: Add quiet mode to stop printing trivial process logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lcruz99 committed Mar 4, 2025
1 parent 0a89ea7 commit a1515c6
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 31 deletions.
Empty file added .unreleased/improve_natlab_logs
Empty file.
4 changes: 2 additions & 2 deletions nat-lab/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async def on_output(output: str) -> None:
]

async with new_connection_raw(vm_tag) as connection:
await connection.create_process(cmd).execute(on_output, on_output)
await connection.create_process(cmd, quiet=True).execute(on_output, on_output)


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -427,7 +427,7 @@ def save_audit_log_from_host(suffix):

async def _save_macos_logs(conn, suffix):
try:
dmesg_proc = await conn.create_process(["dmesg"]).execute()
dmesg_proc = await conn.create_process(["dmesg"], quiet=True).execute()
with open(
os.path.join("logs", f"dmesg-macos-{suffix}.txt"), "w", encoding="utf-8"
) as f:
Expand Down
2 changes: 1 addition & 1 deletion nat-lab/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ async def send_https_request(

log.info("Curl command: %s", curl_command)

process = await connection.create_process(curl_command).execute()
process = await connection.create_process(curl_command, quiet=True).execute()
response = process.get_stdout()
if expect_response:
try:
Expand Down
11 changes: 8 additions & 3 deletions nat-lab/tests/interderp_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(
"--secret-key-2",
sk2,
],
quiet=True,
)
self._connection = connection

Expand All @@ -49,9 +50,11 @@ async def on_output(output: str) -> None:

async def get_log(self) -> str:
process = (
self._connection.create_process(["type", "interderpcli.log"])
self._connection.create_process(["type", "interderpcli.log"], quiet=True)
if self._connection.target_os == TargetOS.Windows
else self._connection.create_process(["cat", "./interderpcli.log"])
else self._connection.create_process(
["cat", "./interderpcli.log"], quiet=True
)
)
await process.execute()
return process.get_stdout()
Expand All @@ -66,7 +69,9 @@ async def save_logs(self) -> None:
log_content = await self.get_log()

if self._connection.target_os == TargetOS.Linux:
process = self._connection.create_process(["cat", "/etc/hostname"])
process = self._connection.create_process(
["cat", "/etc/hostname"], quiet=True
)
await process.execute()
container_id = process.get_stdout().strip()
else:
Expand Down
31 changes: 21 additions & 10 deletions nat-lab/tests/telio.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ async def maybe_write_device_fingerprint_to_moose_db(self):
f" ('device.fp._string', '\"{fingerprint}\"')"
),
],
quiet=True,
).execute()

async def trigger_event_collection(self) -> None:
Expand Down Expand Up @@ -963,6 +964,7 @@ async def get_system_log(self) -> Optional[str]:
" format-table -wrap"
),
],
quiet=True,
).execute()
logs += log_output.get_stdout()
except ProcessExecError:
Expand All @@ -984,20 +986,25 @@ async def clear_system_log(self) -> None:
"-Command",
f"Clear-EventLog -LogName {log_name}",
],
quiet=True,
).execute()

async def get_network_info(self) -> str:
if self._connection.target_os == TargetOS.Mac:
interface_info = self._connection.create_process(["ifconfig", "-a"])
interface_info = self._connection.create_process(
["ifconfig", "-a"], quiet=True
)
await interface_info.execute()
routing_table_info = self._connection.create_process(["netstat", "-rn"])
routing_table_info = self._connection.create_process(
["netstat", "-rn"], quiet=True
)
await routing_table_info.execute()
# syslog does not provide a way to filter events by timestamp, so only using the last 20 lines.
syslog_info = self._connection.create_process(["syslog"])
syslog_info = self._connection.create_process(["syslog"], quiet=True)
await syslog_info.execute()
start_time_str = self._start_time.strftime("%Y-%m-%d %H:%M:%S")
log_info = self._connection.create_process(
["log", "show", "--start", start_time_str]
["log", "show", "--start", start_time_str], quiet=True
)
await log_info.execute()
return (
Expand Down Expand Up @@ -1061,7 +1068,9 @@ async def _save_logs(self) -> None:
system_log_content = await self.get_system_log()

if self._connection.target_os == TargetOS.Linux:
process = self._connection.create_process(["cat", "/etc/hostname"])
process = self._connection.create_process(
["cat", "/etc/hostname"], quiet=True
)
await process.execute()
container_id = process.get_stdout().strip()
else:
Expand Down Expand Up @@ -1174,10 +1183,12 @@ async def clear_core_dumps(self):
coredump_folder, _ = self.get_coredump_folder()

# clear the existing system core dumps
await self._connection.create_process(["rm", "-rf", coredump_folder]).execute()
await self._connection.create_process(
["rm", "-rf", coredump_folder], quiet=True
).execute()
# make sure we have the path where the new cores will be dumped
await self._connection.create_process(
["mkdir", "-p", coredump_folder]
["mkdir", "-p", coredump_folder], quiet=True
).execute()

async def collect_core_dumps(self):
Expand Down Expand Up @@ -1216,7 +1227,7 @@ async def find_files(connection, where, name_pattern):

try:
process = await connection.create_process(
["find", where, "-maxdepth", "1", "-name", name_pattern]
["find", where, "-maxdepth", "1", "-name", name_pattern], quiet=True
).execute()
return process.get_stdout().strip().split()
except ProcessExecError:
Expand Down Expand Up @@ -1249,9 +1260,9 @@ async def get_log_without_flush(connection) -> str:
nothing to flush and attempting to do so will cause errors.
"""
process = (
connection.create_process(["type", "tcli.log"])
connection.create_process(["type", "tcli.log"], quiet=True)
if connection.target_os == TargetOS.Windows
else connection.create_process(["cat", "./tcli.log"])
else connection.create_process(["cat", "./tcli.log"], quiet=True)
)
await process.execute()
return process.get_stdout()
11 changes: 8 additions & 3 deletions nat-lab/tests/test_lana.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,14 @@ def build_telio_features(


async def clean_container(connection: Connection):
await connection.create_process(["rm", "-f", CONTAINER_EVENT_PATH]).execute()
await connection.create_process(["rm", "-f", CONTAINER_EVENT_BACKUP_PATH]).execute()
await connection.create_process(
["rm", "-f", CONTAINER_EVENT_PATH + "-journal"]
["rm", "-f", CONTAINER_EVENT_PATH], quiet=True
).execute()
await connection.create_process(
["rm", "-f", CONTAINER_EVENT_BACKUP_PATH], quiet=True
).execute()
await connection.create_process(
["rm", "-f", CONTAINER_EVENT_PATH + "-journal"], quiet=True
).execute()


Expand All @@ -183,6 +187,7 @@ async def get_moose_db_file(
"PRAGMA busy_timeout = 30000;",
f".backup {container_backup_path}",
],
quiet=True,
).execute(privileged=True)

log.info("get_moose_db_file::download")
Expand Down
2 changes: 1 addition & 1 deletion nat-lab/tests/utils/connection/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, target_os: TargetOS) -> None:

@abstractmethod
def create_process(
self, command: List[str], kill_id=None, term_type=None
self, command: List[str], kill_id=None, term_type=None, quiet=False
) -> "Process":
pass

Expand Down
17 changes: 11 additions & 6 deletions nat-lab/tests/utils/connection/docker_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ def aux():
await to_thread(aux)

def create_process(
self, command: List[str], kill_id=None, term_type=None
self, command: List[str], kill_id=None, term_type=None, quiet=False
) -> "Process":
process = DockerProcess(
self._container, self.container_name(), command, kill_id
)
log.info("[%s] Executing %s", self._name, " ".join(command))
if not quiet:
log.info("[%s] Executing %s", self._name, " ".join(command))
return process

async def get_ip_address(self) -> tuple[str, str]:
Expand All @@ -76,14 +77,18 @@ async def mapped_ports(self) -> tuple[str, str]:
return (str(host_port), str(container_port))

async def restore_ip_tables(self) -> None:
await self.create_process(["conntrack", "-F"]).execute()
await self.create_process(["iptables-restore", "iptables_backup"]).execute()
await self.create_process(["ip6tables-restore", "ip6tables_backup"]).execute()
await self.create_process(["conntrack", "-F"], quiet=True).execute()
await self.create_process(
["iptables-restore", "iptables_backup"], quiet=True
).execute()
await self.create_process(
["ip6tables-restore", "ip6tables_backup"], quiet=True
).execute()

async def clean_interface(self) -> None:
try:
await self.create_process(
["ip", "link", "delete", LINUX_INTERFACE_NAME]
["ip", "link", "delete", LINUX_INTERFACE_NAME], quiet=True
).execute()
except:
pass # Most of the time there will be no interface to be deleted
5 changes: 3 additions & 2 deletions nat-lab/tests/utils/connection/ssh_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ def __init__(
self._target_os = target_os

def create_process(
self, command: List[str], kill_id=None, term_type=None
self, command: List[str], kill_id=None, term_type=None, quiet=False
) -> "Process":
log.info("Executing %s on %s", " ".join(command), self.target_os)
if not quiet:
log.info("Executing %s on %s", " ".join(command), self.target_os)
if self._target_os == TargetOS.Windows:
escape_argument = cmd_exe_escape.escape_argument
elif self._target_os in [TargetOS.Linux, TargetOS.Mac]:
Expand Down
2 changes: 1 addition & 1 deletion nat-lab/tests/utils/connection_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def __init__(
validators: Optional[List[ConnTrackerEventsValidator]] = None,
):
args = ["conntrack", "-E"]
self._process: Process = connection.create_process(args)
self._process: Process = connection.create_process(args, quiet=True)
self._connection: Connection = connection
self._validators: Optional[List[ConnTrackerEventsValidator]] = validators
self._events: List[ConntrackerEvent] = []
Expand Down
3 changes: 3 additions & 0 deletions nat-lab/tests/utils/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, connection: Connection, ip: str) -> None:
if connection.target_os == TargetOS.Windows:
self._process = connection.create_process(
["ping", ("-4" if self._ip_proto == IPProto.IPv4 else "-6"), "-t", ip],
quiet=True,
)
elif connection.target_os == TargetOS.Mac:
self._process = connection.create_process(
Expand All @@ -48,6 +49,7 @@ def __init__(self, connection: Connection, ip: str) -> None:
kill_id,
ip,
],
quiet=True,
)
else:
self._process = connection.create_process(
Expand All @@ -59,6 +61,7 @@ def __init__(self, connection: Connection, ip: str) -> None:
ip,
],
kill_id,
quiet=True,
)
self._next_ping_event = asyncio.Event()

Expand Down
1 change: 1 addition & 0 deletions nat-lab/tests/utils/stun.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async def get(
"--verbosity",
"2",
],
quiet=True,
).execute(),
timeout,
)
Expand Down
3 changes: 2 additions & 1 deletion nat-lab/tests/utils/tcpdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(
# to file, everything works fine
term_type="xterm" if self.connection.target_os == TargetOS.Mac else None,
kill_id="DO_NOT_KILL" + secrets.token_hex(8).upper() if session else None,
quiet=True,
)

def get_stdout(self) -> str:
Expand Down Expand Up @@ -250,7 +251,7 @@ async def make_tcpdump(

if conn.target_os != TargetOS.Windows:
await conn.create_process(
["rm", "-f", PCAP_FILE_PATH[conn.target_os]]
["rm", "-f", PCAP_FILE_PATH[conn.target_os]], quiet=True
).execute()
# TODO(LLT-5942): temporary disable windows tcpdump
# else:
Expand Down
2 changes: 1 addition & 1 deletion nat-lab/tests/utils/telio_log_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, connection: Connection) -> None:
), "TelioLogNotifier supported only on Linux"
self._connection = connection
self._process = connection.create_process(
["tail", "-n", "1", "-F", "/tcli.log"]
["tail", "-n", "1", "-F", "/tcli.log"], quiet=True
)
self._output_notifier = OutputNotifier()

Expand Down

0 comments on commit a1515c6

Please sign in to comment.