edgefirst-imu 3.0.2

EdgeFirst IMU Service for BNO08x sensors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# GitHub Actions workflow for testing and code quality
#
# This workflow runs tests, performs static analysis, and collects coverage data
# for the EdgeFirst IMU service. Coverage is reported to SonarCloud.
#
# Three-phase architecture for on-target testing:
# - Phase 1: Build instrumented binaries on ubuntu-22.04-arm
# - Phase 2: Run hardware integration tests on raivin runner
# - Phase 3: Process coverage data and report to SonarCloud
#
# Runner Notes:
# - ubuntu-22.04: Standard GitHub-hosted runner (x86_64)
# - ubuntu-22.04-arm: GitHub-hosted ARM runner (aarch64)
# - raivin: Self-hosted hardware runner with BNO08x IMU

name: Test

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main, develop]

env:
  CARGO_TERM_COLOR: always

jobs:
  format:
    name: Format Check
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt

      - name: Check formatting
        run: cargo fmt --all --check

  clippy:
    name: Lint with Clippy
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry/index
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-index-

      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-clippy-

      - name: Run Clippy
        run: cargo clippy --workspace --all-targets --all-features -- -D warnings

  # ==========================================================================
  # Phase 1: Build & Unit Test on GitHub-hosted runners
  # ==========================================================================
  test:
    name: Test and Coverage (${{ matrix.platform.name }})
    runs-on: ${{ matrix.platform.runner }}
    permissions:
      contents: read
      checks: write
      pull-requests: write
    strategy:
      matrix:
        platform:
          - name: x86_64
            runner: ubuntu-22.04
            target: x86_64-unknown-linux-gnu
          - name: aarch64
            runner: ubuntu-22.04-arm
            target: aarch64-unknown-linux-gnu
      fail-fast: false

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview
          targets: ${{ matrix.platform.target }}

      - name: Install cargo-llvm-cov and cargo-nextest
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-llvm-cov,cargo-nextest

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry/index
          key: ${{ runner.os }}-${{ matrix.platform.name }}-cargo-index
          restore-keys: ${{ runner.os }}-${{ matrix.platform.name }}-cargo-index

      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-${{ matrix.platform.name }}-cargo-test
          restore-keys: ${{ runner.os }}-${{ matrix.platform.name }}-cargo-test

      - name: Run unit tests with coverage
        run: |
          # Run tests with LCOV coverage output using profiling profile
          # Note: This is a binary-only crate, so coverage will be limited
          cargo llvm-cov nextest --workspace --lcov --output-path coverage.lcov \
            --profile profiling --cargo-profile profiling --no-fail-fast || true

      - name: Build instrumented binary for integration tests (aarch64 only)
        if: matrix.platform.name == 'aarch64'
        run: |
          # Source coverage environment and build instrumented binary
          source <(cargo llvm-cov show-env --export-prefix)
          export CARGO_TARGET_DIR=target/llvm-cov-target

          # Build the main binary with coverage instrumentation
          cargo build --profile profiling --no-default-features

          # Build integration tests with coverage instrumentation
          cargo nextest run --workspace --no-run --run-ignored=all --cargo-profile profiling

          # Prepare artifacts
          mkdir -p instrumented-binaries
          cp target/llvm-cov-target/profiling/edgefirst-imu instrumented-binaries/

          # Copy instrumented integration test binaries
          find target/llvm-cov-target/profiling/deps -name "*integration*" -type f -executable \
            -exec cp {} instrumented-binaries/ \; 2>/dev/null || true

          echo "Instrumented binaries prepared:"
          ls -lh instrumented-binaries/

      - name: Upload coverage artifact
        uses: actions/upload-artifact@v4
        with:
          name: coverage-${{ matrix.platform.name }}
          path: coverage.lcov
          retention-days: 30
          if-no-files-found: ignore

      - name: Upload instrumented binaries (aarch64 only)
        if: matrix.platform.name == 'aarch64'
        uses: actions/upload-artifact@v4
        with:
          name: instrumented-binaries-aarch64
          path: instrumented-binaries/
          retention-days: 7

      - name: Upload Rust instrumented objects for coverage processing (aarch64 only)
        if: matrix.platform.name == 'aarch64'
        uses: actions/upload-artifact@v4
        with:
          name: rust-llvm-cov-aarch64
          path: target/llvm-cov-target/
          retention-days: 7

  # ==========================================================================
  # Phase 2: Hardware Integration Tests
  # ==========================================================================
  hardware-test:
    name: Hardware Integration Tests
    needs: test
    runs-on: raivin
    # Only run on main branch or when explicitly requested via label
    if: github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'test-hardware')

    steps:
      - name: Clean workspace (self-hosted runner)
        run: |
          # Self-hosted runners persist state between runs
          rm -rf target/ instrumented-binaries/ coverage/ 2>/dev/null || true
          find . -name "*.profraw" -delete 2>/dev/null || true

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download instrumented binaries
        uses: actions/download-artifact@v4
        with:
          name: instrumented-binaries-aarch64
          path: instrumented-binaries/

      - name: Make binaries executable
        run: chmod +x instrumented-binaries/*

      - name: Run hardware integration tests
        run: |
          # Set up profraw collection directory for coverage
          mkdir -p coverage/profraw coverage/test-output
          export LLVM_PROFILE_FILE="${{ github.workspace }}/coverage/profraw/raivin-%p-%m.profraw"

          # Set the binary location for integration tests
          export IMU_BINARY="${{ github.workspace }}/instrumented-binaries/edgefirst-imu"

          # Track overall test status
          TEST_FAILED=0

          # Run each instrumented integration test binary
          echo "Running instrumented integration test binaries..."
          for test_bin in instrumented-binaries/*; do
            if [ -x "$test_bin" ] && [ -f "$test_bin" ]; then
              test_name=$(basename "$test_bin")

              # Skip the main binary
              if [ "$test_name" = "edgefirst-imu" ]; then
                continue
              fi

              # Check if this is a test binary (accepts --list)
              if ! "$test_bin" --list >/dev/null 2>&1; then
                continue
              fi

              echo "=== Running $test_name ==="
              # --test-threads=1 for deterministic execution on hardware
              # --include-ignored to run hardware integration tests marked with #[ignore]
              if ! "$test_bin" --test-threads=1 --include-ignored 2>&1 | tee "coverage/test-output/${test_name}.txt"; then
                echo "FAILED: $test_name"
                TEST_FAILED=1
              fi
            fi
          done

          echo ""
          echo "=== Profraw files generated ==="
          ls -la coverage/profraw/ || echo "No profraw files generated"

          # Fail the step if any test failed
          if [ $TEST_FAILED -ne 0 ]; then
            echo ""
            echo "ERROR: One or more integration tests failed!"
            exit 1
          fi

      - name: Upload hardware coverage artifact
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: coverage-hardware
          path: |
            coverage/profraw/
            coverage/test-output/
          retention-days: 30

  # ==========================================================================
  # Phase 3: Process Hardware Coverage
  # ==========================================================================
  process-hardware-coverage:
    name: Process Hardware Coverage
    needs: [test, hardware-test]
    runs-on: ubuntu-22.04-arm
    # Only run if hardware-test ran (even if it failed, to collect partial coverage)
    if: always() && needs.test.result == 'success' && (needs.hardware-test.result == 'success' || needs.hardware-test.result == 'failure')

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

      - name: Download Rust instrumented objects
        uses: actions/download-artifact@v4
        with:
          name: rust-llvm-cov-aarch64
          path: target/llvm-cov-target/

      - name: Download hardware coverage artifacts
        uses: actions/download-artifact@v4
        with:
          name: coverage-hardware
          path: coverage-hardware/

      - name: Process profraw files into coverage report
        run: |
          echo "=== Downloaded llvm-cov-target directory ==="
          ls -la target/llvm-cov-target/ || echo "Directory not found"

          echo ""
          echo "=== Profraw files from hardware ==="
          find coverage-hardware/ -name "*.profraw" -type f || echo "No profraw files found"

          PROFRAW_COUNT=$(find coverage-hardware/ -name "*.profraw" 2>/dev/null | wc -l)
          echo "Found $PROFRAW_COUNT profraw files"

          if [ "$PROFRAW_COUNT" -eq 0 ]; then
            echo "WARNING: No profraw files found from hardware tests - skipping coverage"
            exit 0
          fi

          # Find LLVM tools from Rust toolchain
          TOOLCHAIN_ROOT=$(rustc --print sysroot)
          LLVM_PROFDATA=$(find "$TOOLCHAIN_ROOT" -name "llvm-profdata" -type f | head -1)
          LLVM_COV=$(find "$TOOLCHAIN_ROOT" -name "llvm-cov" -type f | head -1)

          if [ -z "$LLVM_PROFDATA" ] || [ -z "$LLVM_COV" ]; then
            echo "ERROR: Could not find LLVM tools in Rust toolchain"
            exit 1
          fi

          echo ""
          echo "Using LLVM tools:"
          echo "  llvm-profdata: $LLVM_PROFDATA"
          echo "  llvm-cov: $LLVM_COV"

          # Merge profraw files into profdata
          echo ""
          echo "=== Merging profraw files ==="
          mkdir -p coverage
          "$LLVM_PROFDATA" merge -sparse \
            $(find coverage-hardware/ -name "*.profraw" -type f) \
            -o coverage/hardware.profdata

          if [ ! -f coverage/hardware.profdata ]; then
            echo "ERROR: Failed to merge profraw files"
            exit 1
          fi
          echo "Created coverage/hardware.profdata"

          # Find all instrumented binaries
          echo ""
          echo "=== Finding instrumented binaries ==="
          OBJECT_FILES=""
          for obj in $(find target/llvm-cov-target/profiling -maxdepth 2 -type f ! -name "*.d" ! -name "*.rlib" ! -name "*.rmeta" 2>/dev/null); do
            if file "$obj" | grep -q "ELF"; then
              if [ -z "$OBJECT_FILES" ]; then
                OBJECT_FILES="$obj"
              else
                OBJECT_FILES="$OBJECT_FILES --object=$obj"
              fi
            fi
          done

          # Also check deps directory
          for obj in $(find target/llvm-cov-target/profiling/deps -maxdepth 1 -type f ! -name "*.d" ! -name "*.rlib" ! -name "*.rmeta" 2>/dev/null); do
            if file "$obj" | grep -q "ELF"; then
              OBJECT_FILES="$OBJECT_FILES --object=$obj"
            fi
          done

          if [ -z "$OBJECT_FILES" ]; then
            echo "ERROR: No ELF binaries found in target/llvm-cov-target/profiling/"
            exit 1
          fi

          echo "Found binaries for coverage analysis"

          # Generate coverage report
          echo ""
          echo "=== Generating coverage report ==="
          "$LLVM_COV" export \
            --format=lcov \
            --instr-profile=coverage/hardware.profdata \
            --ignore-filename-regex='/.cargo/registry|/rustc/' \
            $OBJECT_FILES \
            > coverage/coverage-hardware.lcov

          if [ ! -s coverage/coverage-hardware.lcov ]; then
            echo "ERROR: Failed to generate coverage report (empty file)"
            exit 1
          fi

          echo "Generated coverage/coverage-hardware.lcov"
          wc -l coverage/coverage-hardware.lcov

      - name: Upload processed hardware coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage-hardware-processed
          path: coverage/coverage-hardware.lcov
          retention-days: 30
          if-no-files-found: ignore

  # ==========================================================================
  # SonarCloud Analysis
  # ==========================================================================
  sonarcloud:
    name: SonarCloud Analysis
    runs-on: ubuntu-22.04
    needs: [test, hardware-test, process-hardware-coverage]
    # Run if test succeeded, even if hardware tests were skipped
    if: |
      always() &&
      needs.test.result == 'success' &&
      (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository)

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download coverage artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: coverage-*
          path: coverage/

      - name: Organize coverage files for SonarCloud
        run: |
          echo "=== Downloaded coverage artifacts ==="
          find coverage/ -type f

          # Collect all LCOV coverage reports
          COVERAGE_REPORTS=$(find coverage/ -name "*.lcov" | tr '\n' ',' | sed 's/,$//')

          echo "=== Coverage report paths ==="
          echo "Coverage reports: $COVERAGE_REPORTS"

          # Export for SonarCloud
          echo "COVERAGE_PATHS=$COVERAGE_REPORTS" >> $GITHUB_ENV

      - name: SonarCloud Scan
        uses: SonarSource/sonarqube-scan-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        with:
          args: >
            -Dsonar.rust.lcov.reportPaths=${{ env.COVERAGE_PATHS }}

      - name: Generate coverage summary
        if: always()
        run: |
          echo "# 📊 EdgeFirst IMU Coverage Summary" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY

          # Test Platforms
          echo "## 🔧 Test Platforms" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "| Platform | Architecture | Environment | Test Types |" >> $GITHUB_STEP_SUMMARY
          echo "|----------|--------------|-------------|------------|" >> $GITHUB_STEP_SUMMARY
          echo "| Ubuntu 22.04 | x86_64 | GitHub Runner | Unit tests |" >> $GITHUB_STEP_SUMMARY
          echo "| Ubuntu 22.04 ARM | aarch64 | GitHub ARM Runner | Unit tests |" >> $GITHUB_STEP_SUMMARY
          if [ "${{ needs.hardware-test.result }}" != "skipped" ]; then
            echo "| Raivin | aarch64 | Real Hardware | Integration tests with BNO08x IMU |" >> $GITHUB_STEP_SUMMARY
          fi
          echo "" >> $GITHUB_STEP_SUMMARY

          # Coverage Summary
          echo "## 📈 Coverage Summary" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY

          # Parse coverage from LCOV files
          for coverage_file in $(find coverage/ -name "*.lcov" 2>/dev/null | sort); do
            if [ -f "$coverage_file" ]; then
              platform=$(basename "$(dirname "$coverage_file")" | sed 's/coverage-//')

              # Extract coverage metrics from LCOV
              lines_found=$(grep -c "^DA:" "$coverage_file" 2>/dev/null || echo 0)
              lines_hit=$(grep "^DA:" "$coverage_file" | grep -v ",0$" | wc -l 2>/dev/null || echo 0)

              if [ "$lines_found" -gt 0 ]; then
                line_pct=$(awk "BEGIN {printf \"%.1f\", ($lines_hit / $lines_found) * 100}")

                echo "**$platform**:" >> $GITHUB_STEP_SUMMARY
                echo "- Line coverage: ${line_pct}% (${lines_hit}/${lines_found} lines)" >> $GITHUB_STEP_SUMMARY
                echo "" >> $GITHUB_STEP_SUMMARY
              fi
            fi
          done

          echo "" >> $GITHUB_STEP_SUMMARY
          echo "📋 Full analysis available on [SonarCloud](https://sonarcloud.io/dashboard?id=EdgeFirstAI_imu)" >> $GITHUB_STEP_SUMMARY