def test_run_bash_check_true_success_exitcode(remote_connection):
        expected_run_bash_response = libs_pb2.RunBashResponse()
        expected_run_bash_response.return_value.exit_code = 0
        expected_run_bash_response.return_value.stdout = "stdout"
        expected_run_bash_response.return_value.stderr = "stderr"

        expected_command = "command"
        expected_variables = None
        expected_use_login_shell = False

        def mock_run_bash(actual_run_bash_request):
            assert actual_run_bash_request.command == expected_command
            assert actual_run_bash_request.use_login_shell == expected_use_login_shell
            assert (
                    actual_run_bash_request.remote_connection.environment.name
                    == remote_connection.environment.name
            )
            assert (
                    actual_run_bash_request.remote_connection.environment.reference
                    == remote_connection.environment.reference
            )
            return expected_run_bash_response

        with mock.patch("dlpx.virtualization._engine.libs.run_bash",
                        side_effect=mock_run_bash, create=True):
            actual_run_bash_result = libs.run_bash(remote_connection,
                                                   expected_command,
                                                   expected_variables,
                                                   expected_use_login_shell,
                                                   check=True)

            assert actual_run_bash_result.exit_code == expected_run_bash_response.return_value.exit_code
            assert actual_run_bash_result.stdout == expected_run_bash_response.return_value.stdout
            assert actual_run_bash_result.stderr == expected_run_bash_response.return_value.stderr
    def test_run_bash(remote_connection):
        expected_run_bash_response = libs_pb2.RunBashResponse()
        expected_run_bash_response.return_value.exit_code = 0
        expected_run_bash_response.return_value.stdout = 'stdout'
        expected_run_bash_response.return_value.stderr = 'stderr'

        expected_command = 'command'
        expected_variables = None
        expected_use_login_shell = False

        def mock_run_bash(actual_run_bash_request):
            assert actual_run_bash_request.command == expected_command
            assert (actual_run_bash_request.use_login_shell ==
                    expected_use_login_shell)

            actual_environment = (
                actual_run_bash_request.remote_connection.environment)
            assert (actual_environment.name ==
                    remote_connection.environment.name)
            assert (actual_environment.reference ==
                    remote_connection.environment.reference)
            return expected_run_bash_response

        with mock.patch('dlpx.virtualization._engine.libs.run_bash',
                        side_effect=mock_run_bash, create=True):
            actual_run_bash_result = libs.run_bash(
                remote_connection,
                expected_command,
                expected_variables,
                expected_use_login_shell)

        expected = expected_run_bash_response.return_value
        assert actual_run_bash_result.exit_code == expected.exit_code
        assert actual_run_bash_result.stdout == expected.stdout
        assert actual_run_bash_result.stderr == expected.stderr
    def test_run_bash_with_nonactionable_error(remote_connection):
        response = libs_pb2.RunBashResponse()
        na_error = libs_pb2.NonActionableLibraryError()
        response.error.non_actionable_error.CopyFrom(na_error)

        with mock.patch('dlpx.virtualization._engine.libs.run_bash',
                        return_value=response, create=True):
            with pytest.raises(SystemExit):
                libs.run_bash(remote_connection, 'command')
    def test_run_bash_with_actionable_error(remote_connection):
        expected_id = 15
        expected_message = 'Some message'

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

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

        assert err_info.value._id == expected_id
        assert err_info.value.message == expected_message
    def test_run_bash_with_check_true_failed_exitcode(remote_connection):
        expected_message = (
            'The script failed with exit code 1.'
            ' stdout : stdout and  stderr : stderr'
        )
        response = libs_pb2.RunBashResponse()
        response.return_value.exit_code = 1
        response.return_value.stdout = "stdout"
        response.return_value.stderr = "stderr"

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