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