Show More
@@ -1,133 +1,133 b'' | |||||
1 | """Test the kernels service API.""" |
|
1 | """Test the kernels service API.""" | |
2 |
|
2 | |||
3 | import json |
|
3 | import json | |
4 | import requests |
|
4 | import requests | |
5 |
|
5 | |||
6 | from IPython.html.utils import url_path_join |
|
6 | from IPython.html.utils import url_path_join | |
7 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error |
|
7 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error | |
8 |
|
8 | |||
9 | class KernelAPI(object): |
|
9 | class KernelAPI(object): | |
10 | """Wrapper for kernel REST API requests""" |
|
10 | """Wrapper for kernel REST API requests""" | |
11 | def __init__(self, base_url): |
|
11 | def __init__(self, base_url): | |
12 | self.base_url = base_url |
|
12 | self.base_url = base_url | |
13 |
|
13 | |||
14 | def _req(self, verb, path, body=None): |
|
14 | def _req(self, verb, path, body=None): | |
15 | response = requests.request(verb, |
|
15 | response = requests.request(verb, | |
16 | url_path_join(self.base_url, 'api/kernels', path), data=body) |
|
16 | url_path_join(self.base_url, 'api/kernels', path), data=body) | |
17 |
|
17 | |||
18 | if 400 <= response.status_code < 600: |
|
18 | if 400 <= response.status_code < 600: | |
19 | try: |
|
19 | try: | |
20 | response.reason = response.json()['message'] |
|
20 | response.reason = response.json()['message'] | |
21 | except: |
|
21 | except: | |
22 | pass |
|
22 | pass | |
23 | response.raise_for_status() |
|
23 | response.raise_for_status() | |
24 |
|
24 | |||
25 | return response |
|
25 | return response | |
26 |
|
26 | |||
27 | def list(self): |
|
27 | def list(self): | |
28 | return self._req('GET', '') |
|
28 | return self._req('GET', '') | |
29 |
|
29 | |||
30 | def get(self, id): |
|
30 | def get(self, id): | |
31 | return self._req('GET', id) |
|
31 | return self._req('GET', id) | |
32 |
|
32 | |||
33 | def start(self, name='python'): |
|
33 | def start(self, name='python'): | |
34 | body = json.dumps({'name': name}) |
|
34 | body = json.dumps({'name': name}) | |
35 | return self._req('POST', '', body) |
|
35 | return self._req('POST', '', body) | |
36 |
|
36 | |||
37 | def shutdown(self, id): |
|
37 | def shutdown(self, id): | |
38 | return self._req('DELETE', id) |
|
38 | return self._req('DELETE', id) | |
39 |
|
39 | |||
40 | def interrupt(self, id): |
|
40 | def interrupt(self, id): | |
41 | return self._req('POST', url_path_join(id, 'interrupt')) |
|
41 | return self._req('POST', url_path_join(id, 'interrupt')) | |
42 |
|
42 | |||
43 | def restart(self, id): |
|
43 | def restart(self, id): | |
44 | return self._req('POST', url_path_join(id, 'restart')) |
|
44 | return self._req('POST', url_path_join(id, 'restart')) | |
45 |
|
45 | |||
46 | class KernelAPITest(NotebookTestBase): |
|
46 | class KernelAPITest(NotebookTestBase): | |
47 | """Test the kernels web service API""" |
|
47 | """Test the kernels web service API""" | |
48 | def setUp(self): |
|
48 | def setUp(self): | |
49 | self.kern_api = KernelAPI(self.base_url()) |
|
49 | self.kern_api = KernelAPI(self.base_url()) | |
50 |
|
50 | |||
51 | def tearDown(self): |
|
51 | def tearDown(self): | |
52 | for k in self.kern_api.list().json(): |
|
52 | for k in self.kern_api.list().json(): | |
53 | self.kern_api.shutdown(k['id']) |
|
53 | self.kern_api.shutdown(k['id']) | |
54 |
|
54 | |||
55 | def test__no_kernels(self): |
|
55 | def test__no_kernels(self): | |
56 | """Make sure there are no kernels running at the start""" |
|
56 | """Make sure there are no kernels running at the start""" | |
57 | kernels = self.kern_api.list().json() |
|
57 | kernels = self.kern_api.list().json() | |
58 | self.assertEqual(kernels, []) |
|
58 | self.assertEqual(kernels, []) | |
59 |
|
59 | |||
60 | def test_default_kernel(self): |
|
60 | def test_default_kernel(self): | |
61 | # POST request |
|
61 | # POST request | |
62 | r = self.kern_api._req('POST', '') |
|
62 | r = self.kern_api._req('POST', '') | |
63 | kern1 = r.json() |
|
63 | kern1 = r.json() | |
64 | self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id']) |
|
64 | self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id']) | |
65 | self.assertEqual(r.status_code, 201) |
|
65 | self.assertEqual(r.status_code, 201) | |
66 | self.assertIsInstance(kern1, dict) |
|
66 | self.assertIsInstance(kern1, dict) | |
67 |
|
67 | |||
68 |
self.assertEqual(r.headers[' |
|
68 | self.assertEqual(r.headers['Content-Security-Policy'], "frame-ancestors 'self'") | |
69 |
|
69 | |||
70 | def test_main_kernel_handler(self): |
|
70 | def test_main_kernel_handler(self): | |
71 | # POST request |
|
71 | # POST request | |
72 | r = self.kern_api.start() |
|
72 | r = self.kern_api.start() | |
73 | kern1 = r.json() |
|
73 | kern1 = r.json() | |
74 | self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id']) |
|
74 | self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id']) | |
75 | self.assertEqual(r.status_code, 201) |
|
75 | self.assertEqual(r.status_code, 201) | |
76 | self.assertIsInstance(kern1, dict) |
|
76 | self.assertIsInstance(kern1, dict) | |
77 |
|
77 | |||
78 |
self.assertEqual(r.headers[' |
|
78 | self.assertEqual(r.headers['Content-Security-Policy'], "frame-ancestors 'self'") | |
79 |
|
79 | |||
80 | # GET request |
|
80 | # GET request | |
81 | r = self.kern_api.list() |
|
81 | r = self.kern_api.list() | |
82 | self.assertEqual(r.status_code, 200) |
|
82 | self.assertEqual(r.status_code, 200) | |
83 | assert isinstance(r.json(), list) |
|
83 | assert isinstance(r.json(), list) | |
84 | self.assertEqual(r.json()[0]['id'], kern1['id']) |
|
84 | self.assertEqual(r.json()[0]['id'], kern1['id']) | |
85 | self.assertEqual(r.json()[0]['name'], kern1['name']) |
|
85 | self.assertEqual(r.json()[0]['name'], kern1['name']) | |
86 |
|
86 | |||
87 | # create another kernel and check that they both are added to the |
|
87 | # create another kernel and check that they both are added to the | |
88 | # list of kernels from a GET request |
|
88 | # list of kernels from a GET request | |
89 | kern2 = self.kern_api.start().json() |
|
89 | kern2 = self.kern_api.start().json() | |
90 | assert isinstance(kern2, dict) |
|
90 | assert isinstance(kern2, dict) | |
91 | r = self.kern_api.list() |
|
91 | r = self.kern_api.list() | |
92 | kernels = r.json() |
|
92 | kernels = r.json() | |
93 | self.assertEqual(r.status_code, 200) |
|
93 | self.assertEqual(r.status_code, 200) | |
94 | assert isinstance(kernels, list) |
|
94 | assert isinstance(kernels, list) | |
95 | self.assertEqual(len(kernels), 2) |
|
95 | self.assertEqual(len(kernels), 2) | |
96 |
|
96 | |||
97 | # Interrupt a kernel |
|
97 | # Interrupt a kernel | |
98 | r = self.kern_api.interrupt(kern2['id']) |
|
98 | r = self.kern_api.interrupt(kern2['id']) | |
99 | self.assertEqual(r.status_code, 204) |
|
99 | self.assertEqual(r.status_code, 204) | |
100 |
|
100 | |||
101 | # Restart a kernel |
|
101 | # Restart a kernel | |
102 | r = self.kern_api.restart(kern2['id']) |
|
102 | r = self.kern_api.restart(kern2['id']) | |
103 | self.assertEqual(r.headers['Location'], '/api/kernels/'+kern2['id']) |
|
103 | self.assertEqual(r.headers['Location'], '/api/kernels/'+kern2['id']) | |
104 | rekern = r.json() |
|
104 | rekern = r.json() | |
105 | self.assertEqual(rekern['id'], kern2['id']) |
|
105 | self.assertEqual(rekern['id'], kern2['id']) | |
106 | self.assertEqual(rekern['name'], kern2['name']) |
|
106 | self.assertEqual(rekern['name'], kern2['name']) | |
107 |
|
107 | |||
108 | def test_kernel_handler(self): |
|
108 | def test_kernel_handler(self): | |
109 | # GET kernel with given id |
|
109 | # GET kernel with given id | |
110 | kid = self.kern_api.start().json()['id'] |
|
110 | kid = self.kern_api.start().json()['id'] | |
111 | r = self.kern_api.get(kid) |
|
111 | r = self.kern_api.get(kid) | |
112 | kern1 = r.json() |
|
112 | kern1 = r.json() | |
113 | self.assertEqual(r.status_code, 200) |
|
113 | self.assertEqual(r.status_code, 200) | |
114 | assert isinstance(kern1, dict) |
|
114 | assert isinstance(kern1, dict) | |
115 | self.assertIn('id', kern1) |
|
115 | self.assertIn('id', kern1) | |
116 | self.assertEqual(kern1['id'], kid) |
|
116 | self.assertEqual(kern1['id'], kid) | |
117 |
|
117 | |||
118 | # Request a bad kernel id and check that a JSON |
|
118 | # Request a bad kernel id and check that a JSON | |
119 | # message is returned! |
|
119 | # message is returned! | |
120 | bad_id = '111-111-111-111-111' |
|
120 | bad_id = '111-111-111-111-111' | |
121 | with assert_http_error(404, 'Kernel does not exist: ' + bad_id): |
|
121 | with assert_http_error(404, 'Kernel does not exist: ' + bad_id): | |
122 | self.kern_api.get(bad_id) |
|
122 | self.kern_api.get(bad_id) | |
123 |
|
123 | |||
124 | # DELETE kernel with id |
|
124 | # DELETE kernel with id | |
125 | r = self.kern_api.shutdown(kid) |
|
125 | r = self.kern_api.shutdown(kid) | |
126 | self.assertEqual(r.status_code, 204) |
|
126 | self.assertEqual(r.status_code, 204) | |
127 | kernels = self.kern_api.list().json() |
|
127 | kernels = self.kern_api.list().json() | |
128 | self.assertEqual(kernels, []) |
|
128 | self.assertEqual(kernels, []) | |
129 |
|
129 | |||
130 | # Request to delete a non-existent kernel id |
|
130 | # Request to delete a non-existent kernel id | |
131 | bad_id = '111-111-111-111-111' |
|
131 | bad_id = '111-111-111-111-111' | |
132 | with assert_http_error(404, 'Kernel does not exist: ' + bad_id): |
|
132 | with assert_http_error(404, 'Kernel does not exist: ' + bad_id): | |
133 | self.kern_api.shutdown(bad_id) |
|
133 | self.kern_api.shutdown(bad_id) |
General Comments 0
You need to be logged in to leave comments.
Login now