예제 #1
0
def test_sudo(mocker, parser, reset_cli_args):
    options = parser.parse_args([])
    context._init_global_context(options)
    play_context = PlayContext()

    default_cmd = "/bin/foo"
    default_exe = "/bin/bash"
    sudo_exe = 'sudo'
    sudo_flags = '-H -s -n'

    cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
    assert cmd == default_cmd

    success = 'BECOME-SUCCESS-.+?'

    play_context.become = True
    play_context.become_user = '******'
    play_context.set_become_plugin(become_loader.get('sudo'))
    play_context.become_flags = sudo_flags
    cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)

    assert (re.match(
        """%s %s  -u %s %s -c 'echo %s; %s'""" %
        (sudo_exe, sudo_flags, play_context.become_user, default_exe, success,
         default_cmd), cmd) is not None)

    play_context.become_pass = '******'
    cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
    assert (re.match(
        """%s %s -p "%s" -u %s %s -c 'echo %s; %s'""" %
        (sudo_exe, sudo_flags.replace(
            '-n', ''), r"\[sudo via assible, key=.+?\] password:",
         play_context.become_user, default_exe, success, default_cmd), cmd)
            is not None)
예제 #2
0
def test_play_context_make_become_bad(mocker, parser, reset_cli_args):
    options = parser.parse_args([])
    context._init_global_context(options)
    play_context = PlayContext()

    default_cmd = "/bin/foo"
    default_exe = "/bin/bash"

    play_context.become = True
    play_context.become_user = '******'
    play_context.set_become_plugin(become_loader.get('bad'))
    play_context.become_method = 'bad'

    with pytest.raises(AssibleError):
        play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
예제 #3
0
def test_su(mocker, parser, reset_cli_args):
    options = parser.parse_args([])
    context._init_global_context(options)
    play_context = PlayContext()

    default_cmd = "/bin/foo"
    default_exe = "/bin/bash"
    su_exe = 'su'
    su_flags = ''

    cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
    assert cmd == default_cmd

    success = 'BECOME-SUCCESS-.+?'

    play_context.become = True
    play_context.become_user = '******'
    play_context.become_pass = None
    play_context.become_method = 'su'
    play_context.set_become_plugin(become_loader.get('su'))
    play_context.become_flags = su_flags
    cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
    assert (re.match("""%s  %s -c '%s -c '"'"'echo %s; %s'"'"''""" % (su_exe, play_context.become_user, default_exe,
                                                                      success, default_cmd), cmd) is not None)
예제 #4
0
    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
        def build_module_command(env_string, shebang, cmd, arg_path=None):
            to_run = [env_string, cmd]
            if arg_path:
                to_run.append(arg_path)
            return " ".join(to_run)

        def get_option(option):
            return {'admin_users': ['root', 'toor']}.get(option)

        mock_connection = MagicMock()
        mock_connection.build_module_command.side_effect = build_module_command
        mock_connection.socket_path = None
        mock_connection._shell.get_remote_filename.return_value = 'copy.py'
        mock_connection._shell.join_path.side_effect = os.path.join
        mock_connection._shell.tmpdir = '/var/tmp/mytempdir'
        mock_connection._shell.get_option = get_option

        # 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()
        action_base._is_pipelining_enabled = MagicMock()
        action_base._make_tmp_path = MagicMock()
        action_base._transfer_data = MagicMock()
        action_base._compute_environment_string = MagicMock()
        action_base._low_level_execute_command = MagicMock()
        action_base._fixup_perms2 = MagicMock()

        action_base._configure_module.return_value = (
            'new', '#!/usr/bin/python', 'this is the module data', 'path')
        action_base._is_pipelining_enabled.return_value = False
        action_base._compute_environment_string.return_value = ''
        action_base._connection.has_pipelining = False
        action_base._make_tmp_path.return_value = '/the/tmp/path'
        action_base._low_level_execute_command.return_value = dict(
            stdout='{"rc": 0, "stdout": "ok"}')
        self.assertEqual(
            action_base._execute_module(module_name=None, module_args=None),
            dict(_assible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
        self.assertEqual(
            action_base._execute_module(module_name='foo',
                                        module_args=dict(z=9, y=8, x=7),
                                        task_vars=dict(a=1)),
            dict(
                _assible_parsed=True,
                rc=0,
                stdout="ok",
                stdout_lines=['ok'],
            ))

        # test with needing/removing a remote tmp path
        action_base._configure_module.return_value = (
            'old', '#!/usr/bin/python', 'this is the module data', 'path')
        action_base._is_pipelining_enabled.return_value = False
        action_base._make_tmp_path.return_value = '/the/tmp/path'
        self.assertEqual(
            action_base._execute_module(),
            dict(_assible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))

        action_base._configure_module.return_value = (
            'non_native_want_json', '#!/usr/bin/python',
            'this is the module data', 'path')
        self.assertEqual(
            action_base._execute_module(),
            dict(_assible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))

        play_context.become = True
        play_context.become_user = '******'
        self.assertEqual(
            action_base._execute_module(),
            dict(_assible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))

        # test an invalid shebang return
        action_base._configure_module.return_value = (
            'new', '', 'this is the module data', 'path')
        action_base._is_pipelining_enabled.return_value = False
        action_base._make_tmp_path.return_value = '/the/tmp/path'
        self.assertRaises(AssibleError, 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
        action_base._configure_module.return_value = (
            'new', '#!/usr/bin/python', 'this is the module data', 'path')
        self.assertEqual(
            action_base._execute_module(),
            dict(_assible_parsed=True, rc=0, stdout="ok", stdout_lines=['ok']))
        action_base._supports_check_mode = False
        self.assertRaises(AssibleError, action_base._execute_module)
예제 #5
0
    def test_action_base__make_tmp_path(self):
        # create our fake task
        mock_task = MagicMock()

        def get_shell_opt(opt):

            ret = None
            if opt == 'admin_users':
                ret = ['root', 'toor', 'Administrator']
            elif opt == 'remote_tmp':
                ret = '~/.assible/tmp'

            return ret

        # 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
        mock_connection._shell.get_option = get_shell_opt
        mock_connection._shell.HOMES_RE = re.compile(r'(\'|\")?(~|\$HOME)(.*)')

        # we're using a real play context here
        play_context = PlayContext()
        play_context.become = True
        play_context.become_user = '******'

        # 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')
        self.assertEqual(action_base._make_tmp_path('root'), '/some/path/')

        # empty path fails
        action_base._low_level_execute_command.return_value = dict(rc=0,
                                                                   stdout='')
        self.assertRaises(AssibleError, action_base._make_tmp_path, 'root')

        # authentication failure
        action_base._low_level_execute_command.return_value = dict(rc=5,
                                                                   stdout='')
        self.assertRaises(AssibleError, action_base._make_tmp_path, 'root')

        # ssh error
        action_base._low_level_execute_command.return_value = dict(rc=255,
                                                                   stdout='',
                                                                   stderr='')
        self.assertRaises(AssibleError, action_base._make_tmp_path, 'root')
        play_context.verbosity = 5
        self.assertRaises(AssibleError, action_base._make_tmp_path, 'root')

        # general error
        action_base._low_level_execute_command.return_value = dict(
            rc=1, stdout='some stuff here', stderr='')
        self.assertRaises(AssibleError, action_base._make_tmp_path, 'root')
        action_base._low_level_execute_command.return_value = dict(
            rc=1, stdout='some stuff here', stderr='No space left on device')
        self.assertRaises(AssibleError, action_base._make_tmp_path, 'root')