From 1c24a13560f3181516b724a14431f0109926d788 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 4 Nov 2018 20:54:34 +0000 Subject: [PATCH 01/26] tests: add Ansible back to requirements Needed for Tox --- dev_requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/dev_requirements.txt b/dev_requirements.txt index c536c154..eb83f8be 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1,4 +1,5 @@ -r docs/docs-requirements.txt +-r tests/ansible/requirements.txt psutil==5.4.8 coverage==4.5.1 Django==1.6.11 # Last version supporting 2.6. From cd6486b0e9211412b32d9effab4dfa9a03b65177 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 4 Nov 2018 21:16:44 +0000 Subject: [PATCH 02/26] tests: fix more DisconnectTest raciness. --- tests/parent_test.py | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/parent_test.py b/tests/parent_test.py index 797845df..0bd8079c 100644 --- a/tests/parent_test.py +++ b/tests/parent_test.py @@ -31,8 +31,25 @@ def wait_for_child(pid, timeout=1.0): @mitogen.core.takes_econtext -def call_func_in_sibling(ctx, econtext): - ctx.call(time.sleep, 99999) +def call_func_in_sibling(ctx, econtext, sync_sender): + recv = ctx.call_async(time.sleep, 99999) + sync_sender.send(None) + recv.get().unpickle() + + +def wait_for_empty_output_queue(sync_recv, context): + # wait for sender to submit their RPC. Since the RPC is sent first, the + # message sent to this sender cannot arrive until we've routed the RPC. + sync_recv.get() + + router = context.router + broker = router.broker + while True: + # Now wait for the RPC to exit the output queue. + stream = router.stream_by_id(context.context_id) + if broker.defer_sync(lambda: stream.pending_bytes()) == 0: + return + time.sleep(0.1) class GetDefaultRemoteNameTest(testlib.TestCase): @@ -353,12 +370,17 @@ class DisconnectTest(testlib.RouterMixin, testlib.TestCase): self.router.stream_by_id(c1.context_id).auth_id = mitogen.context_id c1.call(mitogen.parent.upgrade_router) - recv = c1.call_async(call_func_in_sibling, c2) + sync_recv = mitogen.core.Receiver(self.router) + recv = c1.call_async(call_func_in_sibling, c2, + sync_sender=sync_recv.to_sender()) + + wait_for_empty_output_queue(sync_recv, c2) c2.shutdown(wait=True) + e = self.assertRaises(mitogen.core.CallError, lambda: recv.get().unpickle()) s = 'mitogen.core.ChannelError: ' + self.router.respondent_disconnect_msg - self.assertTrue(e.args[0].startswith(s)) + self.assertTrue(e.args[0].startswith(s), str(e)) def test_far_sibling_disconnected(self): # God mode: child of child notices child of child of parent has @@ -373,8 +395,13 @@ class DisconnectTest(testlib.RouterMixin, testlib.TestCase): self.router.stream_by_id(c1.context_id).auth_id = mitogen.context_id c11.call(mitogen.parent.upgrade_router) - recv = c11.call_async(call_func_in_sibling, c22) + sync_recv = mitogen.core.Receiver(self.router) + recv = c11.call_async(call_func_in_sibling, c22, + sync_sender=sync_recv.to_sender()) + + wait_for_empty_output_queue(sync_recv, c22) c22.shutdown(wait=True) + e = self.assertRaises(mitogen.core.CallError, lambda: recv.get().unpickle()) s = 'mitogen.core.ChannelError: ' + self.router.respondent_disconnect_msg From c286f4f107248641dec58b71465abe4f3c79f117 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 4 Nov 2018 21:17:09 +0000 Subject: [PATCH 03/26] Add tests/ansible/requirements.txt to Tox. --- tests/ansible/requirements.txt | 2 ++ tox.ini | 1 + 2 files changed, 3 insertions(+) diff --git a/tests/ansible/requirements.txt b/tests/ansible/requirements.txt index fdabb0f6..551af999 100644 --- a/tests/ansible/requirements.txt +++ b/tests/ansible/requirements.txt @@ -1,2 +1,4 @@ +ansible; python_version >= '2.7' +ansible<2.7; python_version < '2.7' paramiko==2.3.2 # Last 2.6-compat version. google-api-python-client==1.6.5 diff --git a/tox.ini b/tox.ini index 6bf8bb53..9bff333e 100644 --- a/tox.ini +++ b/tox.ini @@ -8,6 +8,7 @@ envlist = [testenv] deps = -r{toxinidir}/dev_requirements.txt + -r{toxinidir}/tests/ansible/requirements.txt commands = {posargs:bash run_tests} From 574fc27a9ca5340e4f7ea57daf67e6d5746f6ad0 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 00:22:47 +0000 Subject: [PATCH 04/26] issue #414: import test / reproduction. --- tests/ansible/integration/async/all.yml | 3 +- .../integration/async/multiple_items_loop.yml | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/ansible/integration/async/multiple_items_loop.yml diff --git a/tests/ansible/integration/async/all.yml b/tests/ansible/integration/async/all.yml index 17969ead..61d2d35c 100644 --- a/tests/ansible/integration/async/all.yml +++ b/tests/ansible/integration/async/all.yml @@ -1,8 +1,9 @@ +- import_playbook: multiple_items_loop.yml - import_playbook: result_binary_producing_json.yml - import_playbook: result_binary_producing_junk.yml - import_playbook: result_shell_echo_hi.yml - import_playbook: runner_new_process.yml - import_playbook: runner_one_job.yml - import_playbook: runner_timeout_then_polling.yml -- import_playbook: runner_with_polling_and_timeout.yml - import_playbook: runner_two_simultaneous_jobs.yml +- import_playbook: runner_with_polling_and_timeout.yml diff --git a/tests/ansible/integration/async/multiple_items_loop.yml b/tests/ansible/integration/async/multiple_items_loop.yml new file mode 100644 index 00000000..9a9b1192 --- /dev/null +++ b/tests/ansible/integration/async/multiple_items_loop.yml @@ -0,0 +1,36 @@ +# issue #414: verify behaviour of async tasks created in a loop. + +- name: integration/async/multiple_items_loop.yml + hosts: test-targets + any_errors_fatal: true + tasks: + + - name: start long running ops + become: true + shell: "{{item}}" + async: 15 + poll: 0 + register: jobs + with_items: + - "sleep 3; echo hi-from-job-1" + - "sleep 5; echo hi-from-job-2" + + - name: Ensure static files are collected and compressed + async_status: + jid: "{{ item.ansible_job_id }}" + become: yes + register: out + until: out.finished + retries: 30 + with_items: + - "{{ jobs.results }}" + + - assert: + that: + - out.results[0].stdout == 'hi-from-job-1' + - out.results[0].rc == 0 + - out.results[0].delta > '0:00:03' + + - out.results[1].stdout == 'hi-from-job-2' + - out.results[1].rc == 0 + - out.results[1].delta > '0:00:05' From 174b685d16121c4f5760020f7db6ab731f8aa70a Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 12:40:48 +0000 Subject: [PATCH 05/26] tests: CentOS 6 lacks %wheel in sudo by default. --- tests/image_prep/_container_setup.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/image_prep/_container_setup.yml b/tests/image_prep/_container_setup.yml index f62c5955..39ab2dd8 100644 --- a/tests/image_prep/_container_setup.yml +++ b/tests/image_prep/_container_setup.yml @@ -2,7 +2,6 @@ - hosts: all strategy: linear gather_facts: false - become: true tasks: - raw: > if ! python -c ''; then @@ -96,6 +95,11 @@ dest: /etc/sudoers.d/001-mitogen src: ../data/docker/001-mitogen.sudo + - lineinfile: + path: /etc/sudoers + line: "%wheel ALL=(ALL) ALL" + when: distro == "CentOS" + - lineinfile: path: /etc/ssh/sshd_config line: Banner /etc/ssh/banner.txt From 91513f5b7ee32f316f61f64fc9b6b01cbaac8858 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 12:39:39 +0000 Subject: [PATCH 06/26] tests: properly close 'cat' child process on exit. --- .travis/ci_lib.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/.travis/ci_lib.py b/.travis/ci_lib.py index e92564b6..d8d6827d 100644 --- a/.travis/ci_lib.py +++ b/.travis/ci_lib.py @@ -37,11 +37,20 @@ if not hasattr(subprocess, 'check_output'): # Force stdout FD 1 to be a pipe, so tools like pip don't spam progress bars. -sys.stdout = os.popen('stdbuf -oL cat', 'w', 1) -os.dup2(sys.stdout.fileno(), 1) +proc = subprocess.Popen( + args=['stdbuf', '-oL', 'cat'], + stdin=subprocess.PIPE +) + +os.dup2(proc.stdin.fileno(), 1) +os.dup2(proc.stdin.fileno(), 2) + +def cleanup_travis_junk(): + sys.stdout.close() + sys.stderr.close() + proc.terminate() -sys.stderr = sys.stdout -os.dup2(sys.stderr.fileno(), 2) +atexit.register(cleanup_travis_junk) # ----------------- @@ -54,7 +63,9 @@ def _argv(s, *args): def run(s, *args, **kwargs): argv = _argv(s, *args) print('Running: %s' % (argv,)) - return subprocess.check_call(argv, **kwargs) + ret = subprocess.check_call(argv, **kwargs) + print('Finished running: %s' % (argv,)) + return ret def get_output(s, *args, **kwargs): From 816da64df5f974bfd97f88c64133e867949eead7 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 12:49:19 +0000 Subject: [PATCH 07/26] tests: show task args in image_prep --- tests/image_prep/ansible.cfg | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/image_prep/ansible.cfg b/tests/image_prep/ansible.cfg index a3937825..8a8c47fa 100644 --- a/tests/image_prep/ansible.cfg +++ b/tests/image_prep/ansible.cfg @@ -2,3 +2,5 @@ [defaults] strategy_plugins = ../../ansible_mitogen/plugins/strategy retry_files_enabled = false +display_args_to_stdout = True +no_target_syslog = True From f87553b16545da7918300c00dc115880021f4597 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 12:49:47 +0000 Subject: [PATCH 08/26] tests: must set ansible_become_pass in synchronize.yml. --- tests/ansible/integration/action/synchronize.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ansible/integration/action/synchronize.yml b/tests/ansible/integration/action/synchronize.yml index 25649fbf..43bbb265 100644 --- a/tests/ansible/integration/action/synchronize.yml +++ b/tests/ansible/integration/action/synchronize.yml @@ -5,6 +5,7 @@ any_errors_fatal: true vars: ansible_user: mitogen__has_sudo_pubkey + ansible_become_pass: has_sudo_pubkey_password ansible_ssh_private_key_file: /tmp/synchronize-action-key tasks: # must copy git file to set proper file mode. From b29c8eaf2a2f41f96a4083e6555ca14e29061775 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 12:59:21 +0000 Subject: [PATCH 09/26] tests: allow passing -vvv to build_docker_images. --- tests/image_prep/build_docker_images.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/image_prep/build_docker_images.py b/tests/image_prep/build_docker_images.py index 94a17104..0ab722f4 100755 --- a/tests/image_prep/build_docker_images.py +++ b/tests/image_prep/build_docker_images.py @@ -6,9 +6,10 @@ Build the Docker images used for testing. import commands import os -import tempfile import shlex import subprocess +import sys +import tempfile BASEDIR = os.path.dirname(os.path.abspath(__file__)) @@ -42,7 +43,7 @@ with tempfile.NamedTemporaryFile() as fp: try: subprocess.check_call( cwd=BASEDIR, - args=sh('ansible-playbook -i %s -c docker setup.yml', fp.name), + args=sh('ansible-playbook -i %s -c docker setup.yml', fp.name) + sys.argv[1:], ) for container_id, label in label_by_id.items(): From 9ad022107e3b2dff8e85409532c87d2859e1c2cd Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 13:00:05 +0000 Subject: [PATCH 10/26] issue #414: disable test until rest of CI is healthy --- tests/ansible/integration/async/all.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ansible/integration/async/all.yml b/tests/ansible/integration/async/all.yml index 61d2d35c..1a5b20ef 100644 --- a/tests/ansible/integration/async/all.yml +++ b/tests/ansible/integration/async/all.yml @@ -1,4 +1,4 @@ -- import_playbook: multiple_items_loop.yml +# - import_playbook: multiple_items_loop.yml - import_playbook: result_binary_producing_json.yml - import_playbook: result_binary_producing_junk.yml - import_playbook: result_shell_echo_hi.yml From 6bae5869239ac58371936741a3673e801d9bf0f9 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 13:06:01 +0000 Subject: [PATCH 11/26] tests: fix up Travis bodge for Python 2.6. --- .travis/ci_lib.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis/ci_lib.py b/.travis/ci_lib.py index d8d6827d..f5778161 100644 --- a/.travis/ci_lib.py +++ b/.travis/ci_lib.py @@ -45,9 +45,9 @@ proc = subprocess.Popen( os.dup2(proc.stdin.fileno(), 1) os.dup2(proc.stdin.fileno(), 2) -def cleanup_travis_junk(): - sys.stdout.close() - sys.stderr.close() +def cleanup_travis_junk(stdout=sys.stdout, stderr=sys.stderr, proc=proc): + stdout.close() + stderr.close() proc.terminate() atexit.register(cleanup_travis_junk) From 4d443e654b8a9a35a0da70ac5580925425eb249d Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 13:12:31 +0000 Subject: [PATCH 12/26] tests: replace another shell script. --- tests/ansible/mitogen_ansible_playbook.py | 6 ++++++ tests/ansible/mitogen_ansible_playbook.sh | 3 --- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100755 tests/ansible/mitogen_ansible_playbook.py delete mode 100755 tests/ansible/mitogen_ansible_playbook.sh diff --git a/tests/ansible/mitogen_ansible_playbook.py b/tests/ansible/mitogen_ansible_playbook.py new file mode 100755 index 00000000..3af1791c --- /dev/null +++ b/tests/ansible/mitogen_ansible_playbook.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python +import os +import subprocess +import sys +os.environ['ANSIBLE_STRATEGY'] = 'mitogen_linear' +subprocess.check_call(['./run_ansible_playbook.py'] + sys.argv[1:]) diff --git a/tests/ansible/mitogen_ansible_playbook.sh b/tests/ansible/mitogen_ansible_playbook.sh deleted file mode 100755 index 462d985b..00000000 --- a/tests/ansible/mitogen_ansible_playbook.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -export ANSIBLE_STRATEGY=mitogen_linear -exec ./run_ansible_playbook.py "$@" From 35092c5d35f9d2f661f8b1ec68a7e37e97960ee1 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 13:15:20 +0000 Subject: [PATCH 13/26] tests: Unicode/bytes fixes for integration/connection/exec_command.yml --- ansible_mitogen/connection.py | 6 +++--- ansible_mitogen/target.py | 2 +- tests/ansible/integration/connection/exec_command.yml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ansible_mitogen/connection.py b/ansible_mitogen/connection.py index e017608e..f14576ad 100644 --- a/ansible_mitogen/connection.py +++ b/ansible_mitogen/connection.py @@ -914,9 +914,9 @@ class Connection(ansible.plugins.connection.ConnectionBase): emulate_tty=emulate_tty, ) - stderr += 'Shared connection to %s closed.%s' % ( - self._play_context.remote_addr, - ('\r\n' if emulate_tty else '\n'), + stderr += b'Shared connection to %s closed.%s' % ( + self._play_context.remote_addr.encode(), + (b'\r\n' if emulate_tty else b'\n'), ) return rc, stdout, stderr diff --git a/ansible_mitogen/target.py b/ansible_mitogen/target.py index 0e74f960..83069422 100644 --- a/ansible_mitogen/target.py +++ b/ansible_mitogen/target.py @@ -541,7 +541,7 @@ def exec_args(args, in_data='', chdir=None, shell=None, emulate_tty=False): if emulate_tty: stdout = stdout.replace(b'\n', b'\r\n') - return proc.returncode, stdout, stderr or '' + return proc.returncode, stdout, stderr or b'' def exec_command(cmd, in_data='', chdir=None, shell=None, emulate_tty=False): diff --git a/tests/ansible/integration/connection/exec_command.yml b/tests/ansible/integration/connection/exec_command.yml index 6a632961..105505d1 100644 --- a/tests/ansible/integration/connection/exec_command.yml +++ b/tests/ansible/integration/connection/exec_command.yml @@ -15,5 +15,5 @@ - assert: that: - out.result[0] == 0 - - out.result[1] == "hello, world\r\n" - - out.result[2].startswith("Shared connection to ") + - out.result[1].decode() == "hello, world\r\n" + - out.result[2].decode().startswith("Shared connection to ") From 0c3e48468b299c5b5d15b7c385c26837bbc62b8d Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 13:33:33 +0000 Subject: [PATCH 14/26] tests: run disconnect_during_module.yml in subprocess Avoid entire run failing with unreachable --- .../connection/_disconnect_during_module.yml | 13 +++++++++++++ .../connection/disconnect_during_module.yml | 19 ++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 tests/ansible/integration/connection/_disconnect_during_module.yml diff --git a/tests/ansible/integration/connection/_disconnect_during_module.yml b/tests/ansible/integration/connection/_disconnect_during_module.yml new file mode 100644 index 00000000..6bd8cd50 --- /dev/null +++ b/tests/ansible/integration/connection/_disconnect_during_module.yml @@ -0,0 +1,13 @@ +# issue 352: test ability to notice disconnection during a module invocation. +--- + +- name: integration/connection/_disconnect_during_module.yml + hosts: test-targets + gather_facts: no + any_errors_fatal: false + tasks: + - run_once: true # don't run against localhost + shell: | + kill -9 $PPID + register: out + ignore_errors: true diff --git a/tests/ansible/integration/connection/disconnect_during_module.yml b/tests/ansible/integration/connection/disconnect_during_module.yml index f2943b44..2b9c2c55 100644 --- a/tests/ansible/integration/connection/disconnect_during_module.yml +++ b/tests/ansible/integration/connection/disconnect_during_module.yml @@ -2,18 +2,23 @@ --- - name: integration/connection/disconnect_during_module.yml - hosts: test-targets localhost + hosts: test-targets gather_facts: no any_errors_fatal: false tasks: - - run_once: true # don't run against localhost - shell: | - kill -9 $PPID + - connection: local + command: | + ansible-playbook + -i "{{inventory_file}}" + integration/connection/_disconnect_during_module.yml + args: + chdir: ../.. register: out ignore_errors: true + - debug: var=out + - assert: that: - - out.msg.startswith('Mitogen was disconnected from the remote environment while a call was in-progress.') - - - meta: clear_host_errors + - out.rc == 4 + - "'Mitogen was disconnected from the remote environment while a call was in-progress.' in out.stdout" From c7931be524588eb1e47be9713a7e15149d7d8eba Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 14:05:34 +0000 Subject: [PATCH 15/26] issue #420: core: include PID in Latch cookie data. --- mitogen/core.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index 370fb742..0876cd8c 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -1778,16 +1778,16 @@ class Latch(object): self._cls_all_sockets.extend((rsock, wsock)) return rsock, wsock - COOKIE_SIZE = 33 - def _make_cookie(self): """ - Return a 33-byte string encoding the ID of the instance and the current - thread. This disambiguates legitimate wake-ups, accidental writes to - the FD, and buggy internal FD sharing. + Return a string encoding the ID of the instance and the current thread. + This disambiguates legitimate wake-ups, accidental writes to the FD, + and buggy internal FD sharing. """ ident = threading.currentThread().ident - return b(u'%016x-%016x' % (int(id(self)), ident)) + return b(u'%010d-%016x-%016x' % (os.getpid(), int(id(self)), ident)) + + COOKIE_SIZE = len(_make_cookie(None)) def get(self, timeout=None, block=True): """ From 50241a922fec63153723ef7cd5935883898e6b86 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 14:28:19 +0000 Subject: [PATCH 16/26] ansible: call on_fork() on broker shutdown; closes #420. --- ansible_mitogen/connection.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/ansible_mitogen/connection.py b/ansible_mitogen/connection.py index f14576ad..569a5ad8 100644 --- a/ansible_mitogen/connection.py +++ b/ansible_mitogen/connection.py @@ -42,6 +42,7 @@ import ansible.errors import ansible.plugins.connection import ansible.utils.shlex +import mitogen.fork import mitogen.unix import mitogen.utils @@ -802,19 +803,32 @@ class Connection(ansible.plugins.connection.ConnectionBase): self.init_child_result = None self.chain = None - def close(self): + def _shutdown_broker(self): """ - Arrange for the mitogen.master.Router running in the worker to - gracefully shut down, and wait for shutdown to complete. Safe to call - multiple times. + Shutdown the broker thread during :meth:`close` or :meth:`reset`. """ - self._mitogen_reset(mode='put') if self.broker: self.broker.shutdown() self.broker.join() self.broker = None self.router = None + # #420: Ansible executes "meta" actions in the top-level process, + # meaning "reset_connection" will cause :class:`mitogen.core.Latch` FDs + # to be cached and subsequently erroneously shared by children on + # subsequent task forks. To handle that, call on_fork() to ensure any + # shared state is discarded. + mitogen.fork.on_fork() + + def close(self): + """ + Arrange for the mitogen.master.Router running in the worker to + gracefully shut down, and wait for shutdown to complete. Safe to call + multiple times. + """ + self._mitogen_reset(mode='put') + self._shutdown_broker() + reset_compat_msg = ( 'Mitogen only supports "reset_connection" on Ansible 2.5.6 or later' ) @@ -835,6 +849,7 @@ class Connection(ansible.plugins.connection.ConnectionBase): self._connect() self._mitogen_reset(mode='reset') + self._shutdown_broker() # Compatibility with Ansible 2.4 wait_for_connection plug-in. _reset = reset From 905fbe7cbb5c603912394b6a2713a89a2916e6d4 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 14:31:11 +0000 Subject: [PATCH 17/26] issue #420: update Changelog. --- docs/changelog.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index bb1974a8..87ad7f00 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -186,6 +186,11 @@ Fixes * `#410 `_: the sudo method supports the SELinux ``--type`` and ``--role`` options. +* `#420 `_: if a :class:`Connection` + was constructed in the Ansible top-level process, for example while executing + ``meta: reset_connection``, resources could become undesirably shared in + subsequent children. + Core Library ~~~~~~~~~~~~ From f5f72b958f25a256251d044cab52597b09b043ae Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 15:02:55 +0000 Subject: [PATCH 18/26] tests: avoid -u command line parameter conflict --- tests/ansible/integration/stub_connections/mitogen_sudo.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ansible/integration/stub_connections/mitogen_sudo.yml b/tests/ansible/integration/stub_connections/mitogen_sudo.yml index b82b3ac2..cb530a56 100644 --- a/tests/ansible/integration/stub_connections/mitogen_sudo.yml +++ b/tests/ansible/integration/stub_connections/mitogen_sudo.yml @@ -10,6 +10,7 @@ - custom_python_detect_environment: vars: ansible_connection: mitogen_sudo + ansible_user: root ansible_become_exe: stub-sudo.py ansible_become_flags: --type=sometype --role=somerole register: out From 5f815ec6c425ca98abca4c92f7f81f22768853e1 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 15:27:21 +0000 Subject: [PATCH 19/26] tests: try to fix PATH problem on Travis. --- tests/ansible/integration/stub_connections/setns_lxc.yml | 2 +- tests/ansible/integration/stub_connections/setns_lxd.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ansible/integration/stub_connections/setns_lxc.yml b/tests/ansible/integration/stub_connections/setns_lxc.yml index a1feb4fe..32bb161e 100644 --- a/tests/ansible/integration/stub_connections/setns_lxc.yml +++ b/tests/ansible/integration/stub_connections/setns_lxc.yml @@ -14,7 +14,7 @@ - include_tasks: _end_play_if_not_sudo_linux.yml - command: | - sudo -nE ansible + sudo -nE "{{lookup('env', 'VIRTUAL_ENV')}}/bin/ansible" -i localhost, -c setns -e mitogen_kind=lxc diff --git a/tests/ansible/integration/stub_connections/setns_lxd.yml b/tests/ansible/integration/stub_connections/setns_lxd.yml index b507f412..539eab11 100644 --- a/tests/ansible/integration/stub_connections/setns_lxd.yml +++ b/tests/ansible/integration/stub_connections/setns_lxd.yml @@ -14,7 +14,7 @@ - include_tasks: _end_play_if_not_sudo_linux.yml - command: | - sudo -nE ansible + sudo -nE "{{lookup('env', 'VIRTUAL_ENV')}}/bin/ansible" -i localhost, -c setns -e mitogen_kind=lxd From 1bb239189b74eac52c7a4306504beeb91e607724 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 17:17:16 +0000 Subject: [PATCH 20/26] tests: another attempt at working paths. --- tests/ansible/integration/stub_connections/setns_lxc.yml | 2 +- tests/ansible/integration/stub_connections/setns_lxd.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ansible/integration/stub_connections/setns_lxc.yml b/tests/ansible/integration/stub_connections/setns_lxc.yml index 32bb161e..4bb21db7 100644 --- a/tests/ansible/integration/stub_connections/setns_lxc.yml +++ b/tests/ansible/integration/stub_connections/setns_lxc.yml @@ -18,7 +18,7 @@ -i localhost, -c setns -e mitogen_kind=lxc - -e mitogen_lxc_info_path=stub-lxc-info.py + -e mitogen_lxc_info_path={{git_basedir}}/tests/data/stubs/stub-lxc-info.py -m shell -a "echo hi" localhost diff --git a/tests/ansible/integration/stub_connections/setns_lxd.yml b/tests/ansible/integration/stub_connections/setns_lxd.yml index 539eab11..e430daa9 100644 --- a/tests/ansible/integration/stub_connections/setns_lxd.yml +++ b/tests/ansible/integration/stub_connections/setns_lxd.yml @@ -18,7 +18,7 @@ -i localhost, -c setns -e mitogen_kind=lxd - -e mitogen_lxc_path=stub-lxc.py + -e mitogen_lxc_path={{git_basedir}}/tests/data/stubs/stub-lxc.py -m shell -a "echo hi" localhost From de7d4e09087bf7b10605459ea1b4b893c415e5cf Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 17:21:48 +0000 Subject: [PATCH 21/26] setns: decode utility command output for 3.x. --- mitogen/setns.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mitogen/setns.py b/mitogen/setns.py index be87e063..0c94cd0b 100644 --- a/mitogen/setns.py +++ b/mitogen/setns.py @@ -69,7 +69,7 @@ def _run_command(args): output, _ = proc.communicate() if not proc.returncode: - return output + return output.decode('utf-8', 'replace') raise Error("%s exitted with status %d: %s", mitogen.parent.Argv(args), proc.returncode, output) From fcdfd5f107d8acb26c544e5be24cee1eeb26a05c Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 17:36:48 +0000 Subject: [PATCH 22/26] tests: fix disconnect_cleanup.yml target count assumption --- .../integration/context_service/disconnect_cleanup.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ansible/integration/context_service/disconnect_cleanup.yml b/tests/ansible/integration/context_service/disconnect_cleanup.yml index b657a0dc..359be3fb 100644 --- a/tests/ansible/integration/context_service/disconnect_cleanup.yml +++ b/tests/ansible/integration/context_service/disconnect_cleanup.yml @@ -28,7 +28,7 @@ register: out - assert: - that: out.dump|length == 4 # ssh account + 3 sudo accounts + that: out.dump|length == (play_hosts|length) * 4 # ssh account + 3 sudo accounts - meta: reset_connection @@ -43,4 +43,4 @@ register: out - assert: - that: out.dump|length == 1 # just the ssh account + that: out.dump|length == play_hosts|length # just the ssh account From ee2d10375d4629c2b83b88b678ad896e1c707c48 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 17:45:19 +0000 Subject: [PATCH 23/26] tests: don't run reset_connection tests on <2.5.6. --- .../ansible/integration/context_service/disconnect_cleanup.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/ansible/integration/context_service/disconnect_cleanup.yml b/tests/ansible/integration/context_service/disconnect_cleanup.yml index 359be3fb..dbcd27cc 100644 --- a/tests/ansible/integration/context_service/disconnect_cleanup.yml +++ b/tests/ansible/integration/context_service/disconnect_cleanup.yml @@ -8,6 +8,9 @@ - meta: end_play when: not is_mitogen + - meta: end_play + when: ansible_version.full < '2.5.6' + # Start with a clean slate. - mitogen_shutdown_all: From acf0b048767aa721426896596443959045ae6d87 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 18:50:47 +0000 Subject: [PATCH 24/26] tests: run some playbooks against only one target. --- tests/ansible/integration/ssh/variables.yml | 2 +- .../integration/strategy/_mixed_mitogen_vanilla.yml | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/ansible/integration/ssh/variables.yml b/tests/ansible/integration/ssh/variables.yml index dc4fe434..6bfcb2a5 100644 --- a/tests/ansible/integration/ssh/variables.yml +++ b/tests/ansible/integration/ssh/variables.yml @@ -3,7 +3,7 @@ # whatever reason. - name: integration/ssh/variables.yml - hosts: test-targets + hosts: test-targets[0] connection: local vars: # ControlMaster has the effect of caching the previous auth to the same diff --git a/tests/ansible/integration/strategy/_mixed_mitogen_vanilla.yml b/tests/ansible/integration/strategy/_mixed_mitogen_vanilla.yml index 7ac39e8e..1ec76fd1 100644 --- a/tests/ansible/integration/strategy/_mixed_mitogen_vanilla.yml +++ b/tests/ansible/integration/strategy/_mixed_mitogen_vanilla.yml @@ -2,9 +2,10 @@ # issue #294: ensure running mixed vanilla/Mitogen succeeds. - name: integration/strategy/_mixed_mitogen_vanilla.yml (mitogen_linear) - hosts: test-targets + hosts: test-targets[0] any_errors_fatal: true strategy: mitogen_linear + run_once: true tasks: - custom_python_detect_environment: register: out @@ -15,8 +16,9 @@ - assert: that: strategy == 'ansible.plugins.strategy.mitogen_linear.StrategyModule' + - name: integration/strategy/_mixed_mitogen_vanilla.yml (linear) - hosts: test-targets + hosts: test-targets[0] any_errors_fatal: true strategy: linear tasks: @@ -31,7 +33,7 @@ - name: integration/strategy/_mixed_mitogen_vanilla.yml (mitogen_linear) - hosts: test-targets + hosts: test-targets[0] any_errors_fatal: true strategy: mitogen_linear tasks: From 8972dbb7b988443eb0abcc825d9c892252ab0866 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 19:04:09 +0000 Subject: [PATCH 25/26] tests: more Ansible fixes. --- .travis/ansible_tests.py | 5 +++-- .../integration/context_service/disconnect_cleanup.yml | 2 +- .../ansible/integration/strategy/_mixed_vanilla_mitogen.yml | 6 +++--- .../ansible/integration/strategy/mixed_vanilla_mitogen.yml | 4 +++- tests/ansible/run_ansible_playbook.py | 5 +++++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.travis/ansible_tests.py b/.travis/ansible_tests.py index 8bb8f6a1..e664ec8b 100755 --- a/.travis/ansible_tests.py +++ b/.travis/ansible_tests.py @@ -63,5 +63,6 @@ with ci_lib.Fold('job_setup'): with ci_lib.Fold('ansible'): - run('/usr/bin/time ./run_ansible_playbook.py all.yml -i "%s" %s', - HOSTS_DIR, ' '.join(sys.argv[1:])) + playbook = os.environ.get('PLAYBOOK', 'all.yml') + run('/usr/bin/time ./run_ansible_playbook.py %s -i "%s" %s', + playbook, HOSTS_DIR, ' '.join(sys.argv[1:])) diff --git a/tests/ansible/integration/context_service/disconnect_cleanup.yml b/tests/ansible/integration/context_service/disconnect_cleanup.yml index dbcd27cc..575358f6 100644 --- a/tests/ansible/integration/context_service/disconnect_cleanup.yml +++ b/tests/ansible/integration/context_service/disconnect_cleanup.yml @@ -2,7 +2,7 @@ # state of dependent contexts (e.g. sudo, connection delegation, ..). - name: integration/context_service/disconnect_cleanup.yml - hosts: test-targets + hosts: test-targets[0] any_errors_fatal: true tasks: - meta: end_play diff --git a/tests/ansible/integration/strategy/_mixed_vanilla_mitogen.yml b/tests/ansible/integration/strategy/_mixed_vanilla_mitogen.yml index 891787af..babcab3f 100644 --- a/tests/ansible/integration/strategy/_mixed_vanilla_mitogen.yml +++ b/tests/ansible/integration/strategy/_mixed_vanilla_mitogen.yml @@ -2,7 +2,7 @@ # issue #294: ensure running mixed vanilla/Mitogen succeeds. - name: integration/strategy/_mixed_vanilla_mitogen.yml (linear) - hosts: test-targets + hosts: test-targets[0] any_errors_fatal: true strategy: linear tasks: @@ -16,7 +16,7 @@ that: strategy == 'ansible.plugins.strategy.linear.StrategyModule' - name: integration/strategy/_mixed_vanilla_mitogen.yml (mitogen_linear) - hosts: test-targets + hosts: test-targets[0] any_errors_fatal: true strategy: mitogen_linear tasks: @@ -31,7 +31,7 @@ - name: integration/strategy/_mixed_vanilla_mitogen.yml (linear) - hosts: test-targets + hosts: test-targets[0] any_errors_fatal: true strategy: linear tasks: diff --git a/tests/ansible/integration/strategy/mixed_vanilla_mitogen.yml b/tests/ansible/integration/strategy/mixed_vanilla_mitogen.yml index 61a55825..a7aadb19 100644 --- a/tests/ansible/integration/strategy/mixed_vanilla_mitogen.yml +++ b/tests/ansible/integration/strategy/mixed_vanilla_mitogen.yml @@ -1,12 +1,13 @@ - name: integration/strategy/mixed_vanilla_mitogen.yml (linear->mitogen->linear) - hosts: test-targets + hosts: test-targets[0] any_errors_fatal: true tasks: - connection: local command: | ansible-playbook -i "{{inventory_file}}" + -vvv integration/strategy/_mixed_mitogen_vanilla.yml args: chdir: ../.. @@ -16,6 +17,7 @@ command: | ansible-playbook -i "{{inventory_file}}" + -vvv integration/strategy/_mixed_vanilla_mitogen.yml args: chdir: ../.. diff --git a/tests/ansible/run_ansible_playbook.py b/tests/ansible/run_ansible_playbook.py index 2f85c8ac..a2eaded6 100755 --- a/tests/ansible/run_ansible_playbook.py +++ b/tests/ansible/run_ansible_playbook.py @@ -12,6 +12,11 @@ GIT_BASEDIR = os.path.dirname( ) ) +# Ensure VIRTUAL_ENV is exported. +os.environ.setdefault( + 'VIRTUAL_ENV', + os.path.dirname(os.path.dirname(sys.executable)) +) # Used by delegate_to.yml to ensure "sudo -E" preserves environment. os.environ['I_WAS_PRESERVED'] = '1' From 79ca67aadd73509ec3027711829fa916354058c1 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 5 Nov 2018 19:27:00 +0000 Subject: [PATCH 26/26] tests: disable connection tests for non-Mitogen --- .../integration/connection/disconnect_during_module.yml | 3 +++ .../integration/connection/disconnect_resets_connection.yml | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tests/ansible/integration/connection/disconnect_during_module.yml b/tests/ansible/integration/connection/disconnect_during_module.yml index 2b9c2c55..339dc6f5 100644 --- a/tests/ansible/integration/connection/disconnect_during_module.yml +++ b/tests/ansible/integration/connection/disconnect_during_module.yml @@ -6,6 +6,9 @@ gather_facts: no any_errors_fatal: false tasks: + - meta: end_play + when: not is_mitogen + - connection: local command: | ansible-playbook diff --git a/tests/ansible/integration/connection/disconnect_resets_connection.yml b/tests/ansible/integration/connection/disconnect_resets_connection.yml index 9e186182..5f02a8d5 100644 --- a/tests/ansible/integration/connection/disconnect_resets_connection.yml +++ b/tests/ansible/integration/connection/disconnect_resets_connection.yml @@ -14,6 +14,9 @@ gather_facts: no any_errors_fatal: true tasks: + - meta: end_play + when: not is_mitogen + - mitogen_action_script: script: | import sys