[spark] Add a create session test to the connector
# See the License for the specific language governing permissions and
# limitations under the License.
+import sys
+
from builtins import object
from nose.tools import assert_equal, assert_true, assert_false
from notebook.connectors.spark_shell import SparkApi
+if sys.version_info[0] > 2:
+ from unittest.mock import patch, Mock
+else:
+ from mock import patch, Mock
+
+
+class TestSparkApi(object):
+
+ def setUp(self):
+ self.user = 'hue_test'
+ self.api = SparkApi(self.user)
+
+
+ def test_create_session_plain(self):
+ lang = 'pyspark'
+ properties = None
+
+ with patch('notebook.connectors.spark_shell.get_spark_api') as get_spark_api:
+ get_spark_api.return_value = Mock(
+ create_session=Mock(
+ return_value={'id': '1'}
+ ),
+ get_session=Mock(
+ return_value={'state': 'idle', 'log': ''}
+ )
+ )
+
+ session = self.api.create_session(lang=lang, properties=properties)
+
+ assert_equal(session['type'], 'pyspark')
+ assert_equal(session['id'], '1')
+
+ files_properties = [prop for prop in session['properties'] if prop['name'] == 'files']
+ assert_true(files_properties, session['properties'])
+ assert_equal(files_properties[0]['value'], [], session['properties'])
+
+
+ def test_get_jobs(self):
+ local_jobs = [
+ {'url': u'http://172.21.1.246:4040/jobs/job/?id=0', 'name': u'0'}
+ ]
+ jobs = self.api._get_standalone_jobs(LIVY_STANDALONE_LOG)
+ assert_equal(jobs, local_jobs, jobs)
+
+ yarn_jobs = [
+ {'url': u'http://huetest-1.test.com:8088/proxy/application_1444070328046_0002/', 'name': u'application_1444070328046_0002'}
+ ]
+ jobs = self.api._get_yarn_jobs(LIVY_YARN_LOG)
+ assert_equal(jobs, yarn_jobs, jobs)
+
-class TestSparkShellConnector(object):
LIVY_STANDALONE_LOG = """
Starting livy-repl on http://172.21.1.246:58449
@@ -152,21 +203,3 @@ class TestSparkShellConnector(object):
user: huetest
15/10/05 13:52:24 INFO yarn.Client: Application report for application_1444070328046_0002 (state: RUNNING)
"""
-
- def setUp(self):
- self.user = 'hue_test'
- self.api = SparkApi(self.user)
-
-
- def test_get_jobs(self):
- local_jobs = [
- {'url': u'http://172.21.1.246:4040/jobs/job/?id=0', 'name': u'0'}
- ]
- jobs = self.api._get_standalone_jobs(self.LIVY_STANDALONE_LOG)
- assert_equal(jobs, local_jobs, jobs)
-
- yarn_jobs = [
- {'url': u'http://huetest-1.test.com:8088/proxy/application_1444070328046_0002/', 'name': u'application_1444070328046_0002'}
- ]
- jobs = self.api._get_yarn_jobs(self.LIVY_YARN_LOG)
- assert_equal(jobs, yarn_jobs, jobs)
