- hosts: all
  roles:
    - run-devstack

- hosts: controller
  vars:
    rally_fake_image_path: '{{ rally_home_dir }}/extra/fake-image.img'
    rally_task_args_file: "100-percent-not-exist-file"
    # this task will not be launched, but we need to specify something real to
    # pass a check at 'prepare-for-rally-task' role.
    rally_task: "rally-jobs/simple-job.yaml"
    pip_install: "/opt/stack/rally-openstack/.tox/{{ tox_env }}/bin/pip install"
  become: true
  become_user: stack
  tasks:
    - name: "Trigger prepare tasks of rally-task-at-devstack zuul job"
      import_role:
        name: prepare-for-rally-task

    - name: "Precreate a virtualenv for tox {{ tox_env }}"
      command: tox -e {{ tox_env }} --notest
      args:
        chdir: "/opt/stack/rally-openstack"

    - name: Check rally db connection
      command: rally db show
      register: rally_db_connection

    # DevStack enables a special tool for MySQL performance counting. Since
    #   it is enabled globally, rally.conf includes it as well.
    #   tox -e functional utils reuse the same config file and fails
    #   if there is no this tool inside the venv
    - name: Inject dbcounter plugin if needed
      command: "{{ pip_install }} /opt/stack/devstack/tools/dbcounter"
      when: "'plugin=dbcounter' in rally_db_connection.stdout"

    - name: Install python lib for interacting with MySQL
      command: "{{ pip_install }} PyMySQL>=0.7.6"
      when: "'pymysql' in rally_db_connection.stdout"

    - name: Run tox command
      shell: tox -e {{ tox_env }}
      environment:
        REQUESTS_CA_BUNDLE: /opt/stack/data/ca-bundle.pem
        REPORTS_ROOT: "{{ rally_results_dir }}/reports"
        PYTEST_REPORT: "{{ rally_results_dir }}/{{ html_report }}"
      args:
        chdir: "/opt/stack/rally-openstack"
