##// END OF EJS Templates
Preserve nbformat_minor from the future...
Preserve nbformat_minor from the future warn on click of unrecognized elements, rather than on load.

File last commit:

r18553:0fc1c749
r19107:2798f006
Show More
test_kernelspecs_api.py
121 lines | 4.0 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
Min RK
log kernel specs that fail to load
r18552 import shutil
Thomas Kluyver
Test kernel specs REST API...
r16689
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',
}
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())
Min RK
log kernel specs that fail to load
r18552 def test_list_kernelspecs_bad(self):
"""Can list kernelspecs when one is invalid"""
bad_kernel_dir = pjoin(self.ipython_dir.name, 'kernels', 'bad')
try:
os.makedirs(bad_kernel_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
with open(pjoin(bad_kernel_dir, 'kernel.json'), 'w') as f:
f.write("garbage")
specs = self.ks_api.list().json()
assert isinstance(specs, list)
# 2: the sample kernelspec created in setUp, and the native Python kernel
self.assertGreaterEqual(len(specs), 2)
shutil.rmtree(bad_kernel_dir)
Thomas Kluyver
Test kernel specs REST API...
r16689 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
Min RK
language is removed from kernel specs...
r18553 self.assertEqual(spec['display_name'], 'Test kernel')
Thomas Kluyver
Test kernel specs REST API...
r16689
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')