---
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of nvme.
# Copyright (c) 2026 SUSE LLC
#
# Authors: Daniel Wagner <dwagner@suse.de>

name: Run nightly tests

on:
  workflow_dispatch:
  schedule:
    - cron: '0 01 * * *'

permissions:
  actions: read
  contents: read

jobs:
  nightly-tests:
    if: ${{ github.event_name == 'workflow_dispatch' || github.repository == 'linux-nvme/nvme-cli' }}
    runs-on: arc-vm-nvme-cli
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          repository: "linux-blktests/blktests-ci"
      #We don't have to build the kernel here, as we have a cron job running on
      #the k8s cluster that builds the linus-master target nightly.
      - name: Run blktests in VM
        id: blktests
        uses: ./.github/actions/kubevirt-action
        with:
          host_devices: "nvme-wdc-zn540,nvme-wdc-sn640"
          kernel_version: linus-master
          vm_artifact_upload_dir: blktests/results/
          run_cmds: |
            set -e
            set -x
            uname -a

            sudo dnf remove -y nvme-cli libnvme

            sudo dnf install -y gcc clang make util-linux llvm gawk fio udev \
            kmod coreutils g++ gzip e2fsprogs xfsprogs f2fs-tools btrfs-progs \
            device-mapper-multipath blktrace kernel-headers liburing \
            liburing-devel nbd device-mapper ktls-utils dosfstools \
            bc libnl3-cli cryptsetup sg3_utils pciutils unzip jq git wget \
            meson json-c-devel openssl-devel keyutils-libs-devel dbus-devel \
            swig xz-devel libcurl-devel curl libarchive libarchive-devel json-c \
            expect gcovr

            # mdadm has a buffer overflow in v4.3 and was fixed with this commit:
            # https://git.kernel.org/pub/scm/utils/mdadm/mdadm.git/commit/?id=7f960c3bd050e76f8bf0a8a0c8fbdcbaa565fc78
            # The Fedora packages ship v4.3.
            # We are now compiling and installing mdadm from source with the most
            # recent known good commit. The v4.4 release does not compile.

            git clone https://git.kernel.org/pub/scm/utils/mdadm/mdadm.git
            cd mdadm
            git checkout d764c4829947923142a83251296d04edaee7d2f7
            make -j$(nproc)
            sudo make install
            cd -

            git clone https://github.com/${{ github.repository }} nvme-cli
            cd nvme-cli
            git checkout ${{ github.sha }}
            scripts/build.sh -b release -c gcc -p

            # IMPORTANT: do NOT install for coverage run
            export PATH="$PWD/.build-ci:$PATH"

            cd -
            git clone https://github.com/linux-blktests/blktests.git blktests

            cd blktests
            make
            mkdir -p results
            nvme --version | tee -a results/nvme-cli.version
            sudo env PATH="$PWD/.build-ci:$PATH" nvme --version | tee -a results/nvme-cli.version

            # Run blktests nvme and md group
            cat > config << EF
            TEST_DEVS=(${BDEV0})
            NVMET_TRTYPES="loop rdma tcp"
            EXCLUDE=(nvme/010 nvme/012 nvme/034 nvme/035)
            EF
            set +e
            sudo env PATH="$PWD/.build-ci:$PATH" ./check nvme md/001
            blktests_status=$?
            set -e

            cd ../nvme-cli
            sudo find .build-ci -name '*.gcda' -exec chown "$(id -u):$(id -g)" {} +
            gcovr -r . --object-directory .build-ci --xml-pretty -o coverage.xml
            cp coverage.xml ../blktests/results/coverage-blktests.xml

            exit "${blktests_status}"

      - name: Run nvme-cli tests in VM
        if: success() || steps.blktests.conclusion == 'failure'
        uses: ./.github/actions/kubevirt-action
        with:
          host_devices: "nvme-wdc-zn540,nvme-wdc-sn640"
          kernel_version: linus-master
          vm_artifact_upload_dir: nvme-cli/tests/nvmetests/
          run_cmds: |
            #Preventing bash variable expension for the outer cat by using
            #single quotes around EOF
            cat > test.sh << 'EOF'
            #!/bin/bash
            set -e
            set -x
            uname -a
            git clone https://github.com/${{ github.repository }} /nvme-cli
            git config --global --add safe.directory /nvme-cli
            cd /nvme-cli
            git checkout ${{ github.sha }}
            CONTROLLER=$(echo "${BDEV0}" | sed 's/n[0-9]*$//')

            scripts/build.sh -b release -c gcc -p \
                --e2e-controller "${CONTROLLER}" \
                --e2e-ns1 "${BDEV0}" \
                --e2e-log-dir "/nvme-cli/tests/nvmetests" \
                --e2e-log-level "DEBUG" \
                --e2e-nvme-bin "/nvme-cli/.build-ci/nvme" \
                --plugin-tests ocp,micron \
                tests

            cp coverage.xml tests/nvmetests/coverage-nvmetests.xml

            EOF

            sudo chmod +x test.sh

            #Create shared volume mount point for artifact upload
            mkdir -p nvme-cli

            #Expose all devices to the container through the `privileged` flag.
            #
            #BDEV0 is an environment variable of the self-hosted runner instance
            #that contains a valid nvme namespace which is capable of the nvm
            #command set.

            sudo podman run --privileged \
            -v /dev:/dev \
            -e BDEV0 \
            -v "${PWD}/test.sh":"/test.sh" \
            -v "${PWD}/nvme-cli":"/nvme-cli":z \
            ghcr.io/linux-nvme/debian:latest /test.sh
            podman_exit=$?

            # Copy the results into the expected upload directory
            sudo cp -r nvme-cli/.build-ci/meson-logs \
              nvme-cli/tests/nvmetests 2>/dev/null || true

            exit $podman_exit

  codecov-upload:
    if: ${{ always() && (github.event_name == 'workflow_dispatch' || github.repository == 'linux-nvme/nvme-cli') }}
    needs: nightly-tests
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
      - name: Download nightly coverage reports
        uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
        with:
          pattern: artifact-*
          path: coverage
          merge-multiple: true

      - name: Collect nightly coverage reports
        id: collect-coverage
        shell: bash
        run: |
          files=$(find coverage -type f -name 'coverage-*.xml' -print | sort | paste -sd, -)
          echo "files=${files}" >> "${GITHUB_OUTPUT}"

      - name: Upload nightly coverage to Codecov
        if: steps.collect-coverage.outputs.files != ''
        uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
        env:
          CODECOV_WORKING_DIR: ${{ github.workspace }}
        with:
          root_dir: ${{ github.workspace }}
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ${{ steps.collect-coverage.outputs.files }}
          fail_ci_if_error: false
