---
- name: Set the number of workers in tempest
  set_fact:
    tempest_workers: "{{ ansible_processor_vcpus|int // 2 }}"
  when: tempest_workers is not defined

- name: Set rc file to be sourced to run tempest
  set_fact:
    rc_file: "{{ working_dir }}/{% if tempest_undercloud %}stackrc{% else %}overcloudrc{% endif %}"

- name: Set rc file to be sourced to run tempest with in container
  set_fact:
    rc_file_container: "{{ tempest_data }}/{% if tempest_undercloud %}stackrc{% else %}overcloudrc{% endif %}"
  when: tempest_format == 'container'

- name: copy tls related files to undercloud home directory
  copy:
    src: '/etc/novajoin/krb5.keytab'
    dest: '{{ working_dir }}/krb5.keytab'
    owner: '{{ undercloud_user }}'
    group: '{{ undercloud_user }}'
    remote_src: true
  become: true
  when:
    - enable_tls_everywhere is defined
    - enable_tls_everywhere|bool

- name: Create overcloud tempest setup script
  template:
    src: tempest-setup.j2
    dest: "{{ working_dir }}/tempest-setup.sh"
    mode: 0744

- name: Deprecation message for test_regex
  debug:
    msg: "WARNING: The variable test_regex is deprecated and will be removed by end of ROCKY release in the favor of test_white_regex."
  when: test_regex is defined

- name: Load skip list variables (undercloud or overcloud)
  include_vars:
    file: "tempest_skip_{{ release }}.yml"
  when: skip_file_src != ''

- name: Create tempest skipfile
  template:
    src: "{{ skip_file_src }}"
    dest: "{{ working_dir }}/{{ skip_file }}"
    mode: 0644
  when: skip_file_src != ''

- name: Create tempest white file
  template:
    src: "{{ tempest_whitelist_file_src }}"
    dest: "{{ working_dir }}/{{ tempest_whitelist_file }}"
    mode: 0644
  when: tempest_whitelist|length > 0

- name: Install packages to generate subunit results
  become: true
  package:
    name:
      - "{{python_package_prefix}}-os-testr"
      - "{{python_package_prefix}}-subunit"
      - subunit-filters
    state: present
  when: tempest_format in ['container', 'packages']

- name: Install stestr
  become: true
  package:
    name: "{{python_package_prefix}}-stestr"
    state: present
  when: tempest_format in ['container', 'packages'] and release not in ['newton', 'ocata']

- ignore_errors: true
  block:
    - name: Copying bugcheck files
      synchronize:
        src: bugcheck/
        dest: "{{ working_dir }}/bugcheck/"
        use_ssh_args: true

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

    - name: Setting virtualenv
      shell: >
        virtualenv "{{ working_dir }}/bugcheck/.venv"

    - name: Installing requirements
      pip:
        requirements: "{{ working_dir }}/bugcheck/requirements.txt"
        virtualenv: "{{ working_dir }}/bugcheck/.venv"

    - name: Verifying bugs in bugzilla and launchpad and generating skip file
      shell: >
        source "{{ working_dir }}"/bugcheck/.venv/bin/activate;
        python bugcheck.py --skip-file "{{ working_dir }}/bugcheck/tempest_skip_{{ release }}.yml"
        --to-file "{{ working_dir }}/{{ skip_file }}" --format txt
      args:
        chdir: "{{ working_dir }}/bugcheck"
      ignore_errors: true
  when: check_tempest_bugs|bool
