# https://github.com/dw/mitogen/issues/251

# ansible_mitogen.connection internally reinterprets Ansible state into a
# 'connection stack' -- this is just a list of dictionaries specifying a
# sequence of proxied Router connection methods and their kwargs used to
# establish the connection. That list is passed to ContextService, which loops
# over the stack specifying via=(null or previous entry) for each connection
# method.

# mitogen_get_stack is a magic action that returns the stack, so we can test
# all kinds of scenarios without actually needing a real environmnt.

# Updating this file? Install 'pprintpp' and hack lib/callbacks/nice_stdout.py
# to use it instead of the built-in function, then simply s/'/'/ to get the
# cutpasteable formatted dicts below. WARNING: remove the trailing comma from
# the result list element, it seems to cause assert to silently succeed!


- name: integration/connection_delegation/stack_construction.yml
  hosts: cd-normal
  tasks:
    - meta: end_play
      when: not is_mitogen

    # used later for local_action test.
    - local_action: custom_python_detect_environment
      register: local_env


- hosts: cd-normal
  any_errors_fatal: true
  tasks:
    - meta: end_play
      when: not is_mitogen

    - mitogen_get_stack:
      register: out
    - assert_equal:
        left: out.result
        right: [
          {
            "kwargs": {
              "connect_timeout": 10,
              "doas_path": null,
              "password": null,
              "python_path": ["/usr/bin/python"],
              'remote_name': null,
              "username": "normal-user",
            },
            "method": "doas",
          }
        ]


- hosts: cd-normal
  tasks:
    - meta: end_play
      when: not is_mitogen

    - mitogen_get_stack:
      delegate_to: cd-alias
      register: out
    - assert_equal:
        left: out.result
        right: [
              {
                  'kwargs': {
                      'check_host_keys': 'ignore',
                      'compression': True,
                      'connect_timeout': 10,
                      'hostname': 'alias-host',
                      'identities_only': False,
                      'identity_file': null,
                      'keepalive_interval': 30,
                      'keepalive_count': 10,
                      'password': null,
                      'port': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'ssh_args': [
                          '-o',
                          'UserKnownHostsFile=/dev/null',
                          '-o',
                          'ForwardAgent=yes',
                          '-o',
                          'ControlMaster=auto',
                          '-o',
                          'ControlPersist=60s',
                      ],
                      'ssh_debug_level': null,
                      'ssh_path': 'ssh',
                      'username': 'alias-user',
                  },
                  'method': 'ssh',
              },
          ]


- hosts: cd-alias
  tasks:
    - meta: end_play
      when: not is_mitogen

    - mitogen_get_stack:
      register: out
    - assert_equal:
        left: out.result
        right: [
              {
                  'kwargs': {
                      'check_host_keys': 'ignore',
                      'compression': True,
                      'connect_timeout': 10,
                      'hostname': 'alias-host',
                      'identities_only': False,
                      'identity_file': null,
                      'keepalive_interval': 30,
                      'keepalive_count': 10,
                      'password': null,
                      'port': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'ssh_args': [
                          '-o',
                          'UserKnownHostsFile=/dev/null',
                          '-o',
                          'ForwardAgent=yes',
                          '-o',
                          'ControlMaster=auto',
                          '-o',
                          'ControlPersist=60s',
                      ],
                      'ssh_debug_level': null,
                      'ssh_path': 'ssh',
                      'username': 'alias-user',
                  },
                  'method': 'ssh',
              },
          ]


- hosts: cd-normal-normal
  tasks:
    - meta: end_play
      when: not is_mitogen

    - mitogen_get_stack:
      register: out
    - assert_equal:
        left: out.result
        right: [
              {
                  'kwargs': {
                      'connect_timeout': 10,
                      'doas_path': null,
                      'password': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'username': 'normal-user',
                  },
                  'method': 'doas',
              },
              {
                  'kwargs': {
                      'check_host_keys': 'ignore',
                      'compression': True,
                      'connect_timeout': 10,
                      'hostname': 'cd-normal-normal',
                      'identities_only': False,
                      'identity_file': null,
                      'keepalive_interval': 30,
                      'keepalive_count': 10,
                      'password': null,
                      'port': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'ssh_args': [
                          '-o',
                          'UserKnownHostsFile=/dev/null',
                          '-o',
                          'ForwardAgent=yes',
                          '-o',
                          'ControlMaster=auto',
                          '-o',
                          'ControlPersist=60s',
                      ],
                      'ssh_debug_level': null,
                      'ssh_path': 'ssh',
                      'username': 'ansible-cfg-remote-user',
                  },
                  'method': 'ssh',
              },
          ]


- hosts: cd-normal-alias
  tasks:
    - meta: end_play
      when: not is_mitogen

    - mitogen_get_stack:
      register: out
    - assert_equal:
        left: out.result
        right: [
              {
                  'kwargs': {
                      'check_host_keys': 'ignore',
                      'compression': True,
                      'connect_timeout': 10,
                      'hostname': 'alias-host',
                      'identities_only': False,
                      'identity_file': null,
                      'keepalive_interval': 30,
                      'keepalive_count': 10,
                      'password': null,
                      'port': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'ssh_args': [
                          '-o',
                          'UserKnownHostsFile=/dev/null',
                          '-o',
                          'ForwardAgent=yes',
                          '-o',
                          'ControlMaster=auto',
                          '-o',
                          'ControlPersist=60s',
                      ],
                      'ssh_debug_level': null,
                      'ssh_path': 'ssh',
                      'username': 'alias-user',
                  },
                  'method': 'ssh',
              },
              {
                  'kwargs': {
                      'check_host_keys': 'ignore',
                      'compression': True,
                      'connect_timeout': 10,
                      'hostname': 'cd-normal-alias',
                      'identities_only': False,
                      'identity_file': null,
                      'keepalive_interval': 30,
                      'keepalive_count': 10,
                      'password': null,
                      'port': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'ssh_args': [
                          '-o',
                          'UserKnownHostsFile=/dev/null',
                          '-o',
                          'ForwardAgent=yes',
                          '-o',
                          'ControlMaster=auto',
                          '-o',
                          'ControlPersist=60s',
                      ],
                      'ssh_debug_level': null,
                      'ssh_path': 'ssh',
                      'username': 'ansible-cfg-remote-user',
                  },
                  'method': 'ssh',
              },
          ]


- hosts: cd-newuser-normal-normal
  tasks:
    - meta: end_play
      when: not is_mitogen

    - mitogen_get_stack:
      register: out
    - assert_equal:
        left: out.result
        right: [
              {
                  'kwargs': {
                      'connect_timeout': 10,
                      'doas_path': null,
                      'password': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'username': 'normal-user',
                  },
                  'method': 'doas',
              },
              {
                  'kwargs': {
                      'check_host_keys': 'ignore',
                      'compression': True,
                      'connect_timeout': 10,
                      'hostname': 'cd-newuser-normal-normal',
                      'identities_only': False,
                      'identity_file': null,
                      'keepalive_interval': 30,
                      'keepalive_count': 10,
                      'password': null,
                      'port': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'ssh_args': [
                          '-o',
                          'UserKnownHostsFile=/dev/null',
                          '-o',
                          'ForwardAgent=yes',
                          '-o',
                          'ControlMaster=auto',
                          '-o',
                          'ControlPersist=60s',
                      ],
                      'ssh_debug_level': null,
                      'ssh_path': 'ssh',
                      'username': 'newuser-normal-normal-user',
                  },
                  'method': 'ssh',
              },
          ]


- hosts: cd-newuser-normal-normal
  tasks:
    - meta: end_play
      when: not is_mitogen

    - mitogen_get_stack:
      delegate_to: cd-alias
      register: out
    - assert_equal:
        left: out.result
        right: [
              {
                  'kwargs': {
                      'check_host_keys': 'ignore',
                      'compression': True,
                      'connect_timeout': 10,
                      'hostname': 'alias-host',
                      'identities_only': False,
                      'identity_file': null,
                      'keepalive_interval': 30,
                      'keepalive_count': 10,
                      'password': null,
                      'port': null,
                      "python_path": ["/usr/bin/python"],
                      'remote_name': null,
                      'ssh_args': [
                          '-o',
                          'UserKnownHostsFile=/dev/null',
                          '-o',
                          'ForwardAgent=yes',
                          '-o',
                          'ControlMaster=auto',
                          '-o',
                          'ControlPersist=60s',
                      ],
                      'ssh_debug_level': null,
                      'ssh_path': 'ssh',
                      'username': 'alias-user',
                  },
                  'method': 'ssh',
              },
          ]


- hosts: cd-newuser-normal-normal
  tasks:
    - meta: end_play
      when: not is_mitogen

    - local_action: mitogen_get_stack
      register: out
    - assert_equal:
        left: out.result
        right: [
          {
            'kwargs': {
              "python_path": ["{{ansible_playbook_python}}"],
            },
            'method': 'local',
          },
        ]


- hosts: cd-newuser-doas-normal
  tasks:
    - meta: end_play
      when: not is_mitogen

    - mitogen_get_stack:
      register: out
    - assert_equal:
        left: out.result
        right: [
          {
            'kwargs': {
              'connect_timeout': 10,
              'doas_path': null,
              'password': null,
              'python_path': ["/usr/bin/python"],
              'remote_name': null,
              'username': 'normal-user',
            },
            'method': 'doas',
          },
          {
            'kwargs': {
              'connect_timeout': 10,
              'doas_path': null,
              'password': null,
              'python_path': ["/usr/bin/python"],
              'remote_name': null,
              'username': 'newuser-doas-normal-user',
            },
            'method': 'doas',
          },
      ]
