Show More
@@ -1,97 +1,102 | |||||
1 | # coding: utf-8 |
|
1 | # coding: utf-8 | |
2 | """Test the kernel specs webservice API.""" |
|
2 | """Test the kernel specs webservice API.""" | |
3 |
|
3 | |||
4 | import errno |
|
4 | import errno | |
5 | import io |
|
5 | import io | |
6 | import json |
|
6 | import json | |
7 | import os |
|
7 | import os | |
8 |
|
8 | |||
9 | pjoin = os.path.join |
|
9 | pjoin = os.path.join | |
10 |
|
10 | |||
11 | import requests |
|
11 | import requests | |
12 |
|
12 | |||
|
13 | from IPython.kernel.kernelspec import NATIVE_KERNEL_NAME | |||
13 | from IPython.html.utils import url_path_join |
|
14 | from IPython.html.utils import url_path_join | |
14 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error |
|
15 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error | |
15 |
|
16 | |||
16 | # Copied from IPython.kernel.tests.test_kernelspec so updating that doesn't |
|
17 | # Copied from IPython.kernel.tests.test_kernelspec so updating that doesn't | |
17 | # break these tests |
|
18 | # break these tests | |
18 | sample_kernel_json = {'argv':['cat', '{connection_file}'], |
|
19 | sample_kernel_json = {'argv':['cat', '{connection_file}'], | |
19 | 'display_name':'Test kernel', |
|
20 | 'display_name':'Test kernel', | |
20 | 'language':'bash', |
|
21 | 'language':'bash', | |
21 | } |
|
22 | } | |
22 |
|
23 | |||
23 | some_resource = u"The very model of a modern major general" |
|
24 | some_resource = u"The very model of a modern major general" | |
24 |
|
25 | |||
25 |
|
26 | |||
26 | class KernelSpecAPI(object): |
|
27 | class KernelSpecAPI(object): | |
27 | """Wrapper for notebook API calls.""" |
|
28 | """Wrapper for notebook API calls.""" | |
28 | def __init__(self, base_url): |
|
29 | def __init__(self, base_url): | |
29 | self.base_url = base_url |
|
30 | self.base_url = base_url | |
30 |
|
31 | |||
31 | def _req(self, verb, path, body=None): |
|
32 | def _req(self, verb, path, body=None): | |
32 | response = requests.request(verb, |
|
33 | response = requests.request(verb, | |
33 | url_path_join(self.base_url, path), |
|
34 | url_path_join(self.base_url, path), | |
34 | data=body, |
|
35 | data=body, | |
35 | ) |
|
36 | ) | |
36 | response.raise_for_status() |
|
37 | response.raise_for_status() | |
37 | return response |
|
38 | return response | |
38 |
|
39 | |||
39 | def list(self): |
|
40 | def list(self): | |
40 | return self._req('GET', 'api/kernelspecs') |
|
41 | return self._req('GET', 'api/kernelspecs') | |
41 |
|
42 | |||
42 | def kernel_spec_info(self, name): |
|
43 | def kernel_spec_info(self, name): | |
43 | return self._req('GET', url_path_join('api/kernelspecs', name)) |
|
44 | return self._req('GET', url_path_join('api/kernelspecs', name)) | |
44 |
|
45 | |||
45 | def kernel_resource(self, name, path): |
|
46 | def kernel_resource(self, name, path): | |
46 | return self._req('GET', url_path_join('kernelspecs', name, path)) |
|
47 | return self._req('GET', url_path_join('kernelspecs', name, path)) | |
47 |
|
48 | |||
48 | class APITest(NotebookTestBase): |
|
49 | class APITest(NotebookTestBase): | |
49 | """Test the kernelspec web service API""" |
|
50 | """Test the kernelspec web service API""" | |
50 | def setUp(self): |
|
51 | def setUp(self): | |
51 | ipydir = self.ipython_dir.name |
|
52 | ipydir = self.ipython_dir.name | |
52 | sample_kernel_dir = pjoin(ipydir, 'kernels', 'sample') |
|
53 | sample_kernel_dir = pjoin(ipydir, 'kernels', 'sample') | |
53 | try: |
|
54 | try: | |
54 | os.makedirs(sample_kernel_dir) |
|
55 | os.makedirs(sample_kernel_dir) | |
55 | except OSError as e: |
|
56 | except OSError as e: | |
56 | if e.errno != errno.EEXIST: |
|
57 | if e.errno != errno.EEXIST: | |
57 | raise |
|
58 | raise | |
58 |
|
59 | |||
59 | with open(pjoin(sample_kernel_dir, 'kernel.json'), 'w') as f: |
|
60 | with open(pjoin(sample_kernel_dir, 'kernel.json'), 'w') as f: | |
60 | json.dump(sample_kernel_json, f) |
|
61 | json.dump(sample_kernel_json, f) | |
61 |
|
62 | |||
62 | with io.open(pjoin(sample_kernel_dir, 'resource.txt'), 'w', |
|
63 | with io.open(pjoin(sample_kernel_dir, 'resource.txt'), 'w', | |
63 | encoding='utf-8') as f: |
|
64 | encoding='utf-8') as f: | |
64 | f.write(some_resource) |
|
65 | f.write(some_resource) | |
65 |
|
66 | |||
66 | self.ks_api = KernelSpecAPI(self.base_url()) |
|
67 | self.ks_api = KernelSpecAPI(self.base_url()) | |
67 |
|
68 | |||
68 | def test_list_kernelspecs(self): |
|
69 | def test_list_kernelspecs(self): | |
69 | specs = self.ks_api.list().json() |
|
70 | specs = self.ks_api.list().json() | |
70 | assert isinstance(specs, list) |
|
71 | assert isinstance(specs, list) | |
71 |
|
72 | |||
72 | # 2: the sample kernelspec created in setUp, and the native Python kernel |
|
73 | # 2: the sample kernelspec created in setUp, and the native Python kernel | |
73 | self.assertEqual(len(specs), 2) |
|
74 | self.assertGreaterEqual(len(specs), 2) | |
74 |
|
75 | |||
75 | def is_sample_kernelspec(s): |
|
76 | def is_sample_kernelspec(s): | |
76 | return s['name'] == 'sample' and s['display_name'] == 'Test kernel' |
|
77 | return s['name'] == 'sample' and s['display_name'] == 'Test kernel' | |
77 |
|
78 | |||
|
79 | def is_default_kernelspec(s): | |||
|
80 | return s['name'] == NATIVE_KERNEL_NAME and s['display_name'].startswith("IPython") | |||
|
81 | ||||
78 | assert any(is_sample_kernelspec(s) for s in specs), specs |
|
82 | assert any(is_sample_kernelspec(s) for s in specs), specs | |
|
83 | assert any(is_default_kernelspec(s) for s in specs), specs | |||
79 |
|
84 | |||
80 | def test_get_kernelspec(self): |
|
85 | def test_get_kernelspec(self): | |
81 | spec = self.ks_api.kernel_spec_info('Sample').json() # Case insensitive |
|
86 | spec = self.ks_api.kernel_spec_info('Sample').json() # Case insensitive | |
82 | self.assertEqual(spec['language'], 'bash') |
|
87 | self.assertEqual(spec['language'], 'bash') | |
83 |
|
88 | |||
84 | def test_get_nonexistant_kernelspec(self): |
|
89 | def test_get_nonexistant_kernelspec(self): | |
85 | with assert_http_error(404): |
|
90 | with assert_http_error(404): | |
86 | self.ks_api.kernel_spec_info('nonexistant') |
|
91 | self.ks_api.kernel_spec_info('nonexistant') | |
87 |
|
92 | |||
88 | def test_get_kernel_resource_file(self): |
|
93 | def test_get_kernel_resource_file(self): | |
89 | res = self.ks_api.kernel_resource('sAmple', 'resource.txt') |
|
94 | res = self.ks_api.kernel_resource('sAmple', 'resource.txt') | |
90 | self.assertEqual(res.text, some_resource) |
|
95 | self.assertEqual(res.text, some_resource) | |
91 |
|
96 | |||
92 | def test_get_nonexistant_resource(self): |
|
97 | def test_get_nonexistant_resource(self): | |
93 | with assert_http_error(404): |
|
98 | with assert_http_error(404): | |
94 | self.ks_api.kernel_resource('nonexistant', 'resource.txt') |
|
99 | self.ks_api.kernel_resource('nonexistant', 'resource.txt') | |
95 |
|
100 | |||
96 | with assert_http_error(404): |
|
101 | with assert_http_error(404): | |
97 | self.ks_api.kernel_resource('sample', 'nonexistant.txt') |
|
102 | self.ks_api.kernel_resource('sample', 'nonexistant.txt') |
General Comments 0
You need to be logged in to leave comments.
Login now