def test_run_powershell_with_nonactionable_error(remote_connection):
        response = libs_pb2.RunPowerShellResponse()
        na_error = libs_pb2.NonActionableLibraryError()
        response.error.non_actionable_error.CopyFrom(na_error)

        with mock.patch('dlpx.virtualization._engine.libs.run_powershell',
                        return_value=response, create=True):
            with pytest.raises(SystemExit):
                libs.run_powershell(remote_connection, 'command')
Example #2
0
    def test_run_powershell_bad_command(remote_connection):
        # Set the command be an int instead of a string.
        command = 10
        variables = None

        with pytest.raises(IncorrectArgumentTypeError) as err_info:
            libs.run_powershell(remote_connection, command, variables)

        assert err_info.value.message == (
            "The function run_powershell's argument 'command' was"
            " type 'int' but should be of type 'basestring'.")
Example #3
0
    def test_run_powershell_variables_not_dict(remote_connection):
        command = 'command'
        # Set the variables be a string instead of a dict.
        variables = 'not a dict'

        with pytest.raises(IncorrectArgumentTypeError) as err_info:
            libs.run_powershell(remote_connection, command, variables)

        assert err_info.value.message == (
            "The function run_powershell's argument 'variables' was"
            " type 'str' but should be of"
            " type 'dict of basestring:basestring' if defined.")
    def test_run_powershell_bad_remote_connection():
        # Set the connection be a string instead of a RemoteConnection.
        connection = 'BadRemoteConnection'
        command = 'command'
        variables = None

        with pytest.raises(IncorrectArgumentTypeError) as err_info:
            libs.run_powershell(connection, command, variables)

        assert err_info.value.message == (
            "The function run_powershell's argument 'remote_connection' was"
            " type 'str' but should be of"
            " class 'dlpx.virtualization.common._common_classes.RemoteConnection'.")
    def test_run_powershell_with_actionable_error(remote_connection):
        expected_id = 15
        expected_message = 'Some message'

        response = libs_pb2.RunPowerShellResponse()
        response.error.actionable_error.id = expected_id
        response.error.actionable_error.message = expected_message

        with mock.patch('dlpx.virtualization._engine.libs.run_powershell',
                        return_value=response, create=True):
            with pytest.raises(LibraryError) as err_info:
                libs.run_powershell(remote_connection, 'command')

        assert err_info.value._id == expected_id
        assert err_info.value.message == expected_message
Example #6
0
    def test_run_powershell_bad_variables(remote_connection):
        command = 'command'
        #
        # Set the value inside the varibles dict to be an int instead of a
        # string.
        #
        variables = {'test0': 'yes', 'test1': 10}

        with pytest.raises(IncorrectArgumentTypeError) as err_info:
            libs.run_powershell(remote_connection, command, variables)

        assert err_info.value.message == (
            "The function run_powershell's argument 'variables' was"
            " a dict of {type 'str':type 'int', type 'str':type 'str'}"
            " but should be of"
            " type 'dict of basestring:basestring' if defined.")
Example #7
0
    def test_run_powershell_check_true_exitcode_success(remote_connection):
        expected_run_powershell_response = libs_pb2.RunPowerShellResponse()
        expected_run_powershell_response.return_value.exit_code = 0
        expected_run_powershell_response.return_value.stdout = "stdout"
        expected_run_powershell_response.return_value.stderr = "stderr"

        expected_command = "command"
        expected_variables = None

        def mock_run_powershell(actual_run_powershell_request):
            assert actual_run_powershell_request.command == expected_command
            assert (actual_run_powershell_request.remote_connection.
                    environment.name == remote_connection.environment.name)
            assert (actual_run_powershell_request.remote_connection.environment
                    .reference == remote_connection.environment.reference)
            return expected_run_powershell_response

        with mock.patch("dlpx.virtualization._engine.libs.run_powershell",
                        side_effect=mock_run_powershell,
                        create=True):
            actual_run_powershell_result = libs.run_powershell(
                remote_connection,
                expected_command,
                expected_variables,
                check=True)

            assert actual_run_powershell_result.exit_code == expected_run_powershell_response.return_value.exit_code
            assert actual_run_powershell_result.stdout == expected_run_powershell_response.return_value.stdout
            assert actual_run_powershell_result.stderr == expected_run_powershell_response.return_value.stderr
Example #8
0
    def test_run_powershell(remote_connection):
        expected_run_powershell_response = libs_pb2.RunPowerShellResponse()
        expected_run_powershell_response.return_value.exit_code = 0
        expected_run_powershell_response.return_value.stdout = 'stdout'
        expected_run_powershell_response.return_value.stderr = 'stderr'

        expected_command = 'command'
        expected_variables = None

        def mock_run_powershell(actual_run_powershell_request):
            assert actual_run_powershell_request.command == expected_command

            actual_environment = (
                actual_run_powershell_request.remote_connection.environment)
            assert (
                actual_environment.name == remote_connection.environment.name)
            assert (actual_environment.reference ==
                    remote_connection.environment.reference)
            return expected_run_powershell_response

        with mock.patch('dlpx.virtualization._engine.libs.run_powershell',
                        side_effect=mock_run_powershell,
                        create=True):
            actual_run_powershell_result = libs.run_powershell(
                remote_connection, expected_command, expected_variables)

        expected = expected_run_powershell_response.return_value
        assert actual_run_powershell_result.exit_code == expected.exit_code
        assert actual_run_powershell_result.stdout == expected.stdout
        assert actual_run_powershell_result.stderr == expected.stderr
def execute_powershell(source_connection, script_name,env):
    logger = setupLogger._setup_logger(__name__)
    command = pkgutil.get_data('resources', script_name)
    env['DLPX_LIBRARY_SOURCE'] = pkgutil.get_data('resources','library.ps1')
    env['ORA_LIBRARY_SOURCE'] = pkgutil.get_data('resources','oralibrary.ps1')
    result = libs.run_powershell(source_connection,command,variables=env)

    logger.debug("Powershell Result: {}".format(result))

    if result.exit_code != 0:
        message = """The script {} failed with exit code {}. An exception {} was raised along with the message:
        {}""".format(script_name,result.exit_code,result.stdout,result.stderr)
        logger.exception(message)
        raise exceptions.PluginScriptError(message)

    return result.stdout.strip()
Example #10
0
    def test_run_powershell_check_true_exitcode_failed(remote_connection):
        expected_message = ('The script failed with exit code 1.'
                            ' stdout : stdout and  stderr : stderr')

        response = libs_pb2.RunPowerShellResponse()
        response.return_value.exit_code = 1
        response.return_value.stdout = "stdout"
        response.return_value.stderr = "stderr"

        with mock.patch("dlpx.virtualization._engine.libs.run_powershell",
                        return_value=response,
                        create=True):
            with pytest.raises(PluginScriptError) as info:
                response = libs.run_powershell(remote_connection,
                                               "test_command",
                                               check=True)
            assert info.value.message == expected_message