forked from wandb/wandb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
371 lines (335 loc) · 14.6 KB
/
test_cli.py
File metadata and controls
371 lines (335 loc) · 14.6 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import pytest, os, traceback, click
from wandb import cli, Api, GitRepo, __version__
from click.testing import CliRunner
from .api_mocks import *
import netrc, signal, time
import six, time, whaaaaat, yaml
import git
import webbrowser
import wandb
try:
# python 3.4+
from importlib import reload
except ImportError:
# python 3.2, 3.3
from imp import reload
except ImportError:
pass
@pytest.fixture
def runner(monkeypatch):
monkeypatch.setattr(cli, 'api', Api(default_settings={'project': 'test', 'git_tag': True}, load_settings=False))
monkeypatch.setattr(click, 'launch', lambda x: 1)
monkeypatch.setattr(whaaaaat, 'prompt', lambda x: {'project': 'test_model', 'files': ['weights.h5']})
monkeypatch.setattr(webbrowser, 'open_new_tab', lambda x: True)
return CliRunner()
@pytest.fixture
def empty_netrc(monkeypatch):
class FakeNet(object):
@property
def hosts(self):
return {'api.wandb.ai': None}
monkeypatch.setattr(netrc, "netrc", lambda *args: FakeNet())
@pytest.fixture
def local_netrc(monkeypatch):
#TODO: this seems overkill...
origexpand = os.path.expanduser
def expand(path):
return os.path.realpath("netrc") if "netrc" in path else origexpand(path)
monkeypatch.setattr(os.path, "expanduser", expand)
def git_repo():
r = git.Repo.init(".")
open("README", "wb").close()
r.index.add(["README"])
r.index.commit("Initial commit")
return GitRepo(lazy=False)
def test_help(runner):
result = runner.invoke(cli.cli)
assert result.exit_code == 0
assert 'Weights & Biases' in result.output
help_result = runner.invoke(cli.cli, ['--help'])
assert help_result.exit_code == 0
assert 'Show this message and exit.' in help_result.output
def test_version(runner):
result = runner.invoke(cli.cli, ["--version"])
assert result.exit_code == 0
assert __version__ in result.output
@pytest.mark.skip(reason='config reworked, fixes coming...')
def test_config(runner, monkeypatch):
with runner.isolated_filesystem():
result = runner.invoke(cli.config, ["init"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert "wandb config set" in result.output
assert os.path.exists("config-defaults.yaml")
@pytest.mark.skip(reason='config reworked, fixes coming...')
def test_config_show(runner, monkeypatch):
with runner.isolated_filesystem():
with open("config-defaults.yaml", "w") as f:
f.write(yaml.dump({'val': {'value': 'awesome', 'desc': 'cool'}, 'bad': {'value':'shit'}}))
result_py = runner.invoke(cli.config, ["show"])
result_yml = runner.invoke(cli.config, ["show", "--format", "yaml"])
result_json = runner.invoke(cli.config, ["show", "--format", "json"])
print(result_py.output)
print(result_py.exception)
print(traceback.print_tb(result_py.exc_info[2]))
assert "awesome" in result_py.output
assert "awesome" in result_yml.output
assert "awesome" in result_json.output
@pytest.mark.skip(reason='config reworked, fixes coming...')
def test_config_show_empty(runner, monkeypatch):
with runner.isolated_filesystem():
result = runner.invoke(cli.config, ["show"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert "No configuration" in result.output
@pytest.mark.skip(reason='config reworked, fixes coming...')
def test_config_set(runner):
with runner.isolated_filesystem():
runner.invoke(cli.config, ["init"])
result = runner.invoke(cli.config, ["set", "foo=bar"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert "foo='bar'" in result.output
@pytest.mark.skip(reason='config reworked, fixes coming...')
def test_config_del(runner):
with runner.isolated_filesystem():
with open("config-defaults.yaml", "w") as f:
f.write(yaml.dump({'val': {'value': 'awesome', 'desc': 'cool'}, 'bad': {'value':'shit'}}))
result = runner.invoke(cli.config, ["del", "bad"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert "1 parameters changed" in result.output
def test_push(runner, request_mocker, query_project, upload_url, upsert_run, monkeypatch):
query_project(request_mocker)
upload_url(request_mocker)
update_mock = upsert_run(request_mocker)
with runner.isolated_filesystem():
#So GitRepo is in this cwd
os.mkdir('wandb')
monkeypatch.setattr(cli, 'api', Api({'project': 'test'}))
with open("wandb/latest.yaml", "w") as f:
f.write(yaml.dump({'wandb_version': 1, 'test': {'value': 'success', 'desc': 'My life'}}))
with open('weights.h5', 'wb') as f:
f.write(os.urandom(5000))
result = runner.invoke(cli.push, ['test/default', 'weights.h5', '-m', 'My description'])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Updating run: test/default" in result.output
def test_push_no_run(runner):
with runner.isolated_filesystem():
with open('weights.h5', 'wb') as f:
f.write(os.urandom(5000))
result = runner.invoke(cli.push, ['weights.h5', '-p', 'test', '-m', 'Something great'])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 2
assert "Run id is required if files are specified." in result.output
def test_push_dirty_git(runner, monkeypatch):
with runner.isolated_filesystem():
os.mkdir('wandb')
# If the test was run from a directory containing .wandb, then __stage_dir__
# was '.wandb' when imported by api.py, reload to fix. UGH!
reload(wandb)
repo = git_repo()
open("foo.txt", "wb").close()
repo.repo.index.add(["foo.txt"])
monkeypatch.setattr(cli, 'api', Api({'project': 'test'}))
cli.api._settings['git_tag'] = True
result = runner.invoke(cli.push, ["test", "foo.txt", "-p", "test", "-m", "Dirty"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 1
assert "You have un-committed changes." in result.output
def test_push_dirty_force_git(runner, request_mocker, query_project, upload_url, upsert_run, monkeypatch):
query_project(request_mocker)
upload_url(request_mocker)
update_mock = upsert_run(request_mocker)
with runner.isolated_filesystem():
#So GitRepo is in this cwd
monkeypatch.setattr(cli, 'api', Api({'project': 'test'}))
repo = git_repo()
with open('weights.h5', 'wb') as f:
f.write(os.urandom(100))
repo.repo.index.add(["weights.h5"])
result = runner.invoke(cli.push, ["test", "weights.h5", "-f", "-p", "test", "-m", "Dirty"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
def test_push_auto(runner, request_mocker, mocker, query_project, upload_url, monkeypatch):
query_project(request_mocker)
upload_url(request_mocker)
edit_mock = mocker.patch("click.edit")
with runner.isolated_filesystem():
#So GitRepo is in this cwd
monkeypatch.setattr(cli, 'api', Api({'project': 'test'}))
with open('weights.h5', 'wb') as f:
f.write(os.urandom(5000))
with open('fake.json', 'wb') as f:
f.write(os.urandom(100))
result = runner.invoke(cli.push, ['test', '--project', 'test', '-m', 'Testing'])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Uploading file: weights.h5" in result.output
#TODO: test without specifying message
#assert edit_mock.called
def test_pull(runner, request_mocker, query_project, download_url):
query_project(request_mocker)
download_url(request_mocker)
with runner.isolated_filesystem():
result = runner.invoke(cli.pull, ['test', '--project', 'test'])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Downloading: test/test" in result.output
assert os.path.isfile("weights.h5")
assert "File model.json" in result.output
assert "File weights.h5" in result.output
def test_pull_custom_run(runner, request_mocker, query_project, download_url):
query_project(request_mocker)
download_url(request_mocker)
with runner.isolated_filesystem():
result = runner.invoke(cli.pull, ['test/test'])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Downloading: test/test" in result.output
def test_pull_empty_run(runner, request_mocker, query_empty_project, download_url):
query_empty_project(request_mocker)
result = runner.invoke(cli.pull, ['test/test'])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 1
assert "Run has no files" in result.output
def test_projects(runner, request_mocker, query_projects):
query_projects(request_mocker)
result = runner.invoke(cli.projects)
assert result.exit_code == 0
assert "test_2 - Test model" in result.output
def test_status(runner, request_mocker, query_project):
with runner.isolated_filesystem():
query_project(request_mocker)
result = runner.invoke(cli.status, ["-p", "foo"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "/latest" in result.output
def test_status_project_and_run(runner, request_mocker, query_project):
query_project(request_mocker)
result = runner.invoke(cli.status, ["test/awesome"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "test/awesome" in result.output
def test_no_project_bad_command(runner):
result = runner.invoke(cli.cli, ["fsd"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert "No such command" in result.output
assert result.exit_code == 2
@pytest.mark.skip('restore functionality broken for now, fixes coming...')
def test_restore(runner, request_mocker, query_run, monkeypatch):
mock = query_run(request_mocker)
with runner.isolated_filesystem():
os.mkdir("wandb")
repo = git_repo()
with open("patch.txt", "w") as f:
f.write("test")
repo.repo.index.add(["patch.txt"])
repo.repo.commit()
monkeypatch.setattr(cli, 'api', Api({'project': 'test'}))
result = runner.invoke(cli.restore, ["test/abcdef"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Created branch wandb/abcdef" in result.output
assert "Applied patch" in result.output
assert "Restored config variables" in result.output
def test_projects_error(runner, request_mocker, query_projects):
query_projects(request_mocker, status_code=400)
result = runner.invoke(cli.projects)
assert result.exit_code == 1
print(result.output)
assert "Error" in result.output
def test_init_new_login(runner, empty_netrc, local_netrc, request_mocker, query_projects, query_viewer):
query_viewer(request_mocker)
query_projects(request_mocker)
with runner.isolated_filesystem():
# If the test was run from a directory containing .wandb, then __stage_dir__
# was '.wandb' when imported by api.py, reload to fix. UGH!
reload(wandb)
result = runner.invoke(cli.init, input="12345\nvanpelt")
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
with open("netrc", "r") as f:
generatedNetrc = f.read()
with open("wandb/settings", "r") as f:
generatedWandb = f.read()
assert "12345" in generatedNetrc
assert "test_model" in generatedWandb
def test_init_reinit(runner, empty_netrc, local_netrc, request_mocker, query_projects, query_viewer):
query_viewer(request_mocker)
query_projects(request_mocker)
with runner.isolated_filesystem():
os.mkdir('wandb')
result = runner.invoke(cli.init, input="12345\nvanpelt\ny")
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
with open("netrc", "r") as f:
generatedNetrc = f.read()
with open("wandb/settings", "r") as f:
generatedWandb = f.read()
assert "12345" in generatedNetrc
assert "test_model" in generatedWandb
def test_init_add_login(runner, empty_netrc, local_netrc, request_mocker, query_projects, query_viewer):
query_viewer(request_mocker)
query_projects(request_mocker)
with runner.isolated_filesystem():
with open("netrc", "w") as f:
f.write("previous config")
result = runner.invoke(cli.init, input="12345\nvanpelt\n")
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
with open("netrc", "r") as f:
generatedNetrc = f.read()
with open("wandb/settings", "r") as f:
generatedWandb = f.read()
assert "12345" in generatedNetrc
assert "previous config" in generatedNetrc
def test_init_existing_login(runner, local_netrc, request_mocker, query_projects, query_viewer):
query_viewer(request_mocker)
query_projects(request_mocker)
with runner.isolated_filesystem():
with open("netrc", "w") as f:
f.write("machine api.wandb.ai\n\ttest\t12345")
result = runner.invoke(cli.init, input="vanpelt\n")
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
with open("wandb/settings", "r") as f:
generatedWandb = f.read()
assert "test_model" in generatedWandb
assert "This directory is configured" in result.output