Skip to content

Commit

Permalink
collect logs
Browse files Browse the repository at this point in the history
  • Loading branch information
P403n1x87 committed Aug 31, 2023
1 parent edfce19 commit a98f0d0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
5 changes: 4 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ AM_CONDITIONAL([COVERAGE], [test x$coverage = xtrue])
AC_ARG_ENABLE([debug-symbols], [
--enable-debug-symbols Include debug symbols], [
case "${enableval}" in
yes) debugsymbols=true ;;
yes)
debugsymbols=true
AUSTINP_LDADD+=" -lm"
;;
no) debugsymbols=false ;;
*) AC_MSG_ERROR([bad value ${enableval} for --enable-debug-symbols]) ;;
esac], [debugsymbols=false])
Expand Down
5 changes: 3 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ OPT_FLAGS = -O3
STRIP_FLAGS = -Os -s

if DEBUG_SYMBOLS
DEBUG_OPTS = -g
DEBUG_OPTS = -g -DDEBUG
austin_LDADD = -lm
undefine STRIP_FLAGS
endif

Expand Down Expand Up @@ -62,5 +63,5 @@ bin_PROGRAMS += austinp

austinp_SOURCES = $(austin_SOURCES)
austinp_CFLAGS = $(austin_CFLAGS) @AUSTINP_CFLAGS@
austinp_LDADD = @AUSTINP_LDADD@
austinp_LDADD = $(austin_LDADD) @AUSTINP_LDADD@
endif
22 changes: 15 additions & 7 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@

import platform
import sys

import pytest

from test.utils import austin
from test.utils import no_sudo
from test.utils import run_python
from test.utils import target

import pytest


def test_cli_no_arguments():
result = austin()
Expand All @@ -44,6 +43,7 @@ def test_cli_no_python():
sys.executable,
"-c",
"from time import sleep;sleep(2)",
expect_fail=True,
)
assert result.returncode == 32
assert "not a Python" in result.stderr or "Cannot launch" in result.stderr
Expand All @@ -55,20 +55,21 @@ def test_cli_short_lived():
sys.executable,
"-c",
"print('Hello World')",
expect_fail=True,
)
assert result.returncode == 33
assert "too quickly" in result.stderr


def test_cli_invalid_command():
result = austin("snafubar")
result = austin("snafubar", expect_fail=True)
assert "[GCC" in result.stderr
assert result.returncode == 33
assert "Cannot launch" in (result.stderr or result.stdout)


def test_cli_invalid_pid():
result = austin("-p", "9999999")
result = austin("-p", "9999999", expect_fail=True)

assert result.returncode == 36
assert "Cannot attach" in result.stderr
Expand All @@ -80,7 +81,7 @@ def test_cli_invalid_pid():
@no_sudo
def test_cli_permissions():
with run_python("3", target("sleepy.py")) as p:
result = austin("-i", "1ms", "-p", str(p.pid))
result = austin("-i", "1ms", "-p", str(p.pid), expect_fail=True)
assert result.returncode == 37, result.stderr
assert "Insufficient permissions" in result.stderr, result.stderr

Expand All @@ -91,6 +92,13 @@ def test_cli_permissions():
)
@no_sudo
def test_cli_permissions_darwin():
result = austin("-i", "1ms", "python3.10", "-c", "from time import sleep; sleep(1)")
result = austin(
"-i",
"1ms",
"python3.10",
"-c",
"from time import sleep; sleep(1)",
expect_fail=True,
)
assert result.returncode == 37, result.stderr
assert "Insufficient permissions" in result.stderr, result.stderr
73 changes: 71 additions & 2 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@
from pathlib import Path
from shutil import rmtree
from subprocess import PIPE
from subprocess import CalledProcessError
from subprocess import CompletedProcess
from subprocess import Popen
from subprocess import TimeoutExpired
from subprocess import check_output
from subprocess import run
from test import PYTHON_VERSIONS
from time import sleep
from types import ModuleType
from typing import Iterator
from typing import List
from typing import TypeVar


Expand Down Expand Up @@ -131,9 +133,60 @@ def bt(binary: Path) -> str:
return "No core dump available."


def collect_logs(variant: str, pid: int) -> List[str]:
match platform.system():
case "Linux":
with Path("/var/log/syslog").open() as logfile:
needle = f"{variant}[{pid}]"
return [_.strip() for _ in logfile.readlines() if needle in _]
case _:
return []


EXEEXT = ".exe" if platform.system() == "Windows" else ""


# Taken from the subprocess module
def run(
*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs
):
if input is not None:
if kwargs.get("stdin") is not None:
raise ValueError("stdin and input arguments may not both be used.")
kwargs["stdin"] = PIPE

if capture_output:
if kwargs.get("stdout") is not None or kwargs.get("stderr") is not None:
raise ValueError(
"stdout and stderr arguments may not be used " "with capture_output."
)
kwargs["stdout"] = PIPE
kwargs["stderr"] = PIPE

with Popen(*popenargs, **kwargs) as process:
try:
stdout, stderr = process.communicate(input, timeout=timeout)
except TimeoutExpired as exc:
process.kill()
if platform.system() == "Windows":
exc.stdout, exc.stderr = process.communicate()
else:
process.wait()
raise
except: # noqa
process.kill()
raise
retcode = process.poll()
if check and retcode:
raise CalledProcessError(
retcode, process.args, output=stdout, stderr=stderr
)
result = CompletedProcess(process.args, retcode, stdout, stderr)
result.pid = process.pid

return result


class Variant(str):
ALL: list["Variant"] = []

Expand All @@ -144,12 +197,18 @@ def __init__(self, name: str) -> None:
if not path.is_file():
path = Path(name).with_suffix(EXEEXT)

self.name = name
self.path = path

self.ALL.append(self)

def __call__(
self, *args: str, timeout: int = 60, mojo: bool = False, convert: bool = True
self,
*args: str,
timeout: int = 60,
mojo: bool = False,
convert: bool = True,
expect_fail: bool = False,
) -> CompletedProcess:
if not self.path.is_file():
pytest.skip(f"Variant '{self}' not available")
Expand All @@ -174,6 +233,16 @@ def __call__(
result.stdout = result.stdout.decode(errors="ignore")
result.stderr = result.stderr.decode()

if (
result.returncode
and (logs := collect_logs(self.name, result.pid))
and not expect_fail
):
print(f" logs for {result.pid} ".center(80, "="))
for log in logs:
print(log)
print(f" end of logs for {result.pid} ".center(80, "="))

return result


Expand Down

0 comments on commit a98f0d0

Please sign in to comment.