Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix action test #21102

Merged
merged 2 commits into from
Feb 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions test/units/plugins/action/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
except ImportError:
import __builtin__ as builtins

from nose.tools import eq_, raises

from ansible import constants as C
from ansible.compat.six import text_type
Expand All @@ -38,13 +37,11 @@
from ansible.errors import AnsibleError
from ansible.playbook.play_context import PlayContext
from ansible.plugins.action import ActionBase
from ansible.vars.unsafe_proxy import AnsibleUnsafe
from ansible.template import Templar

from units.mock.loader import DictDataLoader
from ansible.module_utils._text import to_bytes


python_module_replacers = b"""
#!/usr/bin/python

Expand Down Expand Up @@ -131,8 +128,8 @@ def mock_find_plugin(name, options):
)

# test python module formatting
with patch.object(builtins, 'open', mock_open(read_data=to_bytes(python_module_replacers.strip(), encoding='utf-8'))) as m:
with patch.object(os, 'rename') as m:
with patch.object(builtins, 'open', mock_open(read_data=to_bytes(python_module_replacers.strip(), encoding='utf-8'))):
with patch.object(os, 'rename'):
mock_task.args = dict(a=1, foo='fö〩')
mock_connection.module_implementation_preferences = ('',)
(style, shebang, data, path) = action_base._configure_module(mock_task.action, mock_task.args)
Expand Down Expand Up @@ -165,7 +162,7 @@ def test_action_base__compute_environment_string(self):

# create a mock connection, so we don't actually try and connect to things
def env_prefix(**args):
return ' '.join(['%s=%s' % (k, shlex_quote(text_type(v))) for k,v in args.items()])
return ' '.join(['%s=%s' % (k, shlex_quote(text_type(v))) for k, v in args.items()])
mock_connection = MagicMock()
mock_connection._shell.env_prefix.side_effect = env_prefix

Expand Down Expand Up @@ -548,7 +545,7 @@ def fake_all(path_only=None):
'ansible_ssh_some_var': 'whatever',
'ansible_ssh_host_key_somehost': 'some key here',
'some_other_var': 'foo bar'}
res = action_base._clean_returned_data(data)
action_base._clean_returned_data(data)
self.assertNotIn('ansible_playbook_python', data)
self.assertNotIn('ansible_python_interpreter', data)
self.assertIn('ansible_ssh_host_key_somehost', data)
Expand Down Expand Up @@ -588,7 +585,7 @@ def test_fail_no_json(self):
res = action_base._parse_returned_data(returned_data)
self.assertFalse(res['_ansible_parsed'])
self.assertTrue(res['failed'])
self.assertEquals(res['module_stderr'], err)
self.assertEqual(res['module_stderr'], err)

def test_json_empty(self):
action_base = self._action_base()
Expand All @@ -600,7 +597,8 @@ def test_json_empty(self):
'stdout_lines': stdout.splitlines(),
'stderr': err}
res = action_base._parse_returned_data(returned_data)
self.assertTrue(res['_ansible_parsed'])
self.assertEqual(len(res), 0)
self.assertFalse(res)

def test_json_facts(self):
action_base = self._action_base()
Expand All @@ -613,7 +611,8 @@ def test_json_facts(self):
'stdout_lines': stdout.splitlines(),
'stderr': err}
res = action_base._parse_returned_data(returned_data)
self.assertTrue(res['_ansible_parsed'])
self.assertTrue(res['ansible_facts'])
self.assertIn('ansible_blip', res['ansible_facts'])
# TODO: Should this be an AnsibleUnsafe?
#self.assertIsInstance(res['ansible_facts'], AnsibleUnsafe)

Expand All @@ -631,6 +630,8 @@ def test_json_facts_add_host(self):
'stdout_lines': stdout.splitlines(),
'stderr': err}
res = action_base._parse_returned_data(returned_data)
self.assertTrue(res['_ansible_parsed'])
self.assertTrue(res['ansible_facts'])
self.assertIn('ansible_blip', res['ansible_facts'])
self.assertIn('add_host', res)
# TODO: Should this be an AnsibleUnsafe?
#self.assertIsInstance(res['ansible_facts'], AnsibleUnsafe)