Debian Codegraph @

test/integration/targets/module_utils/library/test_cwd_missing.py

raw
1 #!/usr/bin/python
2 from __future__ import annotations
3
4 import os
5
6 from ansible.module_utils.basic import AnsibleModule
7
8
9 def main():
10 # This module verifies that AnsibleModule works when cwd does not exist.
11 # This situation can occur as a race condition when the following conditions are met:
12 #
13 # 1) Execute a module which has high startup overhead prior to instantiating AnsibleModule (0.5s is enough in many cases).
14 # 2) Run the module async as the last task in a playbook using connection=local (a fire-and-forget task).
15 # 3) Remove the directory containing the playbook immediately after playbook execution ends (playbook in a temp dir).
16 #
17 # To ease testing of this race condition the deletion of cwd is handled in this module.
18 # This avoids race conditions in the test, including timing cwd deletion between AnsiballZ wrapper execution and AnsibleModule instantiation.
19 # The timing issue with AnsiballZ is due to cwd checking in the wrapper when code coverage is enabled.
20
21 temp = os.path.abspath('temp')
22
23 os.mkdir(temp)
24 os.chdir(temp)
25 os.rmdir(temp)
26
27 module = AnsibleModule(argument_spec=dict())
28 module.exit_json(before=temp, after=os.getcwd())
29
30
31 if __name__ == '__main__':
32 main()