Repository: firecracker-microvm/firecracker
File: tests/integration_tests/functional/test_instrumented_firecracker.py (Python)

Code before:

from framework import utils
from framework.microvm import MicroVMFactory
from host_tools.cargo_build import cargo, get_binary

# Marker tipici emessi dalla macro log_instrument
TRACE_LEVEL_HINT = "TRACE"
MARKER_REGEX = re.compile(r"ThreadId\(\d+\).*?(?:>>|<<)")
PATHS_TO_INSTRUMENT = [
    "../src/firecracker/src/main.rs",
    "../src/firecracker/src/api_server",
    "../src/vmm/src/lib.rs",
    "../src/vmm/src/builder.rs",
]


def build_instrumented_binary():
    """Builds an instrumented Firecracker binary with tracing instrumentation."""
    clippy_tracing = get_binary("clippy-tracing")
    for p in PATHS_TO_INSTRUMENT:
        utils.check_output(
            f"{clippy_tracing} --action fix --suffix log_instrument:: --cfg-attr 'feature =\"tracing\"' --path {p}"
        )
    cargo(
        "build",
        "--workspace --target x86_64-unknown-linux-musl --no-default-features --release --features tracing --bin firecracker",
    )
    return get_binary("firecracker")


def cleanup_instrumentation():
    """Cleans up tracing instrumentation from the Firecracker binary."""
    clippy_tracing = get_binary("clippy-tracing")
    for p in PATHS_TO_INSTRUMENT:
        utils.check_output(
            f"{clippy_tracing} --action strip --suffix log_instrument:: --cfg-attr 'feature =\"tracing\"' --path {p}"
        )


@pytest.fixture(scope="module")
def instrumented_binary():
    """Build and provide the path to an instrumented Firecracker binary."""
    binary_path = build_instrumented_binary()
    yield binary_path
    cleanup_instrumentation()


@pytest.mark.timeout(120)
def test_log_instrument_firecracker_basic_functionality(instrumented_binary):
    """Test that instrumented Firecracker can start and handle basic API calls with trace logging."""
    with tempfile.TemporaryDirectory() as _:

Review comment (bug):
`x86_64-unknown-linux-musl` isn't going to work for ARM (see [build failure](https://buildkite.com/firecracker/firecracker-pr/builds/14961#019a4f94-6326-47df-a807-62fc9e61a22f/58-2005)).

Code after:
import pytest

from framework import utils
from host_tools.cargo_build import cargo, get_binary

# Typical markers emitted by the `log_instrument` macro
TRACE_LEVEL_HINT = "TRACE"
MARKER_REGEX = re.compile(r"ThreadId\(\d+\).*?(?:>>|<<)")
PATHS_TO_INSTRUMENT = [
    "../src/firecracker/src/main.rs",
    "../src/firecracker/src/api_server",
    "../src/vmm/src/lib.rs",
    "../src/vmm/src/builder.rs",
]
TMP_BUILD_DIR = "../test_instrumented_firecracker_build"
ARCH_STR = f"{platform.machine()}"


def build_instrumented_binary():
    """Builds an instrumented Firecracker binary with tracing instrumentation."""
    # we need a different directory to avoid overriding the main bin
    instrumented_binary_dir = (
        Path(TMP_BUILD_DIR) / f"{ARCH_STR}-unknown-linux-musl" / "release"
    )

    clippy_tracing = get_binary("clippy-tracing")
    for p in PATHS_TO_INSTRUMENT:
        utils.check_output(
            f"{clippy_tracing} --action fix --suffix log_instrument:: --cfg-attr 'feature =\"tracing\"' --path {p}"
        )
    cargo(
        "build",
        f"--workspace --target {platform.machine()}-unknown-linux-musl --release "
        f"--features tracing --bin firecracker",
        env={"CARGO_TARGET_DIR": TMP_BUILD_DIR},
    )
    return get_binary("firecracker", binary_dir=instrumented_binary_dir)


def cleanup_instrumentation():
    """Cleans up tracing instrumentation from the Firecracker binary."""
    clippy_tracing = get_binary("clippy-tracing")
    for p in PATHS_TO_INSTRUMENT:
        utils.check_output(
            f"{clippy_tracing} --action strip --suffix log_instrument:: --cfg-attr 'feature =\"tracing\"' --path {p}"
        )


@pytest.fixture(scope="module")
def instrumented_binary():
    """Build and provide the path to an instrumented Firecracker binary."""