Automated Testing

A guide to testing Moonforge images on hardware in CI using lab-moonforge-action and Robot Framework.

This tutorial builds on the Continuous Integration guide and shows how to automatically test your built OS images on real hardware using lab-moonforge-action and Robot Framework.

Prerequisites

In addition to the CI workflow set up in the Continuous Integration tutorial, automated hardware testing requires:

  • A self-hosted runner with HTTP access to a dut-control lab service, and the corresponding DUT_CONTROL_URL repository variable and DUT_CONTROL_CLIENT_KEY secret configured in GitHub.
  • An image built with the kas/common/debug.yml fragment enabled, so the device exposes an SSH server with an empty root password for the test suite to log in.

Writing a Robot Framework test

Create a “hello world” test suite under tests/hello.robot. This test logs into the flashed device over SSH and verifies that /etc/os-release identifies the Moonforge distribution. When lab-moonforge-action reserves a device, it exports DUT_IP and DUT_SSH_PORT into the test environment so the suite knows where to connect:

# tests/hello.robot
*** Settings ***
Documentation     Hello world test for a Moonforge image.
Library           OperatingSystem
Library           SSHLibrary
Suite Setup       Log In To The Device
Suite Teardown    Close All Connections

*** Keywords ***
Log In To The Device
    ${ip}=    Get Environment Variable    DUT_IP    ${EMPTY}
    Skip If    $ip == ''    DUT_IP is not set
    ${port}=    Get Environment Variable    DUT_SSH_PORT    22
    Open Connection    ${ip}    port=${port}    timeout=60s
    Login    root    ${EMPTY}    allow_agent=${False}

Run On Device
    [Arguments]    ${command}
    ${output}    ${rc}=    Execute Command    ${command}    return_rc=${True}
    Should Be Equal As Integers    ${rc}    0    '${command}' failed
    RETURN    ${output}

*** Test Cases ***
The device runs Moonforge
    [Documentation]    Verify /etc/os-release identifies the Moonforge distro.
    ${id}=    Run On Device    . /etc/os-release && echo $ID
    Should Be Equal    ${id.strip()}    moonforge

See Robot Framework’s documentation for more details on how to write your tests.

Running the tests

Moonforge provides lab-moonforge-action, which runs your test suite in a container and manages the full hardware lifecycle: reserving a device from a pool, flashing the image, waiting for SSH, running the suite, wiping storage, and releasing the device.

The action supports running tests with pytest, Robot Framework (robot), or a custom command. For pytest or custom commands, use the main action (moonforgelinux/lab-moonforge-action@v0.2.0). In this tutorial we are using Robot Framework, which requires additional libraries, so we use the robot sub-action (moonforgelinux/lab-moonforge-action/robot@v0.2.0) with runner: robot.

Extend the .github/workflows/main.yml file from the Continuous Integration tutorial with a test job:

# .github/workflows/main.yml
jobs:
  build:
    # ... build and upload steps from the Continuous Integration tutorial

  test:
    timeout-minutes: 45
    runs-on: self-hosted
    needs: build
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - uses: moonforgelinux/lab-moonforge-action/robot@v0.2.0
        with:
          runner: robot
          workdir: tests
          run: hello.robot
          dut_pool: raspberrypi5
          dut_image: ${{ vars.S3_PUBLIC_URL }}/builds/${{ github.run_id }}/images/raspberrypi5/moonforge-image-base-raspberrypi5-${{ github.run_id }}.wic.bz2
          dut_control_url: ${{ vars.DUT_CONTROL_URL }}
          dut_control_client_key: ${{ secrets.DUT_CONTROL_CLIENT_KEY }}

Note: The dut_image input in the example above accepts either an http/https URL or a local path relative to workdir.

See the action’s README for all available inputs, outputs, and environment variables.

Uploading test results

Robot Framework writes log.html, report.html, output.xml, and xunit.xml into tests/test-results by default. To publish these reports alongside your build artifacts even when a test fails, add an upload-moonforge-action step to the test job:

# .github/workflows/main.yml
jobs:
  test:
    # ...
    steps:
      - uses: actions/checkout@v6
      - uses: moonforgelinux/lab-moonforge-action/robot@v0.2.0
        # ...
      - uses: moonforgelinux/upload-moonforge-action@v0.2.1
        if: ${{ !cancelled() }}
        with:
          host_base: ${{ secrets.S3_HOST_BASE }}
          access_key: ${{ secrets.S3_ACCESS_KEY }}
          secret_key: ${{ secrets.S3_SECRET_KEY }}
          bucket: ${{ secrets.S3_BUCKET }}
          source: tests/test-results
          destination: 'builds/${{ github.run_id }}/'
          use_https: true

Alternatively, you can upload tests/test-results using GitHub’s actions/upload-artifact action.

Running tests locally

You can also try the test suite locally without a dut-control server.

Install Robot Framework and SSHLibrary:

python3 -m venv .venv
source .venv/bin/activate
pip install robotframework robotframework-sshlibrary

Flash the device manually and run the suite by passing DUT_IP:

DUT_IP=<device-ip> robot tests/hello.robot

Reports are written to the current directory by default. Use the -d argument to specify a different output directory.

A working example

A full working example using lab-moonforge-action and Robot Framework is available in the meta-moonforge repository: see its workflow file and test suites.