Skip to content

Commit 5c1772c

Browse files
committed
tests: Check that atexit callbacks in Ansible modules
1 parent 7be79d0 commit 5c1772c

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import absolute_import, division, print_function
5+
__metaclass__ = type
6+
7+
import atexit
8+
import errno
9+
import os
10+
11+
from ansible.module_utils.basic import AnsibleModule
12+
13+
CLEANUP_FILE = '/tmp/mitogen_test_atexit_cleanup_canary.txt'
14+
15+
16+
def cleanup(file):
17+
try:
18+
os.unlink(file)
19+
except OSError as exc:
20+
if exc.errno == errno.ENOENT:
21+
pass
22+
raise
23+
24+
25+
def main():
26+
module = AnsibleModule(
27+
argument_spec={},
28+
)
29+
30+
atexit.register(cleanup, CLEANUP_FILE)
31+
32+
with open(CLEANUP_FILE, 'wb') as f:
33+
f.truncate()
34+
35+
result = {
36+
'changed': True,
37+
}
38+
module.exit_json(**result)
39+
40+
41+
42+
if __name__ == '__main__':
43+
main()

tests/ansible/regression/all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
- import_playbook: issue_1066__add_host__host_key_checking.yml
2020
- import_playbook: issue_1079__wait_for_connection_timeout.yml
2121
- import_playbook: issue_1087__template_streamerror.yml
22+
- import_playbook: issue_1360_atexit_cleanup.yml
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
- name: regression/issue_1360_atexit_cleanup.yml
2+
hosts: test-targets
3+
gather_facts: false
4+
become: false
5+
vars:
6+
cleanup_canary_path: /tmp/mitogen_test_atexit_cleanup_canary.txt
7+
tasks:
8+
- block:
9+
- name: Remove lingering cleanup canary
10+
file:
11+
path: "{{ cleanup_canary_path }}"
12+
state: absent
13+
14+
- name: Run module with atexit cleanup
15+
atexit_cleanup:
16+
17+
- name: Trigger exit of remote process
18+
meta: reset_connection
19+
20+
- name: Check presence of cleanup canary
21+
stat:
22+
path: "{{ cleanup_canary_path }}"
23+
register: cleanup_canary_stat
24+
25+
- assert:
26+
that:
27+
- not cleanup_canary_stat.stat.exists
28+
always:
29+
- name: Cleanup
30+
file:
31+
path: "{{ cleanup_canary_path }}"
32+
state: absent
33+
tags:
34+
- issue_1360

0 commit comments

Comments
 (0)