- job:
    name: tcwg-abe-test
    project-type: matrix
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
            everyone-flat:
                - job-build
                - job-cancel
        - build-discarder:
            days-to-keep: 30
            num-to-keep: 100
    parameters:
        - string:
            name: GERRIT_PATCHSET_REVISION
            default: refs/remotes/origin/master
            description: 'Patched ABE revision'
        - string:
            name: test_list
            default: 'cross_build native_build testsuite abe-tests abe-tests-checkout'
            description: 'List of tests to perform'
        - string:
            name: scripts_branch
            default: master
            description: 'Scripts branch to use'
        - bool:
            name: dryrun
            default: false
            description: 'Dry-run, do nothing, just print steps'
    disabled: false
    node: tcwg-coordinator
    retry-count: 3
    concurrent: true
    display-name: 'TCWG BBB ABE Test'
    child-workspace: ../tcwg-abe-test__$EXECUTOR_NUMBER
    scm:
        - git:
            url: https://gitlab.com/Linaro/tcwg/abe.git
            refspec: +refs/changes/*:refs/remotes/changes/*
            branches:
                - ${GERRIT_PATCHSET_REVISION}
            skip-tag: true
            shallow-clone: false
            wipe-workspace: true
            choosing-strategy: gerrit
    triggers:
        - gerrit:
            server-name: 'review.linaro.org'
            override-votes: true
            gerrit-build-successful-verified-value: 1
            gerrit-build-failed-verified-value: -1
            gerrit-build-successful-codereview-value: 1
            gerrit-build-failed-codereview-value: -1
            readable-message: true
            successful-message: 'Works for me'
            failure-message: 'Well ... it worked on my machine'
            trigger-for-unreviewed-patches: true
            projects:
                - project-compare-type: 'ANT'
                  project-pattern: 'toolchain/abe'
                  branches:
                    - branch-compare-type: 'ANT'
                      branch-pattern: 'master'
            silent-start: true
    axes:
        - axis:
            type: slave
            name: label
            values:
                - tcwg-x86_64-build
        - axis:
            type: dynamic
            name: test
            values:
                - test_list
    execution-strategy:
        sequential: false
    wrappers:
        - timeout:
            timeout: 300
        - timestamps
        - ssh-agent-credentials:
            # tcwg-buildslave user id
            users:
                - 'e0958a95-204f-4c14-a66c-5e2be6c5d50a'
        - build-name:
            name: '#${BUILD_NUMBER}-${GIT_REVISION,length=8}'
    builders:
        - shell: |
            #!/bin/bash

            set -ex

            ARTIFACTS=${WORKSPACE}/artifacts
            mkdir -p "${ARTIFACTS}"
            # Create at least an (empty) artifact so that Jenkins does not complain
            # FIXME: Create an artifact for each case below, and no empty.txt
            echo > ${ARTIFACTS}/empty.txt

            # Explicitly checkout the branch/revision, as gerrit choosing
            # strategy doesn't pick up the right one when manually triggered.
            git checkout -f ${GERRIT_PATCHSET_REVISION}

            git clone -b $scripts_branch --depth 1 https://gitlab.com/LinaroLtd/tcwg/jenkins-scripts
            . ./jenkins-scripts/jenkins-helpers.sh

            ./jenkins-scripts/start-container-docker.sh --label $label --node $NODE_NAME --task build --prefix build_ --dryrun $dryrun > build-container.sh
            . ./build-container.sh
            BUILD_SHELL="build_container_exec bash"
            $BUILD_SHELL -c "echo \"Build session is up; ulimit config:\"; ulimit -a"

            # Remove container on exit
            trap "build_container_cleanup" EXIT HUP INT QUIT TRAP KILL PIPE TERM

            if $dryrun; then
                DRYRUNCMD="echo DRYRUN:"
            else
                DRYRUNCMD=
            fi

            status=0
            case "$test" in
              cross_build)
                tester_label=$(print_tester_label_for_target arm-linux-gnueabihf)
                ./jenkins-scripts/start-container-docker.sh --label $tester_label --task test --prefix test_ > test-container.sh
                . ./test-container.sh
                trap "build_container_cleanup; test_container_cleanup" EXIT HUP INT QUIT TRAP KILL PIPE TERM

                ${BUILD_SHELL} -x ${WORKSPACE}/jenkins-scripts/jenkins.sh --workspace ${WORKSPACE} --abedir ${WORKSPACE} --target arm-linux-gnueabihf --testcontainer ${test_container_host}:${test_container_port} --runtests --excludecheck gdb
                status=$?
                ;;
              native_build)
                ${BUILD_SHELL} -x ${WORKSPACE}/jenkins-scripts/jenkins.sh --workspace ${WORKSPACE} --abedir ${WORKSPACE} --target native --runtests --bootstrap --excludecheck gdb
                status=$?
                ;;
              testsuite)
                ${BUILD_SHELL} -c "set -ex; ./configure; make check"
                status=$?
                ;;
              abe-tests)
                ${BUILD_SHELL} -c "set -ex; rm -rf abe-tests; git clone https://gitlab.com/LinaroLtd/tcwg/abe-tests.git; cd abe-tests; ./test-manifest2.sh --abe-path `pwd` --ref-snapshots /home/tcwg-buildslave/snapshots-ref --quiet --display-summary" || status=$?
                $DRYRUNCMD cp abe-tests/full-manifest-test/*.txt ${ARTIFACTS}
                $DRYRUNCMD rm ${ARTIFACTS}/empty.txt
                # full build logs are large, and not very useful, so we
                # don't waste the disk space on them
                $DRYRUNCMD rm -f ${ARTIFACTS}/test-mani-build[12].txt
                ;;
              abe-tests-checkout)
                ${BUILD_SHELL} -c "set -ex; rm -rf abe-tests; git clone https://gitlab.com/LinaroLtd/tcwg/abe-tests.git; cd abe-tests; ./test-checkout.sh --clean-snapshots --abe-path `pwd` --ref-snapshots /home/tcwg-buildslave/snapshots-ref" || status=$?
                $DRYRUNCMD cp abe-tests/checkout-test/workspace*/test_*.log ${ARTIFACTS}
                $DRYRUNCMD rm artifacts/empty.txt
                # the reports from here aren't usually needed, so are
                # worth compressing
                $DRYRUNCMD xz ${ARTIFACTS}/*
                ;;
            esac

            exit ${status}
    publishers:
        - workspace-cleanup:
            include:
              - "nothing"
            dirmatch: true
        - archive:
            artifacts: 'artifacts/*'
            latest-only: false


