tenks/tests/utils.py
Iury Gregory Melo Ferreira 5abd97149c Py2 - Remove six, mock and future
Since we've dropped support for Python 2.7, it's time to look at
the bright future that Python 3.x will bring and stop forcing
compatibility with older versions.

This patch removes the six library , switches the mock library
to unittest.mock and removes future.

Change-Id: I71b11f13691d13df162b203f7ea5979b30c272df
2020-05-04 15:22:23 +02:00

50 lines
1.2 KiB
Python

import json
import unittest
from unittest.mock import patch
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
def set_module_args(args):
if '_ansible_remote_tmp' not in args:
args['_ansible_remote_tmp'] = '/tmp'
if '_ansible_keep_remote_files' not in args:
args['_ansible_keep_remote_files'] = False
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
def exit_json(*args, **kwargs):
if 'changed' not in kwargs:
kwargs['changed'] = False
raise AnsibleExitJson(kwargs)
def fail_json(*args, **kwargs):
kwargs['failed'] = True
raise AnsibleFailJson(kwargs)
class ModuleTestCase(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(ModuleTestCase, self).__init__(*args, **kwargs)
def setUp(self):
self.mock_module = patch.multiple(basic.AnsibleModule,
exit_json=exit_json,
fail_json=fail_json)
self.mock_module.start()
set_module_args({})
self.addCleanup(self.mock_module.stop)