##// END OF EJS Templates
adding to test_kernels_api.py...
Zachary Sailer -
Show More
@@ -45,7 +45,8 b' class MainKernelHandler(IPythonHandler):'
45 km = self.kernel_manager
45 km = self.kernel_manager
46 kernel_id = km.start_kernel()
46 kernel_id = km.start_kernel()
47 model = km.kernel_model(kernel_id, self.ws_url)
47 model = km.kernel_model(kernel_id, self.ws_url)
48 self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id))
48 self.set_header('Location', '{0}api/kernels/{1}'.format(self.base_kernel_url, kernel_id))
49 self.set_status(201)
49 self.finish(jsonapi.dumps(model))
50 self.finish(jsonapi.dumps(model))
50
51
51
52
@@ -57,6 +58,7 b' class KernelHandler(IPythonHandler):'
57 @json_errors
58 @json_errors
58 def get(self, kernel_id):
59 def get(self, kernel_id):
59 km = self.kernel_manager
60 km = self.kernel_manager
61 km._check_kernel_id(kernel_id)
60 model = km.kernel_model(kernel_id, self.ws_url)
62 model = km.kernel_model(kernel_id, self.ws_url)
61 self.finish(jsonapi.dumps(model))
63 self.finish(jsonapi.dumps(model))
62
64
@@ -66,16 +66,19 b' class MappingKernelManager(MultiKernelManager):'
66 'dead',
66 'dead',
67 )
67 )
68 else:
68 else:
69 self._check_kernel_id(kernel_id)
69 self.log.info("Using existing kernel: %s" % kernel_id)
70 self.log.info("Using existing kernel: %s" % kernel_id)
70 return kernel_id
71 return kernel_id
71
72
72 def shutdown_kernel(self, kernel_id, now=False):
73 def shutdown_kernel(self, kernel_id, now=False):
73 """Shutdown a kernel by kernel_id"""
74 """Shutdown a kernel by kernel_id"""
75 self._check_kernel_id(kernel_id)
74 super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now)
76 super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now)
75
77
76 def kernel_model(self, kernel_id, ws_url):
78 def kernel_model(self, kernel_id, ws_url):
77 """Return a dictionary of kernel information described in the
79 """Return a dictionary of kernel information described in the
78 JSON standard model."""
80 JSON standard model."""
81 self._check_kernel_id(kernel_id)
79 model = {"id":kernel_id, "ws_url": ws_url}
82 model = {"id":kernel_id, "ws_url": ws_url}
80 return model
83 return model
81
84
@@ -31,24 +31,70 b' class KernelAPITest(NotebookTestBase):'
31 # POST request
31 # POST request
32 r = requests.post(self.base_url())
32 r = requests.post(self.base_url())
33 data = r.json()
33 data = r.json()
34 status = r.status_code
35 header = r.headers
36 self.assertIn('location', header)
37 self.assertEquals(header['location'], '/api/kernels/' + data['id'])
38 self.assertEquals(status, 201)
34 assert isinstance(data, dict)
39 assert isinstance(data, dict)
35
40
36 # GET request
41 # GET request
37 r = requests.get(self.base_url())
42 r = requests.get(self.base_url())
43 status = r.status_code
44 self.assertEquals(status, 200)
38 assert isinstance(r.json(), list)
45 assert isinstance(r.json(), list)
39 self.assertEqual(r.json()[0]['id'], data['id'])
46 self.assertEqual(r.json()[0]['id'], data['id'])
47
48 # create another kernel and check that they both are added to the
49 # list of kernels from a GET request
50 data2 = self.mkkernel()
51 assert isinstance(data2, dict)
52 r = requests.get(self.base_url())
53 kernels = r.json()
54 status = r.status_code
55 self.assertEquals(status, 200)
56 assert isinstance(kernels, list)
57 self.assertEquals(len(kernels), 2)
40
58
41 def test_kernel_handler(self):
59 def test_kernel_handler(self):
42 # GET kernel with id
60 # GET kernel with given id
43 data = self.mkkernel()
61 data = self.mkkernel()
44 url = self.base_url() +'/' + data['id']
62 url = self.base_url() +'/' + data['id']
45 r = requests.get(url)
63 r = requests.get(url)
46 assert isinstance(r.json(), dict)
64 data1 = r.json()
47 self.assertIn('id', r.json())
65 status = r.status_code
48 self.assertEqual(r.json()['id'], data['id'])
66 self.assertEquals(status, 200)
67 assert isinstance(data1, dict)
68 self.assertIn('id', data1)
69 self.assertIn('ws_url', data1)
70 self.assertEqual(data1['id'], data['id'])
71
72 # Request a bad kernel id and check that a JSON
73 # message is returned!
74 bad_id = '111-111-111-111-111'
75 bad_url = self.base_url() + '/' + bad_id
76 r = requests.get(bad_url)
77 status = r.status_code
78 message = r.json()
79 self.assertEquals(status, 404)
80 assert isinstance(message, dict)
81 self.assertIn('message', message)
82 self.assertEquals(message['message'], 'Kernel does not exist: ' + bad_id)
49
83
50 # DELETE kernel with id
84 # DELETE kernel with id
51 r = requests.delete(url)
85 r = requests.delete(url)
52 self.assertEqual(r.status_code, 204)
86 self.assertEqual(r.status_code, 204)
53 r = requests.get(self.base_url())
87 r = requests.get(self.base_url())
54 self.assertEqual(r.json(), [])
88 self.assertEqual(r.json(), [])
89
90 # Request to delete a non-existent kernel id
91 bad_id = '111-111-111-111-111'
92 bad_url = self.base_url() + '/' + bad_id
93 r = requests.delete(bad_url)
94 status = r.status_code
95 message = r.json()
96 self.assertEquals(status, 404)
97 assert isinstance(message, dict)
98 self.assertIn('message', message)
99 self.assertEquals(message['message'], 'Kernel does not exist: ' + bad_id)
100 No newline at end of file
@@ -47,15 +47,15 b' class SessionRootHandler(IPythonHandler):'
47 km = self.kernel_manager
47 km = self.kernel_manager
48 model = self.get_json_body()
48 model = self.get_json_body()
49 if model is None:
49 if model is None:
50 raise HTTPError(400, "No JSON data provided")
50 raise web.HTTPError(400, "No JSON data provided")
51 try:
51 try:
52 name = model['notebook']['name']
52 name = model['notebook']['name']
53 except KeyError:
53 except KeyError:
54 raise HTTPError(400, "Missing field in JSON data: name")
54 raise web.HTTPError(400, "Missing field in JSON data: name")
55 try:
55 try:
56 path = model['notebook']['path']
56 path = model['notebook']['path']
57 except KeyError:
57 except KeyError:
58 raise HTTPError(400, "Missing field in JSON data: path")
58 raise web.HTTPError(400, "Missing field in JSON data: path")
59 # Check to see if session exists
59 # Check to see if session exists
60 if sm.session_exists(name=name, path=path):
60 if sm.session_exists(name=name, path=path):
61 model = sm.get_session(name=name, path=path)
61 model = sm.get_session(name=name, path=path)
General Comments 0
You need to be logged in to leave comments. Login now