# Run the jobs below on every push to master branch and pull request.
on:
  push:
    branches:
      - master
      - 'ci/**'  # Allow automatic CI testing on ci/* branches
  pull_request:
    branches:
      - master
  workflow_dispatch:  # Allow manual triggering on any branch

# Runs basic configure, make and make check.
jobs:
  ubuntu:
    name: Ubuntu
    runs-on: ubuntu-${{ matrix.release }}
    continue-on-error: true
    strategy:
      fail-fast: false
      matrix:
        release:
          - 24.04

    steps:
      - name: Identify the system
        run: |
          cat /etc/os-release

      - name: Enable source repositories
        run: |
          sudo sed -i 's/Types: deb$/Types: deb deb-src/g' \
                   /etc/apt/sources.list.d/ubuntu.sources

      - name: Install build dependencies
        run: |
          sudo apt-get update
          sudo apt-get -y build-dep libguestfs

      - name: Install extra dependencies
        run: |
          # Git is needed to run git submodule command.
          # json-c is missing from the Ubuntu package deps.
          # python libvirt module used unconditionally in a python test
          sudo apt-get -y install git libjson-c-dev python3-libvirt

      - name: Fix broken Ubuntu kernel permissions
        run: |
          # https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725
          sudo chmod 0644 /boot/vmlinuz*

      - name: Enable KVM
        run: |
          sudo chmod 0666 /dev/kvm

      - name: Checkout sources
        uses: actions/checkout@v5

      - name: Checkout submodule
        run: |
          git submodule update --init

      - name: Compile the code
        run: |
          autoreconf -fiv
          # --disable-ruby needed for
          # https://github.com/libguestfs/libguestfs/issues/309
          ./configure --enable-werror --disable-ruby
          make -j

      - name: Run the quick test
        run: |
          make quickcheck

      - name: Run the full tests
        run: |
          # grub-install: error: /usr/lib/grub/i386-pc/modinfo.sh
          # doesn't exist. Please specify --target or --directory
          export SKIP_TEST_GRUB_INSTALL_0=1
          # Errors from hexdump, maybe not installed?
          export SKIP_TEST_HEXDUMP_0=1
          export SKIP_TEST_HEXDUMP_1=1
          export SKIP_TEST_HEXDUMP_2=1
          # error: luks_close: cryptsetup exited with status 5:
          # Device lukstest is still in use
          export SKIP_TEST_LUKS_SH=1
          # error: passt exited with status 1
          export SKIP_TEST_NETWORK_SH=1
          # error: passt exited with status 1
          export SKIP_TEST_RSYNC_SH=1
          if ! make check; then
              find -name test-suite.log -exec cat {} \;
              exit 1
          fi

  fedora:
    name: Fedora
    runs-on: ubuntu-latest  # VM where the container runs
    strategy:
      fail-fast: false
      matrix:
        release:
         - 42
         - 43

    container:
      image: quay.io/fedora/fedora:${{ matrix.release }}
      options: --privileged

    steps:
      - name: Identify the system
        run: |
          cat /etc/os-release

      - name: Install build dependencies
        run: |
          dnf builddep -y libguestfs

      - name: Install extra dependencies
        run: |
          # Git is needed to run git submodule command.
          # The others are needed to run the tests.
          dnf install -y git kernel sqlite perl-hivex rubygem-minitest

      - name: Checkout sources
        uses: actions/checkout@v5

      - name: Checkout submodule
        run: |
          git config --global --add safe.directory $PWD
          git submodule update --init

      - name: Compile the code
        run: |
            autoreconf -fiv &&
            ./configure CFLAGS="-fPIC -g -O2" --enable-werror &&
            make -j

      - name: Run the quick test
        run: |
          make quickcheck

      - name: Sanity check libvirt connection
        run: |
          virtqemud --daemon
          virsh uri

      - name: Run the full tests
        run: |
          # guestunmount exit(3) doesn't trigger for root user
          # https://github.com/libguestfs/libguestfs/issues/307
          export SKIP_TEST_GUESTUNMOUNT_FD=1

          # test-fuse-unmount-race.sh is flakey:
          # ./test-fuse-umount-race.sh: wait for guestmount to exit failed after 10 seconds
          export SKIP_TEST_FUSE_UMOUNT_RACE_SH=1

          # test-rsync.sh is busted for root user
          # rsync_out: '/dir2/' to 'rsync://root@169.254.2.2:65257/dest/': rsync: [generator] failed to set times on "." (in dest): Operation not permitted (1)
          export SKIP_TEST_RSYNC_SH=1

          if ! make check; then
              find -name test-suite.log -exec cat {} \;
              exit 1
          fi
