# Ensure 'ssh' connections time out correctly.
# mitogen__slow_user performs a long sleep in ~/.profile.
# Mitogen counts this time towards the connection timeout. Ansible doesn't.
# ansible_python_interpreter=python3000 is an optimisation, to avoid waiting
# on the timeout multiple times (e.g. interpreter discovery).

- name: integration/ssh/timeouts.yml
  hosts: test-targets
  gather_facts: false
  tasks:
    - include_tasks: ../_mitogen_only.yml

    - name: Cause Ansible connection timeout
      connection: local
      environment:
        ANSIBLE_CALLBACK_RESULT_FORMAT: json
        ANSIBLE_LOAD_CALLBACK_PLUGINS: "false"
        ANSIBLE_SSH_TIMEOUT: 10
        ANSIBLE_STRATEGY:  "{{ lookup('env', 'ANSIBLE_STRATEGY') | mandatory }}"
        ANSIBLE_VERBOSITY: "{{ ansible_verbosity }}"
      vars:
        ansible_python_interpreter: "{{ ansible_playbook_python }}"
      command: |
        ansible
        {% for inv in ansible_inventory_sources %}
        -i "{{ inv }}"
        {% endfor %}
        "{{ inventory_hostname }}"
        -m ping
        -e ansible_user=mitogen__slow_user -e ansible_password=slow_user_password
        -e ansible_python_interpreter=python3000
      args:
        chdir: ../..
      register: out
      changed_when: false
      check_mode: false
      ignore_errors: true

    - name: Verify connection timeout occurred
      assert:
        that:
        - |
          '"changed": false' in out.stdout
        - |
          '"unreachable": true' in out.stdout
        - |
          '"msg": "Connection timed out."' in out.stdout
          or '"msg": "Task failed: Connection timed out."' in out.stdout
        fail_msg: |
          out={{ out }}
  tags:
    - mitogen_only
    - timeouts
