---
- ignore_errors: true
  when: tempest_format == 'container'
  block:
    - name: Set the proper permission for tempest directory and files
      file:
        dest: "/var/lib/tempestdata"
        owner: "{{ undercloud_user }}"
        group: "{{ undercloud_user }}"
        recurse: true
      become: true

    # Using cp here because copy module doesn't allow recursive copy with
    # remote_src set to true and synchronize is too slow
    - name: copy files from tempest container to home directory
      command: "cp -Rf --reflink=auto /var/lib/tempestdata/tempest {{ working_dir }}"
      args:
        warn: false
      changed_when: true

- block:
    - name: Check for .stestr directory
      stat:
        path: "{{ working_dir }}/tempest/.stestr"
      register: testr_dir

    - name: Define testr facts
      set_fact:
        testr_command: "{{ testr_dir.stat.exists | ternary(python_cmd + ' -m stestr.cli', 'testr') }}"

    - name: Change permission of tempest container log directory
      shell: |
        sudo chmod -R 777 /var/log/containers/tempest
      when: tempest_format == 'container'

    - name: Generate testrepository.subunit results file
      shell: >
        set -o pipefail &&
        {% if tempest_format == 'venv' %}source {{ working_dir }}/tempest_git/.venv/bin/activate; {% endif %}
        {{ testr_command }} last --subunit | subunit-1to2 > {{ working_dir }}/tempest/testrepository.subunit
      args:
        chdir: "{{ working_dir }}/tempest"

    - name: Generate HTML results file
      shell: |
        set -o pipefail &&
        {% if tempest_format == 'venv' %}source {{ working_dir }}/tempest_git/.venv/bin/activate; {% endif %}
        subunit2html {{ working_dir }}/tempest/testrepository.subunit {{ working_dir }}/tempest/tempest.html

    - name: Generate XML results file
      shell: |
        set -o pipefail &&
        {%- if tempest_format == 'venv' -%}
        source {{ working_dir }}/tempest_git/.venv/bin/activate;
        {%- endif -%}
        subunit2junitxml {{ working_dir }}/tempest/testrepository.subunit --output-to {{ working_dir }}/tempest/tempest.xml 2>&1 >> {{ tempest_log_file }}

    - name: Fetch HTML results
      fetch:
        src: "{{ working_dir }}/tempest/tempest.html"
        dest: "{{ local_working_dir }}/tempest.html"
        flat: true

    - name: Fetch nosetests results
      fetch:
        src: "{{ working_dir }}/tempest/tempest.xml"
        dest: "{{ local_working_dir }}/nosetests.xml"
        flat: true

    - name: Copying tempestmail files
      synchronize:
        src: tempestmail/
        dest: "{{ working_dir }}/tempestmail/"
        use_ssh_args: true

    - name: Copying skip file
      synchronize:
        src: "vars/tempest_skip_{{ release }}.yml"
        dest: "{{ working_dir }}/tempestmail/"
        use_ssh_args: true

    - name: Send tempest results by mail
      shell: >
          {% if lookup('env', 'LOG_PATH') %}LOG_PATH='{{ lookup('env', 'LOG_PATH') }}' {% endif %}
          ./tempestmail.py -c {{ tempestmail_config }} --job
          "{{ lookup('env', 'JOB_NAME')|default('Periodic job', true) }}"
          --file "{{ working_dir }}/{{ tempest_log_file }}"
          --log-url "{{ tempestmail_log_server }}"
          --skip-file "{{ working_dir }}/tempestmail/tempest_skip_{{ release }}.yml" ||
          { ERR=$?; echo -e "\0127ARNING: Send tempest results by mail failed with code $ERR"; exit $ERR; }
      args:
        chdir: "{{ working_dir }}/tempestmail"
      ignore_errors: true
