2016-02-27 01:41:13 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-09-17 14:24:27 +02:00
|
|
|
# (c) 2015, Florian Apolloner <florian@apolloner.eu>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2016-02-29 18:59:44 +01:00
|
|
|
import os
|
2018-01-16 06:15:04 +01:00
|
|
|
import re
|
2015-09-17 14:24:27 +02:00
|
|
|
|
2015-09-17 14:59:22 +02:00
|
|
|
from ansible import constants as C
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.mock import patch, MagicMock, mock_open
|
2016-02-27 01:41:13 +01:00
|
|
|
|
2016-02-26 18:47:59 +01:00
|
|
|
from ansible.errors import AnsibleError
|
2017-03-23 21:35:05 +01:00
|
|
|
from ansible.module_utils.six import text_type
|
|
|
|
from ansible.module_utils.six.moves import shlex_quote, builtins
|
|
|
|
from ansible.module_utils._text import to_bytes
|
2015-09-17 14:24:27 +02:00
|
|
|
from ansible.playbook.play_context import PlayContext
|
|
|
|
from ansible.plugins.action import ActionBase
|
2016-02-26 18:47:59 +01:00
|
|
|
from ansible.template import Templar
|
2017-10-29 05:33:02 +01:00
|
|
|
from ansible.vars.clean import clean_facts
|
2016-02-26 18:47:59 +01:00
|
|
|
|
|
|
|
from units.mock.loader import DictDataLoader
|
2017-03-23 21:35:05 +01:00
|
|
|
|
2016-09-07 07:54:17 +02:00
|
|
|
|
2017-09-19 09:45:35 +02:00
|
|
|
python_module_replacers = br"""
|
2016-02-26 18:47:59 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
#ANSIBLE_VERSION = "<<ANSIBLE_VERSION>>"
|
|
|
|
#MODULE_COMPLEX_ARGS = "<<INCLUDE_ANSIBLE_MODULE_COMPLEX_ARGS>>"
|
|
|
|
#SELINUX_SPECIAL_FS="<<SELINUX_SPECIAL_FILESYSTEMS>>"
|
|
|
|
|
2016-02-27 01:41:13 +01:00
|
|
|
test = u'Toshio \u304f\u3089\u3068\u307f'
|
2016-02-26 18:47:59 +01:00
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
"""
|
2015-09-17 14:24:27 +02:00
|
|
|
|
2016-02-27 01:41:13 +01:00
|
|
|
powershell_module_replacers = b"""
|
2016-04-05 20:06:17 +02:00
|
|
|
WINDOWS_ARGS = "<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>"
|
2016-02-26 18:47:59 +01:00
|
|
|
# POWERSHELL_COMMON
|
|
|
|
"""
|
|
|
|
|
2016-02-27 01:41:13 +01:00
|
|
|
|
2019-02-13 17:38:28 +01:00
|
|
|
def _action_base():
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
})
|
|
|
|
mock_module_loader = MagicMock()
|
|
|
|
mock_shared_loader_obj = MagicMock()
|
|
|
|
mock_shared_loader_obj.module_loader = mock_module_loader
|
|
|
|
mock_connection_loader = MagicMock()
|
|
|
|
|
|
|
|
mock_shared_loader_obj.connection_loader = mock_connection_loader
|
|
|
|
mock_connection = MagicMock()
|
|
|
|
|
|
|
|
play_context = MagicMock()
|
|
|
|
|
|
|
|
action_base = DerivedActionBase(task=None,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=fake_loader,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=mock_shared_loader_obj)
|
|
|
|
return action_base
|
|
|
|
|
|
|
|
|
2016-02-26 18:47:59 +01:00
|
|
|
class DerivedActionBase(ActionBase):
|
2016-02-29 18:59:44 +01:00
|
|
|
TRANSFERS_FILES = False
|
2016-09-07 07:54:17 +02:00
|
|
|
|
2016-02-26 18:47:59 +01:00
|
|
|
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)
|
2015-09-17 14:24:27 +02:00
|
|
|
|
2016-09-07 07:54:17 +02:00
|
|
|
|
2015-09-17 14:24:27 +02:00
|
|
|
class TestActionBase(unittest.TestCase):
|
|
|
|
|
2016-02-26 18:47:59 +01:00
|
|
|
def test_action_base_run(self):
|
|
|
|
mock_task = MagicMock()
|
|
|
|
mock_task.action = "foo"
|
|
|
|
mock_task.args = dict(a=1, b=2, c=3)
|
|
|
|
|
|
|
|
mock_connection = MagicMock()
|
|
|
|
|
|
|
|
play_context = PlayContext()
|
|
|
|
|
2017-11-22 21:35:58 +01:00
|
|
|
mock_task.async_val = None
|
2016-02-26 18:47:59 +01:00
|
|
|
action_base = DerivedActionBase(mock_task, mock_connection, play_context, None, None, None)
|
|
|
|
results = action_base.run()
|
|
|
|
self.assertEqual(results, dict())
|
|
|
|
|
2017-11-22 21:35:58 +01:00
|
|
|
mock_task.async_val = 0
|
2016-02-26 18:47:59 +01:00
|
|
|
action_base = DerivedActionBase(mock_task, mock_connection, play_context, None, None, None)
|
|
|
|
results = action_base.run()
|
2016-12-14 18:52:18 +01:00
|
|
|
self.assertEqual(results, {})
|
2016-02-26 18:47:59 +01:00
|
|
|
|
|
|
|
def test_action_base__configure_module(self):
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
})
|
|
|
|
|
|
|
|
# create our fake task
|
|
|
|
mock_task = MagicMock()
|
|
|
|
mock_task.action = "copy"
|
2017-11-22 21:35:58 +01:00
|
|
|
mock_task.async_val = 0
|
2016-02-26 18:47:59 +01:00
|
|
|
|
|
|
|
# create a mock connection, so we don't actually try and connect to things
|
|
|
|
mock_connection = MagicMock()
|
|
|
|
|
|
|
|
# create a mock shared loader object
|
|
|
|
def mock_find_plugin(name, options):
|
|
|
|
if name == 'badmodule':
|
|
|
|
return None
|
|
|
|
elif '.ps1' in options:
|
|
|
|
return '/fake/path/to/%s.ps1' % name
|
|
|
|
else:
|
|
|
|
return '/fake/path/to/%s' % name
|
|
|
|
|
|
|
|
mock_module_loader = MagicMock()
|
|
|
|
mock_module_loader.find_plugin.side_effect = mock_find_plugin
|
|
|
|
mock_shared_obj_loader = MagicMock()
|
|
|
|
mock_shared_obj_loader.module_loader = mock_module_loader
|
|
|
|
|
|
|
|
# we're using a real play context here
|
|
|
|
play_context = PlayContext()
|
|
|
|
|
|
|
|
# our test class
|
|
|
|
action_base = DerivedActionBase(
|
|
|
|
task=mock_task,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=fake_loader,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=mock_shared_obj_loader,
|
|
|
|
)
|
|
|
|
|
|
|
|
# test python module formatting
|
2017-02-07 19:35:51 +01:00
|
|
|
with patch.object(builtins, 'open', mock_open(read_data=to_bytes(python_module_replacers.strip(), encoding='utf-8'))):
|
|
|
|
with patch.object(os, 'rename'):
|
2016-04-06 08:48:37 +02:00
|
|
|
mock_task.args = dict(a=1, foo='fö〩')
|
|
|
|
mock_connection.module_implementation_preferences = ('',)
|
2016-05-11 22:14:01 +02:00
|
|
|
(style, shebang, data, path) = action_base._configure_module(mock_task.action, mock_task.args)
|
2016-04-06 08:48:37 +02:00
|
|
|
self.assertEqual(style, "new")
|
2016-06-05 01:19:57 +02:00
|
|
|
self.assertEqual(shebang, u"#!/usr/bin/python")
|
2016-04-06 08:48:37 +02:00
|
|
|
|
|
|
|
# test module not found
|
|
|
|
self.assertRaises(AnsibleError, action_base._configure_module, 'badmodule', mock_task.args)
|
2016-02-26 18:47:59 +01:00
|
|
|
|
|
|
|
# test powershell module formatting
|
2016-09-07 07:54:17 +02:00
|
|
|
with patch.object(builtins, 'open', mock_open(read_data=to_bytes(powershell_module_replacers.strip(), encoding='utf-8'))):
|
2016-02-26 18:47:59 +01:00
|
|
|
mock_task.action = 'win_copy'
|
|
|
|
mock_task.args = dict(b=2)
|
|
|
|
mock_connection.module_implementation_preferences = ('.ps1',)
|
2016-05-11 22:14:01 +02:00
|
|
|
(style, shebang, data, path) = action_base._configure_module('stat', mock_task.args)
|
2016-02-26 18:47:59 +01:00
|
|
|
self.assertEqual(style, "new")
|
2016-07-11 21:38:18 +02:00
|
|
|
self.assertEqual(shebang, u'#!powershell')
|
2016-02-26 18:47:59 +01:00
|
|
|
|
|
|
|
# test module not found
|
|
|
|
self.assertRaises(AnsibleError, action_base._configure_module, 'badmodule', mock_task.args)
|
|
|
|
|
|
|
|
def test_action_base__compute_environment_string(self):
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
})
|
|
|
|
|
|
|
|
# create our fake task
|
|
|
|
mock_task = MagicMock()
|
|
|
|
mock_task.action = "copy"
|
|
|
|
mock_task.args = dict(a=1)
|
|
|
|
|
|
|
|
# create a mock connection, so we don't actually try and connect to things
|
|
|
|
def env_prefix(**args):
|
2017-02-07 19:35:51 +01:00
|
|
|
return ' '.join(['%s=%s' % (k, shlex_quote(text_type(v))) for k, v in args.items()])
|
2016-02-26 18:47:59 +01:00
|
|
|
mock_connection = MagicMock()
|
|
|
|
mock_connection._shell.env_prefix.side_effect = env_prefix
|
|
|
|
|
|
|
|
# we're using a real play context here
|
|
|
|
play_context = PlayContext()
|
|
|
|
|
|
|
|
# and we're using a real templar here too
|
|
|
|
templar = Templar(loader=fake_loader)
|
|
|
|
|
|
|
|
# our test class
|
|
|
|
action_base = DerivedActionBase(
|
|
|
|
task=mock_task,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=fake_loader,
|
|
|
|
templar=templar,
|
|
|
|
shared_loader_obj=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
# test standard environment setup
|
|
|
|
mock_task.environment = [dict(FOO='foo'), None]
|
|
|
|
env_string = action_base._compute_environment_string()
|
|
|
|
self.assertEqual(env_string, "FOO=foo")
|
|
|
|
|
|
|
|
# test where environment is not a list
|
|
|
|
mock_task.environment = dict(FOO='foo')
|
|
|
|
env_string = action_base._compute_environment_string()
|
|
|
|
self.assertEqual(env_string, "FOO=foo")
|
|
|
|
|
|
|
|
# test environment with a variable in it
|
|
|
|
templar.set_available_variables(variables=dict(the_var='bar'))
|
|
|
|
mock_task.environment = [dict(FOO='{{the_var}}')]
|
|
|
|
env_string = action_base._compute_environment_string()
|
|
|
|
self.assertEqual(env_string, "FOO=bar")
|
|
|
|
|
|
|
|
# test with a bad environment set
|
|
|
|
mock_task.environment = dict(FOO='foo')
|
|
|
|
mock_task.environment = ['hi there']
|
|
|
|
self.assertRaises(AnsibleError, action_base._compute_environment_string)
|
2015-10-23 01:07:26 +02:00
|
|
|
|
2016-02-29 18:59:44 +01:00
|
|
|
def test_action_base__early_needs_tmp_path(self):
|
|
|
|
# create our fake task
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
# create a mock connection, so we don't actually try and connect to things
|
|
|
|
mock_connection = MagicMock()
|
|
|
|
|
|
|
|
# we're using a real play context here
|
|
|
|
play_context = PlayContext()
|
|
|
|
|
|
|
|
# our test class
|
|
|
|
action_base = DerivedActionBase(
|
|
|
|
task=mock_task,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=None,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(action_base._early_needs_tmp_path())
|
|
|
|
|
|
|
|
action_base.TRANSFERS_FILES = True
|
|
|
|
self.assertTrue(action_base._early_needs_tmp_path())
|
|
|
|
|
|
|
|
def test_action_base__make_tmp_path(self):
|
|
|
|
# create our fake task
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
2018-01-16 06:15:04 +01:00
|
|
|
def get_shell_opt(opt):
|
|
|
|
|
|
|
|
ret = None
|
|
|
|
if opt == 'admin_users':
|
|
|
|
ret = ['root', 'toor', 'Administrator']
|
2018-02-15 18:01:02 +01:00
|
|
|
elif opt == 'remote_tmp':
|
2018-01-16 06:15:04 +01:00
|
|
|
ret = '~/.ansible/tmp'
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2016-02-29 18:59:44 +01:00
|
|
|
# create a mock connection, so we don't actually try and connect to things
|
|
|
|
mock_connection = MagicMock()
|
|
|
|
mock_connection.transport = 'ssh'
|
|
|
|
mock_connection._shell.mkdtemp.return_value = 'mkdir command'
|
|
|
|
mock_connection._shell.join_path.side_effect = os.path.join
|
2018-01-16 06:15:04 +01:00
|
|
|
mock_connection._shell.get_option = get_shell_opt
|
|
|
|
mock_connection._shell.HOMES_RE = re.compile(r'(\'|\")?(~|\$HOME)(.*)')
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
# we're using a real play context here
|
|
|
|
play_context = PlayContext()
|
|
|
|
play_context.become = True
|
|
|
|
play_context.become_user = 'foo'
|
|
|
|
|
|
|
|
# our test class
|
|
|
|
action_base = DerivedActionBase(
|
|
|
|
task=mock_task,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=None,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
action_base._low_level_execute_command = MagicMock()
|
|
|
|
action_base._low_level_execute_command.return_value = dict(rc=0, stdout='/some/path')
|
2016-03-21 22:17:53 +01:00
|
|
|
self.assertEqual(action_base._make_tmp_path('root'), '/some/path/')
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
# empty path fails
|
|
|
|
action_base._low_level_execute_command.return_value = dict(rc=0, stdout='')
|
2016-03-21 22:17:53 +01:00
|
|
|
self.assertRaises(AnsibleError, action_base._make_tmp_path, 'root')
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
# authentication failure
|
|
|
|
action_base._low_level_execute_command.return_value = dict(rc=5, stdout='')
|
2016-03-21 22:17:53 +01:00
|
|
|
self.assertRaises(AnsibleError, action_base._make_tmp_path, 'root')
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
# ssh error
|
|
|
|
action_base._low_level_execute_command.return_value = dict(rc=255, stdout='', stderr='')
|
2016-03-21 22:17:53 +01:00
|
|
|
self.assertRaises(AnsibleError, action_base._make_tmp_path, 'root')
|
2016-02-29 18:59:44 +01:00
|
|
|
play_context.verbosity = 5
|
2016-03-21 22:17:53 +01:00
|
|
|
self.assertRaises(AnsibleError, action_base._make_tmp_path, 'root')
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
# general error
|
|
|
|
action_base._low_level_execute_command.return_value = dict(rc=1, stdout='some stuff here', stderr='')
|
2016-03-21 22:17:53 +01:00
|
|
|
self.assertRaises(AnsibleError, action_base._make_tmp_path, 'root')
|
2016-02-29 18:59:44 +01:00
|
|
|
action_base._low_level_execute_command.return_value = dict(rc=1, stdout='some stuff here', stderr='No space left on device')
|
2016-03-21 22:17:53 +01:00
|
|
|
self.assertRaises(AnsibleError, action_base._make_tmp_path, 'root')
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
def test_action_base__remove_tmp_path(self):
|
|
|
|
# create our fake task
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
# create a mock connection, so we don't actually try and connect to things
|
|
|
|
mock_connection = MagicMock()
|
|
|
|
mock_connection._shell.remove.return_value = 'rm some stuff'
|
|
|
|
|
|
|
|
# we're using a real play context here
|
|
|
|
play_context = PlayContext()
|
|
|
|
|
|
|
|
# our test class
|
|
|
|
action_base = DerivedActionBase(
|
|
|
|
task=mock_task,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=None,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
action_base._low_level_execute_command = MagicMock()
|
|
|
|
# these don't really return anything or raise errors, so
|
|
|
|
# we're pretty much calling these for coverage right now
|
|
|
|
action_base._remove_tmp_path('/bad/path/dont/remove')
|
|
|
|
action_base._remove_tmp_path('/good/path/to/ansible-tmp-thing')
|
|
|
|
|
|
|
|
@patch('os.unlink')
|
|
|
|
@patch('os.fdopen')
|
|
|
|
@patch('tempfile.mkstemp')
|
|
|
|
def test_action_base__transfer_data(self, mock_mkstemp, mock_fdopen, mock_unlink):
|
|
|
|
# create our fake task
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
# create a mock connection, so we don't actually try and connect to things
|
|
|
|
mock_connection = MagicMock()
|
|
|
|
mock_connection.put_file.return_value = None
|
|
|
|
|
|
|
|
# we're using a real play context here
|
|
|
|
play_context = PlayContext()
|
|
|
|
|
|
|
|
# our test class
|
|
|
|
action_base = DerivedActionBase(
|
|
|
|
task=mock_task,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=None,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_afd = MagicMock()
|
|
|
|
mock_afile = MagicMock()
|
|
|
|
mock_mkstemp.return_value = (mock_afd, mock_afile)
|
|
|
|
|
|
|
|
mock_unlink.return_value = None
|
|
|
|
|
|
|
|
mock_afo = MagicMock()
|
|
|
|
mock_afo.write.return_value = None
|
|
|
|
mock_afo.flush.return_value = None
|
|
|
|
mock_afo.close.return_value = None
|
|
|
|
mock_fdopen.return_value = mock_afo
|
|
|
|
|
|
|
|
self.assertEqual(action_base._transfer_data('/path/to/remote/file', 'some data'), '/path/to/remote/file')
|
|
|
|
self.assertEqual(action_base._transfer_data('/path/to/remote/file', 'some mixed data: fö〩'), '/path/to/remote/file')
|
|
|
|
self.assertEqual(action_base._transfer_data('/path/to/remote/file', dict(some_key='some value')), '/path/to/remote/file')
|
|
|
|
self.assertEqual(action_base._transfer_data('/path/to/remote/file', dict(some_key='fö〩')), '/path/to/remote/file')
|
|
|
|
|
|
|
|
mock_afo.write.side_effect = Exception()
|
|
|
|
self.assertRaises(AnsibleError, action_base._transfer_data, '/path/to/remote/file', '')
|
|
|
|
|
|
|
|
def test_action_base__execute_remote_stat(self):
|
|
|
|
# create our fake task
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
# create a mock connection, so we don't actually try and connect to things
|
|
|
|
mock_connection = MagicMock()
|
|
|
|
|
|
|
|
# we're using a real play context here
|
|
|
|
play_context = PlayContext()
|
|
|
|
|
|
|
|
# our test class
|
|
|
|
action_base = DerivedActionBase(
|
|
|
|
task=mock_task,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=None,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
action_base._execute_module = MagicMock()
|
|
|
|
|
|
|
|
# test normal case
|
|
|
|
action_base._execute_module.return_value = dict(stat=dict(checksum='1111111111111111111111111111111111', exists=True))
|
|
|
|
res = action_base._execute_remote_stat(path='/path/to/file', all_vars=dict(), follow=False)
|
|
|
|
self.assertEqual(res['checksum'], '1111111111111111111111111111111111')
|
|
|
|
|
|
|
|
# test does not exist
|
|
|
|
action_base._execute_module.return_value = dict(stat=dict(exists=False))
|
|
|
|
res = action_base._execute_remote_stat(path='/path/to/file', all_vars=dict(), follow=False)
|
|
|
|
self.assertFalse(res['exists'])
|
|
|
|
self.assertEqual(res['checksum'], '1')
|
|
|
|
|
|
|
|
# test no checksum in result from _execute_module
|
|
|
|
action_base._execute_module.return_value = dict(stat=dict(exists=True))
|
|
|
|
res = action_base._execute_remote_stat(path='/path/to/file', all_vars=dict(), follow=False)
|
|
|
|
self.assertTrue(res['exists'])
|
|
|
|
self.assertEqual(res['checksum'], '')
|
|
|
|
|
|
|
|
# test stat call failed
|
|
|
|
action_base._execute_module.return_value = dict(failed=True, msg="because I said so")
|
|
|
|
self.assertRaises(AnsibleError, action_base._execute_remote_stat, path='/path/to/file', all_vars=dict(), follow=False)
|
|
|
|
|
|
|
|
def test_action_base__execute_module(self):
|
|
|
|
# create our fake task
|
|
|
|
mock_task = MagicMock()
|
|
|
|
mock_task.action = 'copy'
|
|
|
|
mock_task.args = dict(a=1, b=2, c=3)
|
|
|
|
|
|
|
|
# create a mock connection, so we don't actually try and connect to things
|
2018-01-16 06:15:04 +01:00
|
|
|
def build_module_command(env_string, shebang, cmd, arg_path=None):
|
2016-02-29 18:59:44 +01:00
|
|
|
to_run = [env_string, cmd]
|
|
|
|
if arg_path:
|
|
|
|
to_run.append(arg_path)
|
|
|
|
return " ".join(to_run)
|
|
|
|
|
2018-05-15 01:31:21 +02:00
|
|
|
def get_option(option):
|
2018-07-06 19:49:19 +02:00
|
|
|
return {'admin_users': ['root', 'toor']}.get(option)
|
2018-05-15 01:31:21 +02:00
|
|
|
|
2016-02-29 18:59:44 +01:00
|
|
|
mock_connection = MagicMock()
|
|
|
|
mock_connection.build_module_command.side_effect = build_module_command
|
2017-11-09 21:04:40 +01:00
|
|
|
mock_connection.socket_path = None
|
Fix a test failure on Python 3.6 (#20030)
* Fix a test failure on Python 3.6
tox -e py36 failed with
======================================================================
ERROR: test_action_base__execute_module (units.plugins.action.test_action.TestActionBase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mg/src/ansible/test/units/plugins/action/test_action.py", line 507, in test_action_base__execute_module
self.assertEqual(action_base._execute_module(), dict(_ansible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
File "/home/mg/src/ansible/lib/ansible/plugins/action/__init__.py", line 596, in _execute_module
remote_module_path = self._connection._shell.join_path(tmp, remote_module_filename)
File "/home/mg/opt/python36/lib/python3.6/unittest/mock.py", line 939, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/home/mg/opt/python36/lib/python3.6/unittest/mock.py", line 1005, in _mock_call
ret_val = effect(*args, **kwargs)
File "/home/mg/src/ansible/.tox/py36/lib/python3.6/posixpath.py", line 92, in join
genericpath._check_arg_types('join', a, *p)
File "/home/mg/src/ansible/.tox/py36/lib/python3.6/genericpath.py", line 149, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'MagicMock'
because os.path.join() now checks argument types since Python 3.6 (due
to pathlib support, I expect).
* Use a more realistic module name in test
2017-01-09 21:42:30 +01:00
|
|
|
mock_connection._shell.get_remote_filename.return_value = 'copy.py'
|
2016-02-29 18:59:44 +01:00
|
|
|
mock_connection._shell.join_path.side_effect = os.path.join
|
2018-02-15 18:01:02 +01:00
|
|
|
mock_connection._shell.tmpdir = '/var/tmp/mytempdir'
|
2018-05-15 01:31:21 +02:00
|
|
|
mock_connection._shell.get_option = get_option
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
# we're using a real play context here
|
|
|
|
play_context = PlayContext()
|
|
|
|
|
|
|
|
# our test class
|
|
|
|
action_base = DerivedActionBase(
|
|
|
|
task=mock_task,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=play_context,
|
|
|
|
loader=None,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
# fake a lot of methods as we test those elsewhere
|
|
|
|
action_base._configure_module = MagicMock()
|
|
|
|
action_base._supports_check_mode = MagicMock()
|
2016-12-14 18:52:18 +01:00
|
|
|
action_base._is_pipelining_enabled = MagicMock()
|
2016-02-29 18:59:44 +01:00
|
|
|
action_base._make_tmp_path = MagicMock()
|
|
|
|
action_base._transfer_data = MagicMock()
|
|
|
|
action_base._compute_environment_string = MagicMock()
|
|
|
|
action_base._low_level_execute_command = MagicMock()
|
2016-09-07 01:49:59 +02:00
|
|
|
action_base._fixup_perms2 = MagicMock()
|
2016-02-29 18:59:44 +01:00
|
|
|
|
2016-05-11 22:14:01 +02:00
|
|
|
action_base._configure_module.return_value = ('new', '#!/usr/bin/python', 'this is the module data', 'path')
|
2016-12-14 18:52:18 +01:00
|
|
|
action_base._is_pipelining_enabled.return_value = False
|
2016-02-29 18:59:44 +01:00
|
|
|
action_base._compute_environment_string.return_value = ''
|
2016-12-14 18:52:18 +01:00
|
|
|
action_base._connection.has_pipelining = False
|
|
|
|
action_base._make_tmp_path.return_value = '/the/tmp/path'
|
2016-02-29 18:59:44 +01:00
|
|
|
action_base._low_level_execute_command.return_value = dict(stdout='{"rc": 0, "stdout": "ok"}')
|
2016-08-16 19:11:58 +02:00
|
|
|
self.assertEqual(action_base._execute_module(module_name=None, module_args=None), dict(_ansible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
|
2017-05-30 19:05:19 +02:00
|
|
|
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'],
|
|
|
|
)
|
|
|
|
)
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
# test with needing/removing a remote tmp path
|
2016-05-11 22:14:01 +02:00
|
|
|
action_base._configure_module.return_value = ('old', '#!/usr/bin/python', 'this is the module data', 'path')
|
2016-12-14 18:52:18 +01:00
|
|
|
action_base._is_pipelining_enabled.return_value = False
|
2016-02-29 18:59:44 +01:00
|
|
|
action_base._make_tmp_path.return_value = '/the/tmp/path'
|
2016-08-16 19:11:58 +02:00
|
|
|
self.assertEqual(action_base._execute_module(), dict(_ansible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
|
2016-02-29 18:59:44 +01:00
|
|
|
|
2016-05-11 22:14:01 +02:00
|
|
|
action_base._configure_module.return_value = ('non_native_want_json', '#!/usr/bin/python', 'this is the module data', 'path')
|
2016-08-16 19:11:58 +02:00
|
|
|
self.assertEqual(action_base._execute_module(), dict(_ansible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
play_context.become = True
|
|
|
|
play_context.become_user = 'foo'
|
2016-08-16 19:11:58 +02:00
|
|
|
self.assertEqual(action_base._execute_module(), dict(_ansible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
|
2016-02-29 18:59:44 +01:00
|
|
|
|
|
|
|
# test an invalid shebang return
|
2016-05-11 22:14:01 +02:00
|
|
|
action_base._configure_module.return_value = ('new', '', 'this is the module data', 'path')
|
2016-12-14 18:52:18 +01:00
|
|
|
action_base._is_pipelining_enabled.return_value = False
|
|
|
|
action_base._make_tmp_path.return_value = '/the/tmp/path'
|
2016-02-29 18:59:44 +01:00
|
|
|
self.assertRaises(AnsibleError, action_base._execute_module)
|
|
|
|
|
|
|
|
# test with check mode enabled, once with support for check
|
|
|
|
# mode and once with support disabled to raise an error
|
|
|
|
play_context.check_mode = True
|
2016-05-11 22:14:01 +02:00
|
|
|
action_base._configure_module.return_value = ('new', '#!/usr/bin/python', 'this is the module data', 'path')
|
2016-08-16 19:11:58 +02:00
|
|
|
self.assertEqual(action_base._execute_module(), dict(_ansible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
|
2016-02-29 18:59:44 +01:00
|
|
|
action_base._supports_check_mode = False
|
|
|
|
self.assertRaises(AnsibleError, action_base._execute_module)
|
|
|
|
|
|
|
|
def test_action_base_sudo_only_if_user_differs(self):
|
2016-07-25 14:11:45 +02:00
|
|
|
fake_loader = MagicMock()
|
|
|
|
fake_loader.get_basedir.return_value = os.getcwd()
|
2015-09-17 14:24:27 +02:00
|
|
|
play_context = PlayContext()
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
|
2016-07-25 14:11:45 +02:00
|
|
|
action_base = DerivedActionBase(None, None, play_context, fake_loader, None, None)
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
action_base.get_become_option = MagicMock(return_value='root')
|
|
|
|
action_base._get_remote_user = MagicMock(return_value='root')
|
|
|
|
|
2016-02-26 18:47:59 +01:00
|
|
|
action_base._connection = MagicMock(exec_command=MagicMock(return_value=(0, '', '')))
|
2015-09-17 14:24:27 +02:00
|
|
|
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
action_base._connection._shell = shell = MagicMock(append_command=MagicMock(return_value=('JOINED CMD')))
|
|
|
|
|
|
|
|
action_base._connection.become = become = MagicMock()
|
|
|
|
become.build_become_command.return_value = 'foo'
|
2015-09-17 14:24:27 +02:00
|
|
|
|
2015-12-22 20:12:14 +01:00
|
|
|
action_base._low_level_execute_command('ECHO', sudoable=True)
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
become.build_become_command.assert_not_called()
|
2015-09-17 14:24:27 +02:00
|
|
|
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
action_base._get_remote_user.return_value = 'apo'
|
2016-03-12 02:38:38 +01:00
|
|
|
action_base._low_level_execute_command('ECHO', sudoable=True, executable='/bin/csh')
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
become.build_become_command.assert_called_once_with("ECHO", shell)
|
2015-09-17 14:59:22 +02:00
|
|
|
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
become.build_become_command.reset_mock()
|
2015-09-17 14:59:22 +02:00
|
|
|
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
with patch.object(C, 'BECOME_ALLOW_SAME_USER', new=True):
|
|
|
|
action_base._get_remote_user.return_value = 'root'
|
2015-12-22 20:12:14 +01:00
|
|
|
action_base._low_level_execute_command('ECHO SAME', sudoable=True)
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
become.build_become_command.assert_called_once_with("ECHO SAME", shell)
|
2017-02-07 17:42:03 +01:00
|
|
|
|
2019-02-13 17:38:28 +01:00
|
|
|
def test__remote_expand_user_relative_pathing(self):
|
|
|
|
action_base = _action_base()
|
|
|
|
action_base._play_context.remote_addr = 'bar'
|
|
|
|
action_base._low_level_execute_command = MagicMock(return_value={'stdout': b'../home/user'})
|
|
|
|
action_base._connection._shell.join_path.return_value = '../home/user/foo'
|
|
|
|
with self.assertRaises(AnsibleError) as cm:
|
|
|
|
action_base._remote_expand_user('~/foo')
|
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message,
|
|
|
|
"'bar' returned an invalid relative home directory path containing '..'"
|
|
|
|
)
|
|
|
|
|
2017-02-07 17:42:03 +01:00
|
|
|
|
|
|
|
class TestActionBaseCleanReturnedData(unittest.TestCase):
|
|
|
|
def test(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
})
|
|
|
|
mock_module_loader = MagicMock()
|
|
|
|
mock_shared_loader_obj = MagicMock()
|
|
|
|
mock_shared_loader_obj.module_loader = mock_module_loader
|
|
|
|
connection_loader_paths = ['/tmp/asdfadf', '/usr/lib64/whatever',
|
|
|
|
'dfadfasf',
|
|
|
|
'foo.py',
|
|
|
|
'.*',
|
|
|
|
# FIXME: a path with parans breaks the regex
|
|
|
|
# '(.*)',
|
|
|
|
'/path/to/ansible/lib/ansible/plugins/connection/custom_connection.py',
|
|
|
|
'/path/to/ansible/lib/ansible/plugins/connection/ssh.py']
|
|
|
|
|
|
|
|
def fake_all(path_only=None):
|
|
|
|
for path in connection_loader_paths:
|
|
|
|
yield path
|
|
|
|
|
|
|
|
mock_connection_loader = MagicMock()
|
|
|
|
mock_connection_loader.all = fake_all
|
|
|
|
|
|
|
|
mock_shared_loader_obj.connection_loader = mock_connection_loader
|
|
|
|
mock_connection = MagicMock()
|
2017-05-30 19:05:19 +02:00
|
|
|
# mock_connection._shell.env_prefix.side_effect = env_prefix
|
2017-02-07 17:42:03 +01:00
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
# action_base = DerivedActionBase(mock_task, mock_connection, play_context, None, None, None)
|
2017-02-07 17:42:03 +01:00
|
|
|
action_base = DerivedActionBase(task=None,
|
|
|
|
connection=mock_connection,
|
|
|
|
play_context=None,
|
|
|
|
loader=fake_loader,
|
|
|
|
templar=None,
|
|
|
|
shared_loader_obj=mock_shared_loader_obj)
|
|
|
|
data = {'ansible_playbook_python': '/usr/bin/python',
|
2017-05-30 19:05:19 +02:00
|
|
|
# 'ansible_rsync_path': '/usr/bin/rsync',
|
2017-02-07 17:42:03 +01:00
|
|
|
'ansible_python_interpreter': '/usr/bin/python',
|
|
|
|
'ansible_ssh_some_var': 'whatever',
|
|
|
|
'ansible_ssh_host_key_somehost': 'some key here',
|
|
|
|
'some_other_var': 'foo bar'}
|
2017-10-29 05:33:02 +01:00
|
|
|
data = clean_facts(data)
|
2017-02-07 17:42:03 +01:00
|
|
|
self.assertNotIn('ansible_playbook_python', data)
|
|
|
|
self.assertNotIn('ansible_python_interpreter', data)
|
|
|
|
self.assertIn('ansible_ssh_host_key_somehost', data)
|
|
|
|
self.assertIn('some_other_var', data)
|
|
|
|
|
|
|
|
|
|
|
|
class TestActionBaseParseReturnedData(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_fail_no_json(self):
|
2019-02-13 17:38:28 +01:00
|
|
|
action_base = _action_base()
|
2017-02-07 17:42:03 +01:00
|
|
|
rc = 0
|
|
|
|
stdout = 'foo\nbar\n'
|
|
|
|
err = 'oopsy'
|
|
|
|
returned_data = {'rc': rc,
|
|
|
|
'stdout': stdout,
|
|
|
|
'stdout_lines': stdout.splitlines(),
|
|
|
|
'stderr': err}
|
|
|
|
res = action_base._parse_returned_data(returned_data)
|
|
|
|
self.assertFalse(res['_ansible_parsed'])
|
|
|
|
self.assertTrue(res['failed'])
|
2017-02-07 19:35:51 +01:00
|
|
|
self.assertEqual(res['module_stderr'], err)
|
2017-02-07 17:42:03 +01:00
|
|
|
|
|
|
|
def test_json_empty(self):
|
2019-02-13 17:38:28 +01:00
|
|
|
action_base = _action_base()
|
2017-02-07 17:42:03 +01:00
|
|
|
rc = 0
|
|
|
|
stdout = '{}\n'
|
|
|
|
err = ''
|
|
|
|
returned_data = {'rc': rc,
|
|
|
|
'stdout': stdout,
|
|
|
|
'stdout_lines': stdout.splitlines(),
|
|
|
|
'stderr': err}
|
|
|
|
res = action_base._parse_returned_data(returned_data)
|
2017-05-30 19:05:19 +02:00
|
|
|
del res['_ansible_parsed'] # we always have _ansible_parsed
|
2017-02-07 19:35:51 +01:00
|
|
|
self.assertEqual(len(res), 0)
|
|
|
|
self.assertFalse(res)
|
2017-02-07 17:42:03 +01:00
|
|
|
|
|
|
|
def test_json_facts(self):
|
2019-02-13 17:38:28 +01:00
|
|
|
action_base = _action_base()
|
2017-02-07 17:42:03 +01:00
|
|
|
rc = 0
|
|
|
|
stdout = '{"ansible_facts": {"foo": "bar", "ansible_blip": "blip_value"}}\n'
|
|
|
|
err = ''
|
|
|
|
|
|
|
|
returned_data = {'rc': rc,
|
|
|
|
'stdout': stdout,
|
|
|
|
'stdout_lines': stdout.splitlines(),
|
|
|
|
'stderr': err}
|
|
|
|
res = action_base._parse_returned_data(returned_data)
|
2017-02-07 19:35:51 +01:00
|
|
|
self.assertTrue(res['ansible_facts'])
|
|
|
|
self.assertIn('ansible_blip', res['ansible_facts'])
|
2017-02-07 17:42:03 +01:00
|
|
|
# TODO: Should this be an AnsibleUnsafe?
|
2017-05-30 19:05:19 +02:00
|
|
|
# self.assertIsInstance(res['ansible_facts'], AnsibleUnsafe)
|
2017-02-07 17:42:03 +01:00
|
|
|
|
|
|
|
def test_json_facts_add_host(self):
|
2019-02-13 17:38:28 +01:00
|
|
|
action_base = _action_base()
|
2017-02-07 17:42:03 +01:00
|
|
|
rc = 0
|
|
|
|
stdout = '''{"ansible_facts": {"foo": "bar", "ansible_blip": "blip_value"},
|
|
|
|
"add_host": {"host_vars": {"some_key": ["whatever the add_host object is"]}
|
|
|
|
}
|
|
|
|
}\n'''
|
|
|
|
err = ''
|
|
|
|
|
|
|
|
returned_data = {'rc': rc,
|
|
|
|
'stdout': stdout,
|
|
|
|
'stdout_lines': stdout.splitlines(),
|
|
|
|
'stderr': err}
|
|
|
|
res = action_base._parse_returned_data(returned_data)
|
2017-02-07 19:35:51 +01:00
|
|
|
self.assertTrue(res['ansible_facts'])
|
|
|
|
self.assertIn('ansible_blip', res['ansible_facts'])
|
|
|
|
self.assertIn('add_host', res)
|
2017-02-07 17:42:03 +01:00
|
|
|
# TODO: Should this be an AnsibleUnsafe?
|
2017-05-30 19:05:19 +02:00
|
|
|
# self.assertIsInstance(res['ansible_facts'], AnsibleUnsafe)
|