示例#1
0
def test_add_sessions_command_parses():
    # Do not skip and python
    add_sessions_mock = MagicMock()
    spark_controller.add_session = add_sessions_mock
    command = "add"
    name = "-s name"
    language = "-l python"
    connection_string = "-u http://url.com -a sdf -p w"
    line = " ".join([command, name, language, connection_string])

    magic.spark(line)

    add_sessions_mock.assert_called_once_with(
        "name", Endpoint("http://url.com", "sdf", "w"), False,
        {"kind": "pyspark"})

    # Skip and scala - upper case
    add_sessions_mock = MagicMock()
    spark_controller.add_session = add_sessions_mock
    command = "add"
    name = "-s name"
    language = "-l scala"
    connection_string = "--url http://location:port"
    line = " ".join([command, name, language, connection_string, "-k"])

    magic.spark(line)

    add_sessions_mock.assert_called_once_with("name",
                                              Endpoint("http://location:port"),
                                              True, {"kind": "spark"})
示例#2
0
def test_cleanup_endpoint_command_parses():
    mock_method = MagicMock()
    spark_controller.cleanup_endpoint = mock_method
    line = "cleanup -u endp"

    magic.spark(line)

    mock_method.assert_called_once_with(Endpoint("endp"))

    line = "cleanup -u endp -a user -p passw"
    magic.spark(line)
    mock_method.assert_called_with(Endpoint("endp", "user", "passw"))
示例#3
0
 def refresh_configuration(self):
     credentials = getattr(conf,
                           'kernel_' + self.language + '_credentials')()
     (username, password,
      url) = (credentials['username'], credentials['password'],
              credentials['url'])
     self.endpoint = Endpoint(url, username, password)
示例#4
0
def test_change_language():
    language = constants.LANG_SCALA.upper()
    line = "-l {}".format(language)

    magic._do_not_call_change_language(line)

    assert_equals(constants.LANG_SCALA, magic.language)
    assert_equals(Endpoint("new_url"), magic.endpoint)
示例#5
0
def test_change_language_not_valid():
    language = "not_valid"
    line = "-l {}".format(language)

    magic._do_not_call_change_language(line)

    assert_equals(ipython_display.send_error.call_count, 1)
    assert_equals(constants.LANG_PYTHON, magic.language)
    assert_equals(Endpoint("url"), magic.endpoint)
示例#6
0
    def run(self):
        endpoint = Endpoint(self.address_widget.value, self.user_widget.value,
                            self.password_widget.value)
        self.endpoints[self.address_widget.value] = endpoint
        self.ipython_display.writeln("Added endpoint {}".format(
            self.address_widget.value))

        # We need to call the refresh method because drop down in Tab 2 for endpoints wouldn't refresh with the new
        # value otherwise.
        self.refresh_method()
示例#7
0
def test_delete_sessions_command_parses():
    mock_method = MagicMock()
    spark_controller.delete_session_by_name = mock_method
    command = "delete -s name"
    magic.spark(command)
    mock_method.assert_called_once_with("name")

    command = "delete -u URL -a username -p password -i 4"
    mock_method = MagicMock()
    spark_controller.delete_session_by_id = mock_method
    magic.spark(command)
    mock_method.assert_called_once_with(
        Endpoint("URL", "username", "password"), 4)
示例#8
0
def test_add_session():
    name = "name"
    properties = {"kind": "spark"}
    endpoint = Endpoint("http://location:port", "name", "word")
    session = MagicMock()

    controller._livy_session = MagicMock(return_value=session)
    controller._http_client = MagicMock(return_value=MagicMock())

    controller.add_session(name, endpoint, False, properties)

    controller._livy_session.assert_called_once_with(
        controller._http_client.return_value, properties, ipython_display)
    controller.session_manager.add_session.assert_called_once_with(
        name, session)
    session.start.assert_called_once_with()
示例#9
0
def test_add_sessions_command_exception():
    # Do not skip and python
    add_sessions_mock = MagicMock(side_effect=BadUserDataException('hehe'))
    spark_controller.add_session = add_sessions_mock
    command = "add"
    name = "-s name"
    language = "-l python"
    connection_string = "-u http://url.com -a sdf -p w"
    line = " ".join([command, name, language, connection_string])

    magic.spark(line)

    add_sessions_mock.assert_called_once_with(
        "name", Endpoint("http://url.com", "sdf", "w"), False,
        {"kind": "pyspark"})
    ipython_display.send_error.assert_called_once_with(
        EXPECTED_ERROR_MSG.format(add_sessions_mock.side_effect))
示例#10
0
def test_add_session_throws_when_session_start_fails():
    name = "name"
    properties = {"kind": "spark"}
    endpoint = Endpoint("http://location:port", "name", "word")
    session = MagicMock()

    controller._livy_session = MagicMock(return_value=session)
    controller._http_client = MagicMock(return_value=MagicMock())
    e = ValueError(
        "Failed to create the SqlContext.\nError, '{}'".format("Exception"))
    session.start = MagicMock(side_effect=e)

    try:
        controller.add_session(name, endpoint, False, properties)
        assert False
    except ValueError as ex:
        assert str(ex) == str(e)
        session.start.assert_called_once_with()
示例#11
0
def test_add_sessions_command_extra_properties():
    conf.override_all({})
    magic.spark("config", "{\"extra\": \"yes\"}")
    assert conf.session_configs() == {"extra": "yes"}

    add_sessions_mock = MagicMock()
    spark_controller.add_session = add_sessions_mock
    command = "add"
    name = "-s name"
    language = "-l scala"
    connection_string = "-u http://livyendpoint.com"
    line = " ".join([command, name, language, connection_string])

    magic.spark(line)

    add_sessions_mock.assert_called_once_with(
        "name", Endpoint("http://livyendpoint.com"), False, {
            "kind": "spark",
            "extra": "yes"
        })
    conf.load()
示例#12
0
    def spark(self, line, cell="", local_ns=None):
        """Magic to execute spark remotely.

           This magic allows you to create a Livy Scala or Python session against a Livy endpoint. Every session can
           be used to execute either Spark code or SparkSQL code by executing against the SQL context in the session.
           When the SQL context is used, the result will be a Pandas dataframe of a sample of the results.

           If invoked with no subcommand, the cell will be executed against the specified session.

           Subcommands
           -----------
           info
               Display the available Livy sessions and other configurations for sessions.
           add
               Add a Livy session given a session name (-s), language (-l), and endpoint credentials.
               The -k argument, if present, will skip adding this session if it already exists.
               e.g. `%spark add -s test -l python -u https://sparkcluster.net/livy -a u -p -k`
           config
               Override the livy session properties sent to Livy on session creation. All session creations will
               contain these config settings from then on.
               Expected value is a JSON key-value string to be sent as part of the Request Body for the POST /sessions
               endpoint in Livy.
               e.g. `%%spark config`
                    `{"driverMemory":"1000M", "executorCores":4}`
           run
               Run Spark code against a session.
               e.g. `%%spark -s testsession` will execute the cell code against the testsession previously created
               e.g. `%%spark -s testsession -c sql` will execute the SQL code against the testsession previously created
               e.g. `%%spark -s testsession -c sql -o my_var` will execute the SQL code against the testsession
                        previously created and store the pandas dataframe created in the my_var variable in the
                        Python environment.
           logs
               Returns the logs for a given session.
               e.g. `%spark logs -s testsession` will return the logs for the testsession previously created
           delete
               Delete a Livy session.
               e.g. `%spark delete -s defaultlivy`
           cleanup
               Delete all Livy sessions created by the notebook. No arguments required.
               e.g. `%spark cleanup`
        """
        usage = "Please look at usage of %spark by executing `%spark?`."
        user_input = line
        args = parse_argstring_or_throw(self.spark, user_input)

        subcommand = args.command[0].lower()

        # info
        if subcommand == "info":
            if args.url is not None:
                endpoint = Endpoint(args.url, args.user, args.password)
                info_sessions = self.spark_controller.get_all_sessions_endpoint_info(endpoint)
                self.print_endpoint_info(info_sessions)
            else:
                self._print_local_info()
        # config
        elif subcommand == "config":
            conf.override(conf.session_configs.__name__, json.loads(cell))
        # add
        elif subcommand == "add":
            if args.url is None:
                self.ipython_display.send_error("Need to supply URL argument (e.g. -u https://example.com/livyendpoint)")
                return

            name = args.session
            language = args.language
            endpoint = Endpoint(args.url, args.user, args.password)
            skip = args.skip

            properties = conf.get_session_properties(language)

            self.spark_controller.add_session(name, endpoint, skip, properties)
        # delete
        elif subcommand == "delete":
            if args.session is not None:
                self.spark_controller.delete_session_by_name(args.session)
            elif args.url is not None:
                if args.id is None:
                    self.ipython_display.send_error("Must provide --id or -i option to delete session at endpoint from URL")
                    return
                endpoint = Endpoint(args.url, args.user, args.password)
                session_id = args.id
                self.spark_controller.delete_session_by_id(endpoint, session_id)
            else:
                self.ipython_display.send_error("Subcommand 'delete' requires a session name or a URL and session ID")
        # cleanup
        elif subcommand == "cleanup":
            if args.url is not None:
                endpoint = Endpoint(args.url, args.user, args.password)
                self.spark_controller.cleanup_endpoint(endpoint)
            else:
                self.spark_controller.cleanup()
        # logs
        elif subcommand == "logs":
            if args.session is None:
                self.ipython_display.send_error("Need to provide session argument (-s SESSION_NAME)")
                return
            self.ipython_display.write(self.spark_controller.get_logs(args.session))
        # run
        elif len(subcommand) == 0:
            if args.session is None:
                self.ipython_display.send_error("Need to provide session argument (-s SESSION_NAME)")
                return
            if args.context == CONTEXT_NAME_SPARK:
                (success, out) = self.spark_controller.run_command(Command(cell), args.session)
                if success:
                    self.ipython_display.write(out)
                else:
                    self.ipython_display.send_error(out)
            elif args.context == CONTEXT_NAME_SQL:
                return self.execute_sqlquery(cell, args.samplemethod, args.maxrows, args.samplefraction,
                                             args.session, args.output, args.quiet)
            else:
                self.ipython_display.send_error("Context '{}' not found".format(args.context))
        # error
        else:
            self.ipython_display.send_error("Subcommand '{}' not found. {}".format(subcommand, usage))
# Copyright (c) 2015  [email protected]
# Distributed under the terms of the Modified BSD License.

from mock import patch, PropertyMock, MagicMock
from nose.tools import raises, assert_equals, with_setup
import requests

from remotespark.livyclientlib.endpoint import Endpoint
from remotespark.livyclientlib.exceptions import HttpClientException
from remotespark.livyclientlib.linearretrypolicy import LinearRetryPolicy
from remotespark.livyclientlib.reliablehttpclient import ReliableHttpClient

retry_policy = None
sequential_values = []

endpoint = Endpoint("http://url.com", "username", "password")


def _setup():
    global retry_policy
    retry_policy = LinearRetryPolicy(0.01, 5)


def _teardown():
    pass


def return_sequential():
    global sequential_values
    val = sequential_values[0]
    sequential_values = sequential_values[1:]
示例#14
0
 def refresh_configuration(self):
     self.endpoint = Endpoint("new_url")
示例#15
0
    def __init__(self, shell, data=None, spark_events=None):
        super(TestKernelMagics, self).__init__(shell,
                                               spark_events=spark_events)

        self.language = constants.LANG_PYTHON
        self.endpoint = Endpoint("url")