Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_cli.py 36 KB

You have to be logged in to leave a comment. Sign In
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
  1. import contextlib
  2. import datetime
  3. import pytest
  4. import os
  5. import traceback
  6. import click
  7. from wandb import __version__
  8. from wandb.apis import InternalApi
  9. from wandb import cli, env
  10. from wandb import util
  11. from .utils import runner, git_repo
  12. from .api_mocks import *
  13. import netrc
  14. import signal
  15. import time
  16. import six
  17. import time
  18. import yaml
  19. import git
  20. import re
  21. import shutil
  22. import webbrowser
  23. import wandb
  24. import threading
  25. import subprocess
  26. import platform
  27. DUMMY_API_KEY = '1824812581259009ca9981580f8f8a9012409eee'
  28. try:
  29. # python 3.4+
  30. from importlib import reload
  31. except ImportError:
  32. # python 3.2, 3.3
  33. from imp import reload
  34. except ImportError:
  35. pass
  36. @pytest.fixture
  37. def empty_netrc(monkeypatch):
  38. class FakeNet(object):
  39. @property
  40. def hosts(self):
  41. return {'api.wandb.ai': None}
  42. monkeypatch.setattr(netrc, "netrc", lambda *args: FakeNet())
  43. @contextlib.contextmanager
  44. def config_dir():
  45. try:
  46. os.environ["WANDB_CONFIG"] = os.getcwd()
  47. yield
  48. finally:
  49. del os.environ["WANDB_CONFIG"]
  50. def setup_module(module):
  51. os.environ["WANDB_TEST"] = "true"
  52. def teardown_module(module):
  53. del os.environ["WANDB_TEST"]
  54. def test_help(runner):
  55. result = runner.invoke(cli.cli)
  56. assert result.exit_code == 0
  57. assert 'Weights & Biases' in result.output
  58. help_result = runner.invoke(cli.cli, ['--help'])
  59. assert help_result.exit_code == 0
  60. assert 'Show this message and exit.' in help_result.output
  61. def test_version(runner):
  62. result = runner.invoke(cli.cli, ["--version"])
  63. assert result.exit_code == 0
  64. assert __version__ in result.output
  65. @pytest.mark.skip(reason='config reworked, fixes coming...')
  66. def test_config(runner, monkeypatch):
  67. with runner.isolated_filesystem():
  68. result = runner.invoke(cli.config, ["init"])
  69. print(result.output)
  70. print(result.exception)
  71. print(traceback.print_tb(result.exc_info[2]))
  72. assert "wandb config set" in result.output
  73. assert os.path.exists("config-defaults.yaml")
  74. @pytest.mark.skip(reason='config reworked, fixes coming...')
  75. def test_config_show(runner, monkeypatch):
  76. with runner.isolated_filesystem():
  77. with open("config-defaults.yaml", "w") as f:
  78. f.write(yaml.dump(
  79. {'val': {'value': 'awesome', 'desc': 'cool'}, 'bad': {'value': 'shit'}}))
  80. result_py = runner.invoke(cli.config, ["show"])
  81. result_yml = runner.invoke(cli.config, ["show", "--format", "yaml"])
  82. result_json = runner.invoke(cli.config, ["show", "--format", "json"])
  83. print(result_py.output)
  84. print(result_py.exception)
  85. print(traceback.print_tb(result_py.exc_info[2]))
  86. assert "awesome" in result_py.output
  87. assert "awesome" in result_yml.output
  88. assert "awesome" in result_json.output
  89. @pytest.mark.skip(reason='config reworked, fixes coming...')
  90. def test_config_show_empty(runner, monkeypatch):
  91. with runner.isolated_filesystem():
  92. result = runner.invoke(cli.config, ["show"])
  93. print(result.output)
  94. print(result.exception)
  95. print(traceback.print_tb(result.exc_info[2]))
  96. assert "No configuration" in result.output
  97. @pytest.mark.skip(reason='config reworked, fixes coming...')
  98. def test_config_set(runner):
  99. with runner.isolated_filesystem():
  100. runner.invoke(cli.config, ["init"])
  101. result = runner.invoke(cli.config, ["set", "foo=bar"])
  102. print(result.output)
  103. print(result.exception)
  104. print(traceback.print_tb(result.exc_info[2]))
  105. assert "foo='bar'" in result.output
  106. @pytest.mark.skip(reason='config reworked, fixes coming...')
  107. def test_config_del(runner):
  108. with runner.isolated_filesystem():
  109. with open("config-defaults.yaml", "w") as f:
  110. f.write(yaml.dump(
  111. {'val': {'value': 'awesome', 'desc': 'cool'}, 'bad': {'value': 'shit'}}))
  112. result = runner.invoke(cli.config, ["del", "bad"])
  113. print(result.output)
  114. print(result.exception)
  115. print(traceback.print_tb(result.exc_info[2]))
  116. assert "1 parameters changed" in result.output
  117. def test_pull(runner, request_mocker, query_project, download_url):
  118. query_project(request_mocker)
  119. download_url(request_mocker)
  120. with runner.isolated_filesystem():
  121. result = runner.invoke(cli.pull, ['test', '--project', 'test'])
  122. print(result.output)
  123. print(result.exception)
  124. print(traceback.print_tb(result.exc_info[2]))
  125. assert result.exit_code == 0
  126. assert "Downloading: test/test" in result.output
  127. assert os.path.isfile("weights.h5")
  128. assert "File model.json" in result.output
  129. assert "File weights.h5" in result.output
  130. def test_pull_custom_run(runner, request_mocker, query_project, download_url):
  131. query_project(request_mocker)
  132. download_url(request_mocker)
  133. with runner.isolated_filesystem():
  134. result = runner.invoke(cli.pull, ['test/test'])
  135. print(result.output)
  136. print(result.exception)
  137. print(traceback.print_tb(result.exc_info[2]))
  138. assert result.exit_code == 0
  139. assert "Downloading: test/test" in result.output
  140. def test_pull_empty_run(runner, request_mocker, query_empty_project, download_url):
  141. query_empty_project(request_mocker)
  142. result = runner.invoke(cli.pull, ['test/test'])
  143. print(result.output)
  144. print(result.exception)
  145. print(traceback.print_tb(result.exc_info[2]))
  146. assert result.exit_code == 1
  147. assert "Run has no files" in result.output
  148. def test_projects(runner, request_mocker, query_projects):
  149. query_projects(request_mocker)
  150. result = runner.invoke(cli.projects)
  151. assert result.exit_code == 0
  152. assert "test_2 - Test model" in result.output
  153. def test_status(runner, request_mocker, query_project):
  154. with runner.isolated_filesystem():
  155. os.mkdir("wandb")
  156. query_project(request_mocker)
  157. result = runner.invoke(cli.status, ["-p", "foo"])
  158. print(result.output)
  159. print(result.exception)
  160. print(traceback.print_tb(result.exc_info[2]))
  161. assert result.exit_code == 0
  162. assert "latest" in result.output
  163. @pytest.mark.skip(reason='currently we dont parse entity/project')
  164. def test_status_project_and_run(runner, request_mocker, query_project):
  165. query_project(request_mocker)
  166. result = runner.invoke(cli.status, ["test/awesome"])
  167. print(result.output)
  168. print(result.exception)
  169. print(traceback.print_tb(result.exc_info[2]))
  170. assert result.exit_code == 0
  171. assert "test/awesome" in result.output
  172. def test_no_project_bad_command(runner):
  173. result = runner.invoke(cli.cli, ["fsd"])
  174. print(result.output)
  175. print(result.exception)
  176. print(traceback.print_tb(result.exc_info[2]))
  177. assert "No such command" in result.output
  178. assert result.exit_code == 2
  179. def test_restore_no_remote(runner, request_mocker, query_run, git_repo, docker, monkeypatch):
  180. # git_repo creates it's own isolated filesystem
  181. mock = query_run(request_mocker)
  182. with open("patch.txt", "w") as f:
  183. f.write("test")
  184. git_repo.repo.index.add(["patch.txt"])
  185. git_repo.repo.commit()
  186. monkeypatch.setattr(cli, 'api', InternalApi({'project': 'test'}))
  187. result = runner.invoke(cli.restore, ["wandb/test:abcdef"])
  188. print(result.output)
  189. print(traceback.print_tb(result.exc_info[2]))
  190. assert result.exit_code == 0
  191. assert "Created branch wandb/abcdef" in result.output
  192. assert "Applied patch" in result.output
  193. assert "Restored config variables to wandb" + os.sep in result.output
  194. assert "Launching docker container" in result.output
  195. docker.assert_called_with(['docker', 'run', '-e', 'LANG=C.UTF-8', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123', '--ipc=host', '-v',
  196. wandb.docker.entrypoint+':/wandb-entrypoint.sh', '--entrypoint', '/wandb-entrypoint.sh', '-v', os.getcwd()+':/app', '-w', '/app', '-e',
  197. 'WANDB_API_KEY=test', '-e', 'WANDB_COMMAND=python train.py --test foo', '-it', 'test/docker', '/bin/bash'])
  198. def test_restore_bad_remote(runner, request_mocker, query_run, git_repo, docker, monkeypatch):
  199. # git_repo creates it's own isolated filesystem
  200. mock = query_run(request_mocker, {"git": {"repo": "http://fake.git/foo/bar"}})
  201. api = InternalApi({'project': 'test'})
  202. monkeypatch.setattr(cli, 'api', api)
  203. def bad_commit(cmt):
  204. raise ValueError()
  205. monkeypatch.setattr(api.git.repo, 'commit', bad_commit)
  206. monkeypatch.setattr(api, "download_urls", lambda *args, **kwargs: [])
  207. result = runner.invoke(cli.restore, ["wandb/test:abcdef"])
  208. print(result.output)
  209. print(traceback.print_tb(result.exc_info[2]))
  210. assert result.exit_code == 1
  211. assert "Run `git clone http://fake.git/foo/bar`" in result.output
  212. def test_restore_good_remote(runner, request_mocker, query_run, git_repo, docker, monkeypatch):
  213. # git_repo creates it's own isolated filesystem
  214. git_repo.repo.create_remote('origin', "git@fake.git:foo/bar")
  215. monkeypatch.setattr(subprocess, 'check_call', lambda command: True)
  216. mock = query_run(request_mocker, {"git": {"repo": "http://fake.git/foo/bar"}})
  217. monkeypatch.setattr(cli, 'api', InternalApi({'project': 'test'}))
  218. result = runner.invoke(cli.restore, ["wandb/test:abcdef"])
  219. print(result.output)
  220. print(traceback.print_tb(result.exc_info[2]))
  221. assert result.exit_code == 0
  222. assert "Created branch wandb/abcdef" in result.output
  223. def test_restore_no_git(runner, request_mocker, query_run, git_repo, docker, monkeypatch):
  224. # git_repo creates it's own isolated filesystem
  225. mock = query_run(request_mocker, {"git": {"repo": "http://fake.git/foo/bar"}})
  226. monkeypatch.setattr(cli, 'api', InternalApi({'project': 'test'}))
  227. result = runner.invoke(cli.restore, ["wandb/test:abcdef", "--no-git"])
  228. print(result.output)
  229. print(traceback.print_tb(result.exc_info[2]))
  230. assert result.exit_code == 0
  231. assert "Restored config variables" in result.output
  232. def test_restore_slashes(runner, request_mocker, query_run, git_repo, docker, monkeypatch):
  233. # git_repo creates it's own isolated filesystem
  234. mock = query_run(request_mocker, {"git": {"repo": "http://fake.git/foo/bar"}})
  235. monkeypatch.setattr(cli, 'api', InternalApi({'project': 'test'}))
  236. result = runner.invoke(cli.restore, ["wandb/test/abcdef", "--no-git"])
  237. print(result.output)
  238. print(traceback.print_tb(result.exc_info[2]))
  239. assert result.exit_code == 0
  240. assert "Restored config variables" in result.output
  241. def test_restore_no_entity(runner, request_mocker, query_run, git_repo, docker, monkeypatch):
  242. # git_repo creates it's own isolated filesystem
  243. mock = query_run(request_mocker, {"git": {"repo": "http://fake.git/foo/bar"}})
  244. monkeypatch.setattr(cli, 'api', InternalApi({'project': 'test'}))
  245. result = runner.invoke(cli.restore, ["test/abcdef", "--no-git"])
  246. print(result.output)
  247. print(traceback.print_tb(result.exc_info[2]))
  248. assert result.exit_code == 0
  249. assert "Restored config variables" in result.output
  250. def test_restore_not_git(runner, request_mocker, query_run, docker, monkeypatch):
  251. # git_repo creates it's own isolated filesystem
  252. with runner.isolated_filesystem():
  253. mock = query_run(request_mocker)
  254. monkeypatch.setattr(cli, 'api', InternalApi({'project': 'test'}))
  255. result = runner.invoke(cli.restore, ["test/abcdef"])
  256. print(result.output)
  257. print(traceback.print_tb(result.exc_info[2]))
  258. assert result.exit_code == 0
  259. assert "Original run has no git history" in result.output
  260. @pytest.fixture
  261. def docker(request_mocker, query_run, mocker, monkeypatch):
  262. mock = query_run(request_mocker)
  263. docker = mocker.MagicMock()
  264. api_key = mocker.patch('wandb.apis.InternalApi.api_key', new_callable=mocker.PropertyMock)
  265. api_key.return_value = "test"
  266. api = InternalApi({'project': 'test'})
  267. monkeypatch.setattr(cli, 'find_executable', lambda name: True)
  268. monkeypatch.setattr(cli, 'api', api)
  269. old_call = subprocess.call
  270. def new_call(command, **kwargs):
  271. if command[0] == "docker":
  272. return docker(command)
  273. else:
  274. return old_call(command, **kwargs)
  275. monkeypatch.setattr(subprocess, 'call', new_call)
  276. monkeypatch.setattr(subprocess, 'check_output',
  277. lambda *args, **kwargs: b"wandb/deepo@sha256:abc123")
  278. return docker
  279. @pytest.fixture
  280. def no_tty(mocker):
  281. with mocker.patch("wandb.sys.stdin") as stdin_mock:
  282. stdin_mock.isatty.return_value = False
  283. yield
  284. def test_docker_run_digest(runner, docker, monkeypatch):
  285. runner.invoke(cli.docker_run, ["wandb/deepo@sha256:3ddd2547d83a056804cac6aac48d46c5394a76df76b672539c4d2476eba38177"])
  286. docker.assert_called_once_with(['docker', 'run', '-e', 'WANDB_API_KEY=test', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:3ddd2547d83a056804cac6aac48d46c5394a76df76b672539c4d2476eba38177', '--runtime', 'nvidia', 'wandb/deepo@sha256:3ddd2547d83a056804cac6aac48d46c5394a76df76b672539c4d2476eba38177'])
  287. def test_docker_run_bad_image(runner, docker, monkeypatch):
  288. runner.invoke(cli.docker_run, ["wandb///foo$"])
  289. docker.assert_called_once_with(['docker', 'run', '-e', 'WANDB_API_KEY=test', '--runtime', 'nvidia', "wandb///foo$"])
  290. def test_docker_run_no_nvidia(runner, docker, monkeypatch):
  291. monkeypatch.setattr(cli, 'find_executable', lambda name: False)
  292. runner.invoke(cli.docker_run, ["run", "-v", "cool:/cool", "rad"])
  293. docker.assert_called_once_with(['docker', 'run', '-e', 'WANDB_API_KEY=test', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123', '-v', 'cool:/cool', 'rad'])
  294. def test_docker_run_nvidia(runner, docker):
  295. runner.invoke(cli.docker_run, ["run", "-v", "cool:/cool", "rad", "/bin/bash", "cool"])
  296. docker.assert_called_once_with(['docker', 'run', '-e', 'WANDB_API_KEY=test', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123',
  297. '--runtime', 'nvidia', '-v', 'cool:/cool', 'rad', '/bin/bash', 'cool'])
  298. def test_docker(runner, docker):
  299. with runner.isolated_filesystem():
  300. result = runner.invoke(cli.docker, ["test"])
  301. print(result.output)
  302. print(traceback.print_tb(result.exc_info[2]))
  303. docker.assert_called_once_with(['docker', 'run', '-e', 'LANG=C.UTF-8', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123', '--ipc=host', '-v',
  304. wandb.docker.entrypoint+':/wandb-entrypoint.sh', '--entrypoint', '/wandb-entrypoint.sh', '-v',
  305. os.getcwd()+':/app', '-w', '/app', '-e', 'WANDB_API_KEY=test', '-it', 'test', '/bin/bash'])
  306. assert result.exit_code == 0
  307. def test_docker_basic(runner, docker, git_repo):
  308. result = runner.invoke(cli.docker, ["test:abc123"])
  309. print(result.output)
  310. print(traceback.print_tb(result.exc_info[2]))
  311. assert "Launching docker container" in result.output
  312. docker.assert_called_once_with(['docker', 'run', '-e', 'LANG=C.UTF-8', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123', '--ipc=host', '-v',
  313. wandb.docker.entrypoint+':/wandb-entrypoint.sh', '--entrypoint', '/wandb-entrypoint.sh', '-v',
  314. os.getcwd()+':/app', '-w', '/app', '-e', 'WANDB_API_KEY=test', '-it', 'test:abc123', '/bin/bash'])
  315. assert result.exit_code == 0
  316. def test_docker_sha(runner, docker):
  317. result = runner.invoke(cli.docker, ["test@sha256:abc123"])
  318. print(result.output)
  319. print(traceback.print_tb(result.exc_info[2]))
  320. docker.assert_called_once_with(['docker', 'run', '-e', 'LANG=C.UTF-8', '-e', 'WANDB_DOCKER=test@sha256:abc123', '--ipc=host', '-v',
  321. wandb.docker.entrypoint+':/wandb-entrypoint.sh', '--entrypoint', '/wandb-entrypoint.sh', '-v', os.getcwd()+':/app', '-w', '/app', '-e',
  322. 'WANDB_API_KEY=test', '-it', 'test@sha256:abc123', '/bin/bash'])
  323. assert result.exit_code == 0
  324. def test_docker_no_dir(runner, docker):
  325. result = runner.invoke(cli.docker, ["test:abc123", "--no-dir"])
  326. print(result.output)
  327. print(traceback.print_tb(result.exc_info[2]))
  328. docker.assert_called_once_with(['docker', 'run', '-e', 'LANG=C.UTF-8', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123', '--ipc=host', '-v',
  329. wandb.docker.entrypoint+':/wandb-entrypoint.sh', '--entrypoint', '/wandb-entrypoint.sh', '-e', 'WANDB_API_KEY=test', '-it', 'test:abc123', '/bin/bash'])
  330. assert result.exit_code == 0
  331. def test_docker_no_interactive_custom_command(runner, docker, git_repo):
  332. result = runner.invoke(cli.docker, ["test:abc123", "--no-tty", "--cmd", "python foo.py"])
  333. print(result.output)
  334. print(traceback.print_tb(result.exc_info[2]))
  335. docker.assert_called_once_with(['docker', 'run', '-e', 'LANG=C.UTF-8', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123', '--ipc=host', '-v', wandb.docker.entrypoint+':/wandb-entrypoint.sh',
  336. '--entrypoint', '/wandb-entrypoint.sh', '-v', os.getcwd()+':/app', '-w', '/app', '-e', 'WANDB_API_KEY=test', 'test:abc123', '/bin/bash', '-c', 'python foo.py'])
  337. assert result.exit_code == 0
  338. def test_docker_jupyter(runner, docker):
  339. with runner.isolated_filesystem():
  340. result = runner.invoke(cli.docker, ["test", "--jupyter"])
  341. print(result.output)
  342. print(traceback.print_tb(result.exc_info[2]))
  343. docker.assert_called_once_with(['docker', 'run', '-e', 'LANG=C.UTF-8', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123', '--ipc=host', '-v', wandb.docker.entrypoint+':/wandb-entrypoint.sh',
  344. '--entrypoint', '/wandb-entrypoint.sh', '-v', os.getcwd()+':/app', '-w', '/app', '-e', 'WANDB_API_KEY=test', '-e', 'WANDB_ENSURE_JUPYTER=1', '-p', '8888:8888',
  345. 'test', '/bin/bash', '-c', 'jupyter lab --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token= --notebook-dir /app'])
  346. assert result.exit_code == 0
  347. def test_docker_args(runner, docker):
  348. with runner.isolated_filesystem():
  349. result = runner.invoke(cli.docker, ["test", "-v", "/tmp:/tmp"])
  350. print(result.output)
  351. print(traceback.print_tb(result.exc_info[2]))
  352. docker.assert_called_with(['docker', 'run', '-e', 'LANG=C.UTF-8', '-e', 'WANDB_DOCKER=wandb/deepo@sha256:abc123', '--ipc=host', '-v', wandb.docker.entrypoint+':/wandb-entrypoint.sh',
  353. '--entrypoint', '/wandb-entrypoint.sh', '-v', os.getcwd()+':/app', '-w', '/app', '-e', 'WANDB_API_KEY=test', 'test', '-v', '/tmp:/tmp', '-it', 'wandb/deepo:all-cpu', '/bin/bash'])
  354. assert result.exit_code == 0
  355. def test_docker_digest(runner, docker):
  356. with runner.isolated_filesystem():
  357. result = runner.invoke(cli.docker, ["test", "--digest"])
  358. print(result.output)
  359. print(traceback.print_tb(result.exc_info[2]))
  360. assert result.output == "wandb/deepo@sha256:abc123"
  361. assert result.exit_code == 0
  362. def test_projects_error(runner, request_mocker, query_projects):
  363. query_projects(request_mocker, status_code=400)
  364. # Ugly, reach in to APIs request Retry object and tell it to only
  365. # retry for 50us
  366. cli.api.gql._retry_timedelta = datetime.timedelta(0, 0, 50)
  367. result = runner.invoke(cli.projects)
  368. print(result.exception)
  369. print(result.output)
  370. print(traceback.print_tb(result.exc_info[2]))
  371. assert result.exit_code == 1
  372. assert "Error" in result.output
  373. def test_login_key_arg(runner, empty_netrc, local_netrc):
  374. with runner.isolated_filesystem():
  375. # If the test was run from a directory containing .wandb, then __stage_dir__
  376. # was '.wandb' when imported by api.py, reload to fix. UGH!
  377. reload(wandb)
  378. result = runner.invoke(cli.login, [DUMMY_API_KEY])
  379. print('Output: ', result.output)
  380. print('Exception: ', result.exception)
  381. print('Traceback: ', traceback.print_tb(result.exc_info[2]))
  382. assert result.exit_code == 0
  383. with open("netrc", "r") as f:
  384. generatedNetrc = f.read()
  385. assert DUMMY_API_KEY in generatedNetrc
  386. def test_login_anonymously(runner, monkeypatch, empty_netrc, local_netrc):
  387. with runner.isolated_filesystem():
  388. api = InternalApi()
  389. monkeypatch.setattr(cli, 'api', api)
  390. monkeypatch.setattr(api, 'create_anonymous_api_key', lambda *args, **kwargs: DUMMY_API_KEY)
  391. result = runner.invoke(cli.login, ['--anonymously'])
  392. print('Output: ', result.output)
  393. print('Exception: ', result.exception)
  394. print('Traceback: ', traceback.print_tb(result.exc_info[2]))
  395. assert result.exit_code == 0
  396. with open("netrc", "r") as f:
  397. generated_netrc = f.read()
  398. assert DUMMY_API_KEY in generated_netrc
  399. def test_login_abort(runner, empty_netrc, local_netrc, mocker, monkeypatch):
  400. with runner.isolated_filesystem():
  401. reload(wandb)
  402. def prompt(*args, **kwargs):
  403. raise click.exceptions.Abort()
  404. mocker.patch("click.prompt", prompt)
  405. result = runner.invoke(cli.login)
  406. print('Output: ', result.output)
  407. print('Exception: ', result.exception)
  408. print('Traceback: ', traceback.print_tb(result.exc_info[2]))
  409. assert result.exit_code == 0
  410. assert "Disabling Weights & Biases. Run 'wandb login' again to re-enable" in result.output
  411. def test_signup(runner, empty_netrc, local_netrc, mocker, monkeypatch):
  412. with runner.isolated_filesystem():
  413. # If the test was run from a directory containing .wandb, then __stage_dir__
  414. # was '.wandb' when imported by api.py, reload to fix. UGH!
  415. reload(wandb)
  416. def prompt(*args, **kwargs):
  417. #raise click.exceptions.Abort()
  418. return DUMMY_API_KEY
  419. mocker.patch("wandb.util.prompt_api_key", prompt)
  420. result = runner.invoke(cli.login)
  421. print('Output: ', result.output)
  422. print('Exception: ', result.exception)
  423. print('Traceback: ', traceback.print_tb(result.exc_info[2]))
  424. assert result.exit_code == 0
  425. assert "Successfully logged in to Weights & Biases!" in result.output
  426. def test_init_new_login_no_browser(runner, empty_netrc, local_netrc, request_mocker, query_projects, query_viewer, monkeypatch):
  427. mock = query_projects(request_mocker)
  428. query_viewer(request_mocker)
  429. monkeypatch.setattr(webbrowser, 'open_new_tab', lambda x: False)
  430. with runner.isolated_filesystem():
  431. # If the test was run from a directory containing .wandb, then __stage_dir__
  432. # was '.wandb' when imported by api.py, reload to fix. UGH!
  433. reload(wandb)
  434. login_result = runner.invoke(cli.login, [DUMMY_API_KEY])
  435. init_result = runner.invoke(cli.init, input="y\n\n\n")
  436. print('Output: ', init_result.output)
  437. print('Exception: ', init_result.exception)
  438. print('Traceback: ', traceback.print_tb(init_result.exc_info[2]))
  439. assert mock.called
  440. assert login_result.exit_code == 0
  441. assert init_result.exit_code == 0
  442. with open("netrc", "r") as f:
  443. generatedNetrc = f.read()
  444. with open("wandb/settings", "r") as f:
  445. generatedWandb = f.read()
  446. assert DUMMY_API_KEY in generatedNetrc
  447. assert "test_model" in generatedWandb
  448. assert "Successfully logged in" in login_result.output
  449. assert "This directory is configured!" in init_result.output
  450. @pytest.mark.teams("foo", "bar")
  451. def test_init_multi_team(runner, empty_netrc, local_netrc, request_mocker, query_projects, query_viewer):
  452. mock = query_projects(request_mocker)
  453. query_viewer(request_mocker)
  454. with runner.isolated_filesystem():
  455. # If the test was run from a directory containing .wandb, then __stage_dir__
  456. # was '.wandb' when imported by api.py, reload to fix. UGH!
  457. reload(wandb)
  458. login_result = runner.invoke(cli.login, [DUMMY_API_KEY])
  459. init_result = runner.invoke(cli.init, input="y\nvanpelt\n")
  460. print('Output: ', init_result.output)
  461. print('Exception: ', init_result.exception)
  462. print('Traceback: ', traceback.print_tb(init_result.exc_info[2]))
  463. assert mock.called
  464. assert login_result.exit_code == 0
  465. assert init_result.exit_code == 0
  466. with open("netrc", "r") as f:
  467. generatedNetrc = f.read()
  468. with open("wandb/settings", "r") as f:
  469. generatedWandb = f.read()
  470. assert DUMMY_API_KEY in generatedNetrc
  471. assert "test_model" in generatedWandb
  472. def test_init_reinit(runner, empty_netrc, local_netrc, request_mocker, query_projects, query_viewer):
  473. query_viewer(request_mocker)
  474. query_projects(request_mocker)
  475. with runner.isolated_filesystem():
  476. runner.invoke(cli.login, [DUMMY_API_KEY])
  477. result = runner.invoke(cli.init, input="y\nvanpelt\n")
  478. print(result.output)
  479. print(result.exception)
  480. print(traceback.print_tb(result.exc_info[2]))
  481. assert result.exit_code == 0
  482. with open("netrc", "r") as f:
  483. generatedNetrc = f.read()
  484. with open("wandb/settings", "r") as f:
  485. generatedWandb = f.read()
  486. assert DUMMY_API_KEY in generatedNetrc
  487. assert "test_model" in generatedWandb
  488. def test_init_add_login(runner, empty_netrc, local_netrc, request_mocker, query_projects, query_viewer):
  489. query_viewer(request_mocker)
  490. query_projects(request_mocker)
  491. with runner.isolated_filesystem():
  492. with config_dir():
  493. with open("netrc", "w") as f:
  494. f.write("previous config")
  495. runner.invoke(cli.login, [DUMMY_API_KEY])
  496. result = runner.invoke(cli.init, input="y\n%s\nvanpelt\n" % DUMMY_API_KEY)
  497. print(result.output)
  498. print(result.exception)
  499. print(traceback.print_tb(result.exc_info[2]))
  500. assert result.exit_code == 0
  501. with open("netrc", "r") as f:
  502. generatedNetrc = f.read()
  503. with open("wandb/settings", "r") as f:
  504. generatedWandb = f.read()
  505. assert DUMMY_API_KEY in generatedNetrc
  506. assert "previous config" in generatedNetrc
  507. def test_init_existing_login(runner, local_netrc, request_mocker, query_projects, query_viewer):
  508. query_viewer(request_mocker)
  509. query_projects(request_mocker)
  510. with runner.isolated_filesystem():
  511. with open("netrc", "w") as f:
  512. f.write("machine api.wandb.ai\n\tlogin test\tpassword 12345")
  513. result = runner.invoke(cli.init, input="vanpelt\n")
  514. print(result.output)
  515. print(result.exception)
  516. print(traceback.print_tb(result.exc_info[2]))
  517. assert result.exit_code == 0
  518. with open("wandb/settings", "r") as f:
  519. generatedWandb = f.read()
  520. assert "test_model" in generatedWandb
  521. assert "This directory is configured" in result.output
  522. def test_run_with_error(runner, request_mocker, upsert_run, git_repo, query_viewer, no_tty):
  523. upsert_run(request_mocker)
  524. query_viewer(request_mocker)
  525. runner.invoke(cli.off)
  526. result = runner.invoke(cli.run, ["python", "missing.py"])
  527. print(result.output)
  528. print(result.exception)
  529. print(traceback.print_tb(result.exc_info[2]))
  530. output = result.output.encode("utf8")
  531. assert "not found" in str(output) or "No such file" in str(output)
  532. # TODO: there's a race between the sigint and the actual failure so exit_code could be 1 or 255
  533. assert result.exit_code > 0
  534. @pytest.mark.updateAvailable(True)
  535. def test_run_update(runner, request_mocker, upsert_run, git_repo, upload_logs):
  536. upload_logs(request_mocker, "abc123")
  537. upsert_run(request_mocker)
  538. runner.invoke(cli.off)
  539. with open("simple.py", "w") as f:
  540. f.write('print("Done!")')
  541. result = runner.invoke(cli.run, ["--id=abc123", "--", "simple.py"])
  542. print(result.output)
  543. print(result.exception)
  544. print(traceback.print_tb(result.exc_info[2]))
  545. def test_enable_on(runner, git_repo):
  546. with open("wandb/settings", "w") as f:
  547. f.write("[default]\nproject=rad")
  548. result = runner.invoke(cli.on)
  549. print(result.output)
  550. print(result.exception)
  551. print(traceback.print_tb(result.exc_info[2]))
  552. assert "W&B enabled" in str(result.output)
  553. def test_enable_off(runner, git_repo):
  554. with open("wandb/settings", "w") as f:
  555. f.write("[default]\nproject=rad")
  556. result = runner.invoke(cli.off)
  557. print(result.output)
  558. print(result.exception)
  559. print(traceback.print_tb(result.exc_info[2]))
  560. assert "W&B disabled" in str(result.output)
  561. assert "disabled" in open("wandb/settings").read()
  562. def test_sync(runner, git_repo, mock_server):
  563. # Un comment this line when re-recording the cassette
  564. os.environ['WANDB_API_KEY'] = DUMMY_API_KEY
  565. with open("wandb-history.jsonl", "w") as f:
  566. f.write('{"acc":25}')
  567. result = runner.invoke(cli.sync, ["--id", "7ojulnsc", "."])
  568. print(result.output)
  569. print(result.exception)
  570. print(traceback.print_tb(result.exc_info[2]))
  571. assert "Uploading history metrics" in str(result.output)
  572. assert result.exit_code == 0
  573. @pytest.mark.skipif(os.getenv("NO_ML") == "true" or sys.version_info < (3, 5), reason="Tensorboard not installed and we don't support tensorboard syncing in py2")
  574. def test_sync_tensorboard_ignore(runner, git_repo, mock_server):
  575. # Un comment this line when re-recording the cassette
  576. os.environ['WANDB_API_KEY'] = DUMMY_API_KEY
  577. wandb.util.mkdir_exists_ok("logs/train")
  578. wandb.util.mkdir_exists_ok("logs/val")
  579. with open("logs/garbage.txt", "w") as f:
  580. f.write("NOTHING")
  581. tf_events="events.out.tfevents.111.test.localdomain"
  582. shutil.copyfile(os.path.dirname(__file__) + "/fixtures/"+tf_events, "./logs/train/"+tf_events)
  583. shutil.copyfile(os.path.dirname(__file__) + "/fixtures/"+tf_events, "./logs/val/"+tf_events)
  584. result = runner.invoke(cli.sync, ["--id", "abc123", "-e", "vanpelt", "--ignore", "garbage.txt", "logs"], env=os.environ)
  585. print(result.output)
  586. print(result.exception)
  587. print(traceback.print_tb(result.exc_info[2]))
  588. assert "Found tfevents file, converting..." in str(result.output)
  589. assert result.exit_code == 0
  590. @pytest.mark.skipif(os.getenv("NO_ML") == "true" or sys.version_info < (3, 5), reason="Tensorboard not installed and we don't support tensorboard syncing in py2")
  591. def test_sync_tensorboard_single(runner, git_repo, mock_server):
  592. # Un comment this line when re-recording the cassette
  593. os.environ['WANDB_API_KEY'] = DUMMY_API_KEY
  594. wandb.util.mkdir_exists_ok("logs")
  595. tf_events="events.out.tfevents.111.simple.localdomain"
  596. shutil.copyfile(os.path.dirname(__file__) + "/fixtures/"+tf_events, "./logs/"+tf_events)
  597. result = runner.invoke(cli.sync, ["--id", "abc123", "-e", "vanpelt", "logs"], env=os.environ)
  598. print(result.output)
  599. print(result.exception)
  600. print(traceback.print_tb(result.exc_info[2]))
  601. assert "Found tfevents file, converting..." in str(result.output)
  602. assert "WARNING Not logging key \"histo\"" in str(result.output)
  603. assert result.exit_code == 0
  604. print(mock_server.requests["file_stream"][0]["files"]["wandb-history.jsonl"]["content"])
  605. assert len(json.loads(mock_server.requests["file_stream"][0]["files"]["wandb-history.jsonl"]["content"][0]).keys()) == 5
  606. def test_sync_runs(runner, request_mocker, upsert_run, upload_url, upload_logs, query_viewer, git_repo):
  607. os.environ["WANDB_API_KEY"] = "some invalid key"
  608. query_viewer(request_mocker)
  609. upsert_run(request_mocker)
  610. upload_url(request_mocker)
  611. upload_logs(request_mocker, "abc123zz")
  612. upload_logs(request_mocker, "cba321zz")
  613. run_1 = "wandb/run-120199-abc123zz"
  614. run_2 = "wandb/dryrun-120300-cba321zz"
  615. wandb.util.mkdir_exists_ok(run_1)
  616. wandb.util.mkdir_exists_ok(run_2)
  617. with open(run_1 + "/wandb-history.jsonl", "w") as f:
  618. f.write('{"acc":25}')
  619. with open(run_2 + "/wandb-history.jsonl", "w") as f:
  620. f.write('{"acc":25}')
  621. result = runner.invoke(cli.sync, ".")
  622. print(result.output)
  623. print(result.exception)
  624. print(traceback.print_tb(result.exc_info[2]))
  625. found = re.findall(r"Uploading history metrics", str(result.output))
  626. assert len(found) == 2
  627. def test_run_simple(runner, git_repo, mock_server, monkeypatch):
  628. run_id = "abc123"
  629. with open("simple.py", "w") as f:
  630. f.write('print("Done!")')
  631. print(os.getcwd())
  632. monkeypatch.setattr('time.sleep', lambda s: True)
  633. result = runner.invoke(
  634. cli.run, ["--id=%s" % run_id, "python", "simple.py"])
  635. print(result.output)
  636. print(result.exception)
  637. print(traceback.print_tb(result.exc_info[2]))
  638. # This is disabled for now because it hasn't worked for a long time:
  639. #assert "Verifying uploaded files... verified!" in result.output
  640. assert result.exit_code == 0
  641. assert "Syncing run lovely-dawn-32" in result.output
  642. def test_run_ignore_diff(runner, git_repo, mock_server, monkeypatch):
  643. run_id = "abc123"
  644. os.environ["WANDB_IGNORE_GLOBS"] = "*.patch"
  645. with open("simple.py", "w") as f:
  646. f.write('print("Done!")')
  647. with open("README", "w") as f:
  648. f.write("Making it dirty")
  649. print(os.getcwd())
  650. result = runner.invoke(
  651. cli.run, ["--id=%s" % run_id, "python", "simple.py"])
  652. print(result.output)
  653. print(result.exception)
  654. print(traceback.print_tb(result.exc_info[2]))
  655. # This is disabled for now because it hasn't worked for a long time:
  656. #assert "Verifying uploaded files... verified!" in result.output
  657. assert result.exit_code == 0
  658. assert "Syncing run lovely-dawn-32" in result.output
  659. assert 'storage?file=diff.patch' not in mock_server.requests.keys()
  660. wandb.reset_env()
  661. @pytest.mark.skipif(os.getenv("NO_ML") == "true" or platform.system() == "@indows", reason="No PIL in NO_ML, this was failing in windows for some reason")
  662. def test_run_image(runner, git_repo, mock_server):
  663. run_id = "123abc"
  664. with open("image.py", "w") as f:
  665. f.write("""import wandb
  666. import sys
  667. import numpy as np
  668. wandb.init(entity="test", project="test")
  669. wandb.log({"img": [wandb.Image(np.ones((28,28,1)))]})
  670. """)
  671. result = runner.invoke(cli.run, ["--id=%s" % run_id, "python", "image.py"])
  672. print(result.output)
  673. print(result.exception)
  674. #print(traceback.print_tb(result.exc_info[2]))
  675. assert result.exit_code == 0
  676. assert "Syncing run lovely-dawn-32" in result.output
  677. assert "CommError" not in result.output
  678. @pytest.mark.skip("Sweep command is disabled")
  679. def test_sweep_no_config(runner):
  680. result = runner.invoke(cli.sweep, ["missing.yaml"])
  681. print(result.output)
  682. print(result.exception)
  683. print(traceback.print_tb(result.exc_info[2]))
  684. assert "ERROR: Couldn't open sweep file" in result.output
  685. assert result.exit_code == 0
  686. @pytest.mark.skip("Bring the board back")
  687. def test_board_error(runner, git_repo):
  688. result = runner.invoke(cli.board)
  689. print(result.output)
  690. print(result.exception)
  691. print(traceback.print_tb(result.exc_info[2]))
  692. assert result.exit_code == 1
  693. assert "No runs found in this directory" in result.output
  694. @pytest.mark.skip("Bring the board back")
  695. def test_board_bad_dir(runner, mocker):
  696. result = runner.invoke(cli.board, ["--logdir", "non-existent"])
  697. print("F", result.output)
  698. print("E", result.exception)
  699. print(traceback.print_tb(result.exc_info[2]))
  700. assert result.exit_code != 0
  701. assert "Directory does not exist" in str(result.output)
  702. @pytest.mark.skip("Bring the board back")
  703. def test_board_custom_dir(runner, mocker, monkeypatch):
  704. from wandb.board.tests.util import basic_fixture_path
  705. from wandb.board.app.graphql.loader import load
  706. app = mocker.MagicMock()
  707. def create(config, path):
  708. load(path)
  709. return app
  710. monkeypatch.setattr('wandb.board.create_app', create)
  711. result = runner.invoke(cli.board, ["--logdir", basic_fixture_path])
  712. print(result.output)
  713. print(result.exception)
  714. print(traceback.print_tb(result.exc_info[2]))
  715. assert result.exit_code == 0
  716. assert app.run.called
  717. def test_resume_never(runner, request_mocker, upsert_run, query_run_resume_status, git_repo, query_viewer):
  718. query_viewer(request_mocker)
  719. query_run_resume_status(request_mocker)
  720. upsert_run(request_mocker, error=['Bucket with that name already exists'])
  721. # default is --resume="never"
  722. result = runner.invoke(cli.run, ["missing.py"])
  723. print(result.output)
  724. print(result.exception)
  725. print(traceback.print_tb(result.exc_info[2]))
  726. assert "resume='never'" in str(result.output)
  727. assert result.exit_code == 1
  728. def test_resume_must(runner, request_mocker, query_no_run_resume_status, query_viewer, git_repo):
  729. query_no_run_resume_status(request_mocker)
  730. query_viewer(request_mocker)
  731. result = runner.invoke(cli.run, ["--resume=must", "missing.py"])
  732. print(result.output)
  733. print(result.exception)
  734. print(traceback.print_tb(result.exc_info[2]))
  735. assert "resume='must'" in str(result.output)
  736. assert result.exit_code == 1
  737. # TODO: test actual resume
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...