From 21ad299d7bf6d273a8f50d19bff2445775429bc6 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Tue, 12 Feb 2019 14:33:36 +0000 Subject: [PATCH] tests/ansible: Spec.remote_user() test & mitogen_via= fix. ansible_ssh_user precedence was incorrect. --- ansible_mitogen/transport_config.py | 2 +- tests/ansible/hosts/transport_config.hosts | 5 + .../integration/transport_config/all.yml | 1 + .../transport_config/remote_user.yml | 96 +++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/ansible/integration/transport_config/remote_user.yml diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py index c9aa9860..7af4dc55 100644 --- a/ansible_mitogen/transport_config.py +++ b/ansible_mitogen/transport_config.py @@ -456,8 +456,8 @@ class MitogenViaSpec(Spec): def remote_user(self): return ( - self._host_vars.get('ansible_user') or self._host_vars.get('ansible_ssh_user') or + self._host_vars.get('ansible_user') or C.DEFAULT_REMOTE_USER ) diff --git a/tests/ansible/hosts/transport_config.hosts b/tests/ansible/hosts/transport_config.hosts index 18cac116..ab83c65d 100644 --- a/tests/ansible/hosts/transport_config.hosts +++ b/tests/ansible/hosts/transport_config.hosts @@ -18,4 +18,9 @@ tc-remote-addr-explicit-ssh ansible_ssh_host=ansi.ssh.host tc-remote-addr-explicit-host ansible_host=ansi.host tc-remote-addr-explicit-both ansible_ssh_host=a.b.c ansible_host=b.c.d +# remote_addr() +tc-remote-user-unset # defaults to C.DEFAULT_REMOTE_USER +tc-remote-user-explicit-ssh ansible_ssh_user=ansi-ssh-user +tc-remote-user-explicit-user ansible_user=ansi-user +tc-remote-user-explicit-both ansible_user=a.b.c ansible_ssh_user=c.b.a diff --git a/tests/ansible/integration/transport_config/all.yml b/tests/ansible/integration/transport_config/all.yml index ae86bb18..618e72e3 100644 --- a/tests/ansible/integration/transport_config/all.yml +++ b/tests/ansible/integration/transport_config/all.yml @@ -1,3 +1,4 @@ - include: python_path.yml - include: remote_addr.yml +- include: remote_user.yml - include: transport.yml diff --git a/tests/ansible/integration/transport_config/remote_user.yml b/tests/ansible/integration/transport_config/remote_user.yml new file mode 100644 index 00000000..b873fcbe --- /dev/null +++ b/tests/ansible/integration/transport_config/remote_user.yml @@ -0,0 +1,96 @@ + +# Each case is followed by mitogen_via= case to test hostvars method. + + +# When no ansible_user/ansible_ssh_user= is set, username is +# C.DEFAULT_REMOTE_USER. +- name: integration/transport_config/remote_user.yml + hosts: tc-remote-user-unset + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.username + # We set DEFAULT_REMOTE_USER in our ansible.cfg + right: "ansible-cfg-remote-user" + +- hosts: tc-remote-user-unset + vars: {mitogen_via: tc-remote-user-explicit-ssh} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.username + right: "ansi-ssh-user" + - assert_equal: + left: out.result[1].kwargs.username + right: "ansible-cfg-remote-user" + + +# ansible_ssh_user= + +- hosts: tc-remote-user-explicit-ssh + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.username + right: "ansi-ssh-user" + +- hosts: tc-remote-user-explicit-ssh + vars: {mitogen_via: tc-remote-user-unset} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.username + right: "ansible-cfg-remote-user" + - assert_equal: + left: out.result[1].kwargs.username + right: "ansi-ssh-user" + + +# ansible_user= + +- hosts: tc-remote-user-explicit-user + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.username + right: "ansi-user" + +- hosts: tc-remote-user-explicit-host + vars: {mitogen_via: tc-remote-user-unset} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.username + right: "ansible-cfg-remote-user" + - assert_equal: + left: out.result[1].kwargs.username + right: "ansi-user" + + +# both; ansible_ssh_user= takes precedence according to play_context.py. + +- hosts: tc-remote-user-explicit-both + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.username + right: "c.b.a" + +- hosts: tc-remote-user-explicit-both + vars: {mitogen_via: tc-remote-user-unset} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.username + right: "ansible-cfg-remote-user" + - assert_equal: + left: out.result[1].kwargs.username + right: "c.b.a"