mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
Move uses of to_bytes, to_text, to_native to use the module_utils version (#17423)
We couldn't copy to_unicode, to_bytes, to_str into module_utils because of licensing. So once created it we had two sets of functions that did the same things but had different implementations. To remedy that, this change removes the ansible.utils.unicode versions of those functions.
This commit is contained in:
@@ -21,8 +21,6 @@
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import ast
|
||||
import json
|
||||
import pipes
|
||||
import os
|
||||
|
||||
@@ -33,7 +31,6 @@ except ImportError:
|
||||
|
||||
from nose.tools import eq_, raises
|
||||
|
||||
from ansible.release import __version__ as ansible_version
|
||||
from ansible import constants as C
|
||||
from ansible.compat.six import text_type
|
||||
from ansible.compat.tests import unittest
|
||||
@@ -41,12 +38,12 @@ from ansible.compat.tests.mock import patch, MagicMock, mock_open
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.playbook.play_context import PlayContext
|
||||
from ansible.plugins import PluginLoader
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.template import Templar
|
||||
from ansible.utils.unicode import to_bytes
|
||||
|
||||
from units.mock.loader import DictDataLoader
|
||||
from ansible.module_utils._text import to_bytes
|
||||
|
||||
|
||||
python_module_replacers = b"""
|
||||
#!/usr/bin/python
|
||||
@@ -67,11 +64,13 @@ WINDOWS_ARGS = "<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>"
|
||||
|
||||
class DerivedActionBase(ActionBase):
|
||||
TRANSFERS_FILES = False
|
||||
|
||||
def run(self, tmp=None, task_vars=None):
|
||||
# We're not testing the plugin run() method, just the helper
|
||||
# methods ActionBase defines
|
||||
return super(DerivedActionBase, self).run(tmp=tmp, task_vars=task_vars)
|
||||
|
||||
|
||||
class TestActionBase(unittest.TestCase):
|
||||
|
||||
def test_action_base_run(self):
|
||||
@@ -144,7 +143,7 @@ class TestActionBase(unittest.TestCase):
|
||||
self.assertRaises(AnsibleError, action_base._configure_module, 'badmodule', mock_task.args)
|
||||
|
||||
# test powershell module formatting
|
||||
with patch.object(builtins, 'open', mock_open(read_data=to_bytes(powershell_module_replacers.strip(), encoding='utf-8'))) as m:
|
||||
with patch.object(builtins, 'open', mock_open(read_data=to_bytes(powershell_module_replacers.strip(), encoding='utf-8'))):
|
||||
mock_task.action = 'win_copy'
|
||||
mock_task.args = dict(b=2)
|
||||
mock_connection.module_implementation_preferences = ('.ps1',)
|
||||
@@ -497,7 +496,10 @@ class TestActionBase(unittest.TestCase):
|
||||
action_base._connection.has_pipelining = True
|
||||
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')
|
||||
@@ -555,6 +557,7 @@ class TestActionBase(unittest.TestCase):
|
||||
finally:
|
||||
C.BECOME_ALLOW_SAME_USER = become_allow_same_user
|
||||
|
||||
|
||||
# Note: Using nose's generator test cases here so we can't inherit from
|
||||
# unittest.TestCase
|
||||
class TestFilterNonJsonLines(object):
|
||||
@@ -592,4 +595,3 @@ class TestFilterNonJsonLines(object):
|
||||
def test_unparsable_filter_non_json_lines(self):
|
||||
for stdout_line in self.unparsable_cases:
|
||||
yield self.check_unparsable_filter_non_json_lines, stdout_line
|
||||
|
||||
|
||||
@@ -22,17 +22,17 @@ from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import pipes
|
||||
import sys
|
||||
from io import StringIO
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch, MagicMock, mock_open
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNotFound
|
||||
from ansible.playbook.play_context import PlayContext
|
||||
from ansible.plugins.connection import ssh
|
||||
from ansible.utils.unicode import to_bytes, to_unicode
|
||||
from ansible.module_utils._text import to_bytes
|
||||
|
||||
|
||||
class TestConnectionBaseClass(unittest.TestCase):
|
||||
|
||||
@@ -277,20 +277,22 @@ class TestConnectionBaseClass(unittest.TestCase):
|
||||
C.ANSIBLE_SSH_RETRIES = 9
|
||||
|
||||
# test a regular, successful execution
|
||||
conn._exec_command.return_value = (0, 'stdout', '')
|
||||
conn._exec_command.return_value = (0, b'stdout', b'')
|
||||
res = conn.exec_command('ssh', 'some data')
|
||||
self.assertEquals(res, (0, b'stdout', b''), msg='exec_command did not return what the _exec_command helper returned')
|
||||
|
||||
# test a retry, followed by success
|
||||
conn._exec_command.return_value = None
|
||||
conn._exec_command.side_effect = [(255, '', ''), (0, 'stdout', '')]
|
||||
conn._exec_command.side_effect = [(255, '', ''), (0, b'stdout', b'')]
|
||||
res = conn.exec_command('ssh', 'some data')
|
||||
self.assertEquals(res, (0, b'stdout', b''), msg='exec_command did not return what the _exec_command helper returned')
|
||||
|
||||
# test multiple failures
|
||||
conn._exec_command.side_effect = [(255, '', '')]*10
|
||||
conn._exec_command.side_effect = [(255, b'', b'')] * 10
|
||||
self.assertRaises(AnsibleConnectionFailure, conn.exec_command, 'ssh', 'some data')
|
||||
|
||||
# test other failure from exec_command
|
||||
conn._exec_command.side_effect = [Exception('bad')]*10
|
||||
conn._exec_command.side_effect = [Exception('bad')] * 10
|
||||
self.assertRaises(Exception, conn.exec_command, 'ssh', 'some data')
|
||||
|
||||
@patch('os.path.exists')
|
||||
@@ -308,20 +310,22 @@ class TestConnectionBaseClass(unittest.TestCase):
|
||||
|
||||
# test with C.DEFAULT_SCP_IF_SSH enabled
|
||||
C.DEFAULT_SCP_IF_SSH = True
|
||||
res = conn.put_file('/path/to/in/file', '/path/to/dest/file')
|
||||
conn.put_file('/path/to/in/file', '/path/to/dest/file')
|
||||
conn._run.assert_called_with('some command to run', None)
|
||||
|
||||
res = conn.put_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩')
|
||||
conn.put_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩')
|
||||
conn._run.assert_called_with('some command to run', None)
|
||||
|
||||
# test with C.DEFAULT_SCP_IF_SSH disabled
|
||||
C.DEFAULT_SCP_IF_SSH = False
|
||||
expected_in_data = b' '.join((b'put', to_bytes(pipes.quote('/path/to/in/file')), to_bytes(pipes.quote('/path/to/dest/file')))) + b'\n'
|
||||
res = conn.put_file('/path/to/in/file', '/path/to/dest/file')
|
||||
conn.put_file('/path/to/in/file', '/path/to/dest/file')
|
||||
conn._run.assert_called_with('some command to run', expected_in_data)
|
||||
|
||||
expected_in_data = b' '.join((b'put', to_bytes(pipes.quote('/path/to/in/file/with/unicode-fö〩')), to_bytes(pipes.quote('/path/to/dest/file/with/unicode-fö〩')))) + b'\n'
|
||||
res = conn.put_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩')
|
||||
expected_in_data = b' '.join((b'put',
|
||||
to_bytes(pipes.quote('/path/to/in/file/with/unicode-fö〩')),
|
||||
to_bytes(pipes.quote('/path/to/dest/file/with/unicode-fö〩')))) + b'\n'
|
||||
conn.put_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩')
|
||||
conn._run.assert_called_with('some command to run', expected_in_data)
|
||||
|
||||
# test that a non-zero rc raises an error
|
||||
@@ -346,23 +350,24 @@ class TestConnectionBaseClass(unittest.TestCase):
|
||||
|
||||
# test with C.DEFAULT_SCP_IF_SSH enabled
|
||||
C.DEFAULT_SCP_IF_SSH = True
|
||||
res = conn.fetch_file('/path/to/in/file', '/path/to/dest/file')
|
||||
conn.fetch_file('/path/to/in/file', '/path/to/dest/file')
|
||||
conn._run.assert_called_with('some command to run', None)
|
||||
|
||||
res = conn.fetch_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩')
|
||||
conn.fetch_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩')
|
||||
conn._run.assert_called_with('some command to run', None)
|
||||
|
||||
# test with C.DEFAULT_SCP_IF_SSH disabled
|
||||
C.DEFAULT_SCP_IF_SSH = False
|
||||
expected_in_data = b' '.join((b'get', to_bytes(pipes.quote('/path/to/in/file')), to_bytes(pipes.quote('/path/to/dest/file')))) + b'\n'
|
||||
res = conn.fetch_file('/path/to/in/file', '/path/to/dest/file')
|
||||
conn.fetch_file('/path/to/in/file', '/path/to/dest/file')
|
||||
conn._run.assert_called_with('some command to run', expected_in_data)
|
||||
|
||||
expected_in_data = b' '.join((b'get', to_bytes(pipes.quote('/path/to/in/file/with/unicode-fö〩')), to_bytes(pipes.quote('/path/to/dest/file/with/unicode-fö〩')))) + b'\n'
|
||||
res = conn.fetch_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩')
|
||||
expected_in_data = b' '.join((b'get',
|
||||
to_bytes(pipes.quote('/path/to/in/file/with/unicode-fö〩')),
|
||||
to_bytes(pipes.quote('/path/to/dest/file/with/unicode-fö〩')))) + b'\n'
|
||||
conn.fetch_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩')
|
||||
conn._run.assert_called_with('some command to run', expected_in_data)
|
||||
|
||||
# test that a non-zero rc raises an error
|
||||
conn._run.return_value = (1, 'stdout', 'some errors')
|
||||
self.assertRaises(AnsibleError, conn.fetch_file, '/path/to/bad/file', '/remote/path/to/file')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user