---
- name: "from which host the cleanup will be executed"
  set_fact:
    cleanup_delegate_to: '{{ groups.get("undercloud", []) | first | default(test_host) }}'

- block:
    - include_role: name=tobiko-ir-jenkins
      when:  "(lookup('env','JENKINS_URL') | length) > 0"

    - name: "consolidate deploy facts"
      set_fact:
        tobiko_dir: '{{ tobiko_dir | realpath }}'
        tobiko_src_dir: '{{ tobiko_src_dir | realpath }}'
        tobiko_git_repo: '{{ tobiko_git_repo }}'
        tobiko_git_refspec: "{{ tobiko_git_refspec }}"
        tobiko_git_remote: "{{ tobiko_git_remote }}"
        test_dir: '{{ test_dir | realpath }}'
        test_src_dir: '{{ test_src_dir | realpath }}'
        test_git_repo: '{{ test_git_repo }}'
        test_git_refspec: "{{ test_git_refspec }}"
        test_git_remote: "{{ test_git_remote }}"

    - name: "deploy Tobiko files"
      include_role: name=tobiko-ir-deploy
      vars:
        deploy_dir: '{{ tobiko_dir }}'
        deploy_src_dir: '{{ tobiko_src_dir }}'
        deploy_git_repo: '{{ tobiko_git_repo }}'
        deploy_git_refspec: "{{ tobiko_git_refspec }}"
        deploy_git_remote: '{{ tobiko_git_remote  }}'

    - name: "deploy test files"
      include_role: name=tobiko-ir-deploy
      vars:
        deploy_dir: '{{ test_dir }}'
        deploy_src_dir: '{{ test_src_dir }}'
        deploy_git_repo: '{{ test_git_repo }}'
        deploy_git_refspec: "{{ test_git_refspec }}"
        deploy_git_remote: '{{ test_git_remote }}'
      when: test_dir != tobiko_dir

    - name: deploy OpenShift InfraRed plugin files
      include_role: name=tobiko-ir-deploy
      vars:
        deploy_dir: '{{ openshift_infrared_dir | realpath }}'
        deploy_src_dir: '{{ openshift_infrared_src_dir | realpath }}'

        deploy_git_repo: '{{ openshift_infrared_git_repo }}'
        deploy_git_refspec: '{{ openshift_infrared_git_refspec }}'
        deploy_git_remote: '{{ openshift_infrared_git_remote }}'

    - name: apply pre-requisites before tests run
      include_role: name=tobiko-ir-before-run

    - name: "download images"
      include_role: name=tobiko-download-images
      vars:
        fedora_image_override_pattern:
          advanced_vm:
            customized: true
            url: "{{ customized_fedora_image_url | default('') }}"
        download_images_override: >-
          {{
            customized_fedora_image_url is defined |
            ternary(fedora_image_override_pattern, {})
          }}

    - name: "initialize test execution"
      include_role: name=tobiko-configure

    - name: "run tests"
      include_role: name=tobiko-run

  rescue:
    - name: "clean up heat stacks generated during tobiko tests"
      include_role:
        name: tobiko-cleanup
        apply:
          delegate_to: '{{ cleanup_delegate_to }}'
      when: stacks_cleanup

    - name: "finalize test execution"
      include_role: name=tobiko-collect
      ignore_errors: yes

    - fail:
        msg: 'TEST EXECUTION FAILED'


- name: "clean up heat stacks generated during tobiko tests"
  include_role:
    name: tobiko-cleanup
    apply:
      delegate_to: '{{ cleanup_delegate_to }}'
  when: stacks_cleanup

- name: "finalize test execution"
  include_role: name=tobiko-collect


- debug:
    msg: 'TEST EXECUTION SUCCEEDED'
