mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-10 18:34:03 -07:00
Fix unittests
This commit is contained in:
parent
452034564c
commit
75546678d9
7 changed files with 291 additions and 114 deletions
|
@ -22,18 +22,38 @@ __metaclass__ = type
|
|||
|
||||
import sys
|
||||
import json
|
||||
from io import BytesIO, StringIO
|
||||
|
||||
from ansible.compat.six import PY3
|
||||
from ansible.utils.unicode import to_bytes
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import MagicMock
|
||||
|
||||
class TestModuleUtilsBasic(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.real_stdin = sys.stdin
|
||||
args = json.dumps(
|
||||
dict(
|
||||
ANSIBLE_MODULE_ARGS=dict(
|
||||
foo=False, bar=[1,2,3], bam="bam", baz=u'baz'),
|
||||
ANSIBLE_MODULE_CONSTANTS=dict()
|
||||
)
|
||||
)
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
|
||||
def tearDown(self):
|
||||
sys.stdin = self.real_stdin
|
||||
|
||||
@unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)")
|
||||
def test_module_utils_basic__log_invocation(self):
|
||||
from ansible.module_utils import basic
|
||||
|
||||
# test basic log invocation
|
||||
basic.MODULE_COMPLEX_ARGS = json.dumps(dict(foo=False, bar=[1,2,3], bam="bam", baz=u'baz'))
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
am = basic.AnsibleModule(
|
||||
argument_spec=dict(
|
||||
foo = dict(default=True, type='bool'),
|
||||
|
|
|
@ -23,8 +23,10 @@ __metaclass__ = type
|
|||
import copy
|
||||
import json
|
||||
import sys
|
||||
from io import BytesIO
|
||||
from io import BytesIO, StringIO
|
||||
|
||||
from ansible.compat.six import PY3
|
||||
from ansible.utils.unicode import to_bytes
|
||||
from ansible.compat.tests import unittest
|
||||
|
||||
from ansible.module_utils import basic
|
||||
|
@ -37,9 +39,13 @@ empty_invocation = {u'module_args': {}}
|
|||
class TestAnsibleModuleExitJson(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.COMPLEX_ARGS = basic.MODULE_COMPLEX_ARGS
|
||||
basic.MODULE_COMPLEX_ARGS = '{}'
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
self.old_stdin = sys.stdin
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
|
||||
self.old_stdout = sys.stdout
|
||||
self.fake_stream = BytesIO()
|
||||
|
@ -48,8 +54,8 @@ class TestAnsibleModuleExitJson(unittest.TestCase):
|
|||
self.module = basic.AnsibleModule(argument_spec=dict())
|
||||
|
||||
def tearDown(self):
|
||||
basic.MODULE_COMPLEX_ARGS = self.COMPLEX_ARGS
|
||||
sys.stdout = self.old_stdout
|
||||
sys.stdin = self.old_stdin
|
||||
|
||||
def test_exit_json_no_args_exits(self):
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
|
@ -118,19 +124,31 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
|
|||
)
|
||||
|
||||
def setUp(self):
|
||||
self.COMPLEX_ARGS = basic.MODULE_COMPLEX_ARGS
|
||||
self.old_stdin = sys.stdin
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
|
||||
self.old_stdout = sys.stdout
|
||||
|
||||
def tearDown(self):
|
||||
basic.MODULE_COMPLEX_ARGS = self.COMPLEX_ARGS
|
||||
sys.stdin = self.old_stdin
|
||||
sys.stdout = self.old_stdout
|
||||
|
||||
def test_exit_json_removes_values(self):
|
||||
self.maxDiff = None
|
||||
for args, return_val, expected in self.dataset:
|
||||
sys.stdout = BytesIO()
|
||||
basic.MODULE_COMPLEX_ARGS = json.dumps(args)
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
params = dict(ANSIBLE_MODULE_ARGS=args, ANSIBLE_MODULE_CONSTANTS={})
|
||||
params = json.dumps(params)
|
||||
if PY3:
|
||||
sys.stdin = StringIO(params)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(params))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(params))
|
||||
module = basic.AnsibleModule(
|
||||
argument_spec = dict(
|
||||
username=dict(),
|
||||
|
@ -149,8 +167,13 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
|
|||
del expected['changed']
|
||||
expected['failed'] = True
|
||||
sys.stdout = BytesIO()
|
||||
basic.MODULE_COMPLEX_ARGS = json.dumps(args)
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
params = dict(ANSIBLE_MODULE_ARGS=args, ANSIBLE_MODULE_CONSTANTS={})
|
||||
params = json.dumps(params)
|
||||
if PY3:
|
||||
sys.stdin = StringIO(params)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(params))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(params))
|
||||
module = basic.AnsibleModule(
|
||||
argument_spec = dict(
|
||||
username=dict(),
|
||||
|
|
|
@ -21,7 +21,12 @@ from __future__ import (absolute_import, division)
|
|||
__metaclass__ = type
|
||||
|
||||
import sys
|
||||
import json
|
||||
import syslog
|
||||
from io import BytesIO, StringIO
|
||||
|
||||
from ansible.compat.six import PY3
|
||||
from ansible.utils.unicode import to_bytes
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
|
@ -41,10 +46,14 @@ except ImportError:
|
|||
class TestAnsibleModuleSysLogSmokeTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.complex_args_token = basic.MODULE_COMPLEX_ARGS
|
||||
self.constants_sentinel = basic.MODULE_CONSTANTS
|
||||
basic.MODULE_COMPLEX_ARGS = '{}'
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
|
||||
self.real_stdin = sys.stdin
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
|
||||
self.am = basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
)
|
||||
|
@ -55,8 +64,7 @@ class TestAnsibleModuleSysLogSmokeTest(unittest.TestCase):
|
|||
basic.has_journal = False
|
||||
|
||||
def tearDown(self):
|
||||
basic.MODULE_COMPLEX_ARGS = self.complex_args_token
|
||||
basic.MODULE_CONSTANTS = self.constants_sentinel
|
||||
sys.stdin = self.real_stdin
|
||||
basic.has_journal = self.has_journal
|
||||
|
||||
def test_smoketest_syslog(self):
|
||||
|
@ -75,17 +83,21 @@ class TestAnsibleModuleSysLogSmokeTest(unittest.TestCase):
|
|||
class TestAnsibleModuleJournaldSmokeTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.complex_args_token = basic.MODULE_COMPLEX_ARGS
|
||||
self.constants_sentinel = basic.MODULE_CONSTANTS
|
||||
basic.MODULE_COMPLEX_ARGS = '{}'
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
|
||||
self.real_stdin = sys.stdin
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
|
||||
|
||||
self.am = basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
basic.MODULE_COMPLEX_ARGS = self.complex_args_token
|
||||
basic.MODULE_CONSTANTS = self.constants_sentinel
|
||||
sys.stdin = self.real_stdin
|
||||
|
||||
@unittest.skipUnless(basic.has_journal, 'python systemd bindings not installed')
|
||||
def test_smoketest_journal(self):
|
||||
|
@ -121,10 +133,15 @@ class TestAnsibleModuleLogSyslog(unittest.TestCase):
|
|||
}
|
||||
|
||||
def setUp(self):
|
||||
self.complex_args_token = basic.MODULE_COMPLEX_ARGS
|
||||
self.constants_sentinel = basic.MODULE_CONSTANTS
|
||||
basic.MODULE_COMPLEX_ARGS = '{}'
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
|
||||
self.real_stdin = sys.stdin
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
|
||||
|
||||
self.am = basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
)
|
||||
|
@ -134,8 +151,7 @@ class TestAnsibleModuleLogSyslog(unittest.TestCase):
|
|||
basic.has_journal = False
|
||||
|
||||
def tearDown(self):
|
||||
basic.MODULE_COMPLEX_ARGS = self.complex_args_token
|
||||
basic.MODULE_CONSTANTS = self.constants_sentinel
|
||||
sys.stdin = self.real_stdin
|
||||
basic.has_journal = self.has_journal
|
||||
|
||||
@patch('syslog.syslog', autospec=True)
|
||||
|
@ -176,10 +192,14 @@ class TestAnsibleModuleLogJournal(unittest.TestCase):
|
|||
}
|
||||
|
||||
def setUp(self):
|
||||
self.complex_args_token = basic.MODULE_COMPLEX_ARGS
|
||||
self.constants_sentinel = basic.MODULE_CONSTANTS
|
||||
basic.MODULE_COMPLEX_ARGS = '{}'
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
|
||||
self.real_stdin = sys.stdin
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
|
||||
self.am = basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
)
|
||||
|
@ -198,8 +218,8 @@ class TestAnsibleModuleLogJournal(unittest.TestCase):
|
|||
self._fake_out_reload(basic)
|
||||
|
||||
def tearDown(self):
|
||||
basic.MODULE_COMPLEX_ARGS = self.complex_args_token
|
||||
basic.MODULE_CONSTANTS = self.constants_sentinel
|
||||
sys.stdin = self.real_stdin
|
||||
|
||||
basic.has_journal = self.has_journal
|
||||
if self.module_patcher:
|
||||
self.module_patcher.stop()
|
||||
|
|
|
@ -20,9 +20,13 @@ from __future__ import (absolute_import, division)
|
|||
__metaclass__ = type
|
||||
|
||||
import errno
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
from io import BytesIO
|
||||
from io import BytesIO, StringIO
|
||||
|
||||
from ansible.compat.six import PY3
|
||||
from ansible.utils.unicode import to_bytes
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import call, MagicMock, Mock, patch, sentinel
|
||||
|
@ -61,8 +65,12 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
|
|||
if path == '/inaccessible':
|
||||
raise OSError(errno.EPERM, "Permission denied: '/inaccessible'")
|
||||
|
||||
basic.MODULE_COMPLEX_ARGS = '{}'
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
self.module = AnsibleModule(argument_spec=dict())
|
||||
self.module.fail_json = MagicMock(side_effect=SystemExit)
|
||||
|
||||
|
|
|
@ -20,16 +20,32 @@
|
|||
from __future__ import (absolute_import, division)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
import sys
|
||||
import json
|
||||
from io import BytesIO, StringIO
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.six import PY3
|
||||
from ansible.utils.unicode import to_bytes
|
||||
|
||||
class TestAnsibleModuleExitJson(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.real_stdin = sys.stdin
|
||||
|
||||
def tearDown(self):
|
||||
sys.stdin = self.real_stdin
|
||||
|
||||
def test_module_utils_basic_safe_eval(self):
|
||||
from ansible.module_utils import basic
|
||||
|
||||
basic.MODULE_COMPLEX_ARGS = '{}'
|
||||
basic.MODULE_CONSTANTS = '{}'
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
|
||||
if PY3:
|
||||
sys.stdin = StringIO(args)
|
||||
sys.stdin.buffer = BytesIO(to_bytes(args))
|
||||
else:
|
||||
sys.stdin = BytesIO(to_bytes(args))
|
||||
|
||||
am = basic.AnsibleModule(
|
||||
argument_spec=dict(),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue