-
-
Notifications
You must be signed in to change notification settings - Fork 607
/
remote_tests.rs
342 lines (290 loc) · 7.52 KB
/
remote_tests.rs
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
use std::collections::{BTreeMap, HashSet};
use std::time::Duration;
use bytes::Bytes;
use grpc_util::tls;
use hashing::Digest;
use mock::StubCAS;
use testutil::data::{TestData, TestDirectory};
use workunit_store::WorkunitStore;
use crate::remote::ByteStore;
use crate::tests::{big_file_bytes, big_file_fingerprint, new_cas};
use crate::MEGABYTES;
#[tokio::test]
async fn loads_file() {
let testdata = TestData::roland();
let cas = new_cas(10);
assert_eq!(
load_file_bytes(&new_byte_store(&cas), testdata.digest())
.await
.unwrap(),
Some(testdata.bytes())
);
}
#[tokio::test]
async fn missing_file() {
let _ = WorkunitStore::setup_for_tests();
let cas = StubCAS::empty();
assert_eq!(
load_file_bytes(&new_byte_store(&cas), TestData::roland().digest()).await,
Ok(None)
);
}
#[tokio::test]
async fn load_directory() {
let cas = new_cas(10);
let testdir = TestDirectory::containing_roland();
assert_eq!(
load_directory_proto_bytes(&new_byte_store(&cas), testdir.digest()).await,
Ok(Some(testdir.bytes()))
);
}
#[tokio::test]
async fn missing_directory() {
let _ = WorkunitStore::setup_for_tests();
let cas = StubCAS::empty();
assert_eq!(
load_directory_proto_bytes(
&new_byte_store(&cas),
TestDirectory::containing_roland().digest()
)
.await,
Ok(None)
);
}
#[tokio::test]
async fn load_file_grpc_error() {
let _ = WorkunitStore::setup_for_tests();
let cas = StubCAS::cas_always_errors();
let error = load_file_bytes(&new_byte_store(&cas), TestData::roland().digest())
.await
.expect_err("Want error");
assert!(
error.contains("StubCAS is configured to always fail"),
"Bad error message, got: {}",
error
)
}
#[tokio::test]
async fn load_directory_grpc_error() {
let _ = WorkunitStore::setup_for_tests();
let cas = StubCAS::cas_always_errors();
let error = load_directory_proto_bytes(
&new_byte_store(&cas),
TestDirectory::containing_roland().digest(),
)
.await
.expect_err("Want error");
assert!(
error.contains("StubCAS is configured to always fail"),
"Bad error message, got: {}",
error
)
}
#[tokio::test]
async fn fetch_less_than_one_chunk() {
let testdata = TestData::roland();
let cas = new_cas(testdata.bytes().len() + 1);
assert_eq!(
load_file_bytes(&new_byte_store(&cas), testdata.digest()).await,
Ok(Some(testdata.bytes()))
)
}
#[tokio::test]
async fn fetch_exactly_one_chunk() {
let testdata = TestData::roland();
let cas = new_cas(testdata.bytes().len());
assert_eq!(
load_file_bytes(&new_byte_store(&cas), testdata.digest()).await,
Ok(Some(testdata.bytes()))
)
}
#[tokio::test]
async fn fetch_multiple_chunks_exact() {
let testdata = TestData::roland();
let cas = new_cas(1);
assert_eq!(
load_file_bytes(&new_byte_store(&cas), testdata.digest()).await,
Ok(Some(testdata.bytes()))
)
}
#[tokio::test]
async fn fetch_multiple_chunks_nonfactor() {
let testdata = TestData::roland();
let cas = new_cas(9);
assert_eq!(
load_file_bytes(&new_byte_store(&cas), testdata.digest()).await,
Ok(Some(testdata.bytes()))
)
}
#[tokio::test]
async fn write_file_one_chunk() {
let testdata = TestData::roland();
let cas = StubCAS::empty();
let store = new_byte_store(&cas);
assert_eq!(store.store_bytes(testdata.bytes()).await, Ok(()));
let blobs = cas.blobs.lock();
assert_eq!(blobs.get(&testdata.fingerprint()), Some(&testdata.bytes()));
}
#[tokio::test]
async fn write_file_multiple_chunks() {
let _ = WorkunitStore::setup_for_tests();
let cas = StubCAS::empty();
let store = ByteStore::new(
&cas.address(),
None,
tls::Config::default(),
BTreeMap::new(),
10 * 1024,
Duration::from_secs(5),
1,
256,
None,
0, // disable batch API, force streaming API
)
.unwrap();
let all_the_henries = big_file_bytes();
let fingerprint = big_file_fingerprint();
assert_eq!(store.store_bytes(all_the_henries.clone()).await, Ok(()));
let blobs = cas.blobs.lock();
assert_eq!(blobs.get(&fingerprint), Some(&all_the_henries));
let write_message_sizes = cas.write_message_sizes.lock();
assert_eq!(
write_message_sizes.len(),
98,
"Wrong number of chunks uploaded"
);
for size in write_message_sizes.iter() {
assert!(
size <= &(10 * 1024),
"Size {} should have been <= {}",
size,
10 * 1024
);
}
}
#[tokio::test]
async fn write_empty_file() {
let empty_file = TestData::empty();
let cas = StubCAS::empty();
let store = new_byte_store(&cas);
assert_eq!(store.store_bytes(empty_file.bytes()).await, Ok(()));
let blobs = cas.blobs.lock();
assert_eq!(
blobs.get(&empty_file.fingerprint()),
Some(&empty_file.bytes())
);
}
#[tokio::test]
async fn write_file_errors() {
let cas = StubCAS::cas_always_errors();
let store = new_byte_store(&cas);
let error = store
.store_bytes(TestData::roland().bytes())
.await
.expect_err("Want error");
assert!(
error.contains("StubCAS is configured to always fail"),
"Bad error message, got: {}",
error
);
}
#[tokio::test]
async fn write_connection_error() {
let store = ByteStore::new(
"http://doesnotexist.example",
None,
tls::Config::default(),
BTreeMap::new(),
10 * 1024 * 1024,
Duration::from_secs(1),
1,
256,
None,
super::tests::STORE_BATCH_API_SIZE_LIMIT,
)
.unwrap();
let error = store
.store_bytes(TestData::roland().bytes())
.await
.expect_err("Want error");
assert!(
error.contains("Unavailable: \"error trying to connect: dns error"),
"Bad error message, got: {}",
error
);
}
#[tokio::test]
async fn list_missing_digests_none_missing() {
let cas = new_cas(1024);
let store = new_byte_store(&cas);
assert_eq!(
store
.list_missing_digests(
store.find_missing_blobs_request(vec![TestData::roland().digest()].iter()),
)
.await,
Ok(HashSet::new())
);
}
#[tokio::test]
async fn list_missing_digests_some_missing() {
let _ = WorkunitStore::setup_for_tests();
let cas = StubCAS::empty();
let store = new_byte_store(&cas);
let digest = TestData::roland().digest();
let mut digest_set = HashSet::new();
digest_set.insert(digest);
assert_eq!(
store
.list_missing_digests(store.find_missing_blobs_request(vec![digest].iter()),)
.await,
Ok(digest_set)
);
}
#[tokio::test]
async fn list_missing_digests_error() {
let _ = WorkunitStore::setup_for_tests();
let cas = StubCAS::cas_always_errors();
let store = new_byte_store(&cas);
let error = store
.list_missing_digests(
store.find_missing_blobs_request(vec![TestData::roland().digest()].iter()),
)
.await
.expect_err("Want error");
assert!(
error.contains("StubCAS is configured to always fail"),
"Bad error message, got: {}",
error
);
}
fn new_byte_store(cas: &StubCAS) -> ByteStore {
ByteStore::new(
&cas.address(),
None,
tls::Config::default(),
BTreeMap::new(),
10 * MEGABYTES,
Duration::from_secs(1),
1,
256,
None,
super::tests::STORE_BATCH_API_SIZE_LIMIT,
)
.unwrap()
}
pub async fn load_file_bytes(store: &ByteStore, digest: Digest) -> Result<Option<Bytes>, String> {
load_bytes(&store, digest).await
}
pub async fn load_directory_proto_bytes(
store: &ByteStore,
digest: Digest,
) -> Result<Option<Bytes>, String> {
load_bytes(&store, digest).await
}
async fn load_bytes(store: &ByteStore, digest: Digest) -> Result<Option<Bytes>, String> {
store
.load_bytes_with(digest, |b| Ok(b))
.await
.map_err(|err| format!("{}", err))
}