Debian Codegraph @

test/units/module_utils/basic/test_command_nonexisting.py

raw
1 from __future__ import annotations
2
3 import sys
4 import pytest
5 import subprocess
6
7 from ansible.module_utils import basic
8 from ansible.module_utils.testing import patch_module_args
9
10
11 def test_run_non_existent_command(monkeypatch):
12 """ Test that `command` returns std{out,err} even if the executable is not found """
13 def fail_json(msg, **kwargs):
14 assert kwargs["stderr"] == ''
15 assert kwargs["stdout"] == ''
16 sys.exit(1)
17
18 def popen(*args, **kwargs):
19 raise OSError()
20
21 monkeypatch.setattr(subprocess, 'Popen', popen)
22
23 with patch_module_args():
24 am = basic.AnsibleModule(argument_spec={})
25
26 monkeypatch.setattr(am, 'fail_json', fail_json)
27 with pytest.raises(SystemExit):
28 am.run_command("lecho", "whatever")