This repository was archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_cli.py
751 lines (665 loc) · 23.9 KB
/
test_cli.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
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
import wandb
from wandb.cli import cli
from wandb.apis.internal import InternalApi
import contextlib
import datetime
import traceback
import platform
import getpass
import pytest
import netrc
import subprocess
import os
DUMMY_API_KEY = "1824812581259009ca9981580f8f8a9012409eee"
DOCKER_SHA = (
"wandb/deepo@sha256:"
"3ddd2547d83a056804cac6aac48d46c5394a76df76b672539c4d2476eba38177"
)
@pytest.fixture
def docker(request, mock_server, mocker, monkeypatch):
wandb_args = {"check_output": b"wandb/deepo@sha256:abc123"}
marker = request.node.get_closest_marker("wandb_args")
if marker:
wandb_args.update(marker.kwargs)
docker = mocker.MagicMock()
api_key = mocker.patch(
"wandb.apis.InternalApi.api_key", new_callable=mocker.PropertyMock
)
api_key.return_value = "test"
monkeypatch.setattr(cli, "find_executable", lambda name: True)
old_call = subprocess.call
def new_call(command, **kwargs):
if command[0] == "docker":
return docker(command)
else:
return old_call(command, **kwargs)
monkeypatch.setattr(subprocess, "call", new_call)
monkeypatch.setattr(
subprocess, "check_output", lambda *args, **kwargs: wandb_args["check_output"]
)
return docker
@pytest.fixture
def no_tty(mocker):
with mocker.patch("wandb.sys.stdin") as stdin_mock:
stdin_mock.isatty.return_value = False
yield
@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())
@contextlib.contextmanager
def config_dir():
try:
os.environ["WANDB_CONFIG"] = os.getcwd()
yield
finally:
del os.environ["WANDB_CONFIG"]
def test_init_reinit(runner, empty_netrc, local_netrc, mock_server):
with runner.isolated_filesystem():
runner.invoke(cli.login, [DUMMY_API_KEY])
result = runner.invoke(cli.init, input="y\n\n\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 DUMMY_API_KEY in generatedNetrc
assert "mock_server_entity" in generatedWandb
def test_init_add_login(runner, empty_netrc, mock_server):
with runner.isolated_filesystem():
with config_dir():
with open("netrc", "w") as f:
f.write("previous config")
runner.invoke(cli.login, [DUMMY_API_KEY])
result = runner.invoke(cli.init, input="y\n%s\nvanpelt\n" % DUMMY_API_KEY)
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 DUMMY_API_KEY in generatedNetrc
assert "base_url" in generatedWandb
def test_init_existing_login(runner, mock_server):
with runner.isolated_filesystem():
with open("netrc", "w") as f:
f.write("machine api.wandb.ai\n\tlogin test\tpassword 12345")
result = runner.invoke(cli.init, input="vanpelt\nfoo\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 "mock_server_entity" in generatedWandb
assert "This directory is configured" in result.output
@pytest.mark.skip(reason="Currently dont have on in cling")
def test_enable_on(runner, git_repo):
with open("wandb/settings", "w") as f:
f.write("[default]\nproject=rad")
result = runner.invoke(cli.on)
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert "W&B enabled" in str(result.output)
assert result.exit_code == 0
@pytest.mark.skip(reason="Currently dont have off in cling")
def test_enable_off(runner, git_repo):
with open("wandb/settings", "w") as f:
f.write("[default]\nproject=rad")
result = runner.invoke(cli.off)
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert "W&B disabled" in str(result.output)
assert "disabled" in open("wandb/settings").read()
assert result.exit_code == 0
def test_pull(runner, mock_server):
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 weights.h5" 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
def test_login_key_arg(runner, empty_netrc, local_netrc):
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.login, [DUMMY_API_KEY])
print("Output: ", result.output)
print("Exception: ", result.exception)
print("Traceback: ", traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
with open("netrc", "r") as f:
generatedNetrc = f.read()
assert DUMMY_API_KEY in generatedNetrc
@pytest.mark.skip(reason="Just need to make the mocking work correctly")
def test_login_anonymously(runner, monkeypatch, empty_netrc, local_netrc):
with runner.isolated_filesystem():
api = InternalApi()
monkeypatch.setattr(cli, "api", api)
monkeypatch.setattr(
api, "create_anonymous_api_key", lambda *args, **kwargs: DUMMY_API_KEY
)
result = runner.invoke(cli.login, ["--anonymously"])
print("Output: ", result.output)
print("Exception: ", result.exception)
print("Traceback: ", traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
with open("netrc", "r") as f:
generated_netrc = f.read()
assert DUMMY_API_KEY in generated_netrc
def test_artifact_download(runner, git_repo, mock_server):
result = runner.invoke(cli.artifact, ["get", "test/mnist:v0"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Downloading dataset artifact" in result.output
path = os.path.join(".", "artifacts", "mnist:v0")
if platform.system() == "Windows":
head, tail = os.path.splitdrive(path)
path = head + tail.replace(":", "-")
assert "Artifact downloaded to %s" % path in result.output
assert os.path.exists(path)
def test_artifact_upload(runner, git_repo, mock_server, mocker, mocked_run):
with open("artifact.txt", "w") as f:
f.write("My Artifact")
mocker.patch("wandb.init", lambda *args, **kwargs: mocked_run)
result = runner.invoke(cli.artifact, ["put", "artifact.txt", "-n", "test/simple"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Uploading file artifact.txt to:" in result.output
# TODO: one of the tests above is setting entity to y
assert "test/simple:v0" in result.output
def test_artifact_ls(runner, git_repo, mock_server):
result = runner.invoke(cli.artifact, ["ls", "test"])
print(result.output)
print(result.exception)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "9KB" in result.output
assert "mnist:v2" in result.output
def test_docker_run_digest(runner, docker, monkeypatch):
result = runner.invoke(cli.docker_run, [DOCKER_SHA],)
assert result.exit_code == 0
docker.assert_called_once_with(
[
"docker",
"run",
"-e",
"WANDB_API_KEY=test",
"-e",
"WANDB_DOCKER=%s" % DOCKER_SHA,
"--runtime",
"nvidia",
"%s" % DOCKER_SHA,
]
)
def test_docker_run_bad_image(runner, docker, monkeypatch):
result = runner.invoke(cli.docker_run, ["wandb///foo$"])
assert result.exit_code == 0
docker.assert_called_once_with(
[
"docker",
"run",
"-e",
"WANDB_API_KEY=test",
"--runtime",
"nvidia",
"wandb///foo$",
]
)
def test_docker_run_no_nvidia(runner, docker, monkeypatch):
monkeypatch.setattr(cli, "find_executable", lambda name: False)
result = runner.invoke(cli.docker_run, ["run", "-v", "cool:/cool", "rad"])
assert result.exit_code == 0
docker.assert_called_once_with(
[
"docker",
"run",
"-e",
"WANDB_API_KEY=test",
"-e",
"WANDB_DOCKER=wandb/deepo@sha256:abc123",
"-v",
"cool:/cool",
"rad",
]
)
def test_docker_run_nvidia(runner, docker):
result = runner.invoke(
cli.docker_run, ["run", "-v", "cool:/cool", "rad", "/bin/bash", "cool"]
)
assert result.exit_code == 0
docker.assert_called_once_with(
[
"docker",
"run",
"-e",
"WANDB_API_KEY=test",
"-e",
"WANDB_DOCKER=wandb/deepo@sha256:abc123",
"--runtime",
"nvidia",
"-v",
"cool:/cool",
"rad",
"/bin/bash",
"cool",
]
)
def test_docker(runner, docker):
with runner.isolated_filesystem():
result = runner.invoke(cli.docker, ["test"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
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",
"--entrypoint",
"/wandb-entrypoint.sh",
"-v",
os.getcwd() + ":/app",
"-w",
"/app",
"-e",
"WANDB_API_KEY=test",
"-it",
"test",
"/bin/bash",
]
)
assert result.exit_code == 0
def test_docker_basic(runner, docker, git_repo):
result = runner.invoke(cli.docker, ["test:abc123"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
assert "Launching docker container" in result.output
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",
"--entrypoint",
"/wandb-entrypoint.sh",
"-v",
os.getcwd() + ":/app",
"-w",
"/app",
"-e",
"WANDB_API_KEY=test",
"-it",
"test:abc123",
"/bin/bash",
]
)
assert result.exit_code == 0
def test_docker_sha(runner, docker):
result = runner.invoke(cli.docker, ["test@sha256:abc123"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
docker.assert_called_once_with(
[
"docker",
"run",
"-e",
"LANG=C.UTF-8",
"-e",
"WANDB_DOCKER=test@sha256:abc123",
"--ipc=host",
"-v",
wandb.docker.entrypoint + ":/wandb-entrypoint.sh",
"--entrypoint",
"/wandb-entrypoint.sh",
"-v",
os.getcwd() + ":/app",
"-w",
"/app",
"-e",
"WANDB_API_KEY=test",
"-it",
"test@sha256:abc123",
"/bin/bash",
]
)
assert result.exit_code == 0
def test_docker_no_dir(runner, docker):
result = runner.invoke(cli.docker, ["test:abc123", "--no-dir"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
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",
"--entrypoint",
"/wandb-entrypoint.sh",
"-e",
"WANDB_API_KEY=test",
"-it",
"test:abc123",
"/bin/bash",
]
)
assert result.exit_code == 0
def test_docker_no_interactive_custom_command(runner, docker, git_repo):
result = runner.invoke(
cli.docker, ["test:abc123", "--no-tty", "--cmd", "python foo.py"]
)
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
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",
"--entrypoint",
"/wandb-entrypoint.sh",
"-v",
os.getcwd() + ":/app",
"-w",
"/app",
"-e",
"WANDB_API_KEY=test",
"test:abc123",
"/bin/bash",
"-c",
"python foo.py",
]
)
assert result.exit_code == 0
def test_docker_jupyter(runner, docker):
with runner.isolated_filesystem():
result = runner.invoke(cli.docker, ["test", "--jupyter"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
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",
"--entrypoint",
"/wandb-entrypoint.sh",
"-v",
os.getcwd() + ":/app",
"-w",
"/app",
"-e",
"WANDB_API_KEY=test",
"-e",
"WANDB_ENSURE_JUPYTER=1",
"-p",
"8888:8888",
"test",
"/bin/bash",
"-c",
(
"jupyter lab --no-browser --ip=0.0.0.0 --allow-root "
"--NotebookApp.token= --notebook-dir /app"
),
]
)
assert result.exit_code == 0
def test_docker_args(runner, docker):
with runner.isolated_filesystem():
result = runner.invoke(cli.docker, ["test", "-v", "/tmp:/tmp"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
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",
"--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",
]
)
assert result.exit_code == 0
def test_docker_digest(runner, docker):
with runner.isolated_filesystem():
result = runner.invoke(cli.docker, ["test", "--digest"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
assert result.output == "wandb/deepo@sha256:abc123"
assert result.exit_code == 0
@pytest.mark.wandb_args(check_output=b"")
def test_local_default(runner, docker, local_settings):
result = runner.invoke(cli.local)
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
user = getpass.getuser()
docker.assert_called_with(
[
"docker",
"run",
"--rm",
"-v",
"wandb:/vol",
"-p",
"8080:8080",
"--name",
"wandb-local",
"-e",
"LOCAL_USERNAME=%s" % user,
"-d",
"wandb/local",
]
)
@pytest.mark.wandb_args(check_output=b"")
def test_local_custom_port(runner, docker, local_settings):
result = runner.invoke(cli.local, ["-p", "3030"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
user = getpass.getuser()
docker.assert_called_with(
[
"docker",
"run",
"--rm",
"-v",
"wandb:/vol",
"-p",
"3030:8080",
"--name",
"wandb-local",
"-e",
"LOCAL_USERNAME=%s" % user,
"-d",
"wandb/local",
]
)
@pytest.mark.wandb_args(check_output=b"")
def test_local_custom_env(runner, docker, local_settings):
result = runner.invoke(cli.local, ["-e", b"FOO=bar"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
user = getpass.getuser()
docker.assert_called_with(
[
"docker",
"run",
"--rm",
"-v",
"wandb:/vol",
"-p",
"8080:8080",
"--name",
"wandb-local",
"-e",
"LOCAL_USERNAME=%s" % user,
"-e",
"FOO=bar",
"-d",
"wandb/local",
]
)
def test_local_already_running(runner, docker, local_settings):
result = runner.invoke(cli.local)
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
assert "A container named wandb-local is already running" in result.output
def test_restore_no_remote(runner, mock_server, git_repo, docker, monkeypatch):
with open("patch.txt", "w") as f:
f.write("test")
git_repo.repo.index.add(["patch.txt"])
git_repo.repo.commit()
result = runner.invoke(cli.restore, ["wandb/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 to " in result.output
assert "Launching docker container" in result.output
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', '--entrypoint', '/wandb-entrypoint.sh', '-v', os.getcwd()+
':/app', '-w', '/app', '-e',
'WANDB_API_KEY=test', '-e', 'WANDB_COMMAND=python train.py --test foo', '-it', 'test/docker', '/bin/bash'])
def test_restore_bad_remote(runner, mock_server, git_repo, docker, monkeypatch):
# git_repo creates it's own isolated filesystem
mock_server.set_context("git", {"repo": "http://fake.git/foo/bar"})
api = InternalApi({'project': 'test'})
monkeypatch.setattr(cli, '_api', api)
def bad_commit(cmt):
raise ValueError()
monkeypatch.setattr(api.git.repo, 'commit', bad_commit)
monkeypatch.setattr(api, "download_urls", lambda *args, **kwargs: [])
result = runner.invoke(cli.restore, ["wandb/test:abcdef"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 1
assert "Run `git clone http://fake.git/foo/bar`" in result.output
def test_restore_good_remote(runner, mock_server, git_repo, docker, monkeypatch):
# git_repo creates it's own isolated filesystem
git_repo.repo.create_remote('origin', "[email protected]:foo/bar")
monkeypatch.setattr(subprocess, 'check_call', lambda command: True)
mock_server.set_context("git", {"repo": "http://fake.git/foo/bar"})
monkeypatch.setattr(cli, '_api', InternalApi({'project': 'test'}))
result = runner.invoke(cli.restore, ["wandb/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
def test_restore_slashes(runner, mock_server, git_repo, docker, monkeypatch):
# git_repo creates it's own isolated filesystem
mock_server.set_context("git", {"repo": "http://fake.git/foo/bar"})
monkeypatch.setattr(cli, '_api', InternalApi({'project': 'test'}))
result = runner.invoke(cli.restore, ["wandb/test/abcdef", "--no-git"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Restored config variables" in result.output
def test_restore_no_entity(runner, mock_server, git_repo, docker, monkeypatch):
# git_repo creates it's own isolated filesystem
mock_server.set_context("git", {"repo": "http://fake.git/foo/bar"})
monkeypatch.setattr(cli, '_api', InternalApi({'project': 'test'}))
result = runner.invoke(cli.restore, ["test/abcdef", "--no-git"])
print(result.output)
print(traceback.print_tb(result.exc_info[2]))
assert result.exit_code == 0
assert "Restored config variables" in result.output
def test_restore_not_git(runner, mock_server, docker, monkeypatch):
with runner.isolated_filesystem():
monkeypatch.setattr(cli, '_api', InternalApi({'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 "Original run has no git history" in result.output
def test_gc(runner):
with runner.isolated_filesystem():
if not os.path.isdir("wandb"):
os.mkdir("wandb")
d1 = datetime.datetime.now()
d2 = d1 - datetime.timedelta(hours=3)
run1 = d1.strftime("run-%Y%m%d_%H%M%S-abcd")
run2 = d2.strftime("run-%Y%m%d_%H%M%S-efgh")
run1_dir = os.path.join("wandb", run1)
run2_dir = os.path.join("wandb", run2)
os.mkdir(run1_dir)
with open(os.path.join(run1_dir, "run-abcd.wandb"), 'w') as f:
f.write('')
with open(os.path.join(run1_dir, "run-abcd.wandb.synced"), 'w') as f:
f.write('')
os.mkdir(run2_dir)
with open(os.path.join(run2_dir, "run-efgh.wandb"), 'w') as f:
f.write('')
with open(os.path.join(run2_dir, "run-efgh.wandb.synced"), 'w') as f:
f.write('')
assert runner.invoke(cli.sync, ["--clean", "--clean-old-hours", "2"], input='y\n').exit_code == 0
assert os.path.exists(run1_dir)
assert not os.path.exists(run2_dir)
assert runner.invoke(cli.sync, ["--clean", "--clean-old-hours", "0"], input='y\n').exit_code == 0
assert not os.path.exists(run1_dir)