##// END OF EJS Templates
test some connection file utilities...
MinRK -
Show More
@@ -0,0 +1,124 b''
1 """Tests for kernel connection utilities
2
3 Authors
4 -------
5 * MinRK
6 """
7 #-----------------------------------------------------------------------------
8 # Copyright (c) 2013, the IPython Development Team.
9 #
10 # Distributed under the terms of the Modified BSD License.
11 #
12 # The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18
19 import json
20 import os
21
22 import nose.tools as nt
23
24 from IPython.config import Config
25 from IPython.consoleapp import IPythonConsoleApp
26 from IPython.core.application import BaseIPythonApplication
27 from IPython.utils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory
28 from IPython.utils.py3compat import str_to_bytes
29 from IPython.kernel import connect
30
31 #-----------------------------------------------------------------------------
32 # Classes and functions
33 #-----------------------------------------------------------------------------
34
35 class DummyConsoleApp(BaseIPythonApplication, IPythonConsoleApp):
36 def initialize(self, argv=[]):
37 BaseIPythonApplication.initialize(self, argv=argv)
38 self.init_connection_file()
39
40 sample_info = dict(ip='1.2.3.4', transport='ipc',
41 shell_port=1, hb_port=2, iopub_port=3, stdin_port=4, control_port=5,
42 key=b'abc123', signature_scheme='hmac-md5',
43 )
44
45 def test_write_connection_file():
46 with TemporaryDirectory() as d:
47 cf = os.path.join(d, 'kernel.json')
48 connect.write_connection_file(cf, **sample_info)
49 nt.assert_true(os.path.exists(cf))
50 with open(cf, 'r') as f:
51 info = json.load(f)
52 info['key'] = str_to_bytes(info['key'])
53 nt.assert_equal(info, sample_info)
54
55 def test_app_load_connection_file():
56 """test `ipython console --existing` loads a connection file"""
57 with TemporaryDirectory() as d:
58 cf = os.path.join(d, 'kernel.json')
59 connect.write_connection_file(cf, **sample_info)
60 app = DummyConsoleApp(connection_file=cf)
61 app.initialize(argv=[])
62
63 for attr, expected in sample_info.items():
64 if attr in ('key', 'signature_scheme'):
65 continue
66 value = getattr(app, attr)
67 nt.assert_equal(value, expected, "app.%s = %s != %s" % (attr, value, expected))
68
69 def test_get_connection_file():
70 cfg = Config()
71 with TemporaryWorkingDirectory() as d:
72 cfg.ProfileDir.location = d
73 cf = 'kernel.json'
74 app = DummyConsoleApp(config=cfg, connection_file=cf)
75 app.initialize(argv=[])
76
77 profile_cf = os.path.join(app.profile_dir.location, 'security', cf)
78 nt.assert_equal(profile_cf, app.connection_file)
79 with open(profile_cf, 'w') as f:
80 f.write("{}")
81 nt.assert_true(os.path.exists(profile_cf))
82 nt.assert_equal(connect.get_connection_file(app), profile_cf)
83
84 app.connection_file = cf
85 nt.assert_equal(connect.get_connection_file(app), profile_cf)
86
87 def test_find_connection_file():
88 cfg = Config()
89 with TemporaryDirectory() as d:
90 cfg.ProfileDir.location = d
91 cf = 'kernel.json'
92 app = DummyConsoleApp(config=cfg, connection_file=cf)
93 app.initialize(argv=[])
94 BaseIPythonApplication._instance = app
95
96 profile_cf = os.path.join(app.profile_dir.location, 'security', cf)
97 with open(profile_cf, 'w') as f:
98 f.write("{}")
99
100 for query in (
101 'kernel.json',
102 'kern*',
103 '*ernel*',
104 'k*',
105 ):
106 nt.assert_equal(connect.find_connection_file(query), profile_cf)
107
108 BaseIPythonApplication._instance = None
109
110 def test_get_connection_info():
111 with TemporaryDirectory() as d:
112 cf = os.path.join(d, 'kernel.json')
113 connect.write_connection_file(cf, **sample_info)
114 json_info = connect.get_connection_info(cf)
115 info = connect.get_connection_info(cf, unpack=True)
116
117 nt.assert_equal(type(json_info), type(""))
118 nt.assert_equal(info, sample_info)
119
120 info2 = json.loads(json_info)
121 info2['key'] = str_to_bytes(info2['key'])
122 nt.assert_equal(info2, sample_info)
123
124
General Comments 0
You need to be logged in to leave comments. Login now