##// END OF EJS Templates
Merge pull request #7429 from takluyver/i7428...
Merge pull request #7429 from takluyver/i7428 Update help link about Markdown

File last commit:

r19764:353f680b
r19880:2ce594fa merge
Show More
test_kernelspecs_api.py
130 lines | 4.5 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")
Min RK
kernelspecs is a dict...
r19263 model = self.ks_api.list().json()
assert isinstance(model, dict)
self.assertEqual(model['default'], NATIVE_KERNEL_NAME)
specs = model['kernelspecs']
assert isinstance(specs, dict)
Min RK
log kernel specs that fail to load
r18552 # 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):
Min RK
kernelspecs is a dict...
r19263 model = self.ks_api.list().json()
assert isinstance(model, dict)
self.assertEqual(model['default'], NATIVE_KERNEL_NAME)
specs = model['kernelspecs']
assert isinstance(specs, dict)
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):
Min RK
update kernelspec model tests
r19682 return s['name'] == 'sample' and s['spec']['display_name'] == 'Test kernel'
Thomas Kluyver
Put full kernel info in REST API response for all kernels
r16714
MinRK
handle system-wide kernelspecs...
r17817 def is_default_kernelspec(s):
Min RK
update kernelspec test...
r19764 return s['name'] == NATIVE_KERNEL_NAME and s['spec']['display_name'].startswith("Python")
MinRK
handle system-wide kernelspecs...
r17817
Min RK
kernelspecs is a dict...
r19263 assert any(is_sample_kernelspec(s) for s in specs.values()), specs
assert any(is_default_kernelspec(s) for s in specs.values()), specs
Thomas Kluyver
Test kernel specs REST API...
r16689
def test_get_kernelspec(self):
Min RK
update kernelspec model tests
r19682 model = self.ks_api.kernel_spec_info('Sample').json() # Case insensitive
self.assertEqual(model['name'].lower(), 'sample')
self.assertIsInstance(model['spec'], dict)
self.assertEqual(model['spec']['display_name'], 'Test kernel')
self.assertIsInstance(model['resources'], dict)
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')