@@ -16,6 +16,11 @@ RUN apk update && apk --no-cache add \
1616
1717WORKDIR /app
1818
19+ # Install llvm-tools-preview component and cargo-llvm-cov for coverage
20+ RUN rustup default stable && \
21+ rustup component add llvm-tools-preview && \
22+ cargo install cargo-llvm-cov --locked
23+
1924# Copy manifests
2025COPY Cargo.toml Cargo.lock ./
2126
@@ -28,27 +33,40 @@ RUN --mount=type=cache,target=/usr/local/cargo/registry \
2833COPY src ./src
2934COPY tests ./tests
3035
31- # Build integration tests
32- # Clear cached integration test artifacts to force recompilation when source changes.
33- # This ensures changes to test files are always reflected in the binary while still
34- # keeping dependency compilation cached for fast builds.
36+ # Build integration tests with coverage instrumentation
37+ # Using RUSTFLAGS to enable coverage instrumentation during compilation
38+ # This avoids the triple compilation issue (normal build, test build, coverage build)
3539RUN --mount=type=cache,target=/usr/local/cargo/registry \
3640 --mount=type=cache,target=/usr/local/cargo/git \
3741 --mount=type=cache,target=/app/target \
38- rm -rf /app/target/release/deps/integration-* \
39- /app/target/release/.fingerprint/*integration* && \
42+ RUSTFLAGS="-C instrument-coverage" \
4043 cargo test --features integration-tests --test integration --release --no-run && \
4144 TEST_BINARY=$(find target/release/deps -name 'integration-*' -type f -executable | head -n 1) && \
4245 cp "$TEST_BINARY" /app/integration-test-binary
4346
4447# ============================================================================
45- # Runtime Stage - Minimal image with only what's needed to run tests
48+ # Runtime Stage - Minimal image with only what's needed to run tests and generate coverage
4649# ============================================================================
47- FROM cgr.dev/chainguard/wolfi-base
50+ FROM cgr.dev/chainguard/wolfi-base AS runtime
4851
4952WORKDIR /app
5053
51- # Copy test binary from builder
54+ # Copy LLVM tools and libraries from builder (architecture-agnostic with wildcards)
55+ # This fixes the hardcoded aarch64 issue - works on both x86_64 and aarch64
56+ COPY --from=builder /root/.rustup/toolchains/stable-*/lib/rustlib/*/bin/llvm-cov \
57+ /usr/local/bin/llvm-cov
58+ COPY --from=builder /root/.rustup/toolchains/stable-*/lib/rustlib/*/bin/llvm-profdata \
59+ /usr/local/bin/llvm-profdata
60+
61+ # Copy LLVM shared libraries that llvm-cov and llvm-profdata depend on
62+ # Use a broad pattern to cover libLLVM.so.* and libLLVM-*.so variants.
63+ COPY --from=builder /root/.rustup/toolchains/stable-*/lib/libLLVM*.so* /usr/local/lib/
64+ COPY --from=builder /root/.rustup/toolchains/stable-*/lib/librustc_driver-*.so /usr/local/lib/
65+
66+ # Update library path so the LLVM tools can find their dependencies
67+ ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
68+
69+ # Copy instrumented test binary from builder
5270COPY --from=builder /app/integration-test-binary ./integration-tests
5371
5472# Copy SSL libraries from builder
@@ -62,9 +80,19 @@ COPY tests/integration ./tests/integration
6280# Networks is mounted separately at /app/networks to avoid nested mount issues
6381RUN mkdir -p /app/config /app/networks
6482
65- # Create directory for test results
66- RUN mkdir -p /app/test-results
83+ # Create directories for coverage output with proper permissions
84+ RUN mkdir -p /app/coverage /app/profraw && chmod -R 777 /app/coverage /app/profraw
6785
68- # Default test command
69- # Can be overridden via docker-compose or docker run
70- CMD ["./integration-tests", "--nocapture", "--test-threads=1"]
86+ # Execute tests directly, then generate lcov report using llvm-cov
87+ # This approach:
88+ # 1. Runs the instrumented test binary (generates .profraw files)
89+ # 2. Merges .profraw files into .profdata
90+ # 3. Generates lcov report from .profdata
91+ # All without invoking cargo or recompiling
92+ CMD ["sh", "-c", "\
93+ LLVM_PROFILE_FILE=/app/profraw/integration-%p-%m.profraw \
94+ ./integration-tests --nocapture --test-threads=1 && \
95+ llvm-profdata merge -sparse /app/profraw/*.profraw -o /app/coverage/integration.profdata && \
96+ llvm-cov export --format=lcov \
97+ --instr-profile=/app/coverage/integration.profdata \
98+ ./integration-tests > /app/coverage/integration-lcov.info"]
0 commit comments