def test_run_expect_check_true_exitcode_success(remote_connection):
        expected_run_expect_response = libs_pb2.RunPowerShellResponse()
        expected_run_expect_response.return_value.exit_code = 0
        expected_run_expect_response.return_value.stdout = "stdout"
        expected_run_expect_response.return_value.stderr = "stderr"

        expected_command = "command"
        expected_variables = None

        def mock_run_expect(actual_run_expect_request):
            assert actual_run_expect_request.command == expected_command
            assert (
                    actual_run_expect_request.remote_connection.environment.name
                    == remote_connection.environment.name
            )
            assert (
                    actual_run_expect_request.remote_connection.environment.reference
                    == remote_connection.environment.reference
            )
            return expected_run_expect_response

        with mock.patch("dlpx.virtualization._engine.libs.run_expect",
                        side_effect=mock_run_expect, create=True):
            actual_run_expect_result = libs.run_expect(
                remote_connection,
                expected_command, expected_variables, check=True)

            assert actual_run_expect_result.exit_code == expected_run_expect_response.return_value.exit_code
            assert actual_run_expect_result.stdout == expected_run_expect_response.return_value.stdout
            assert actual_run_expect_result.stderr == expected_run_expect_response.return_value.stderr
    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 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')
    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
    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