# Provide docker in container for installing dependencies as root.
# https://github.com/Microsoft/azure-pipelines-agent/issues/2043#issuecomment-687983301
resources:
  containers:
  # This no longer works for Fedora
  # - container: fedora_latest
  #   image: fedora:latest
  #   options: '--name runner -v /usr/bin/docker:/usr/bin/docker:ro'
  - container: debian_testing
    image: debian:testing
    options: '--name runner -v /usr/bin/docker:/usr/bin/docker:ro'
  - container: ubuntu_rolling
    image: ubuntu:rolling
    options: '--name runner -v /usr/bin/docker:/usr/bin/docker:ro'

jobs:
- job: BuildTest_dnf
  pool:
    vmImage: ubuntu-latest
  strategy:
    matrix:
      fedora_latest:
        image: registry.fedoraproject.org/fedora:latest
      # Disable CentOS due to missing dependencies
      # centos_7:
      #   image: centos:7
      # centos_8:
      #   image: centos:8
  steps:
  - script: |
      docker build \
          --build-arg BASE_IMAGE=$(image) \
          --target jss-base \
          --tag jss-base:latest \
          .

      docker run \
          --name runner \
          --privileged \
          --detach \
          jss-base:latest

      while :
      do
          docker exec runner echo "Runner is ready" && break
          echo "Waiting for runner..."
          sleep 1
          [ $((++i)) -ge 10 ] && exit 1
      done
    displayName: Create runner container

  - script: |
      docker exec runner dnf install -y dnf-plugins-core rpm-build
      docker exec runner dnf copr enable -y @pki/11.3
      docker cp . runner:/root/src
      docker exec -w /root/src runner dnf builddep -y --spec jss.spec
    displayName: Install build dependencies

  - script: docker exec -w /root/src runner ./build.sh --with-tests
    displayName: Build JSS binaries, Javadoc, and run tests

- job: BuildTest_apt
  pool:
    vmImage: ubuntu-latest
  strategy:
    matrix:
      debian_testing:
        image: debian_testing
      ubuntu_rolling:
        image: ubuntu_rolling
  container: $[variables['image']]
  steps:
  - script: |
      docker exec -u 0 runner apt-get update
      docker exec -u 0 runner apt-get install -y \
          cmake zip unzip \
          g++ libnss3-dev libnss3-tools \
          openjdk-17-jdk libcommons-lang3-java libslf4j-java junit4
    displayName: Install build dependencies

  - script: ./build.sh --with-tests
    displayName: Build JSS binaries, Javadoc, and run tests
    env:
      JAVA_HOME: /usr/lib/jvm/java-17-openjdk-amd64

- job: SymbolTest
  pool:
    vmImage: ubuntu-latest
  steps:
  - script: |
      grep -iroh '^Java_org_mozilla[^(;]*' native/src/main/native/ | sort -u > /tmp/functions.txt
      cat /tmp/functions.txt
    displayName: Get JNI symbols in the code

  - script: |
      grep -iroh '^Java_org_mozilla[^(;]*' lib/ | sort -u > /tmp/version.txt
      cat /tmp/version.txt
    displayName: Get JNI symbols in the version script

  - script: |
      diff /tmp/functions.txt /tmp/version.txt || true
      comm -23 --check-order /tmp/functions.txt /tmp/version.txt > /tmp/diff.txt
      test ! -s /tmp/diff.txt
    displayName: Compare JNI symbols
