This file is indexed.

/usr/lib/python2.7/dist-packages/notebook/services/sessions/tests/test_sessionmanager.py is in python-notebook 5.2.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Tests for the session manager."""

from functools import partial
from unittest import TestCase

from tornado import gen, web
from tornado.ioloop import IOLoop

from ..sessionmanager import SessionManager
from notebook.services.kernels.kernelmanager import MappingKernelManager
from notebook.services.contents.manager import ContentsManager
from notebook._tz import utcnow, isoformat

class DummyKernel(object):
    def __init__(self, kernel_name='python'):
        self.kernel_name = kernel_name

dummy_date = utcnow()
dummy_date_s = isoformat(dummy_date)

class DummyMKM(MappingKernelManager):
    """MappingKernelManager interface that doesn't start kernels, for testing"""
    def __init__(self, *args, **kwargs):
        super(DummyMKM, self).__init__(*args, **kwargs)
        self.id_letters = iter(u'ABCDEFGHIJK')

    def _new_id(self):
        return next(self.id_letters)
    
    def start_kernel(self, kernel_id=None, path=None, kernel_name='python', **kwargs):
        kernel_id = kernel_id or self._new_id()
        k = self._kernels[kernel_id] = DummyKernel(kernel_name=kernel_name)
        self._kernel_connections[kernel_id] = 0
        k.last_activity = dummy_date
        k.execution_state = 'idle'
        return kernel_id

    def shutdown_kernel(self, kernel_id, now=False):
        del self._kernels[kernel_id]


class TestSessionManager(TestCase):
    
    def setUp(self):
        self.sm = SessionManager(
            kernel_manager=DummyMKM(),
            contents_manager=ContentsManager(),
        )
        self.loop = IOLoop()
        self.addCleanup(partial(self.loop.close, all_fds=True))

    def create_sessions(self, *kwarg_list):
        @gen.coroutine
        def co_add():
            sessions = []
            for kwargs in kwarg_list:
                kwargs.setdefault('type', 'notebook')
                session = yield self.sm.create_session(**kwargs)
                sessions.append(session)
            raise gen.Return(sessions)
        return self.loop.run_sync(co_add)
    
    def create_session(self, **kwargs):
        return self.create_sessions(kwargs)[0]
    
    def test_get_session(self):
        sm = self.sm
        session_id = self.create_session(path='/path/to/test.ipynb', kernel_name='bar')['id']
        model = sm.get_session(session_id=session_id)
        expected = {'id':session_id,
                    'path': u'/path/to/test.ipynb',
                    'notebook': {'path': u'/path/to/test.ipynb', 'name': None},
                    'type': 'notebook',
                    'name': None,
                    'kernel': {
                        'id': 'A',
                        'name': 'bar',
                        'connections': 0,
                        'last_activity': dummy_date_s,
                        'execution_state': 'idle',
                    }}
        self.assertEqual(model, expected)

    def test_bad_get_session(self):
        # Should raise error if a bad key is passed to the database.
        sm = self.sm
        session_id = self.create_session(path='/path/to/test.ipynb',
                                       kernel_name='foo')['id']
        self.assertRaises(TypeError, sm.get_session, bad_id=session_id) # Bad keyword

    def test_get_session_dead_kernel(self):
        sm = self.sm
        session = self.create_session(path='/path/to/1/test1.ipynb', kernel_name='python')
        # kill the kernel
        sm.kernel_manager.shutdown_kernel(session['kernel']['id'])
        with self.assertRaises(KeyError):
            sm.get_session(session_id=session['id'])
        # no sessions left
        listed = sm.list_sessions()
        self.assertEqual(listed, [])

    def test_list_sessions(self):
        sm = self.sm
        sessions = self.create_sessions(
            dict(path='/path/to/1/test1.ipynb', kernel_name='python'),
            dict(path='/path/to/2/test2.py', type='file', kernel_name='python'),
            dict(path='/path/to/3', name='foo', type='console', kernel_name='python'),
        )
        
        sessions = sm.list_sessions()
        expected = [
            {
                'id':sessions[0]['id'],
                'path': u'/path/to/1/test1.ipynb',
                'type': 'notebook',
                'notebook': {'path': u'/path/to/1/test1.ipynb', 'name': None},
                'name': None,
                'kernel': {
                    'id': 'A',
                    'name':'python',
                    'connections': 0,
                    'last_activity': dummy_date_s,
                    'execution_state': 'idle',
                }
            }, {
                'id':sessions[1]['id'],
                'path': u'/path/to/2/test2.py',
                'type': 'file',
                'name': None,
                'kernel': {
                    'id': 'B',
                    'name':'python',
                    'connections': 0,
                    'last_activity': dummy_date_s,
                    'execution_state': 'idle',
                }
            }, {
                'id':sessions[2]['id'],
                'path': u'/path/to/3',
                'type': 'console',
                'name': 'foo',
                'kernel': {
                    'id': 'C',
                    'name':'python',
                    'connections': 0,
                    'last_activity': dummy_date_s,
                    'execution_state': 'idle',
                }
            }
        ]
        self.assertEqual(sessions, expected)

    def test_list_sessions_dead_kernel(self):
        sm = self.sm
        sessions = self.create_sessions(
            dict(path='/path/to/1/test1.ipynb', kernel_name='python'),
            dict(path='/path/to/2/test2.ipynb', kernel_name='python'),
        )
        # kill one of the kernels
        sm.kernel_manager.shutdown_kernel(sessions[0]['kernel']['id'])
        listed = sm.list_sessions()
        expected = [
            {
                'id': sessions[1]['id'],
                'path': u'/path/to/2/test2.ipynb',
                'type': 'notebook',
                'name': None,
                'notebook': {'path': u'/path/to/2/test2.ipynb', 'name': None},
                'kernel': {
                    'id': 'B',
                    'name':'python',
                    'connections': 0,
                    'last_activity': dummy_date_s,
                    'execution_state': 'idle',
                }
            }
        ]
        self.assertEqual(listed, expected)

    def test_update_session(self):
        sm = self.sm
        session_id = self.create_session(path='/path/to/test.ipynb',
                                       kernel_name='julia')['id']
        sm.update_session(session_id, path='/path/to/new_name.ipynb')
        model = sm.get_session(session_id=session_id)
        expected = {'id':session_id,
                    'path': u'/path/to/new_name.ipynb',
                    'type': 'notebook',
                    'name': None,
                    'notebook': {'path': u'/path/to/new_name.ipynb', 'name': None},
                    'kernel': {
                        'id': 'A',
                        'name':'julia',
                        'connections': 0,
                        'last_activity': dummy_date_s,
                        'execution_state': 'idle',
                    }
        }
        self.assertEqual(model, expected)
    
    def test_bad_update_session(self):
        # try to update a session with a bad keyword ~ raise error
        sm = self.sm
        session_id = self.create_session(path='/path/to/test.ipynb',
                                       kernel_name='ir')['id']
        self.assertRaises(TypeError, sm.update_session, session_id=session_id, bad_kw='test.ipynb') # Bad keyword

    def test_delete_session(self):
        sm = self.sm
        sessions = self.create_sessions(
            dict(path='/path/to/1/test1.ipynb', kernel_name='python'),
            dict(path='/path/to/2/test2.ipynb', kernel_name='python'),
            dict(path='/path/to/3', name='foo', type='console', kernel_name='python'),
        )
        sm.delete_session(sessions[1]['id'])
        new_sessions = sm.list_sessions()
        expected = [{
                'id': sessions[0]['id'],
                'path': u'/path/to/1/test1.ipynb',
                'type': 'notebook',
                'name': None,
                'notebook': {'path': u'/path/to/1/test1.ipynb', 'name': None},
                'kernel': {
                    'id': 'A',
                    'name':'python',
                    'connections': 0,
                    'last_activity': dummy_date_s,
                    'execution_state': 'idle',
                }
            }, {
                'id': sessions[2]['id'],
                'type': 'console',
                'path': u'/path/to/3',
                'name': 'foo',
                'kernel': {
                    'id': 'C',
                    'name':'python',
                    'connections': 0,
                    'last_activity': dummy_date_s,
                    'execution_state': 'idle',
                }
            }
        ]
        self.assertEqual(new_sessions, expected)

    def test_bad_delete_session(self):
        # try to delete a session that doesn't exist ~ raise error
        sm = self.sm
        self.create_session(path='/path/to/test.ipynb', kernel_name='python')
        with self.assertRaises(TypeError):
            self.loop.run_sync(lambda : sm.delete_session(bad_kwarg='23424')) # Bad keyword
        with self.assertRaises(web.HTTPError):
            self.loop.run_sync(lambda : sm.delete_session(session_id='23424')) # nonexistent