-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
tests.rs
237 lines (199 loc) · 6.92 KB
/
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
// Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
use tempfile;
use testutil;
use crate::mount;
use hashing;
use store::Store;
use testutil::{
data::{TestData, TestDirectory},
file,
};
#[tokio::test]
async fn missing_digest() {
let (store_dir, mount_dir) = make_dirs();
let runtime = task_executor::Executor::new();
let store =
Store::local_only(runtime.clone(), store_dir.path()).expect("Error creating local store");
let _fs = mount(mount_dir.path(), store, runtime).expect("Mounting");
assert!(!&mount_dir
.path()
.join("digest")
.join(digest_to_filepath(&TestData::roland().digest()))
.exists());
}
#[tokio::test]
async fn read_file_by_digest() {
let (store_dir, mount_dir) = make_dirs();
let runtime = task_executor::Executor::new();
let store =
Store::local_only(runtime.clone(), store_dir.path()).expect("Error creating local store");
let test_bytes = TestData::roland();
store
.store_file_bytes(test_bytes.bytes(), false)
.await
.expect("Storing bytes");
let _fs = mount(mount_dir.path(), store, runtime).expect("Mounting");
let file_path = mount_dir
.path()
.join("digest")
.join(digest_to_filepath(&test_bytes.digest()));
assert_eq!(test_bytes.bytes(), file::contents(&file_path));
assert!(file::is_executable(&file_path));
}
#[tokio::test]
async fn list_directory() {
let (store_dir, mount_dir) = make_dirs();
let runtime = task_executor::Executor::new();
let store =
Store::local_only(runtime.clone(), store_dir.path()).expect("Error creating local store");
let test_bytes = TestData::roland();
let test_directory = TestDirectory::containing_roland();
store
.store_file_bytes(test_bytes.bytes(), false)
.await
.expect("Storing bytes");
store
.record_directory(&test_directory.directory(), false)
.await
.expect("Storing directory");
let _fs = mount(mount_dir.path(), store, runtime).expect("Mounting");
let virtual_dir = mount_dir
.path()
.join("directory")
.join(digest_to_filepath(&test_directory.digest()));
assert_eq!(vec!["roland.ext"], file::list_dir(&virtual_dir));
}
#[tokio::test]
async fn read_file_from_directory() {
let (store_dir, mount_dir) = make_dirs();
let runtime = task_executor::Executor::new();
let store =
Store::local_only(runtime.clone(), store_dir.path()).expect("Error creating local store");
let test_bytes = TestData::roland();
let test_directory = TestDirectory::containing_roland();
store
.store_file_bytes(test_bytes.bytes(), false)
.await
.expect("Storing bytes");
store
.record_directory(&test_directory.directory(), false)
.await
.expect("Storing directory");
let _fs = mount(mount_dir.path(), store, runtime).expect("Mounting");
let roland = mount_dir
.path()
.join("directory")
.join(digest_to_filepath(&test_directory.digest()))
.join("roland.ext");
assert_eq!(test_bytes.bytes(), file::contents(&roland));
assert!(!file::is_executable(&roland));
}
#[tokio::test]
async fn list_recursive_directory() {
let (store_dir, mount_dir) = make_dirs();
let runtime = task_executor::Executor::new();
let store =
Store::local_only(runtime.clone(), store_dir.path()).expect("Error creating local store");
let test_bytes = TestData::roland();
let treat_bytes = TestData::catnip();
let test_directory = TestDirectory::containing_roland();
let recursive_directory = TestDirectory::recursive();
store
.store_file_bytes(test_bytes.bytes(), false)
.await
.expect("Storing bytes");
store
.store_file_bytes(treat_bytes.bytes(), false)
.await
.expect("Storing bytes");
store
.record_directory(&test_directory.directory(), false)
.await
.expect("Storing directory");
store
.record_directory(&recursive_directory.directory(), false)
.await
.expect("Storing directory");
let _fs = mount(mount_dir.path(), store, runtime).expect("Mounting");
let virtual_dir = mount_dir
.path()
.join("directory")
.join(digest_to_filepath(&recursive_directory.digest()));
assert_eq!(vec!["cats", "treats.ext"], file::list_dir(&virtual_dir));
assert_eq!(
vec!["roland.ext"],
file::list_dir(&virtual_dir.join("cats"))
);
}
#[tokio::test]
async fn read_file_from_recursive_directory() {
let (store_dir, mount_dir) = make_dirs();
let runtime = task_executor::Executor::new();
let store =
Store::local_only(runtime.clone(), store_dir.path()).expect("Error creating local store");
let test_bytes = TestData::roland();
let treat_bytes = TestData::catnip();
let test_directory = TestDirectory::containing_roland();
let recursive_directory = TestDirectory::recursive();
store
.store_file_bytes(test_bytes.bytes(), false)
.await
.expect("Storing bytes");
store
.store_file_bytes(treat_bytes.bytes(), false)
.await
.expect("Storing bytes");
store
.record_directory(&test_directory.directory(), false)
.await
.expect("Storing directory");
store
.record_directory(&recursive_directory.directory(), false)
.await
.expect("Storing directory");
let _fs = mount(mount_dir.path(), store, runtime).expect("Mounting");
let virtual_dir = mount_dir
.path()
.join("directory")
.join(digest_to_filepath(&recursive_directory.digest()));
let treats = virtual_dir.join("treats.ext");
assert_eq!(treat_bytes.bytes(), file::contents(&treats));
assert!(!file::is_executable(&treats));
let roland = virtual_dir.join("cats").join("roland.ext");
assert_eq!(test_bytes.bytes(), file::contents(&roland));
assert!(!file::is_executable(&roland));
}
#[tokio::test]
async fn files_are_correctly_executable() {
let (store_dir, mount_dir) = make_dirs();
let runtime = task_executor::Executor::new();
let store =
Store::local_only(runtime.clone(), store_dir.path()).expect("Error creating local store");
let treat_bytes = TestData::catnip();
let directory = TestDirectory::with_maybe_executable_files(true);
store
.store_file_bytes(treat_bytes.bytes(), false)
.await
.expect("Storing bytes");
store
.record_directory(&directory.directory(), false)
.await
.expect("Storing directory");
let _fs = mount(mount_dir.path(), store, runtime).expect("Mounting");
let virtual_dir = mount_dir
.path()
.join("directory")
.join(digest_to_filepath(&directory.digest()));
assert_eq!(vec!["feed.ext", "food.ext"], file::list_dir(&virtual_dir));
assert!(file::is_executable(&virtual_dir.join("feed.ext")));
assert!(!file::is_executable(&virtual_dir.join("food.ext")));
}
pub fn digest_to_filepath(digest: &hashing::Digest) -> String {
format!("{}-{}", digest.hash, digest.size_bytes)
}
pub fn make_dirs() -> (tempfile::TempDir, tempfile::TempDir) {
let store_dir = tempfile::Builder::new().prefix("store").tempdir().unwrap();
let mount_dir = tempfile::Builder::new().prefix("mount").tempdir().unwrap();
(store_dir, mount_dir)
}