mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 20:31:27 -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
|
@ -20,8 +20,8 @@
|
|||
from __future__ import (absolute_import, division)
|
||||
__metaclass__ = type
|
||||
|
||||
import sys
|
||||
import json
|
||||
import sys
|
||||
|
||||
from units.mock.procenv import swap_stdin_and_argv
|
||||
|
||||
|
@ -31,23 +31,21 @@ from ansible.compat.tests.mock import MagicMock
|
|||
|
||||
class TestModuleUtilsBasic(unittest.TestCase):
|
||||
def test_module_utils_basic__log_invocation(self):
|
||||
with swap_stdin_and_argv(stdin_data=json.dumps(
|
||||
dict(
|
||||
ANSIBLE_MODULE_ARGS=dict(
|
||||
foo=False, bar=[1,2,3], bam="bam", baz=u'baz'),
|
||||
))):
|
||||
with swap_stdin_and_argv(stdin_data=json.dumps(dict(
|
||||
ANSIBLE_MODULE_ARGS=dict(foo=False, bar=[1, 2, 3], bam="bam", baz=u'baz')),
|
||||
)):
|
||||
from ansible.module_utils import basic
|
||||
|
||||
# test basic log invocation
|
||||
basic._ANSIBLE_ARGS = None
|
||||
am = basic.AnsibleModule(
|
||||
argument_spec=dict(
|
||||
foo = dict(default=True, type='bool'),
|
||||
bar = dict(default=[], type='list'),
|
||||
bam = dict(default="bam"),
|
||||
baz = dict(default=u"baz"),
|
||||
password = dict(default=True),
|
||||
no_log = dict(default="you shouldn't see me", no_log=True),
|
||||
foo=dict(default=True, type='bool'),
|
||||
bar=dict(default=[], type='list'),
|
||||
bam=dict(default="bam"),
|
||||
baz=dict(default=u"baz"),
|
||||
password=dict(default=True),
|
||||
no_log=dict(default="you shouldn't see me", no_log=True),
|
||||
),
|
||||
)
|
||||
|
||||
|
@ -73,13 +71,14 @@ class TestModuleUtilsBasic(unittest.TestCase):
|
|||
self.assertIn(' password=NOT_LOGGING_PASSWORD', message)
|
||||
|
||||
kwargs = am.log.call_args[1]
|
||||
self.assertEqual(kwargs,
|
||||
dict(log_args={
|
||||
'foo': 'False',
|
||||
'bar': '[1, 2, 3]',
|
||||
'bam': 'bam',
|
||||
'baz': 'baz',
|
||||
'password': 'NOT_LOGGING_PASSWORD',
|
||||
'no_log': 'NOT_LOGGING_PARAMETER',
|
||||
})
|
||||
)
|
||||
self.assertEqual(
|
||||
kwargs,
|
||||
dict(log_args={
|
||||
'foo': 'False',
|
||||
'bar': '[1, 2, 3]',
|
||||
'bam': 'bam',
|
||||
'baz': 'baz',
|
||||
'password': 'NOT_LOGGING_PASSWORD',
|
||||
'no_log': 'NOT_LOGGING_PARAMETER',
|
||||
})
|
||||
)
|
||||
|
|
|
@ -34,7 +34,7 @@ class TestAnsibleModuleWarnDeprecate(unittest.TestCase):
|
|||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
argument_spec=dict(),
|
||||
)
|
||||
am._name = 'unittest'
|
||||
|
||||
|
@ -51,7 +51,7 @@ class TestAnsibleModuleWarnDeprecate(unittest.TestCase):
|
|||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
argument_spec=dict(),
|
||||
)
|
||||
am._name = 'unittest'
|
||||
|
||||
|
@ -76,7 +76,7 @@ class TestAnsibleModuleWarnDeprecate(unittest.TestCase):
|
|||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
argument_spec=dict(),
|
||||
)
|
||||
am._name = 'unittest'
|
||||
|
||||
|
|
|
@ -25,13 +25,13 @@ import json
|
|||
import sys
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from units.mock.procenv import swap_stdin_and_argv, swap_stdout
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from units.mock.procenv import swap_stdin_and_argv, swap_stdout
|
||||
|
||||
|
||||
empty_invocation = {u'module_args': {}}
|
||||
|
||||
|
||||
class TestAnsibleModuleExitJson(unittest.TestCase):
|
||||
def setUp(self):
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
||||
|
@ -89,31 +89,35 @@ class TestAnsibleModuleExitJson(unittest.TestCase):
|
|||
return_val = json.loads(self.fake_stream.getvalue())
|
||||
self.assertEquals(return_val, dict(changed=True, msg='success', invocation=empty_invocation))
|
||||
|
||||
|
||||
class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
|
||||
OMIT = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER'
|
||||
dataset = (
|
||||
(dict(username='person', password='$ecret k3y'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:password12345@foo.com/login/',
|
||||
not_secret='following the leader', msg='here'),
|
||||
dict(one=1, pwd=OMIT, url='https://username:password12345@foo.com/login/',
|
||||
not_secret='following the leader', changed=False, msg='here',
|
||||
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
||||
),
|
||||
(dict(username='person', password='password12345'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:password12345@foo.com/login/',
|
||||
not_secret='following the leader', msg='here'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:********@foo.com/login/',
|
||||
not_secret='following the leader', changed=False, msg='here',
|
||||
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
||||
),
|
||||
(dict(username='person', password='$ecret k3y'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:$ecret k3y@foo.com/login/',
|
||||
not_secret='following the leader', msg='here'),
|
||||
dict(one=1, pwd=OMIT, url='https://username:********@foo.com/login/',
|
||||
not_secret='following the leader', changed=False, msg='here',
|
||||
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
||||
),
|
||||
)
|
||||
(
|
||||
dict(username='person', password='$ecret k3y'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:password12345@foo.com/login/',
|
||||
not_secret='following the leader', msg='here'),
|
||||
dict(one=1, pwd=OMIT, url='https://username:password12345@foo.com/login/',
|
||||
not_secret='following the leader', changed=False, msg='here',
|
||||
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
||||
),
|
||||
(
|
||||
dict(username='person', password='password12345'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:password12345@foo.com/login/',
|
||||
not_secret='following the leader', msg='here'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:********@foo.com/login/',
|
||||
not_secret='following the leader', changed=False, msg='here',
|
||||
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
||||
),
|
||||
(
|
||||
dict(username='person', password='$ecret k3y'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:$ecret k3y@foo.com/login/',
|
||||
not_secret='following the leader', msg='here'),
|
||||
dict(one=1, pwd=OMIT, url='https://username:********@foo.com/login/',
|
||||
not_secret='following the leader', changed=False, msg='here',
|
||||
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
||||
),
|
||||
)
|
||||
|
||||
def test_exit_json_removes_values(self):
|
||||
self.maxDiff = None
|
||||
|
@ -125,12 +129,12 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
|
|||
with swap_stdout():
|
||||
basic._ANSIBLE_ARGS = None
|
||||
module = basic.AnsibleModule(
|
||||
argument_spec = dict(
|
||||
argument_spec=dict(
|
||||
username=dict(),
|
||||
password=dict(no_log=True),
|
||||
token=dict(no_log=True),
|
||||
),
|
||||
)
|
||||
),
|
||||
)
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
self.assertEquals(module.exit_json(**return_val), expected)
|
||||
self.assertEquals(json.loads(sys.stdout.getvalue()), expected)
|
||||
|
@ -147,12 +151,12 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
|
|||
with swap_stdout():
|
||||
basic._ANSIBLE_ARGS = None
|
||||
module = basic.AnsibleModule(
|
||||
argument_spec = dict(
|
||||
argument_spec=dict(
|
||||
username=dict(),
|
||||
password=dict(no_log=True),
|
||||
token=dict(no_log=True),
|
||||
),
|
||||
)
|
||||
),
|
||||
)
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
self.assertEquals(module.fail_json(**return_val), expected)
|
||||
self.assertEquals(json.loads(sys.stdout.getvalue()), expected)
|
||||
|
|
|
@ -25,9 +25,9 @@ import syslog
|
|||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
|
||||
from ansible.module_utils.basic import heuristic_log_sanitize
|
||||
|
||||
|
||||
class TestHeuristicLogSanitize(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.URL_SECRET = 'http://username:pas:word@foo.com/data'
|
||||
|
@ -39,13 +39,16 @@ class TestHeuristicLogSanitize(unittest.TestCase):
|
|||
def _gen_data(self, records, per_rec, top_level, secret_text):
|
||||
hostvars = {'hostvars': {}}
|
||||
for i in range(1, records, 1):
|
||||
host_facts = {'host%s' % i:
|
||||
{'pstack':
|
||||
{'running': '875.1',
|
||||
'symlinked': '880.0',
|
||||
'tars': [],
|
||||
'versions': ['885.0']},
|
||||
}}
|
||||
host_facts = {
|
||||
'host%s' % i: {
|
||||
'pstack': {
|
||||
'running': '875.1',
|
||||
'symlinked': '880.0',
|
||||
'tars': [],
|
||||
'versions': ['885.0']
|
||||
},
|
||||
}
|
||||
}
|
||||
if per_rec:
|
||||
host_facts['host%s' % i]['secret'] = secret_text
|
||||
hostvars['hostvars'].update(host_facts)
|
||||
|
|
|
@ -51,7 +51,7 @@ class TestAnsibleModuleSysLogSmokeTest(unittest.TestCase):
|
|||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
self.am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
argument_spec=dict(),
|
||||
)
|
||||
self.am._name = 'unittest'
|
||||
|
||||
|
@ -89,7 +89,7 @@ class TestAnsibleModuleJournaldSmokeTest(unittest.TestCase):
|
|||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
self.am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
argument_spec=dict(),
|
||||
)
|
||||
self.am._name = 'unittest'
|
||||
|
||||
|
@ -120,7 +120,7 @@ class TestAnsibleModuleLogSyslog(unittest.TestCase):
|
|||
b'Byte string': b'Byte string',
|
||||
u'Toshio くらとみ non-ascii test'.encode('utf-8'): u'Toshio くらとみ non-ascii test'.encode('utf-8'),
|
||||
b'non-utf8 :\xff: test': b'non-utf8 :\xff: test'.decode('utf-8', 'replace').encode('utf-8'),
|
||||
}
|
||||
}
|
||||
|
||||
py3_output_data = {
|
||||
u'Text string': u'Text string',
|
||||
|
@ -128,7 +128,7 @@ class TestAnsibleModuleLogSyslog(unittest.TestCase):
|
|||
b'Byte string': u'Byte string',
|
||||
u'Toshio くらとみ non-ascii test'.encode('utf-8'): u'Toshio くらとみ non-ascii test',
|
||||
b'non-utf8 :\xff: test': b'non-utf8 :\xff: test'.decode('utf-8', 'replace')
|
||||
}
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
||||
|
@ -138,7 +138,7 @@ class TestAnsibleModuleLogSyslog(unittest.TestCase):
|
|||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
self.am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
argument_spec=dict(),
|
||||
)
|
||||
self.am._name = 'unittest'
|
||||
|
||||
|
@ -189,7 +189,7 @@ class TestAnsibleModuleLogJournal(unittest.TestCase):
|
|||
b'Byte string': u'Byte string',
|
||||
u'Toshio くらとみ non-ascii test'.encode('utf-8'): u'Toshio くらとみ non-ascii test',
|
||||
b'non-utf8 :\xff: test': b'non-utf8 :\xff: test'.decode('utf-8', 'replace')
|
||||
}
|
||||
}
|
||||
|
||||
# overriding run lets us use context managers for setup/teardown-esque behavior
|
||||
def setUp(self):
|
||||
|
@ -200,7 +200,7 @@ class TestAnsibleModuleLogJournal(unittest.TestCase):
|
|||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
self.am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
argument_spec=dict(),
|
||||
)
|
||||
self.am._name = 'unittest'
|
||||
|
||||
|
@ -270,4 +270,3 @@ class TestAnsibleModuleLogJournal(unittest.TestCase):
|
|||
# We added this journal field
|
||||
self.assertIn('TEST', mock_func.call_args[1])
|
||||
self.assertIn('log unittest', mock_func.call_args[1]['TEST'])
|
||||
|
||||
|
|
|
@ -42,10 +42,21 @@ class TestReturnValues(unittest.TestCase):
|
|||
(['1', '2', '3'], frozenset(['1', '2', '3'])),
|
||||
(('1', '2', '3'), frozenset(['1', '2', '3'])),
|
||||
({'one': 1, 'two': 'dos'}, frozenset(['1', 'dos'])),
|
||||
({'one': 1, 'two': 'dos',
|
||||
'three': ['amigos', 'musketeers', None,
|
||||
{'ping': 'pong', 'base': ('balls', 'raquets')}]},
|
||||
frozenset(['1', 'dos', 'amigos', 'musketeers', 'pong', 'balls', 'raquets'])),
|
||||
(
|
||||
{
|
||||
'one': 1,
|
||||
'two': 'dos',
|
||||
'three': [
|
||||
'amigos', 'musketeers', None, {
|
||||
'ping': 'pong',
|
||||
'base': (
|
||||
'balls', 'raquets'
|
||||
)
|
||||
}
|
||||
]
|
||||
},
|
||||
frozenset(['1', 'dos', 'amigos', 'musketeers', 'pong', 'balls', 'raquets'])
|
||||
),
|
||||
(u'Toshio くらとみ', frozenset(['Toshio くらとみ'])),
|
||||
('Toshio くらとみ', frozenset(['Toshio くらとみ'])),
|
||||
)
|
||||
|
@ -67,13 +78,22 @@ class TestRemoveValues(unittest.TestCase):
|
|||
(1.0, frozenset(['4321'])),
|
||||
(['string', 'strang', 'strung'], frozenset(['nope'])),
|
||||
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['nope'])),
|
||||
({'one': 1, 'two': 'dos',
|
||||
'three': ['amigos', 'musketeers', None,
|
||||
{'ping': 'pong', 'base': ['balls', 'raquets']}]},
|
||||
frozenset(['nope'])),
|
||||
(
|
||||
{
|
||||
'one': 1,
|
||||
'two': 'dos',
|
||||
'three': [
|
||||
'amigos', 'musketeers', None, {
|
||||
'ping': 'pong', 'base': ['balls', 'raquets']
|
||||
}
|
||||
]
|
||||
},
|
||||
frozenset(['nope'])
|
||||
),
|
||||
('Toshio くら', frozenset(['とみ'])),
|
||||
(u'Toshio くら', frozenset(['とみ'])),
|
||||
)
|
||||
)
|
||||
|
||||
dataset_remove = (
|
||||
('string', frozenset(['string']), OMIT),
|
||||
(1234, frozenset(['1234']), OMIT),
|
||||
|
@ -84,23 +104,43 @@ class TestRemoveValues(unittest.TestCase):
|
|||
(('string', 'strang', 'strung'), frozenset(['string', 'strung']), [OMIT, 'strang', OMIT]),
|
||||
((1234567890, 345678, 987654321), frozenset(['1234567890']), [OMIT, 345678, 987654321]),
|
||||
((1234567890, 345678, 987654321), frozenset(['345678']), [OMIT, OMIT, 987654321]),
|
||||
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key']),
|
||||
{'one': 1, 'two': 'dos', 'secret': OMIT}),
|
||||
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key', 'dos', '1']),
|
||||
{'one': OMIT, 'two': OMIT, 'secret': OMIT}),
|
||||
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key', 'dos', '1']),
|
||||
{'one': OMIT, 'two': OMIT, 'secret': OMIT}),
|
||||
({'one': 1, 'two': 'dos', 'three': ['amigos', 'musketeers', None,
|
||||
{'ping': 'pong', 'base': ['balls', 'raquets']}]},
|
||||
frozenset(['balls', 'base', 'pong', 'amigos']),
|
||||
{'one': 1, 'two': 'dos', 'three': [OMIT, 'musketeers',
|
||||
None, {'ping': OMIT, 'base': [OMIT, 'raquets']}]}),
|
||||
('This sentence has an enigma wrapped in a mystery inside of a secret. - mr mystery',
|
||||
frozenset(['enigma', 'mystery', 'secret']),
|
||||
'This sentence has an ******** wrapped in a ******** inside of a ********. - mr ********'),
|
||||
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key']), {'one': 1, 'two': 'dos', 'secret': OMIT}),
|
||||
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key', 'dos', '1']), {'one': OMIT, 'two': OMIT, 'secret': OMIT}),
|
||||
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key', 'dos', '1']), {'one': OMIT, 'two': OMIT, 'secret': OMIT}),
|
||||
(
|
||||
{
|
||||
'one': 1,
|
||||
'two': 'dos',
|
||||
'three': [
|
||||
'amigos', 'musketeers', None, {
|
||||
'ping': 'pong', 'base': [
|
||||
'balls', 'raquets'
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
frozenset(['balls', 'base', 'pong', 'amigos']),
|
||||
{
|
||||
'one': 1,
|
||||
'two': 'dos',
|
||||
'three': [
|
||||
OMIT, 'musketeers', None, {
|
||||
'ping': OMIT,
|
||||
'base': [
|
||||
OMIT, 'raquets'
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
),
|
||||
(
|
||||
'This sentence has an enigma wrapped in a mystery inside of a secret. - mr mystery',
|
||||
frozenset(['enigma', 'mystery', 'secret']),
|
||||
'This sentence has an ******** wrapped in a ******** inside of a ********. - mr ********'
|
||||
),
|
||||
('Toshio くらとみ', frozenset(['くらとみ']), 'Toshio ********'),
|
||||
(u'Toshio くらとみ', frozenset(['くらとみ']), u'Toshio ********'),
|
||||
)
|
||||
)
|
||||
|
||||
def test_no_removal(self):
|
||||
for value, no_log_strings in self.dataset_no_remove:
|
||||
|
@ -112,5 +152,3 @@ class TestRemoveValues(unittest.TestCase):
|
|||
|
||||
def test_unknown_type(self):
|
||||
self.assertRaises(TypeError, remove_values, object(), frozenset())
|
||||
|
||||
|
||||
|
|
|
@ -27,13 +27,13 @@ from io import BytesIO, StringIO
|
|||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils.six import PY3
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import call, MagicMock, Mock, patch, sentinel
|
||||
from ansible.module_utils.six import PY3
|
||||
import ansible.module_utils.basic
|
||||
|
||||
from units.mock.procenv import swap_stdin_and_argv
|
||||
|
||||
import ansible.module_utils.basic
|
||||
|
||||
class OpenBytesIO(BytesIO):
|
||||
"""BytesIO with dummy close() method
|
||||
|
@ -68,7 +68,7 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
|
|||
if path.startswith('/'):
|
||||
return path
|
||||
else:
|
||||
return self.os.getcwd.return_value + '/' + path
|
||||
return self.os.getcwd.return_value + '/' + path
|
||||
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
||||
# unittest doesn't have a clean place to use a context manager, so we have to enter/exit manually
|
||||
|
@ -207,4 +207,3 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
|
|||
else:
|
||||
self.assertEqual(stdout.decode('utf-8'), u'Žarn§')
|
||||
self.assertEqual(stderr.decode('utf-8'), u'لرئيسية')
|
||||
|
||||
|
|
|
@ -36,12 +36,12 @@ VALID_STRINGS = (
|
|||
[("True", True)],
|
||||
[("False", False)],
|
||||
[("{}", {})],
|
||||
)
|
||||
)
|
||||
|
||||
# Passing things that aren't strings should just return the object
|
||||
NONSTRINGS = (
|
||||
[({'a':1}, {'a':1})],
|
||||
)
|
||||
[({'a': 1}, {'a': 1})],
|
||||
)
|
||||
|
||||
# These strings are not basic types. For security, these should not be
|
||||
# executed. We return the same string and get an exception for some
|
||||
|
@ -50,25 +50,29 @@ INVALID_STRINGS = (
|
|||
[("a.foo()", "a.foo()", None)],
|
||||
[("import foo", "import foo", None)],
|
||||
[("__import__('foo')", "__import__('foo')", ValueError)],
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _check_simple_types(self, code, expected):
|
||||
# test some basic usage for various types
|
||||
self.assertEqual(self.am.safe_eval(code), expected)
|
||||
|
||||
|
||||
def _check_simple_types_with_exceptions(self, code, expected):
|
||||
# Test simple types with exceptions requested
|
||||
self.assertEqual(self.am.safe_eval(code, include_exceptions=True), (expected, None))
|
||||
|
||||
|
||||
def _check_invalid_strings(self, code, expected):
|
||||
self.assertEqual(self.am.safe_eval(code), expected)
|
||||
|
||||
|
||||
def _check_invalid_strings_with_exceptions(self, code, expected, exception):
|
||||
res = self.am.safe_eval("a=1", include_exceptions=True)
|
||||
self.assertEqual(res[0], "a=1")
|
||||
self.assertEqual(type(res[1]), SyntaxError)
|
||||
|
||||
|
||||
@add_method(_check_simple_types, *VALID_STRINGS)
|
||||
@add_method(_check_simple_types, *NONSTRINGS)
|
||||
@add_method(_check_simple_types_with_exceptions, *VALID_STRINGS)
|
||||
|
|
|
@ -35,6 +35,7 @@ from ansible.module_utils import known_hosts
|
|||
from units.mock.procenv import ModuleTestCase
|
||||
from units.mock.generator import add_method
|
||||
|
||||
|
||||
class TestSetModeIfDifferentBase(ModuleTestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
@ -49,7 +50,7 @@ class TestSetModeIfDifferentBase(ModuleTestCase):
|
|||
basic._ANSIBLE_ARGS = None
|
||||
|
||||
self.am = basic.AnsibleModule(
|
||||
argument_spec = dict(),
|
||||
argument_spec=dict(),
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
|
@ -60,8 +61,10 @@ class TestSetModeIfDifferentBase(ModuleTestCase):
|
|||
|
||||
def _check_no_mode_given_returns_previous_changes(self, previous_changes=True):
|
||||
with patch('os.lstat', side_effect=[self.mock_stat1]):
|
||||
|
||||
self.assertEqual(self.am.set_mode_if_different('/path/to/file', None, previous_changes), previous_changes)
|
||||
|
||||
|
||||
def _check_mode_changed_to_0660(self, mode):
|
||||
# Note: This is for checking that all the different ways of specifying
|
||||
# 0660 mode work. It cannot be used to check that setting a mode that is
|
||||
|
@ -71,6 +74,7 @@ def _check_mode_changed_to_0660(self, mode):
|
|||
self.assertEqual(self.am.set_mode_if_different('/path/to/file', mode, False), True)
|
||||
m_lchmod.assert_called_with(b'/path/to/file', 0o660)
|
||||
|
||||
|
||||
def _check_mode_unchanged_when_already_0660(self, mode):
|
||||
# Note: This is for checking that all the different ways of specifying
|
||||
# 0660 mode work. It cannot be used to check that setting a mode that is
|
||||
|
@ -83,18 +87,12 @@ SYNONYMS_0660 = (
|
|||
[[0o660]],
|
||||
[['0o660']],
|
||||
[['660']],
|
||||
)
|
||||
)
|
||||
|
||||
@add_method(_check_no_mode_given_returns_previous_changes,
|
||||
[dict(previous_changes=True)],
|
||||
[dict(previous_changes=False)],
|
||||
)
|
||||
@add_method(_check_mode_changed_to_0660,
|
||||
*SYNONYMS_0660
|
||||
)
|
||||
@add_method(_check_mode_unchanged_when_already_0660,
|
||||
*SYNONYMS_0660
|
||||
)
|
||||
|
||||
@add_method(_check_no_mode_given_returns_previous_changes, [dict(previous_changes=True)], [dict(previous_changes=False)], )
|
||||
@add_method(_check_mode_changed_to_0660, *SYNONYMS_0660)
|
||||
@add_method(_check_mode_unchanged_when_already_0660, *SYNONYMS_0660)
|
||||
class TestSetModeIfDifferent(TestSetModeIfDifferentBase):
|
||||
def test_module_utils_basic_ansible_module_set_mode_if_different(self):
|
||||
with patch('os.lstat') as m:
|
||||
|
@ -105,6 +103,7 @@ class TestSetModeIfDifferent(TestSetModeIfDifferentBase):
|
|||
self.am.set_mode_if_different('/path/to/file', 'o+w,g+w,a-r', False)
|
||||
|
||||
original_hasattr = hasattr
|
||||
|
||||
def _hasattr(obj, name):
|
||||
if obj == os and name == 'lchmod':
|
||||
return False
|
||||
|
@ -131,16 +130,10 @@ def _check_knows_to_change_to_0660_in_check_mode(self, mode):
|
|||
with patch('os.lstat', side_effect=[self.mock_stat1, self.mock_stat2, self.mock_stat2]) as m_lstat:
|
||||
self.assertEqual(self.am.set_mode_if_different('/path/to/file', mode, False), True)
|
||||
|
||||
@add_method(_check_no_mode_given_returns_previous_changes,
|
||||
[dict(previous_changes=True)],
|
||||
[dict(previous_changes=False)],
|
||||
)
|
||||
@add_method(_check_knows_to_change_to_0660_in_check_mode,
|
||||
*SYNONYMS_0660
|
||||
)
|
||||
@add_method(_check_mode_unchanged_when_already_0660,
|
||||
*SYNONYMS_0660
|
||||
)
|
||||
|
||||
@add_method(_check_no_mode_given_returns_previous_changes, [dict(previous_changes=True)], [dict(previous_changes=False)],)
|
||||
@add_method(_check_knows_to_change_to_0660_in_check_mode, *SYNONYMS_0660)
|
||||
@add_method(_check_mode_unchanged_when_already_0660, *SYNONYMS_0660)
|
||||
class TestSetModeIfDifferentWithCheckMode(TestSetModeIfDifferentBase):
|
||||
def setUp(self):
|
||||
super(TestSetModeIfDifferentWithCheckMode, self).setUp()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue