name: CI

on:
  pull_request:
  push:
    branches: [main]

env:
  CARGO_TERM_COLOR: always

jobs:
  checks:
    name: checks
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo fmt -- --check
      - run: cargo clippy --all-targets -- -D warnings
      - run: cargo build --release

  vm-tests:
    name: vm-tests
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      # /dev/kvm exists on GitHub's standard runners but isn't accessible
      # to the runner user by default
      - name: Enable KVM for the runner user
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' \
            | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm
      - name: Install QEMU and ISO tooling
        run: |
          sudo apt-get update -qq
          sudo apt-get install -y -qq qemu-system-x86 qemu-utils genisoimage
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Cache Debian cloud base image
        uses: actions/cache@v4
        with:
          path: ~/.cache/timberfs-vm-tests
          key: debian-13-genericcloud-amd64
      - uses: taiki-e/install-action@v2
        with:
          tool: cargo-deb
      - name: Build the .deb
        run: cargo deb
      - name: Run the VM package test suite
        run: tests/vm/run-vm-test.sh
