type: direct

tasks:
  setup: |
    sudo apt-get -y update
    pkgs=$(qa/admin/list-packages -m -v -x cpan -x pip3 -x not4ci)
    sudo DEBIAN_FRONTEND=noninteractive apt-get install -y $pkgs

    # setup libbpf + BTF to test the bpf PMDA
    # howto: https://github.com/rafaeldtinoco/portablebpf/tree/v4.15-kernels#portable-libbpf-based-ebpf-code-older-kernels
    sudo apt-get install -y libelf-dev libdw-dev ubuntu-dbgsym-keyring cmake llvm clang

    ## install libbpf-dev
    pushd $(mktemp -d)
    wget http://archive.ubuntu.com/ubuntu/pool/main/libb/libbpf/libbpf0_0.4.0-1ubuntu1_amd64.deb
    wget http://archive.ubuntu.com/ubuntu/pool/main/libb/libbpf/libbpf-dev_0.4.0-1ubuntu1_amd64.deb
    sudo dpkg -i libbpf*.deb
    popd

    ## install kernel debugsymbols
    echo "deb http://ddebs.ubuntu.com $(lsb_release -cs) main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list.d/ddebs.list
    echo "deb http://ddebs.ubuntu.com $(lsb_release -cs)-updates main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list.d/ddebs.list
    echo "deb http://ddebs.ubuntu.com $(lsb_release -cs)-proposed main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list.d/ddebs.list
    sudo apt-get update
    sudo apt-get install -y linux-image-$(uname -r)-dbgsym

    ## compile and install dwarves
    pushd $(mktemp -d)
    git clone https://github.com/acmel/dwarves.git
    cd dwarves
    git checkout v1.22
    mkdir build
    cd build
    cmake -D__LIB=lib ..
    sudo make install
    sudo ldconfig # otherwise libs in /usr/local/lib are not considered...(??)
    popd

    ## add a new .BTF ELF section to the original vmlinux file
    sudo pahole -J /usr/lib/debug/boot/vmlinux-$(uname -r)

    ## extract this .BTF section into a new file with a single .BTF section
    sudo llvm-objcopy --only-section=.BTF --set-section-flags .BTF=alloc,readonly --strip-all /usr/lib/debug/boot/vmlinux-$(uname -r) /boot/vmlinux-$(uname -r)
    # verify with `bpftool btf dump file /boot/vmlinux-$(uname -r) format c` (requires linux-tools-generic package)
  build: |
    ./Makepkgs --nonrpm --verbose
    dpkg -I build/deb/pcp_*.deb | grep Depends | grep -q libuv1
  copy_build_artifacts: |
    cp build/deb/*.deb ../artifacts/build
  install: |
    sudo dpkg -i build/deb/*.deb
    echo 'pcpqa ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/pcpqa
  init_qa: |
    sudo systemctl start redis
    sudo -i -u pcpqa ./check 002
  qa_sanity: |
    set -o pipefail
    sudo -i -u pcpqa ./check -TT -g sanity -x not_in_ci |& tee ../artifacts/test/test.log
  qa: |
    set -o pipefail
    DEFAULT_PCP_QA_ARGS="-x x11 -x remote -x not_in_ci"
    sudo -i -u pcpqa ./check -TT ${PCP_QA_ARGS:-${DEFAULT_PCP_QA_ARGS}} |& tee ../artifacts/test/test.log
  copy_test_artifacts: |
    cp /var/lib/pcp/testsuite/check.timings ../artifacts/test
    # add current timestamp if the last QA test got stuck
    [ $(awk 'END{print NF}' ../artifacts/test/check.timings) = 2 ] && date '+%s' >> ../artifacts/test/check.timings
    shopt -s nullglob
    for test in /var/lib/pcp/testsuite/*.out.bad; do cp $test ${test/.out.bad/.full} ../artifacts/test || true; done
