def _collect_logs(instance: IntegrationInstance, node_id: str, test_failed: bool): """Collect logs from remote instance. Args: instance: The current IntegrationInstance to collect logs from node_id: The pytest representation of this test, E.g.: tests/integration_tests/test_example.py::TestExample.test_example test_failed: If test failed or not """ if any([ integration_settings.COLLECT_LOGS == 'NEVER', integration_settings.COLLECT_LOGS == 'ON_ERROR' and not test_failed ]): return instance.execute( 'cloud-init collect-logs -u -t /var/tmp/cloud-init.tar.gz') node_id_path = Path( node_id.replace('.py', '') # Having a directory with '.py' would be weird .replace('::', os.path.sep) # Turn classes/tests into paths .replace('[', '-') # For parametrized names .replace(']', '') # For parameterized names ) log_dir = Path(integration_settings.LOCAL_LOG_PATH ) / session_start_time / node_id_path log.info("Writing logs to %s", log_dir) if not log_dir.exists(): log_dir.mkdir(parents=True) tarball_path = log_dir / 'cloud-init.tar.gz' instance.pull_file('/var/tmp/cloud-init.tar.gz', tarball_path) tarball = TarFile.open(str(tarball_path)) tarball.extractall(path=str(log_dir)) tarball_path.unlink()
def _collect_logs(instance: IntegrationInstance, node_id: str, test_failed: bool): """Collect logs from remote instance. Args: instance: The current IntegrationInstance to collect logs from node_id: The pytest representation of this test, E.g.: tests/integration_tests/test_example.py::TestExample.test_example test_failed: If test failed or not """ if any([ integration_settings.COLLECT_LOGS == "NEVER", integration_settings.COLLECT_LOGS == "ON_ERROR" and not test_failed, ]): return instance.execute( "cloud-init collect-logs -u -t /var/tmp/cloud-init.tar.gz") node_id_path = Path( node_id.replace(".py", "") # Having a directory with '.py' would be weird .replace("::", os.path.sep) # Turn classes/tests into paths .replace("[", "-") # For parametrized names .replace("]", "") # For parameterized names ) log_dir = (Path(integration_settings.LOCAL_LOG_PATH) / session_start_time / node_id_path) log.info("Writing logs to %s", log_dir) if not log_dir.exists(): log_dir.mkdir(parents=True) # Add a symlink to the latest log output directory last_symlink = Path(integration_settings.LOCAL_LOG_PATH) / "last" if os.path.islink(last_symlink): os.unlink(last_symlink) os.symlink(log_dir.parent, last_symlink) tarball_path = log_dir / "cloud-init.tar.gz" try: instance.pull_file("/var/tmp/cloud-init.tar.gz", tarball_path) except Exception as e: log.error("Failed to pull logs: %s", e) return tarball = TarFile.open(str(tarball_path)) tarball.extractall(path=str(log_dir)) tarball_path.unlink()