---
# Create a directory for artifacts on remote host
- name: create remote artifacts directory
  file:
    path: /tmp/artifacts
    state: directory
    mode: 0777

# Create empty results file, world-writable so rootless test can log to it
- name: initialize test.log file
  copy: dest=/tmp/artifacts/test.log content='' force=yes mode=0666

# Same with results.yml file
- name: initialize results.yml file
  copy: dest=/tmp/artifacts/results.yml content='results:\n' force=yes mode=0666

- name: execute tests
  include: run_one_test.yml
  with_items: "{{ tests }}"
  loop_control:
    loop_var: test

- name: pull test.log and results.yml
  fetch:
    src: "{{ item }}"
    dest: "{{ artifacts }}/"
    flat: yes
  with_items:
    - /tmp/artifacts/test.log
    - /tmp/artifacts/results.yml

# Copied from standard-test-basic
- name: check results
  shell: grep "^FAIL" /tmp/artifacts/test.log
  register: test_fails
  # Never fail at this step. Just store result of tests.
  failed_when: False

- name: preserve results
  set_fact:
    role_result_failed: "{{ (test_fails.stdout|d|length > 0) or (test_fails.stderr|d|length > 0) }}"
    role_result_msg: "{{ test_fails.stdout|d('tests failed.') }}"

- name: display results
  vars:
    msg: |
       Tests failed: {{ role_result_failed|d('Undefined') }}
       Tests msg: {{ role_result_msg|d('None') }}
  debug:
    msg: "{{ msg.split('\n') }}"