Debian Codegraph @

test/units/modules/test_async_wrapper.py

raw
1 # Copyright (c) 2017 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 from __future__ import annotations
4
5
6 import os
7 import json
8 import shutil
9 import sys
10 import tempfile
11
12 from ansible.modules import async_wrapper
13
14
15 class TestAsyncWrapper:
16
17 def test_run_module(self, monkeypatch):
18
19 module_result = {'rc': 0}
20 module_lines = [
21 'import sys',
22 'sys.stderr.write("stderr stuff")',
23 "print('%s')" % json.dumps(module_result)
24 ]
25 module_data = '\n'.join(module_lines) + '\n'
26 module_data = module_data.encode('utf-8')
27
28 workdir = tempfile.mkdtemp()
29 fh, fn = tempfile.mkstemp(dir=workdir)
30
31 with open(fn, 'wb') as f:
32 f.write(module_data)
33
34 command = fn
35 jobid = 0
36 job_path = os.path.join(os.path.dirname(command), 'job')
37
38 monkeypatch.setattr(async_wrapper, 'job_path', job_path)
39
40 res = async_wrapper._run_module(jobid, sys.executable, command)
41
42 with open(os.path.join(workdir, 'job'), 'r') as f:
43 jres = json.loads(f.read())
44
45 shutil.rmtree(workdir)
46
47 assert jres.get('rc') == 0
48 assert jres.get('stderr') == 'stderr stuff'