---
- xtesting-jobs: &xtesting-jobs
    name: 'xtesting-jobs'
    current-parameters: true

- xtesting-params: &xtesting-params
    name: 'xtesting-params'
    repo: 'opnfv'
    port:
    tag:
      - latest:
          branch: master
      - iruya:
          branch: stable/iruya
      - hunter:
          branch: stable/hunter

- parameter:
    name: xtesting-slave
    parameters:
      - label:
          name: slave
          default: lf-build2

- parameter:
    name: xtesting-branch
    parameters:
      - string:
          name: branch
          default: '{branch}'

- xtesting-containers: &xtesting-containers
    name: 'xtesting-containers'
    repo: '{repo}'
    port: '{port}'
    container: '{container}'
    tag: '{tag}'

- xtesting-run-containers: &xtesting-run-containers
    name: 'xtesting-build-containers'
    <<: *xtesting-containers
    test: '{test}'

- xtesting-build-containers: &xtesting-build-containers
    name: 'xtesting-build-containers'
    <<: *xtesting-containers
    ref_arg: '{ref_arg}'
    path: '{path}'

- builder:
    name: xtesting-pull-containers
    builders:
      - shell: |
          set +x
          if [ "{repo}" = "_" ]; then
            image={container}:{tag}
          elif [ "{port}" = "None" ]; then
            image={repo}/{container}:{tag}
          else
            image={repo}:{port}/{container}:{tag}
          fi
          sudo docker pull $image

- builder:
    name: xtesting-build-containers
    builders:
      - shell: |
          set +x
          if [ "{repo}" = "_" ]; then
            image={container}:{tag}
          elif [ "{port}" = "None" ]; then
            image={repo}/{container}:{tag}
          else
            image={repo}:{port}/{container}:{tag}
          fi
          if [ "{ref_arg}" = "None" ]; then
            build_arg=""
          else
            build_arg="--build-arg {ref_arg}={ref}"
          fi
          cd {path}
          sudo docker build $build_arg \
            --pull=false --no-cache --force-rm=true \
            -t $image .

- builder:
    name: xtesting-run-containers
    builders:
      - shell: |
          set +ex
          [ ! -z "$WORKSPACE" ] && sudo rm -rf $WORKSPACE/* || true
          if [ "{repo}" = "_" ]; then
            image={container}:{tag}
          elif [ "{port}" = "None" ]; then
            image={repo}/{container}:{tag}
          else
            image={repo}:{port}/{container}:{tag}
          fi
          sudo docker run --rm \
            -v $WORKSPACE/../$JOB_NAME/results:/var/lib/xtesting/results \
            -e DEBUG=true \
            $image run_tests -t {test}
          res=$?
          gsutil -m cp -r $WORKSPACE/ \
            gs://artifacts.opnfv.org/xtesting/$JOB_NAME-$BUILD_ID > /dev/null 2>&1
          find $WORKSPACE -type f \
            -printf \
            "http://artifacts.opnfv.org/xtesting/$JOB_NAME-$BUILD_ID/%P\n"
          exit $res

- builder:
    name: xtesting-remove-images
    builders:
      - shell: |
          set +x
          if [ "{repo}" = "_" ]; then
            image={container}:{tag}
          elif [ "{port}" = "None" ]; then
            image={repo}/{container}:{tag}
          else
            image={repo}:{port}/{container}:{tag}
          fi
          sudo docker rmi $image || true

- scm:
    name: xtesting-scm
    scm:
      - git:
          url: https://gerrit.opnfv.org/gerrit/functest-xtesting
          refspec: '+refs/changes/*:refs/changes/*'
          branches:
            - '{ref}'

- job-template:
    name: 'xtesting-{repo}-{container}-{tag}-pull'
    parameters:
      - xtesting-slave
    builders:
      - xtesting-pull-containers:
          <<: *xtesting-containers

- project:
    name: 'xtesting-opnfv-xtesting-pull'
    <<: *xtesting-params
    container: 'xtesting'
    jobs:
      - 'xtesting-{repo}-{container}-{tag}-pull'

- job-template:
    name: 'xtesting-{repo}-{container}-{tag}-rmi'
    parameters:
      - xtesting-slave
    builders:
      - xtesting-remove-images:
          <<: *xtesting-containers

- project:
    name: 'xtesting-opnfv-xtesting-rmi'
    <<: *xtesting-params
    container: 'xtesting'
    jobs:
      - 'xtesting-{repo}-{container}-{tag}-rmi'

- job-template:
    name: 'xtesting-{repo}-{container}-{tag}-{test}-run'
    parameters:
      - xtesting-slave
    builders:
      - xtesting-run-containers:
          <<: *xtesting-run-containers

- project:
    name: 'xtesting-opnfv-xtesting'
    <<: *xtesting-params
    container: 'xtesting'
    test:
      - first
      - second
      - third
      - fourth
      - fifth
    jobs:
      - 'xtesting-{repo}-{container}-{tag}-{test}-run'

- job-template:
    name: 'xtesting-{tag}-daily'
    project-type: multijob
    triggers:
      - timed: '@daily'
    parameters:
      - xtesting-slave
    properties:
      - build-blocker:
          use-build-blocker: true
          blocking-level: 'NODE'
          blocking-jobs:
            - '^xtesting-[a-z]+-(daily|check|gate)$'
    builders:
      - multijob:
          name: remove former images
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-rmi'
              <<: *xtesting-jobs
      - multijob:
          name: pull containers
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-pull'
              <<: *xtesting-jobs
      - multijob:
          name: opnfv/xtesting:{tag}
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-first-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-second-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-third-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-fourth-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-fifth-run'
              <<: *xtesting-jobs

- job-template:
    name: 'xtesting-{repo}-{container}-{tag}-gate'
    parameters:
      - xtesting-slave
    scm:
      - xtesting-scm:
          ref: $GERRIT_REFSPEC
    builders:
      - xtesting-build-containers:
          <<: *xtesting-build-containers
          ref: $GERRIT_REFSPEC

- job-template:
    name: 'xtesting-{repo}-{container}-{tag}-check'
    parameters:
      - xtesting-slave
      - xtesting-branch:
          branch: '{branch}'
    scm:
      - xtesting-scm:
          ref: $branch
    builders:
      - xtesting-build-containers:
          <<: *xtesting-build-containers
          ref: $branch

- project:
    name: 'xtesting-_-alpine-3.9-rmi'
    repo: _
    port:
    container: alpine
    tag: 3.9
    jobs:
      - 'xtesting-{repo}-{container}-{tag}-rmi'

- project:
    name: 'xtesting-_-alpine-3.9-pull'
    repo: _
    port:
    container: alpine
    tag: 3.9
    jobs:
      - 'xtesting-{repo}-{container}-{tag}-pull'

- project:
    name: xtesting-opnfv-xtesting-{tag}-build
    <<: *xtesting-params
    container: xtesting
    ref_arg: BRANCH
    path: docker
    jobs:
      - 'xtesting-{repo}-{container}-{tag}-gate'
      - 'xtesting-{repo}-{container}-{tag}-check'

- job-template:
    name: 'xtesting-{tag}-check'
    project-type: multijob
    parameters:
      - xtesting-slave
      - xtesting-branch:
          branch: '{branch}'
    properties:
      - build-blocker:
          use-build-blocker: true
          blocking-level: 'NODE'
          blocking-jobs:
            - '^xtesting-[a-z]+-(daily|check|gate)$'
    builders:
      - multijob:
          name: remove former images
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-rmi'
              <<: *xtesting-jobs
      - multijob:
          name: remove dependencies
          projects:
            - name: 'xtesting-_-alpine-3.9-rmi'
              <<: *xtesting-jobs
      - multijob:
          name: pull dependencies
          projects:
            - name: 'xtesting-_-alpine-3.9-pull'
              <<: *xtesting-jobs
      - multijob:
          name: opnfv/functest-xtesting
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-check'
              <<: *xtesting-jobs
      - multijob:
          name: opnfv/xtesting:{tag}
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-first-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-second-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-third-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-fourth-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-fifth-run'
              <<: *xtesting-jobs

- trigger:
    name: xtesting-patchset-created
    triggers:
      - gerrit:
          server-name: 'gerrit.opnfv.org'
          trigger-on:
            - patchset-created-event
            - comment-added-contains-event:
                comment-contains-value: 'recheck'
            - comment-added-contains-event:
                comment-contains-value: 'reverify'
          projects:
            - project-compare-type: 'ANT'
              project-pattern: 'functest-xtesting'
              branches:
                - branch-compare-type: 'ANT'
                  branch-pattern: '**/{branch}'
          skip-vote:
            successful: false
            failed: false
            unstable: false
            notbuilt: false

- job-template:
    name: 'xtesting-{tag}-gate'
    project-type: multijob
    triggers:
      - xtesting-patchset-created:
          branch: '{branch}'
    parameters:
      - xtesting-slave
    properties:
      - build-blocker:
          use-build-blocker: true
          blocking-level: 'NODE'
          blocking-jobs:
            - '^xtesting-[a-z]+-(daily|check|gate)$'
    builders:
      - multijob:
          name: remove former images
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-rmi'
              <<: *xtesting-jobs
      - multijob:
          name: remove dependencies
          projects:
            - name: 'xtesting-_-alpine-3.9-rmi'
              <<: *xtesting-jobs
      - multijob:
          name: pull dependencies
          projects:
            - name: 'xtesting-_-alpine-3.9-pull'
              <<: *xtesting-jobs
      - multijob:
          name: opnfv/functest-xtesting
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-gate'
              <<: *xtesting-jobs
      - multijob:
          name: opnfv/xtesting:{tag}
          projects:
            - name: 'xtesting-opnfv-xtesting-{tag}-first-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-second-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-third-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-fourth-run'
              <<: *xtesting-jobs
            - name: 'xtesting-opnfv-xtesting-{tag}-fifth-run'
              <<: *xtesting-jobs

- project:
    name: 'xtesting'
    <<: *xtesting-params
    jobs:
      - 'xtesting-{tag}-daily'
      - 'xtesting-{tag}-check'
      - 'xtesting-{tag}-gate'

- view:
    name: xtesting
    view-type: list
    columns:
      - status
      - weather
      - job
      - last-success
      - last-failure
      - last-duration
    regex: ^xtesting-[a-z]+-(daily|check|gate)$