-
Notifications
You must be signed in to change notification settings - Fork 21
Add core dump extraction from container in docker plugin #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -62,6 +62,21 @@ def pytest_addoption(parser): | |||||
| help="Directory to write extracted coverage files. " | ||||||
| "Defaults to $TEST_UNDECLARED_OUTPUTS_DIR/sysroot or /tmp/sysroot.", | ||||||
| ) | ||||||
| parser.addoption( | ||||||
| "--extract-core-dumps", | ||||||
| action="store_true", | ||||||
| default=False, | ||||||
| help="Copy core dump files from the container to the host before teardown.", | ||||||
| ) | ||||||
| parser.addoption( | ||||||
| "--core-dumps-output-dir", | ||||||
| default=os.path.join( | ||||||
| os.environ.get("TEST_UNDECLARED_OUTPUTS_DIR", "/tmp"), | ||||||
| "cores", | ||||||
| ), | ||||||
| help="Directory to write extracted core dump files. " | ||||||
| "Defaults to $TEST_UNDECLARED_OUTPUTS_DIR/cores or /tmp/cores.", | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| class DockerAsyncProcess(AsyncProcess): | ||||||
|
|
@@ -335,6 +350,34 @@ def _extract_coverage_from_container(target, output_base): | |||||
| logger.warning(f"Failed to extract {remote_path}", exc_info=True) | ||||||
|
|
||||||
|
|
||||||
| def _extract_core_dumps_from_container(target, output_base): | ||||||
| """Extract core dump files created inside the container.""" | ||||||
| logger.info(f"Attempting core extraction to {output_base}") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| os.makedirs(output_base, exist_ok=True) | ||||||
| # Look for core files in typical locations, being specific to avoid false positives | ||||||
| exit_code, output = target.execute( | ||||||
| "(ls -1 /core* 2>/dev/null || true) && (ls -1 /opt/*/core* 2>/dev/null || true) && (ls -1 /root/core* 2>/dev/null || true) && (ls -1 /tmp/core* 2>/dev/null || true)" | ||||||
| ) | ||||||
|
|
||||||
| core_paths = [line.strip() for line in output.decode().splitlines() if line.strip()] | ||||||
| logger.info(f"Found {len(core_paths)} core files: {core_paths}") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if not core_paths: | ||||||
| return | ||||||
|
|
||||||
| for remote_path in core_paths: | ||||||
| local_path = os.path.join(output_base, remote_path.lstrip("/")) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check Path.is_dir() before attempting extraction?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a check — if the given output directory isn't a valid directory, we log a warning and skip extraction instead of trying to download into an invalid path. |
||||||
| if not os.path.realpath(local_path).startswith(os.path.realpath(output_base)): | ||||||
| logger.warning(f"Skipping path traversal attempt: {remote_path}") | ||||||
| continue | ||||||
| os.makedirs(os.path.dirname(local_path), exist_ok=True) | ||||||
| try: | ||||||
| logger.info(f"Extracting core from {remote_path} to {local_path}") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| target.download(remote_path, local_path) | ||||||
| logger.info(f"Successfully extracted {remote_path}") | ||||||
| except Exception: | ||||||
| logger.warning(f"Failed to extract core file {remote_path}", exc_info=True) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
|
|
||||||
| @pytest.fixture(scope=determine_target_scope) | ||||||
| def target_init(request, _docker_configuration): | ||||||
| print(_docker_configuration) | ||||||
|
|
@@ -397,6 +440,17 @@ def target_init(request, _docker_configuration): | |||||
| ) | ||||||
| except Exception: | ||||||
| logger.warning("Coverage extraction failed", exc_info=True) | ||||||
| try: | ||||||
| extract_core_enabled = request.config.getoption("extract_core_dumps") | ||||||
| logger.info(f"Core extraction enabled: {extract_core_enabled}") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if target is not None and extract_core_enabled: | ||||||
| logger.info(f"Extracting cores to {request.config.getoption('core_dumps_output_dir')}") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| _extract_core_dumps_from_container( | ||||||
| target, | ||||||
| request.config.getoption("core_dumps_output_dir"), | ||||||
| ) | ||||||
| except Exception: | ||||||
| logger.warning("Core extraction failed", exc_info=True) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| try: | ||||||
| try: | ||||||
| container.stop(timeout=1) | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -80,6 +80,55 @@ def pytest_addoption(parser): | |||||||||
| help="Path to a QEMU disk image (qcow2, wic, or img). " | ||||||||||
| "An ephemeral overlay is created so the original image is not modified.", | ||||||||||
| ) | ||||||||||
| parser.addoption( | ||||||||||
| "--extract-core-dump", | ||||||||||
| action="store_true", | ||||||||||
| default=False, | ||||||||||
| help="Copy core dump files from the QEMU target to the host before teardown.", | ||||||||||
| ) | ||||||||||
| parser.addoption( | ||||||||||
| "--core-dump-output-dir", | ||||||||||
| default=os.path.join( | ||||||||||
| os.environ.get("TEST_UNDECLARED_OUTPUTS_DIR", "/tmp"), | ||||||||||
| "coredumps", | ||||||||||
| ), | ||||||||||
| help="Directory to write extracted core dump files. " | ||||||||||
| "Defaults to $TEST_UNDECLARED_OUTPUTS_DIR/coredumps or /tmp/coredumps.", | ||||||||||
| ) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _extract_cores_from_qemu(target, output_base): | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add parameters to doc string like this |
||||||||||
| """Extract core dump files from a QEMU (QNX) target via SSH/SFTP.""" | ||||||||||
| logger.info(f"Attempting core dumps extraction to {output_base}") | ||||||||||
| os.makedirs(output_base, exist_ok=True) | ||||||||||
| # Search common core file locations (works on QNX and Linux guests). | ||||||||||
| _exit_code, output = target.execute( | ||||||||||
| "(ls -1 /core* 2>/dev/null || true)" | ||||||||||
| " && (ls -1 /opt/*/core* 2>/dev/null || true)" | ||||||||||
| " && (ls -1 /root/core* 2>/dev/null || true)" | ||||||||||
| " && (ls -1 /tmp/core* 2>/dev/null || true)" | ||||||||||
| " && (ls -1 /data/*/core* 2>/dev/null || true)" | ||||||||||
| " && (ls -1 /tmp/*.core /tmp/*.core.gz /var/*.core /var/*.core.gz 2>/dev/null || true)" | ||||||||||
| " && (ls -1 /opt/*/*.core /opt/*/*.core.gz /root/*.core /root/*.core.gz /data/*/*.core /data/*/*.core.gz 2>/dev/null || true)" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| coredumps_paths = [line.strip() for line in output.decode().splitlines() if line.strip()] | ||||||||||
| logger.info(f"Found {len(coredumps_paths)} core files: {coredumps_paths}") | ||||||||||
| if not coredumps_paths: | ||||||||||
| return | ||||||||||
|
|
||||||||||
| for remote_path in coredumps_paths: | ||||||||||
| local_path = os.path.join(output_base, remote_path.lstrip("/")) | ||||||||||
| if not os.path.realpath(local_path).startswith(os.path.realpath(output_base)): | ||||||||||
| logger.warning(f"Skipping path traversal attempt: {remote_path}") | ||||||||||
| continue | ||||||||||
| os.makedirs(os.path.dirname(local_path), exist_ok=True) | ||||||||||
| try: | ||||||||||
| logger.info(f"Extracting core from {remote_path} to {local_path}") | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| target.download(remote_path, local_path) | ||||||||||
| logger.info(f"Successfully extracted {remote_path}") | ||||||||||
| except Exception: | ||||||||||
| logger.warning(f"Failed to extract core file {remote_path}", exc_info=True) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| @pytest.fixture(scope="session") | ||||||||||
|
|
@@ -127,6 +176,19 @@ def target_init(config, request, dlt): | |||||||||
| ) as qemu: | ||||||||||
| pre_tests_phase(qemu) | ||||||||||
| yield qemu | ||||||||||
| try: | ||||||||||
| extract_core_enabled = request.config.getoption("extract_core_dump") | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| logger.info(f"Core extraction enabled: {extract_core_enabled}") | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| if qemu is not None and extract_core_enabled: | ||||||||||
| logger.info( | ||||||||||
| f"Extracting cores to {request.config.getoption('core_dump_output_dir')}" | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| ) | ||||||||||
| _extract_cores_from_qemu( | ||||||||||
| qemu, | ||||||||||
| request.config.getoption("core_dump_output_dir"), | ||||||||||
| ) | ||||||||||
| except Exception: | ||||||||||
| logger.warning("Core extraction failed", exc_info=True) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| finally: | ||||||||||
| if overlay_path and os.path.exists(overlay_path): | ||||||||||
| os.unlink(overlay_path) | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add doc string for parameters