---
# yamllint disable rule:line-length

# reduce load on CI systems by not running the pipeline for all changes
# https://docs.gitlab.com/ci/yaml/workflow/#common-if-clauses-for-workflowrules
# https://docs.gitlab.com/ci/jobs/job_rules/#ci_pipeline_source-predefined-variable
workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
    - if: $CI_PIPELINE_SOURCE == "web"
    - if: $CI_PIPELINE_SOURCE == "parent_pipeline"
    - when: never

variables:
  # override these by CI variables if needed
  ENABLE_FIPS:
    description: Enable FIPS mode in testing farm. Set to empty to disable FIPS
    value: --insert --order 0 --how feature --fips enabled
    options:
      - --insert --order 0 --how feature --fips enabled
      - ""
  PLAN_FILTER:
    description: Predefined plan filters for testing farm. Default is empty
      which means run all plans.
    value: "name:/tmt/plans/.*"
    options:
      - ""
      - "name:/tmt/plans/.*"
      - "name:/tmt/plans/ansible_versions/ansible_29/tier0"
      - "name:/tmt/plans/ansible_versions/ansible_29/tier1/cib"
      - "name:/tmt/plans/ansible_versions/ansible_29/tier1/cluster"
      - "name:/tmt/plans/ansible_versions/ansible_29/tier1/mixed"
      - "name:/tmt/plans/ansible_versions/ansible_29/tier1/sbd"
      - "name:/tmt/plans/ansible_versions/ansible_current/tier0"
      - "name:/tmt/plans/ansible_versions/ansible_current/tier1/cib"
      - "name:/tmt/plans/ansible_versions/ansible_current/tier1/cluster"
      - "name:/tmt/plans/ansible_versions/ansible_current/tier1/mixed"
      - "name:/tmt/plans/ansible_versions/ansible_current/tier1/sbd"
  TEST_FILTER:
    description: Predefined test filters for testing farm. Default is empty
      which means run all test.
    value: ""
    options:
      - ""
      - "tag:tier0"
      - "tag:tier1"
      - "tag:tier0 & tag:ansible"
      - "tag:tier0 & tag:ansible29"
      - "tag:tier1 & tag:cib"
      - "tag:tier1 & tag:cluster"
      - "tag:tier1 & tag:mixed"
      - "tag:tier1 & tag:sbd"
  TFCLI_PLAN_FILTER:
    description: Set to $PLAN_FILTER to use a predefined plan filter or specify
      a custom one.
    value: $PLAN_FILTER
  TFCLI_TEST_FILTER:
    description: Set to $TEST_FILTER to use a predefined test filter or specify
      a custom one.
    value: $TEST_FILTER
  TFCLI_TMT_PREPARE: $ENABLE_FIPS
  TF_REQUEST_ID_FILE: /tmp/tf_request_id-$CI_JOB_ID
  TF_EXIT_CODE_FILE: /tmp/tf_exit_code-$CI_JOB_ID
  LSR_ARTIFACTS_URL: $CI_API_V4_URL/projects/pcs%2Fci-job-dependencies/jobs/artifacts/main/download?job=lsr-auto-maintenance
  WOKE_ARTIFACTS_URL: $CI_API_V4_URL/projects/pcs%2Fci-job-dependencies/jobs/artifacts/main/download?job=woke
  # for context variable development to distinguish development compose
  TMT_DEVELOPMENT: false
  PLAN_VARIANTS: ansible_current ansible_29


# https://docs.gitlab.com/ci/yaml/#parallelmatrix
# COPMOSE_NAME
#   * values must use only letters and numbers because they are used as part
#     of rpm name
#   * must be kept short because they are used in job names
#   * other pipelines use a job name to download the job's artifacts
# TF_COMPOSE
#   * name of a testing farm compose
#   * list of composes:
#     https://api.testing-farm.io/v0.1/composes/public
#     https://api.testing-farm.io/v0.1/composes/redhat
# TMT_DISTRO
#   * value for the tmt context variable distro:
#     https://tmt.readthedocs.io/en/stable/spec/context.html
#     https://fmf.readthedocs.io/en/latest/context.html
#
.parallel:
  parallel:
    matrix:
      - COMPOSE_NAME:
          - Rhel10Next
          - Rhel10CurrentRelease
          - Rhel9Next
          - Rhel9CurrentRelease
          - Rhel8Next
          - Rhel8Oldest
  rules:
    - if: $COMPOSE_NAME == "Rhel10Next"
      variables:
        TF_COMPOSE: RHEL-10.2-Nightly
        TMT_DEVELOPMENT: true
        TMT_DISTRO: rhel-10.2
        PCS_BRANCH: "main"
        PCS_ARTIFACTS_URL: "${CI_API_V4_URL}/projects/pcs%2Fpcs/jobs/\
          artifacts/${PCS_BRANCH}/download?\
          job=tf_tests%3A%20%5B${COMPOSE_NAME}%5D"
    - if: $COMPOSE_NAME == "Rhel10CurrentRelease"
      variables:
        TF_COMPOSE: RHEL-10.1-Nightly
        TMT_DISTRO: rhel-10.1
        PCS_BRANCH: "main"
        PCS_ARTIFACTS_URL: "${CI_API_V4_URL}/projects/pcs%2Fpcs/jobs/\
          artifacts/${PCS_BRANCH}/download?\
          job=tf_tests%3A%20%5B${COMPOSE_NAME}%5D"
    - if: $COMPOSE_NAME == "Rhel9Next"
      variables:
        TF_COMPOSE: RHEL-9.8.0-Nightly
        TMT_DEVELOPMENT: true
        TMT_DISTRO: rhel-9.8
        PCS_BRANCH: "pcs-0.11"
        PCS_ARTIFACTS_URL: "${CI_API_V4_URL}/projects/pcs%2Fpcs/jobs/\
          artifacts/${PCS_BRANCH}/download?\
          job=tf_tests%3A%20%5B${COMPOSE_NAME}%5D"
    - if: $COMPOSE_NAME == "Rhel9CurrentRelease"
      variables:
        TF_COMPOSE: RHEL-9.7.0-Nightly
        TMT_DISTRO: rhel-9.7
        PCS_BRANCH: "pcs-0.11"
        PCS_ARTIFACTS_URL: "${CI_API_V4_URL}/projects/pcs%2Fpcs/jobs/\
          artifacts/${PCS_BRANCH}/download?\
          job=tf_tests%3A%20%5B${COMPOSE_NAME}%5D"
    - if: $COMPOSE_NAME == "Rhel8Next"
      variables:
        TF_COMPOSE: RHEL-8.10.0-Nightly
        TMT_DEVELOPMENT: true
        TMT_DISTRO: rhel-8.10
        PCS_BRANCH: "pcs-0.10"
        PCS_ARTIFACTS_URL: "${CI_API_V4_URL}/projects/pcs%2Fpcs/jobs/\
          artifacts/${PCS_BRANCH}/download?\
          job=rpm_build%3A%20%5BPcsRhel8Next%5D"
    - if: $COMPOSE_NAME == "Rhel8Oldest"
      variables:
        TF_COMPOSE: RHEL-8.4.0-Nightly
        TMT_DISTRO: rhel-8.4
        PCS_BRANCH: "pcs-0.10"
        PCS_ARTIFACTS_URL: "${CI_API_V4_URL}/projects/pcs%2Fpcs/jobs/\
          artifacts/${PCS_BRANCH}/download?\
          job=rpm_build%3A%20%5BPcsRhel8Next%5D"

.download_artifacts_from_tf: &download_artifacts_from_tf
  - UUID_REGEX="[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}"
  - REQUEST_ID=$(sed -n -E "s/.*($UUID_REGEX).*/\1/p" output.txt | head -n 1)
  - echo "$REQUEST_ID" > "$TF_REQUEST_ID_FILE"
  - echo "Testing Farm request id is $REQUEST_ID"
  - EXIT_CODE=0
  - testing-farm watch --id "$REQUEST_ID" || EXIT_CODE=$?
  - echo $EXIT_CODE > "$TF_EXIT_CODE_FILE"
  - >
    curl --insecure --location --remote-name \
      "https://artifacts.osci.redhat.com/testing-farm/$REQUEST_ID/results.xml"
  - >
    time for PLAN in $PLAN_VARIANTS; do
      for ARTIFACT_URL in $(
        xmllint \
          --xpath "//testcase[contains(@name, 'tests/tier1')]/*/log[@name='data' and contains(@href, '$PLAN')]/@href" \
          results.xml \
        | sed "s/.*=\"\(.*\)\"/\1/"
      ); do
        wget --no-verbose --no-parent -r --reject '*.html*' \
          --no-host-directories --directory-prefix "var_log/$PLAN" \
          --cut-dirs=19 "$ARTIFACT_URL";
      done
    done
  - exit $EXIT_CODE

tf_tests:
  extends: .parallel
  image: $SHARED_RUNNER_CONTAINER_IMAGE
  script:
    # increased parallel-limit because we have 10 plans (default: 5)
    - testing-farm request
      --parallel-limit 10
      --git-url
      https://gitlab-ci-token:$CI_JOB_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH
      --git-ref $CI_COMMIT_REF_NAME
      --compose $TF_COMPOSE
      --context development=$TMT_DEVELOPMENT
      --context distro=$TMT_DISTRO
      --context initiator=gitlab-ci
      --context ci-dependencies=github
      --secret CI_JOB_TOKEN="$CI_JOB_TOKEN"
      --environment PCS_ARTIFACTS_URL="$PCS_ARTIFACTS_URL"
      --environment LSR_ARTIFACTS_URL="$LSR_ARTIFACTS_URL"
      --environment WOKE_ARTIFACTS_URL="$WOKE_ARTIFACTS_URL"
      --plan-filter "$TFCLI_PLAN_FILTER"
      --test-filter "$TFCLI_TEST_FILTER"
      --tmt-prepare "$TFCLI_TMT_PREPARE"
      --no-wait
      | tee output.txt
    - *download_artifacts_from_tf
  after_script:
    - REQUEST_ID=$(<"$TF_REQUEST_ID_FILE")
    - EXIT_CODE=$(<"$TF_EXIT_CODE_FILE")
    - echo "CI_JOB_STATUS=$CI_JOB_STATUS EXIT_CODE=$EXIT_CODE"
    - >
      if [[ "$CI_JOB_STATUS" != "success" && "$EXIT_CODE" != "1" ]]; then \
        echo "Canceling testing farm request $REQUEST_ID"; \
        testing-farm cancel "$REQUEST_ID"; \
      fi
  tags:
    - $SHARED_RUNNER_TAG
  artifacts:
    name: "ha_cluster-${CI_COMMIT_SHORT_SHA}-${TF_COMPOSE}"
    expire_in: 1 week
    when: always
    paths:
      - var_log/ansible*/var_log*
