forked from nameko/nameko
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_shell.py
More file actions
259 lines (193 loc) · 6.8 KB
/
Copy pathtest_shell.py
File metadata and controls
259 lines (193 loc) · 6.8 KB
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
254
255
256
257
258
259
import os
import subprocess
import sys
import pytest
from mock import Mock, patch
from nameko.cli.commands import Shell
from nameko.cli.main import setup_parser
from nameko.cli.shell import make_nameko_helper
from nameko.constants import (
AMQP_URI_CONFIG_KEY, SERIALIZER_CONFIG_KEY, WEB_SERVER_CONFIG_KEY
)
from nameko.standalone.rpc import ClusterProxy
def test_helper_module(rabbit_config):
helper = make_nameko_helper(rabbit_config)
assert isinstance(helper.rpc, ClusterProxy)
helper.disconnect()
@pytest.yield_fixture
def pystartup(tmpdir):
startup = tmpdir.join('startup.py')
startup.write('foo = 42')
with patch.dict(os.environ, {'PYTHONSTARTUP': str(startup)}):
yield
@pytest.yield_fixture(autouse=True)
def fake_alternative_interpreters():
# Make sure these appear unavailable even if installed. We cheat slightly,
# and have the call to `embed` raise the ImportError, rather than the
# actual module import (this is easier to do and has the same effect in our
# case).
fake_module = Mock()
fake_module.embed.side_effect = ImportError
with patch.dict(sys.modules, {
'IPython': fake_module,
'bpython': fake_module,
}):
yield
@pytest.fixture
def isatty():
with patch('nameko.cli.shell.sys.stdin.isatty') as isatty:
yield isatty
def test_basic(pystartup, rabbit_config):
parser = setup_parser()
args = parser.parse_args([
'shell', '--broker', rabbit_config[AMQP_URI_CONFIG_KEY]
])
with patch("nameko.cli.shell.code") as code:
Shell.main(args)
_, kwargs = code.interact.call_args
local = kwargs['local']
assert 'n' in local.keys()
assert local['foo'] == 42
local['n'].disconnect()
def test_plain(pystartup, rabbit_config):
parser = setup_parser()
args = parser.parse_args([
'shell', '--broker', rabbit_config[AMQP_URI_CONFIG_KEY],
'--interface', 'plain'
])
with patch('nameko.cli.shell.code') as code:
Shell.main(args)
_, kwargs = code.interact.call_args
local = kwargs['local']
assert 'n' in local.keys()
assert local['foo'] == 42
local['n'].disconnect()
def test_plain_fallback(pystartup, rabbit_config):
parser = setup_parser()
args = parser.parse_args([
'shell', '--broker', rabbit_config[AMQP_URI_CONFIG_KEY],
'--interface', 'bpython'
])
with patch('nameko.cli.shell.code') as code:
Shell.main(args)
_, kwargs = code.interact.call_args
local = kwargs['local']
assert 'n' in local.keys()
assert local['foo'] == 42
local['n'].disconnect()
def test_bpython(pystartup, rabbit_config, isatty):
parser = setup_parser()
args = parser.parse_args([
'shell', '--broker', rabbit_config[AMQP_URI_CONFIG_KEY],
'--interface', 'bpython'
])
isatty.return_value = True
with patch('bpython.embed') as embed:
Shell.main(args)
_, kwargs = embed.call_args
local = kwargs['locals_']
assert 'n' in local.keys()
assert local['foo'] == 42
local['n'].disconnect()
def test_ipython(pystartup, rabbit_config, isatty):
parser = setup_parser()
args = parser.parse_args([
'shell', '--broker', rabbit_config[AMQP_URI_CONFIG_KEY],
'--interface', 'ipython'
])
isatty.return_value = True
with patch('IPython.embed') as embed:
Shell.main(args)
_, kwargs = embed.call_args
local = kwargs['user_ns']
assert 'n' in local.keys()
assert local['foo'] == 42
local['n'].disconnect()
def test_uses_plain_when_not_tty(pystartup, rabbit_config, isatty):
parser = setup_parser()
args = parser.parse_args([
'shell', '--broker', rabbit_config[AMQP_URI_CONFIG_KEY],
'--interface', 'ipython'
])
isatty.return_value = False
with patch('nameko.cli.shell.code') as code:
Shell.main(args)
assert code.interact.called
def test_config(pystartup, rabbit_config, tmpdir):
config = tmpdir.join('config.yaml')
config.write("""
WEB_SERVER_ADDRESS: '0.0.0.0:8001'
AMQP_URI: '{}'
serializer: 'json'
""".format(rabbit_config[AMQP_URI_CONFIG_KEY]))
parser = setup_parser()
args = parser.parse_args(['shell', '--config', config.strpath])
with patch('nameko.cli.shell.code') as code:
Shell.main(args)
_, kwargs = code.interact.call_args
local = kwargs['local']
assert 'n' in local.keys()
assert local['n'].config == {
WEB_SERVER_CONFIG_KEY: '0.0.0.0:8001',
AMQP_URI_CONFIG_KEY: rabbit_config[AMQP_URI_CONFIG_KEY],
SERIALIZER_CONFIG_KEY: 'json'
}
local['n'].disconnect()
class TestBanner(object):
@pytest.yield_fixture(autouse=True)
def patch_nameko_helper(self):
with patch('nameko.cli.shell.make_nameko_helper'):
yield
def test_broker_as_param(self):
amqp_uri = "amqp://broker/param"
parser = setup_parser()
args = parser.parse_args(['shell', '--broker', amqp_uri])
with patch('nameko.cli.shell.ShellRunner') as shell_runner:
Shell.main(args)
expected_message = (
"Broker: {}".format(amqp_uri)
)
(banner, _), _ = shell_runner.call_args
assert expected_message in banner
def test_broker_from_config(self, tmpdir):
amqp_uri = "amqp://broker/config"
config = tmpdir.join('config.yaml')
config.write("""
WEB_SERVER_ADDRESS: '0.0.0.0:8001'
AMQP_URI: '{}'
serializer: 'json'
""".format(amqp_uri))
parser = setup_parser()
args = parser.parse_args(['shell', '--config', config.strpath])
with patch('nameko.cli.shell.ShellRunner') as shell_runner:
Shell.main(args)
expected_message = (
"Broker: {} (from --config)".format(amqp_uri)
)
(banner, _), _ = shell_runner.call_args
assert expected_message in banner
class TestExitCode:
def test_exit_code_0(self):
""" Test ``echo print(1) | nameko shell``
This command should return an exit code of 0.
"""
echo_process = subprocess.Popen(
("echo", "print(1)"), stdout=subprocess.PIPE
)
nameko_process = subprocess.Popen(
("nameko", "shell"), stdin=echo_process.stdout
)
nameko_process.wait()
assert nameko_process.returncode == 0
def test_exit_code_1(self):
""" Test ``echo raise Exception() | nameko shell``
This command should return an exit code of 1.
"""
echo_process = subprocess.Popen(
("echo", "raise Exception()"), stdout=subprocess.PIPE
)
nameko_process = subprocess.Popen(
("nameko", "shell"), stdin=echo_process.stdout
)
nameko_process.wait()
assert nameko_process.returncode == 1