Skip to content

Commit a47b9f3

Browse files
authored
Merge pull request #969 from moreati/transport_config_python_undiscover
tests: Speed up transport config tests by avoiding interpreter discovery
2 parents 572636a + 392c41d commit a47b9f3

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

.ci/ci_lib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ def proc_is_docker(pid):
300300

301301

302302
def get_interesting_procs(container_name=None):
303+
"""
304+
Return a list of (pid, line) tuples for processes considered interesting.
305+
"""
303306
args = ['ps', 'ax', '-oppid=', '-opid=', '-ocomm=', '-ocommand=']
304307
if container_name is not None:
305308
args = ['docker', 'exec', container_name] + args
@@ -312,6 +315,9 @@ def get_interesting_procs(container_name=None):
312315
any(comm.startswith(s) for s in INTERESTING_COMMS) or
313316
'mitogen:' in rest
314317
) and
318+
(
319+
'WALinuxAgent' not in rest
320+
) and
315321
(
316322
container_name is not None or
317323
(not proc_is_docker(pid))

ansible_mitogen/connection.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ class Connection(ansible.plugins.connection.ConnectionBase):
484484
login_context = None
485485

486486
#: Only sudo, su, and doas are supported for now.
487+
# Ansible ConnectionBase attribute, removed in Ansible >= 2.8
487488
become_methods = ['sudo', 'su', 'doas']
488489

489490
#: Dict containing init_child() return value as recorded at startup by
@@ -521,15 +522,6 @@ class Connection(ansible.plugins.connection.ConnectionBase):
521522
# set by `_get_task_vars()` for interpreter discovery
522523
_action = None
523524

524-
def __del__(self):
525-
"""
526-
Ansible cannot be trusted to always call close() e.g. the synchronize
527-
action constructs a local connection like this. So provide a destructor
528-
in the hopes of catching these cases.
529-
"""
530-
# https://github.com/dw/mitogen/issues/140
531-
self.close()
532-
533525
def on_action_run(self, task_vars, delegate_to_hostname, loader_basedir):
534526
"""
535527
Invoked by ActionModuleMixin to indicate a new task is about to start
@@ -684,6 +676,9 @@ def get_binding(self):
684676

685677
@property
686678
def connected(self):
679+
"""
680+
Ansible connection plugin property. Used by ansible-connection command.
681+
"""
687682
return self.context is not None
688683

689684
def _spec_from_via(self, proxied_inventory_name, via_spec):
@@ -842,7 +837,11 @@ def _connect(self):
842837
the _connect_*() service calls defined above to cause the master
843838
process to establish the real connection on our behalf, or return a
844839
reference to the existing one.
840+
841+
Ansible connection plugin method.
845842
"""
843+
# In some Ansible connection plugins this method returns self.
844+
# However nothing I've found uses it, it's not even assigned.
846845
if self.connected:
847846
return
848847

@@ -880,6 +879,8 @@ def close(self):
880879
Arrange for the mitogen.master.Router running in the worker to
881880
gracefully shut down, and wait for shutdown to complete. Safe to call
882881
multiple times.
882+
883+
Ansible connection plugin method.
883884
"""
884885
self._put_connection()
885886
if self.binding:
@@ -896,6 +897,8 @@ def reset(self):
896897
any local state we hold for the connection, returns the Connection to
897898
the 'disconnected' state, and informs ContextService the connection is
898899
bad somehow, and should be shut down and discarded.
900+
901+
Ansible connection plugin method.
899902
"""
900903
if self._play_context.remote_addr is None:
901904
# <2.5.6 incorrectly populate PlayContext for reset_connection
@@ -1002,6 +1005,8 @@ def exec_command(self, cmd, in_data='', sudoable=True, mitogen_chdir=None):
10021005
Data to supply on ``stdin`` of the process.
10031006
:returns:
10041007
(return code, stdout bytes, stderr bytes)
1008+
1009+
Ansible connection plugin method.
10051010
"""
10061011
emulate_tty = (not in_data and sudoable)
10071012
rc, stdout, stderr = self.get_chain().call(
@@ -1027,6 +1032,8 @@ def fetch_file(self, in_path, out_path):
10271032
Remote filesystem path to read.
10281033
:param str out_path:
10291034
Local filesystem path to write.
1035+
1036+
Ansible connection plugin method.
10301037
"""
10311038
self._connect()
10321039
ansible_mitogen.target.transfer_file(
@@ -1076,6 +1083,8 @@ def put_file(self, in_path, out_path):
10761083
Local filesystem path to read.
10771084
:param str out_path:
10781085
Remote filesystem path to write.
1086+
1087+
Ansible connection plugin method.
10791088
"""
10801089
try:
10811090
st = os.stat(in_path)

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ v0.3.4.dev0
2222

2323
* :gh:issue:`929` Support Ansible 6 and ansible-core 2.13
2424
* :gh:issue:`832` Fix runtime error when using the ansible.builtin.dnf module multiple times
25+
* :gh:issue:`925` :class:`ansible_mitogen.connection.Connection` no longer tries to close the
26+
connection on destruction. This is expected to reduce cases of `mitogen.core.Error: An attempt
27+
was made to enqueue a message with a Broker that has already exitted`. However it may result in
28+
resource leaks.
29+
2530

2631
v0.3.3 (2022-06-03)
2732
-------------------

tests/ansible/hosts/transport_config.hosts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,76 @@
11
# integration/transport_config
22
# Hosts with twiddled configs that need to be checked somehow.
33

4+
[transport_config:children]
5+
transport_config_undiscover
6+
tc_python_path
47

5-
# tansport()
8+
[transport_config_undiscover:children]
9+
tc_become
10+
tc_become_method
11+
tc_become_pass
12+
tc_become_user
13+
tc_password
14+
tc_port
15+
tc_remote_addr
16+
tc_remote_user
17+
tc_transport
18+
19+
[transport_config_undiscover:vars]
20+
# If python interpreter path is unset, Ansible tries to connect & discover it.
21+
# That causes approx 10 seconds timeout per task - there's no host to connect to.
22+
# This optimisation should not be relied in any test.
23+
# Note: tc-python-path-* are intentionally not included.
24+
ansible_python_interpreter = python3000 # Not expected to exist
25+
26+
[tc_transport]
627
tc-transport-unset
728
tc-transport-local ansible_connection=local
829
tc-transport-smart ansible_connection=smart
930

10-
# python_path()
31+
[tc_python_path]
1132
tc-python-path-unset
1233
tc-python-path-hostvar ansible_python_interpreter=/hostvar/path/to/python
1334
tc-python-path-local-unset ansible_connection=local
1435
tc-python-path-local-explicit ansible_connection=local ansible_python_interpreter=/a/b/c
1536

16-
# remote_addr()
37+
[tc_remote_addr]
1738
tc-remote-addr-unset # defaults to inventory_hostname
1839
tc-remote-addr-explicit-ssh ansible_ssh_host=ansi.ssh.host
1940
tc-remote-addr-explicit-host ansible_host=ansi.host
2041
tc-remote-addr-explicit-both ansible_ssh_host=a.b.c ansible_host=b.c.d
2142

22-
# password()
43+
[tc_password]
2344
tc-password-unset
2445
tc-password-explicit-ssh ansible_ssh_pass=ansi-ssh-pass
2546
tc-password-explicit-pass ansible_password=ansi-pass
2647
tc-password-explicit-both ansible_password=a.b.c ansible_ssh_pass=c.b.a
2748

28-
# remote_user()
49+
[tc_remote_user]
2950
tc-remote-user-unset # defaults to C.DEFAULT_REMOTE_USER
3051
tc-remote-user-explicit-ssh ansible_ssh_user=ansi-ssh-user
3152
tc-remote-user-explicit-user ansible_user=ansi-user
3253
tc-remote-user-explicit-both ansible_user=a.b.c ansible_ssh_user=c.b.a
3354

34-
# become()
55+
[tc_become]
3556
tc-become-unset
3657
tc-become-set
3758

38-
# become_method()
59+
[tc_become_method]
3960
tc-become-method-unset
4061
tc-become-method-su ansible_become_method=su
4162

42-
# become_user()
63+
[tc_become_user]
4364
tc-become-user-unset
4465
tc-become-user-set ansible_become_user=ansi-become-user
4566

46-
# become_pass()
67+
[tc_become_pass]
4768
tc-become-pass-unset
4869
tc-become-pass-password ansible_become_password=apassword
4970
tc-become-pass-pass ansible_become_pass=apass
5071
tc-become-pass-both ansible_become_pass=bpass ansible_become_password=bpassword
5172

52-
# port()
73+
[tc_port]
5374
tc-port-unset
5475
tc-port-explicit-port ansible_port=1234
5576
tc-port-explicit-ssh ansible_ssh_port=4321

0 commit comments

Comments
 (0)