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