cargo_task_tempate: &cargo_task_template
  registry_cache:
    folder: $CARGO_HOME/registry
    fingerprint_script: cat Cargo.lock
  target_cache:
    folder: target
    fingerprint_script:
      - rustc --version
      - cat Cargo.lock
  setup_script:
    - apt-get update
    - apt-get -y install llvm clang libelf-dev libpcap-dev python3-pip make jq
    - rustup component add rustfmt
    - rustup component add clippy
  before_cache_script: rm -rf $CARGO_HOME/registry/index

unittest_task:
  << : *cargo_task_template
  container:
    image: rust:bookworm
  build_script: make V=1 CARGO_OPTS=--verbose
  test_script: make test V=1 CARGO_OPTS=--verbose
  check_script:
    - cargo fmt --check
    - cargo clippy -- -D warnings

benchmark_task:
  << : *cargo_task_template
  container:
    image: rust:bookworm
  build_script: make bench V=1 CARGO_OPTS=--verbose
  test_script:
    - ./target/release/retis benchmark --ci events_parsing
    - ./target/release/retis benchmark --ci events_output
  check_script:
    - cargo clippy -F benchmark -- -D warnings

aarch64_task:
  << : *cargo_task_template
  name: aarch64 build & basic tests
  arm_container:
    image: rust:bookworm
  build_script:
    - ln -s /usr/include/asm-generic /usr/include/asm
    - make V=1 CARGO_OPTS=--verbose
  test_script: make test V=1 CARGO_OPTS=--verbose

python_task:
  name: Python lint & format
  container:
    image: python:3.11-slim
  lint_script:
    - python3 -m pip install flake8
    - cd tests && python3 -m flake8
  format_script:
    - python3 -m pip install black
    - python3 -m black --check --diff tests/*.py

build_python_lib_task:
  container:
    image: ghcr.io/pyo3/maturin
  build_script:
    - maturin build --release -m retis-events/Cargo.toml -F python-lib
  dist_artifacts:
    path: "target/wheels/*"

test_python_lib_task:
  container:
    matrix:
      - image: python:3.8
      - image: python:3.9
      - image: python:3.10
      - image: python:3.11
      - image: python:3.12
  setup_script:
    - apt-get update
    - apt-get install -y curl
    - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup-init.sh
    - chmod +x rustup-init.sh && ./rustup-init.sh -y --profile minimal
  build_script:
    - source $HOME/.cargo/env
    - python -m pip install tox
    - make pytest

functional_task:
  # Run for PRs with a specific label
  required_pr_labels: run-functional-tests
  only_if: "$CIRRUS_PR != ''"

  matrix: &distro_matrix
    - name: Fedora 41 (x86_64)
      env:
        DISTRO: x86_64-f41
        TEST_EXTRA_ARGS: "--ovs-track"
    - name: Fedora Rawhide (x86_64)
      env:
        DISTRO: x86_64-rawhide
        TEST_EXTRA_ARGS: "--ovs-track"
    - name: Centos 8 Stream (x86_64)
      env:
        DISTRO: x86_64-c8s
        TEST_EXTRA_ARGS: "--ignore=test_ovs.py --ignore=test_nft.py --ignore=test_pcap.py"
        CARGO_CMD_OPTS: "--no-default-features"
    - name: Centos 9 Stream (x86_64)
      env:
        DISTRO: x86_64-c9s
        TEST_EXTRA_ARGS: "--ovs-track"
    - name: Centos 10 Stream (x86_64)
      env:
        DISTRO: x86_64-c10s
        TEST_EXTRA_ARGS: "--ovs-track"
    - name: Ubuntu Jammy (x86_64)
      env:
        DISTRO: x86_64-jammy

  compute_engine_instance: &nestedvm
    image_project: cirrus-images
    image: family/docker-kvm
    platform: linux
    cpu: 4
    memory: 16G
    nested_virtualization: true

  # Initialization of the test environment
  setup_script: &vagrant_setup
    - grep -q vmx /proc/cpuinfo # Ensure nested virtualization is enabled
      # The version of vagrant shipped with ubuntu fails to download some boxes (e.g: f38).
      # See https://bugs.launchpad.net/vagrant/+bug/2017828.
      # Installing it from Hashicorp directly
    - wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
    - echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/hashicorp.list
    - DEBIAN_FRONTEND=noninteractive apt-get -y update && DEBIAN_FRONTEND=noninteractiven apt-get -y install vagrant ruby-libvirt qemu-kvm virt-manager libvirt-daemon-system virtinst libvirt-clients bridge-utils pkg-config libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev gcc make ruby-nokogiri
    - vagrant plugin install vagrant-libvirt
    - systemctl enable --now libvirtd

  # Download, provision and cache test image(s).
  vagrant_cache: &vagrant_cache
    reupload_on_changes: true
    fingerprint_script: |
      # Invalidate once a week
      date -d 'next monday - 1 week' +%Y%m%d
      echo ${DISTRO}; cat Vagrantfile
    folder: /root/.vagrant.d/boxes

  up_script: &vagrant_up
    - vagrant box update -f ${DISTRO}
    - vagrant box prune -f
    - vagrant up ${DISTRO} --no-tty || vagrant up ${DISTRO} --no-tty # Retry to overcome network glitches.
    - mkdir -p -m 0700 /root/.ssh
    - vagrant ssh-config ${DISTRO} >> /root/.ssh/config

  upload_caches: vagrant

  guest_info_script: &guest_info
    - echo "--- Host info ---"
    - ssh ${DISTRO} 'sh -exc "uname -a"'

  test_script: &functional_test
    - ssh -tt ${DISTRO} "env CARGO_CMD_OPTS=$CARGO_CMD_OPTS bash --login -exc 'cd /vagrant && make V=1'"
    - ssh -tt ${DISTRO} "cd /vagrant/tests && sudo python3 -m pytest ${TEST_EXTRA_ARGS}"

# Manual trigger for non-PR branches.
manual_test_funtional_task:
  trigger_type: manual
  only_if: "$CIRRUS_BRANCH !=~ 'pull/.*'"
  matrix: *distro_matrix
  compute_engine_instance: *nestedvm
  setup_script: *vagrant_setup
  vagrant_cache: *vagrant_cache
  up_script: *vagrant_up
  guest_info_script: *guest_info
  test_script: *functional_test