mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-02 14:29:10 -07:00
test/: PEP8 compliancy (#24803)
* test/: PEP8 compliancy - Make PEP8 compliant * Python3 chokes on casting int to bytes (#24952) But if we tell the formatter that the var is a number, it works
This commit is contained in:
parent
31c59ad5f9
commit
4efec414e7
110 changed files with 1702 additions and 1547 deletions
|
@ -436,10 +436,19 @@ class TestActionBase(unittest.TestCase):
|
|||
action_base._make_tmp_path.return_value = '/the/tmp/path'
|
||||
action_base._low_level_execute_command.return_value = dict(stdout='{"rc": 0, "stdout": "ok"}')
|
||||
self.assertEqual(action_base._execute_module(module_name=None, module_args=None), dict(_ansible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
|
||||
self.assertEqual(action_base._execute_module(module_name='foo',
|
||||
module_args=dict(z=9, y=8, x=7), task_vars=dict(a=1)),
|
||||
dict(_ansible_parsed=True, rc=0, stdout="ok",
|
||||
stdout_lines=['ok']))
|
||||
self.assertEqual(
|
||||
action_base._execute_module(
|
||||
module_name='foo',
|
||||
module_args=dict(z=9, y=8, x=7),
|
||||
task_vars=dict(a=1)
|
||||
),
|
||||
dict(
|
||||
_ansible_parsed=True,
|
||||
rc=0,
|
||||
stdout="ok",
|
||||
stdout_lines=['ok'],
|
||||
)
|
||||
)
|
||||
|
||||
# test with needing/removing a remote tmp path
|
||||
action_base._configure_module.return_value = ('old', '#!/usr/bin/python', 'this is the module data', 'path')
|
||||
|
@ -525,9 +534,9 @@ class TestActionBaseCleanReturnedData(unittest.TestCase):
|
|||
|
||||
mock_shared_loader_obj.connection_loader = mock_connection_loader
|
||||
mock_connection = MagicMock()
|
||||
#mock_connection._shell.env_prefix.side_effect = env_prefix
|
||||
# mock_connection._shell.env_prefix.side_effect = env_prefix
|
||||
|
||||
#action_base = DerivedActionBase(mock_task, mock_connection, play_context, None, None, None)
|
||||
# action_base = DerivedActionBase(mock_task, mock_connection, play_context, None, None, None)
|
||||
action_base = DerivedActionBase(task=None,
|
||||
connection=mock_connection,
|
||||
play_context=None,
|
||||
|
@ -535,7 +544,7 @@ class TestActionBaseCleanReturnedData(unittest.TestCase):
|
|||
templar=None,
|
||||
shared_loader_obj=mock_shared_loader_obj)
|
||||
data = {'ansible_playbook_python': '/usr/bin/python',
|
||||
#'ansible_rsync_path': '/usr/bin/rsync',
|
||||
# 'ansible_rsync_path': '/usr/bin/rsync',
|
||||
'ansible_python_interpreter': '/usr/bin/python',
|
||||
'ansible_ssh_some_var': 'whatever',
|
||||
'ansible_ssh_host_key_somehost': 'some key here',
|
||||
|
@ -592,7 +601,7 @@ class TestActionBaseParseReturnedData(unittest.TestCase):
|
|||
'stdout_lines': stdout.splitlines(),
|
||||
'stderr': err}
|
||||
res = action_base._parse_returned_data(returned_data)
|
||||
del res['_ansible_parsed'] # we always have _ansible_parsed
|
||||
del res['_ansible_parsed'] # we always have _ansible_parsed
|
||||
self.assertEqual(len(res), 0)
|
||||
self.assertFalse(res)
|
||||
|
||||
|
@ -610,7 +619,7 @@ class TestActionBaseParseReturnedData(unittest.TestCase):
|
|||
self.assertTrue(res['ansible_facts'])
|
||||
self.assertIn('ansible_blip', res['ansible_facts'])
|
||||
# TODO: Should this be an AnsibleUnsafe?
|
||||
#self.assertIsInstance(res['ansible_facts'], AnsibleUnsafe)
|
||||
# self.assertIsInstance(res['ansible_facts'], AnsibleUnsafe)
|
||||
|
||||
def test_json_facts_add_host(self):
|
||||
action_base = self._action_base()
|
||||
|
@ -630,4 +639,4 @@ class TestActionBaseParseReturnedData(unittest.TestCase):
|
|||
self.assertIn('ansible_blip', res['ansible_facts'])
|
||||
self.assertIn('add_host', res)
|
||||
# TODO: Should this be an AnsibleUnsafe?
|
||||
#self.assertIsInstance(res['ansible_facts'], AnsibleUnsafe)
|
||||
# self.assertIsInstance(res['ansible_facts'], AnsibleUnsafe)
|
||||
|
|
|
@ -24,6 +24,7 @@ from ansible.compat.tests.mock import patch, MagicMock, Mock
|
|||
from ansible.plugins.action.raw import ActionModule
|
||||
from ansible.playbook.task import Task
|
||||
|
||||
|
||||
class TestCopyResultExclude(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
@ -38,7 +39,6 @@ class TestCopyResultExclude(unittest.TestCase):
|
|||
# Issue: https://github.com/ansible/ansible/issues/16054
|
||||
# PR: https://github.com/ansible/ansible/pull/16085
|
||||
|
||||
|
||||
def test_raw_executable_is_not_empty_string(self):
|
||||
|
||||
play_context = Mock()
|
||||
|
@ -50,7 +50,7 @@ class TestCopyResultExclude(unittest.TestCase):
|
|||
play_context.check_mode = False
|
||||
|
||||
self.mock_am = ActionModule(task, connection, play_context, loader=None, templar=None, shared_loader_obj=None)
|
||||
self.mock_am._low_level_execute_command = Mock(return_value = {})
|
||||
self.mock_am._low_level_execute_command = Mock(return_value={})
|
||||
self.mock_am.display = Mock()
|
||||
|
||||
self.mock_am.run()
|
||||
|
@ -83,7 +83,7 @@ class TestCopyResultExclude(unittest.TestCase):
|
|||
play_context.check_mode = False
|
||||
|
||||
self.mock_am = ActionModule(task, connection, play_context, loader=None, templar=None, shared_loader_obj=None)
|
||||
self.mock_am._low_level_execute_command = Mock(return_value = {})
|
||||
self.mock_am._low_level_execute_command = Mock(return_value={})
|
||||
self.mock_am.display = Mock()
|
||||
|
||||
self.assertEqual(task.environment, None)
|
||||
|
@ -100,10 +100,8 @@ class TestCopyResultExclude(unittest.TestCase):
|
|||
play_context.check_mode = False
|
||||
|
||||
self.mock_am = ActionModule(task, connection, play_context, loader=None, templar=None, shared_loader_obj=None)
|
||||
self.mock_am._low_level_execute_command = Mock(return_value = {})
|
||||
self.mock_am._low_level_execute_command = Mock(return_value={})
|
||||
self.mock_am.display = Mock()
|
||||
|
||||
self.mock_am.run(task_vars={'a': 'b'})
|
||||
self.assertEqual(task.environment, None)
|
||||
|
||||
|
||||
|
|
|
@ -17,11 +17,12 @@ import unittest
|
|||
import yaml
|
||||
|
||||
from pprint import pprint
|
||||
from ansible import plugins
|
||||
|
||||
import ansible.plugins
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
from ansible.plugins.action.synchronize import ActionModule
|
||||
|
||||
|
||||
# Getting the incoming and outgoing task vars from the plugin's run method
|
||||
|
||||
'''
|
||||
|
@ -41,8 +42,6 @@ with open('task_vars.json', 'wb') as f:
|
|||
'''
|
||||
|
||||
|
||||
|
||||
|
||||
class TaskMock(object):
|
||||
args = {'src': u'/tmp/deleteme',
|
||||
'dest': '/tmp/deleteme',
|
||||
|
@ -52,16 +51,19 @@ class TaskMock(object):
|
|||
become_user = None
|
||||
become_method = None
|
||||
|
||||
|
||||
class StdinMock(object):
|
||||
shell = None
|
||||
|
||||
|
||||
class ConnectionMock(object):
|
||||
ismock = True
|
||||
_play_context = None
|
||||
#transport = 'ssh'
|
||||
# transport = 'ssh'
|
||||
transport = None
|
||||
_new_stdin = StdinMock()
|
||||
|
||||
|
||||
class PlayContextMock(object):
|
||||
shell = None
|
||||
private_key_file = None
|
||||
|
@ -75,13 +77,16 @@ class PlayContextMock(object):
|
|||
remote_user = None
|
||||
password = None
|
||||
|
||||
|
||||
class ModuleLoaderMock(object):
|
||||
def find_plugin(self, module_name, mod_type):
|
||||
pass
|
||||
|
||||
|
||||
class SharedLoaderMock(object):
|
||||
module_loader = ModuleLoaderMock()
|
||||
|
||||
|
||||
class SynchronizeTester(object):
|
||||
|
||||
''' A wrapper for mocking out synchronize environments '''
|
||||
|
@ -96,7 +101,6 @@ class SynchronizeTester(object):
|
|||
final_task_vars = None
|
||||
execute_called = False
|
||||
|
||||
|
||||
def _execute_module(self, module_name, module_args=None, task_vars=None):
|
||||
self.execute_called = True
|
||||
self.final_module_args = module_args
|
||||
|
@ -114,7 +118,7 @@ class SynchronizeTester(object):
|
|||
if '_play_context' in test_meta:
|
||||
if test_meta['_play_context']:
|
||||
self.task.args = {}
|
||||
for k,v in test_meta['_play_context'].items():
|
||||
for (k, v) in test_meta['_play_context'].items():
|
||||
if v == 'None':
|
||||
v = None
|
||||
setattr(self._play_context, k, v)
|
||||
|
@ -123,8 +127,8 @@ class SynchronizeTester(object):
|
|||
if '_task' in test_meta:
|
||||
if test_meta['_task']:
|
||||
self.task.args = {}
|
||||
for k,v in test_meta['_task'].items():
|
||||
#import epdb; epdb.st()
|
||||
for (k, v) in test_meta['_task'].items():
|
||||
# import epdb; epdb.st()
|
||||
if v == 'None':
|
||||
v = None
|
||||
setattr(self.task, k, v)
|
||||
|
@ -133,32 +137,30 @@ class SynchronizeTester(object):
|
|||
if 'task_args' in test_meta:
|
||||
if test_meta['task_args']:
|
||||
self.task.args = {}
|
||||
for k,v in test_meta['task_args'].items():
|
||||
for (k, v) in test_meta['task_args'].items():
|
||||
self.task.args[k] = v
|
||||
|
||||
# load inital task vars
|
||||
invarspath = os.path.join(fixturepath,
|
||||
test_meta.get('fixtures', {}).get('taskvars_in', 'taskvars_in.json'))
|
||||
invarspath = os.path.join(fixturepath, test_meta.get('fixtures', {}).get('taskvars_in', 'taskvars_in.json'))
|
||||
with open(invarspath, 'rb') as f:
|
||||
fdata = f.read()
|
||||
fdata = fdata.decode("utf-8")
|
||||
in_task_vars = json.loads(fdata)
|
||||
|
||||
# load expected final task vars
|
||||
outvarspath = os.path.join(fixturepath,
|
||||
test_meta.get('fixtures', {}).get('taskvars_out', 'taskvars_out.json'))
|
||||
outvarspath = os.path.join(fixturepath, test_meta.get('fixtures', {}).get('taskvars_out', 'taskvars_out.json'))
|
||||
with open(outvarspath, 'rb') as f:
|
||||
fdata = f.read()
|
||||
fdata = fdata.decode("utf-8")
|
||||
out_task_vars = json.loads(fdata)
|
||||
|
||||
# fixup the connection
|
||||
for k,v in test_meta['connection'].items():
|
||||
for (k, v) in test_meta['connection'].items():
|
||||
setattr(self.connection, k, v)
|
||||
|
||||
# fixup the hostvars
|
||||
if test_meta['hostvars']:
|
||||
for k,v in test_meta['hostvars'].items():
|
||||
for (k, v) in test_meta['hostvars'].items():
|
||||
in_task_vars['hostvars'][k] = v
|
||||
|
||||
# initalize and run the module
|
||||
|
@ -170,9 +172,9 @@ class SynchronizeTester(object):
|
|||
# run assertions
|
||||
for check in test_meta['asserts']:
|
||||
value = eval(check)
|
||||
#if not value:
|
||||
# print(check, value)
|
||||
# import epdb; epdb.st()
|
||||
# if not value:
|
||||
# print(check, value)
|
||||
# import epdb; epdb.st()
|
||||
assert value, check
|
||||
|
||||
|
||||
|
@ -190,54 +192,52 @@ class FakePluginLoader(object):
|
|||
|
||||
class TestSynchronizeAction(unittest.TestCase):
|
||||
|
||||
|
||||
fixturedir = os.path.dirname(__file__)
|
||||
fixturedir = os.path.join(fixturedir, 'fixtures', 'synchronize')
|
||||
#print(basedir)
|
||||
|
||||
# print(basedir)
|
||||
|
||||
@patch('ansible.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||
def test_basic(self):
|
||||
x = SynchronizeTester()
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir,'basic'))
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'basic'))
|
||||
|
||||
@patch('ansible.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||
def test_basic_become(self):
|
||||
x = SynchronizeTester()
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir,'basic_become'))
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'basic_become'))
|
||||
|
||||
@patch('ansible.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||
def test_basic_become_cli(self):
|
||||
# --become on the cli sets _play_context.become
|
||||
x = SynchronizeTester()
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir,'basic_become_cli'))
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'basic_become_cli'))
|
||||
|
||||
@patch('ansible.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||
def test_basic_vagrant(self):
|
||||
# simple vagrant example
|
||||
x = SynchronizeTester()
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir,'basic_vagrant'))
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'basic_vagrant'))
|
||||
|
||||
@patch('ansible.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||
def test_basic_vagrant_sudo(self):
|
||||
# vagrant plus sudo
|
||||
x = SynchronizeTester()
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir,'basic_vagrant_sudo'))
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'basic_vagrant_sudo'))
|
||||
|
||||
@patch('ansible.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||
def test_basic_vagrant_become_cli(self):
|
||||
# vagrant plus sudo
|
||||
x = SynchronizeTester()
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir,'basic_vagrant_become_cli'))
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'basic_vagrant_become_cli'))
|
||||
|
||||
@patch('ansible.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||
def test_delegate_remote(self):
|
||||
# delegate to other remote host
|
||||
x = SynchronizeTester()
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir,'delegate_remote'))
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'delegate_remote'))
|
||||
|
||||
@patch('ansible.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||
def test_delegate_remote_su(self):
|
||||
# delegate to other remote host with su enabled
|
||||
x = SynchronizeTester()
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir,'delegate_remote_su'))
|
||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'delegate_remote_su'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue