1- name: get the ansible-test imposed file descriptor limit
2  check_rlimit_and_maxfd:
3  register: rlimit_limited_return
4
5- name: get existing file descriptor limit
6  check_rlimit_and_maxfd:
7  register: rlimit_original_return
8  vars:
9    ansible_python_module_rlimit_nofile: 0  # ignore limit set by ansible-test
10
11- name: attempt to set a value lower than existing soft limit
12  check_rlimit_and_maxfd:
13  vars:
14    ansible_python_module_rlimit_nofile: '{{ rlimit_original_return.rlimit_nofile[0] - 1 }}'
15  register: rlimit_below_soft_return
16
17- name: attempt to set a value higher than existing soft limit
18  check_rlimit_and_maxfd:
19  vars:
20    ansible_python_module_rlimit_nofile: '{{ rlimit_original_return.rlimit_nofile[0] + 1 }}'
21  register: rlimit_above_soft_return
22
23- name: attempt to set a value lower than existing hard limit
24  check_rlimit_and_maxfd:
25  vars:
26    ansible_python_module_rlimit_nofile: '{{ rlimit_original_return.rlimit_nofile[1] - 1 }}'
27  register: rlimit_below_hard_return
28
29- name: attempt to set a value higher than existing hard limit
30  check_rlimit_and_maxfd:
31  vars:
32    ansible_python_module_rlimit_nofile: '{{ rlimit_original_return.rlimit_nofile[1] + 1 }}'
33  register: rlimit_above_hard_return
34
35- name: run a role module which uses a role module_util using relative imports
36  custom_module:
37  register: custom_module_return
38
39- assert:
40    that:
41      # make sure ansible-test was able to set the limit unless it exceeds the hard limit or the value is lower on macOS
42      - rlimit_limited_return.rlimit_nofile[0] == 1024 or rlimit_original_return.rlimit_nofile[1] < 1024 or (rlimit_limited_return.rlimit_nofile[0] < 1024 and ansible_distribution == 'MacOSX')
43      # make sure that maxfd matches the soft limit on Python 2.x (-1 on Python 3.x)
44      - rlimit_limited_return.maxfd == rlimit_limited_return.rlimit_nofile[0] or rlimit_limited_return.maxfd == -1
45
46      # we should always be able to set the limit lower than the existing soft limit
47      - rlimit_below_soft_return.rlimit_nofile[0] == rlimit_original_return.rlimit_nofile[0] - 1
48      # the hard limit should not have changed
49      - rlimit_below_soft_return.rlimit_nofile[1] == rlimit_original_return.rlimit_nofile[1]
50      # lowering the limit should also lower the max file descriptors reported by Python 2.x (-1 on Python 3.x)
51      - rlimit_below_soft_return.maxfd == rlimit_original_return.rlimit_nofile[0] - 1 or rlimit_below_soft_return.maxfd == -1
52
53      # we should be able to set the limit higher than the existing soft limit if it does not exceed the hard limit (except on macOS)
54      - rlimit_above_soft_return.rlimit_nofile[0] == rlimit_original_return.rlimit_nofile[0] + 1 or rlimit_original_return.rlimit_nofile[0] == rlimit_original_return.rlimit_nofile[1] or ansible_distribution == 'MacOSX'
55
56      # we should be able to set the limit lower than the existing hard limit (except on macOS)
57      - rlimit_below_hard_return.rlimit_nofile[0] == rlimit_original_return.rlimit_nofile[1] - 1 or ansible_distribution == 'MacOSX'
58
59      # setting the limit higher than the existing hard limit should use the hard limit (except on macOS)
60      - rlimit_above_hard_return.rlimit_nofile[0] == rlimit_original_return.rlimit_nofile[1] or ansible_distribution == 'MacOSX'
61
62      # custom module returned the correct answer
63      - custom_module_return.answer == 42
64
65# https://github.com/ansible/ansible/issues/64664
66# https://github.com/ansible/ansible/issues/64479
67- name: Run module that tries to access itself via sys.modules
68  sys_check:
69