##// END OF EJS Templates
Fix kernel and session tests
Fix kernel and session tests

File last commit:

r17817:a7c19772
r18210:d1591ab0
Show More
test_kernelspecs_api.py
102 lines | 3.3 KiB | text/x-python | PythonLexer
Thomas Kluyver
Test kernel specs REST API...
r16689 # coding: utf-8
"""Test the kernel specs webservice API."""
import errno
import io
import json
import os
pjoin = os.path.join
import requests
MinRK
handle system-wide kernelspecs...
r17817 from IPython.kernel.kernelspec import NATIVE_KERNEL_NAME
Thomas Kluyver
Test kernel specs REST API...
r16689 from IPython.html.utils import url_path_join
from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
# Copied from IPython.kernel.tests.test_kernelspec so updating that doesn't
# break these tests
sample_kernel_json = {'argv':['cat', '{connection_file}'],
'display_name':'Test kernel',
'language':'bash',
}
some_resource = u"The very model of a modern major general"
class KernelSpecAPI(object):
"""Wrapper for notebook API calls."""
def __init__(self, base_url):
self.base_url = base_url
def _req(self, verb, path, body=None):
response = requests.request(verb,
Thomas Kluyver
Refactor kernelspec resource handler to separate URL prefix
r16706 url_path_join(self.base_url, path),
Thomas Kluyver
Test kernel specs REST API...
r16689 data=body,
)
response.raise_for_status()
return response
def list(self):
Thomas Kluyver
Refactor kernelspec resource handler to separate URL prefix
r16706 return self._req('GET', 'api/kernelspecs')
Thomas Kluyver
Test kernel specs REST API...
r16689
def kernel_spec_info(self, name):
Thomas Kluyver
Refactor kernelspec resource handler to separate URL prefix
r16706 return self._req('GET', url_path_join('api/kernelspecs', name))
Thomas Kluyver
Test kernel specs REST API...
r16689
def kernel_resource(self, name, path):
Thomas Kluyver
Refactor kernelspec resource handler to separate URL prefix
r16706 return self._req('GET', url_path_join('kernelspecs', name, path))
Thomas Kluyver
Test kernel specs REST API...
r16689
class APITest(NotebookTestBase):
"""Test the kernelspec web service API"""
def setUp(self):
ipydir = self.ipython_dir.name
sample_kernel_dir = pjoin(ipydir, 'kernels', 'sample')
try:
os.makedirs(sample_kernel_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
with open(pjoin(sample_kernel_dir, 'kernel.json'), 'w') as f:
json.dump(sample_kernel_json, f)
with io.open(pjoin(sample_kernel_dir, 'resource.txt'), 'w',
encoding='utf-8') as f:
f.write(some_resource)
self.ks_api = KernelSpecAPI(self.base_url())
def test_list_kernelspecs(self):
specs = self.ks_api.list().json()
assert isinstance(specs, list)
Thomas Kluyver
Put full kernel info in REST API response for all kernels
r16714
Thomas Kluyver
Test kernel specs REST API...
r16689 # 2: the sample kernelspec created in setUp, and the native Python kernel
MinRK
handle system-wide kernelspecs...
r17817 self.assertGreaterEqual(len(specs), 2)
Thomas Kluyver
Put full kernel info in REST API response for all kernels
r16714
def is_sample_kernelspec(s):
return s['name'] == 'sample' and s['display_name'] == 'Test kernel'
MinRK
handle system-wide kernelspecs...
r17817 def is_default_kernelspec(s):
return s['name'] == NATIVE_KERNEL_NAME and s['display_name'].startswith("IPython")
Thomas Kluyver
Put full kernel info in REST API response for all kernels
r16714 assert any(is_sample_kernelspec(s) for s in specs), specs
MinRK
handle system-wide kernelspecs...
r17817 assert any(is_default_kernelspec(s) for s in specs), specs
Thomas Kluyver
Test kernel specs REST API...
r16689
def test_get_kernelspec(self):
spec = self.ks_api.kernel_spec_info('Sample').json() # Case insensitive
self.assertEqual(spec['language'], 'bash')
def test_get_nonexistant_kernelspec(self):
with assert_http_error(404):
self.ks_api.kernel_spec_info('nonexistant')
def test_get_kernel_resource_file(self):
res = self.ks_api.kernel_resource('sAmple', 'resource.txt')
self.assertEqual(res.text, some_resource)
def test_get_nonexistant_resource(self):
with assert_http_error(404):
self.ks_api.kernel_resource('nonexistant', 'resource.txt')
with assert_http_error(404):
self.ks_api.kernel_resource('sample', 'nonexistant.txt')