-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_core.py
213 lines (174 loc) · 7.05 KB
/
test_core.py
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
import datetime
import json
import os
from contextlib import contextmanager
import pytest
import jupyter_forward
from .misc import sample_log_file_contents
SHELLS = json.loads(os.environ.get('JUPYTER_FORWARD_TEST_SHELLS', '["bash", null]'))
JUPYTER_FORWARD_ENABLE_SSH_TESTS = os.environ.get('JUPYTER_FORWARD_ENABLE_SSH_TESTS') is None
requires_ssh = pytest.mark.skipif(JUPYTER_FORWARD_ENABLE_SSH_TESTS, reason='SSH tests disabled')
ON_GITHUB_ACTIONS = os.environ.get('GITHUB_ACTIONS') is not None
@contextmanager
def tempfile(session):
out = session.run('mktemp')
path = out.stdout.strip()
try:
yield path
finally:
session.run(f'rm {path}')
@pytest.fixture(scope='package')
def runner(request):
remote = jupyter_forward.RemoteRunner(
f"{os.environ['JUPYTER_FORWARD_SSH_TEST_USER']}@{os.environ['JUPYTER_FORWARD_SSH_TEST_HOSTNAME']}",
shell=request.param,
auth_handler=lambda t, i, p: ['Loremipsumdolorsitamet'] * len(p),
fallback_auth_handler=lambda: 'Loremipsumdolorsitamet',
)
try:
yield remote
finally:
remote.close()
@requires_ssh
@pytest.mark.parametrize(
'port, conda_env, notebook, notebook_dir, port_forwarding, identity, shell',
[
(8888, None, None, None, True, None, None),
(8888, None, None, '~/notebooks/', False, None, None),
(8888, None, '~/my_notebook.ipynb', None, True, None, 'bash'),
(8888, 'base', None, None, False, None, 'bash'),
],
)
def test_runner_init(port, conda_env, notebook, notebook_dir, port_forwarding, identity, shell):
remote_runner = jupyter_forward.RemoteRunner(
f"{os.environ['JUPYTER_FORWARD_SSH_TEST_USER']}@{os.environ['JUPYTER_FORWARD_SSH_TEST_HOSTNAME']}",
port=port,
conda_env=conda_env,
notebook=notebook,
notebook_dir=notebook_dir,
identity=identity,
port_forwarding=port_forwarding,
shell=shell,
auth_handler=lambda t, i, p: ['Loremipsumdolorsitamet'] * len(p),
fallback_auth_handler=lambda: 'Loremipsumdolorsitamet',
)
assert remote_runner.port == port
assert remote_runner.conda_env == conda_env
@requires_ssh
def test_runner_init_notebook_dir_error():
with pytest.raises(ValueError):
jupyter_forward.RemoteRunner(
f"{os.environ['JUPYTER_FORWARD_SSH_TEST_USER']}@{os.environ['JUPYTER_FORWARD_SSH_TEST_HOSTNAME']}",
notebook_dir='~/notebooks/',
notebook='~/my_notebook.ipynb',
)
@requires_ssh
def test_runner_init_port_unavailable():
with pytest.raises(SystemExit):
jupyter_forward.RemoteRunner(
f"{os.environ['JUPYTER_FORWARD_SSH_TEST_USER']}@{os.environ['JUPYTER_FORWARD_SSH_TEST_HOSTNAME']}",
port=22,
)
@requires_ssh
def test_runner_authentication_error():
with pytest.raises(SystemExit):
jupyter_forward.RemoteRunner(
f"foobar@{os.environ['JUPYTER_FORWARD_SSH_TEST_HOSTNAME']}",
auth_handler=lambda t, i, p: ['Loremipsumdolorsitamet'] * len(p),
fallback_auth_handler=lambda: 'Loremipsumdolorsitamet',
)
@requires_ssh
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_connection(runner):
USER = os.environ['JUPYTER_FORWARD_SSH_TEST_USER']
assert runner.session.is_connected
assert runner.session.host in [
'127.0.0.1',
'localhost',
{os.environ['JUPYTER_FORWARD_SSH_TEST_HOSTNAME']},
]
assert runner.session.user == USER
@requires_ssh
@pytest.mark.parametrize('command', ['echo $HOME'])
@pytest.mark.parametrize('kwargs', [{}, dict(asynchronous=True)])
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_run_command(runner, command, kwargs):
out = runner.run_command(command, **kwargs)
if kwargs.get('asynchronous', False):
out = out.join()
assert not out.failed
f"{os.environ['HOME']}" in out.stdout.strip()
@requires_ssh
@pytest.mark.parametrize('command', ['echod $HOME'])
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_run_command_failure(runner, command):
out = runner.run_command(command, exit=False)
assert out.failed
assert 'not found' in out.stdout.strip().lower()
with pytest.raises(SystemExit):
runner.run_command(command)
@requires_ssh
@pytest.mark.parametrize('content', ['echo $HOME', 'echo $(hostname -f)'])
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_put_file(runner, content):
with tempfile(runner.session) as path:
runner.put_file(path, content)
out = runner.session.run(f'cat {path}')
assert content == out.stdout
@requires_ssh
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_set_logs(runner):
runner._set_log_directory()
assert '/.jupyter_forward' in runner.log_dir
runner._set_log_file()
now = datetime.datetime.now()
assert f"log_{now.strftime('%Y-%m-%dT%H')}" in runner.log_file
@requires_ssh
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_prepare_batch_job_script(runner):
if ON_GITHUB_ACTIONS and ('csh' in runner.shell):
pytest.xfail('Fails on GitHub Actions due to inconsistent shell behavior')
runner._set_log_directory()
script_file = runner._prepare_batch_job_script('echo hello world')
assert 'batch_job_script' in script_file
assert 'hello world' in runner.run_command(f'cat {script_file}').stdout.strip()
@requires_ssh
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_parse_log_file(runner):
runner._set_log_directory()
runner._set_log_file()
runner.run_command(f"echo '{sample_log_file_contents[0]}' > {runner.log_file}")
for line in sample_log_file_contents[1:]:
runner.run_command(f"echo '{line}' >> {runner.log_file}")
out = runner._parse_log_file()
assert out == {
'hostname': 'eniac01',
'port': '59628',
'token': 'Loremipsumdolorsitamet',
'url': 'http://eniac01:59628/?token=Loremipsumdolorsitamet',
}
@requires_ssh
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
@pytest.mark.parametrize('environment', ['jupyter-forward-dev', None])
def test_conda_activate_cmd(runner, environment):
if ON_GITHUB_ACTIONS and ('csh' in runner.shell or 'zsh' in runner.shell):
pytest.xfail('Fails on GitHub Actions due to inconsistent shell behavior')
runner.conda_env = environment
cmd = runner._conda_activate_cmd()
assert cmd in ['source activate', 'conda activate']
@requires_ssh
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_conda_activate_cmd_error(runner):
runner.conda_env = 'DOES_NOT_EXIST'
with pytest.raises(SystemExit):
runner._conda_activate_cmd()
@requires_ssh
@pytest.mark.parametrize('runner', SHELLS, indirect=True)
def test_generate_redirect_cmd(runner):
runner._set_log_directory()
runner._set_log_file()
cmd = runner._generate_redirect_command(command='echo "hello world"', log_file=runner.log_file)
if 'csh' in runner.shell:
assert cmd.endswith(runner.log_file)
else:
assert cmd.endswith(f'{runner.log_file} 2>&1')