-
Notifications
You must be signed in to change notification settings - Fork 618
/
test_wandb_artifacts_cache.py
189 lines (136 loc) 路 5.41 KB
/
test_wandb_artifacts_cache.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
import base64
import os
import random
from multiprocessing import Pool
import pytest
from wandb import wandb_sdk
# This function should only be used in `test_check_write_parallel`,
# but it needs to be a global function for multiprocessing.
def _cache_writer(cache_path):
etag = "abcdef"
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(cache_path)
_, _, opener = cache.check_etag_obj_path("http://wandb.example/foo", etag, 10)
with opener() as f:
f.write("".join(random.choice("0123456") for _ in range(10)))
def test_opener_rejects_append_mode(tmp_path):
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
path, exists, opener = cache.check_md5_obj_path(base64.b64encode(b"abcdef"), 10)
with pytest.raises(ValueError):
with opener("a"):
pass
# make sure that the ValueError goes away if we use a valid mode
with opener("w"):
pass
def test_check_md5_obj_path(tmp_path):
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
md5 = base64.b64encode(b"abcdef")
path, exists, opener = cache.check_md5_obj_path(md5, 10)
expected_path = os.path.join(tmp_path, "obj", "md5", "61", "6263646566")
with opener() as f:
f.write("hi")
with open(path) as f:
contents = f.read()
assert path == expected_path
assert exists is False
assert contents == "hi"
def test_check_etag_obj_path_points_to_opener_dst(tmp_path):
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
path, exists, opener = cache.check_etag_obj_path("http://my/url", "abc", 10)
with opener() as f:
f.write("hi")
with open(path) as f:
contents = f.read()
assert contents == "hi"
def test_check_etag_obj_path_returns_exists_if_exists(tmp_path):
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
size = 123
_, exists, opener = cache.check_etag_obj_path("http://my/url", "abc", size)
assert not exists
with opener() as f:
f.write(size * "a")
_, exists, _ = cache.check_etag_obj_path("http://my/url", "abc", size)
assert exists
def test_check_etag_obj_path_returns_not_exists_if_incomplete(tmp_path):
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
size = 123
_, exists, opener = cache.check_etag_obj_path("http://my/url", "abc", size)
assert not exists
with opener() as f:
f.write((size - 1) * "a")
_, exists, _ = cache.check_etag_obj_path("http://my/url", "abc", size)
assert not exists
with opener() as f:
f.write(size * "a")
_, exists, _ = cache.check_etag_obj_path("http://my/url", "abc", size)
assert exists
def test_check_etag_obj_path_does_not_include_etag(tmp_path):
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
path, _, _ = cache.check_etag_obj_path("http://url/1", "abcdef", 10)
assert "abcdef" not in path
@pytest.mark.parametrize(
["url1", "url2", "etag1", "etag2", "path_equal"],
[
("http://url/1", "http://url/1", "abc", "abc", True),
("http://url/1", "http://url/1", "abc", "def", False),
("http://url/1", "http://url/2", "abc", "abc", False),
],
)
def test_check_etag_obj_path_hashes_url_and_etag(
url1, url2, etag1, etag2, path_equal, tmp_path
):
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
path_1, _, _ = cache.check_etag_obj_path(url1, etag1, 10)
path_2, _, _ = cache.check_etag_obj_path(url2, etag2, 10)
if path_equal:
assert path_1 == path_2
else:
assert path_1 != path_2
def test_check_write_parallel(tmp_path):
num_parallel = 5
p = Pool(num_parallel)
p.map(_cache_writer, [tmp_path for _ in range(num_parallel)])
_cache_writer(tmp_path) # run in this process too for code coverage
p.close()
p.join()
# Regardless of the ordering, we should be left with one
# file at the end.
files = [f for f in (tmp_path / "obj" / "etag").rglob("*") if f.is_file()]
assert len(files) == 1
def test_artifacts_cache_cleanup_empty(tmp_path):
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
reclaimed_bytes = cache.cleanup(100000)
assert reclaimed_bytes == 0
def test_artifacts_cache_cleanup(tmp_path):
cache_root = os.path.join(tmp_path, "obj", "md5")
path_1 = os.path.join(cache_root, "aa")
os.makedirs(path_1)
with open(os.path.join(path_1, "aardvark"), "w") as f:
f.truncate(5000)
f.flush()
os.fsync(f)
path_2 = os.path.join(cache_root, "ab")
os.makedirs(path_2)
with open(os.path.join(path_2, "absolute"), "w") as f:
f.truncate(2000)
f.flush()
os.fsync(f)
path_3 = os.path.join(cache_root, "ac")
os.makedirs(path_3)
with open(os.path.join(path_3, "accelerate"), "w") as f:
f.truncate(1000)
f.flush()
os.fsync(f)
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
reclaimed_bytes = cache.cleanup(5000)
# We should get rid of "aardvark" in this case
assert reclaimed_bytes == 5000
def test_artifacts_cache_cleanup_tmp_files(tmp_path):
path = os.path.join(tmp_path, "obj", "md5", "aa")
os.makedirs(path)
with open(os.path.join(path, "tmp_abc"), "w") as f:
f.truncate(1000)
cache = wandb_sdk.wandb_artifacts.ArtifactsCache(tmp_path)
# Even if we are above our target size, the cleanup
# should reclaim tmp files.
reclaimed_bytes = cache.cleanup(10000)
assert reclaimed_bytes == 1000