Skip to content

Commit 157ef8d

Browse files
authored
Update reftests with small refactor and renames for clarity (#1225)
This change makes minor updates to improve clarity in the reference tests. ### Does this change impact existing behavior? No, refactors reftests only. ### Does this change need a changelog entry? No. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and I agree to the terms of the [Developer Certificate of Origin (DCO)](https://developercertificate.org/). Signed-off-by: Daniel Carl Jones <[email protected]>
1 parent 5eb74d5 commit 157ef8d

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

mountpoint-s3/tests/reftests/harness.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -282,26 +282,40 @@ impl Harness {
282282
/// Compared to [compare_contents], this avoids doing a `readdir` before `lookup`, and so tests
283283
/// a different path through the inode code.
284284
pub async fn compare_single_path(&self, idx: usize) {
285-
let inodes = self.reference.list_recursive();
286-
if inodes.is_empty() {
285+
let ref_inodes = self.reference.list_recursive();
286+
if ref_inodes.is_empty() {
287287
return;
288288
}
289-
let (path, node) = &inodes[idx % inodes.len()];
289+
let (path_components, node) = &ref_inodes[idx % ref_inodes.len()];
290290

291291
let mut parent = FUSE_ROOT_INODE;
292292
let mut seen_inos = HashSet::from([FUSE_ROOT_INODE]);
293-
for name in path.iter().take(path.len().saturating_sub(1)) {
294-
let lookup = self.fs.lookup(parent, name.as_ref()).await.unwrap();
293+
294+
let (leaf, ancestors) = path_components
295+
.split_last()
296+
.expect("there should always be at least one path component");
297+
for name in ancestors {
298+
let lookup = self
299+
.fs
300+
.lookup(parent, name.as_ref())
301+
.await
302+
.expect("ancestor must exist in MP fs");
295303
assert_eq!(lookup.attr.kind, FileType::Directory);
304+
let ino = lookup.attr.ino;
296305
assert!(
297-
seen_inos.insert(lookup.attr.ino),
298-
"should not have seen ancestor before"
306+
seen_inos.insert(ino),
307+
"should not have seen ancestor ino {ino} in MP fs before"
299308
);
300-
parent = lookup.attr.ino;
309+
parent = ino;
301310
}
302311

303-
let lookup = self.fs.lookup(parent, path.last().unwrap().as_ref()).await.unwrap();
304-
assert!(seen_inos.insert(lookup.attr.ino), "should not have seen inode before");
312+
let lookup = self
313+
.fs
314+
.lookup(parent, leaf.as_ref())
315+
.await
316+
.expect("directory entry at path should exist in MP fs");
317+
let ino = lookup.attr.ino;
318+
assert!(seen_inos.insert(ino), "should not have seen ino {ino} before");
305319
match node {
306320
Node::Directory { .. } => {
307321
assert_eq!(lookup.attr.kind, FileType::Directory);
@@ -643,8 +657,8 @@ impl Harness {
643657
) -> BoxFuture<'a, ()> {
644658
async move {
645659
let dir_handle = self.fs.opendir(fs_dir, 0).await.unwrap().fh;
646-
let children = ref_dir.children();
647-
let mut keys = children.keys().cloned().collect::<HashSet<_>>();
660+
let ref_children = ref_dir.children();
661+
let mut unvisited_ref_children = ref_children.keys().cloned().collect::<HashSet<_>>();
648662

649663
let mut reply = DirectoryReply::new(self.readdir_limit);
650664
self.fs.readdir(fs_dir, dir_handle, 0, &mut reply).await.unwrap();
@@ -686,7 +700,7 @@ impl Harness {
686700
let lkup = self.fs.lookup(fs_dir, &reply.name).await.unwrap();
687701
let attr = lkup.attr;
688702

689-
match children.get(name) {
703+
match ref_children.get(name) {
690704
Some(node) => {
691705
let ref_kind = node.node_type().into();
692706
assert_eq!(
@@ -710,7 +724,7 @@ impl Harness {
710724
self.compare_contents_recursive(fs_dir, reply.ino, node).await;
711725
}
712726
assert!(
713-
keys.remove(name),
727+
unvisited_ref_children.remove(name),
714728
"file name must be in the set of child names in the reference fs",
715729
);
716730
}
@@ -722,8 +736,8 @@ impl Harness {
722736
}
723737

724738
assert!(
725-
keys.is_empty(),
726-
"reference contained elements not in the filesystem in dir inode {fs_dir}: {keys:?}"
739+
unvisited_ref_children.is_empty(),
740+
"reference contained elements not in the filesystem in dir inode {fs_dir}: {unvisited_ref_children:?}"
727741
);
728742

729743
self.fs.releasedir(fs_dir, dir_handle, 0).await.unwrap();
@@ -733,10 +747,10 @@ impl Harness {
733747

734748
/// Compare the contents of a given reference file to the system under test (SUT),
735749
/// by opening and reading the file from the SUT.
736-
async fn compare_file<'a>(&'a self, fs_file: InodeNo, ref_file: &'a MockObject) {
737-
let fh = match self.fs.open(fs_file, OpenFlags::empty(), 0).await {
750+
async fn compare_file<'a>(&'a self, fs_ino: InodeNo, ref_file: &'a MockObject) {
751+
let fh = match self.fs.open(fs_ino, OpenFlags::empty(), 0).await {
738752
Ok(ret) => ret.fh,
739-
Err(e) => panic!("failed to open ino {fs_file} for content comparison: {e:?}"),
753+
Err(e) => panic!("failed to open ino {fs_ino} for content comparison: {e:?}"),
740754
};
741755
let mut offset = 0;
742756
const MAX_READ_SIZE: usize = 128 * 1024;
@@ -747,7 +761,7 @@ impl Harness {
747761
assert_eq!(ref_bytes.len(), num_bytes, "number of bytes did not match");
748762
let bytes_from_read = self
749763
.fs
750-
.read(fs_file, fh, offset as i64, num_bytes as u32, 0, None)
764+
.read(fs_ino, fh, offset as i64, num_bytes as u32, 0, None)
751765
.await
752766
.expect("read should succeed");
753767
assert_eq!(&ref_bytes[..], &bytes_from_read, "read bytes did not match");

mountpoint-s3/tests/reftests/reference.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Node {
7676
#[derive(Debug)]
7777
pub struct Reference {
7878
/// Contents of our S3 bucket
79-
remote_keys: BTreeMap<String, MockObject>,
79+
remote_objects: BTreeMap<String, MockObject>,
8080
/// Local files
8181
local_files: Vec<PathBuf>,
8282
/// Local directories
@@ -121,25 +121,25 @@ impl MaterializedReference {
121121
);
122122

123123
let mut parent_node = &mut self.root;
124-
while let Some(dir) = components.next() {
124+
while let Some(name) = components.next() {
125125
let Node::Directory { children, .. } = parent_node else {
126126
return false;
127127
// TODO: see above -- implicit directories are allowed to disappear
128128
// panic!("unexpected internal file node while adding {:?}", path.as_ref());
129129
};
130-
let dir = dir.as_os_str().to_str().unwrap();
130+
let name = name.as_os_str().to_str().unwrap();
131131
if components.peek().is_none() {
132132
// If both a local and a remote directory exist, don't overwrite the remote one's
133133
// contents, as they will be visible even though the directory is local. But
134134
// remember the directory is still local.
135135
if typ == NodeType::Directory {
136-
if let Some(Node::Directory { is_local, .. }) = children.get_mut(dir) {
136+
if let Some(Node::Directory { is_local, .. }) = children.get_mut(name) {
137137
*is_local = true;
138138
break;
139139
}
140140
}
141141
// If a directory of this name exists, ignore any local file
142-
if let Some(node) = children.get(dir) {
142+
if let Some(node) = children.get(name) {
143143
if node.node_type() == NodeType::Directory {
144144
return false;
145145
}
@@ -151,15 +151,15 @@ impl MaterializedReference {
151151
},
152152
NodeType::File => Node::File(File::Local),
153153
};
154-
children.insert(dir.to_owned(), new_node);
154+
children.insert(name.to_owned(), new_node);
155155
break;
156156
} else {
157157
// TODO: see above -- implicit directories are allowed to disappear
158158
// parent_node = children.entry(dir.to_owned()).or_insert_with(|| Node::Directory {
159159
// children: BTreeMap::new(),
160160
// is_local: true,
161161
// })
162-
let Some(child_node) = children.get_mut(dir) else {
162+
let Some(child_node) = children.get_mut(name) else {
163163
return false;
164164
};
165165
parent_node = child_node;
@@ -176,7 +176,7 @@ impl Reference {
176176
let local_directories = vec![];
177177
let materialized = build_reference(remote_keys.iter().map(|(k, o): &(_, _)| (k, o)));
178178
Self {
179-
remote_keys: remote_keys.into_iter().collect(),
179+
remote_objects: remote_keys.into_iter().collect(),
180180
local_files,
181181
local_directories,
182182
materialized,
@@ -189,10 +189,10 @@ impl Reference {
189189
/// and produce a result which should maintain our promised semantics.
190190
fn rematerialize(&self) -> MaterializedReference {
191191
tracing::debug!(
192-
remote_keys=?self.remote_keys, local_files=?self.local_files, local_directories=?self.local_directories,
192+
remote_keys=?self.remote_objects.keys(), local_files=?self.local_files, local_directories=?self.local_directories,
193193
"rematerialize",
194194
);
195-
let mut materialized = build_reference(self.remote_keys.iter());
195+
let mut materialized = build_reference(self.remote_objects.iter());
196196
for local_dir in self.local_directories.iter() {
197197
let added = materialized.add_local_node(local_dir, NodeType::Directory);
198198
if added {
@@ -279,12 +279,12 @@ impl Reference {
279279
}
280280

281281
pub fn add_remote_key(&mut self, key: &str, object: MockObject) {
282-
self.remote_keys.insert(key.to_owned(), object);
282+
self.remote_objects.insert(key.to_owned(), object);
283283
self.materialized = self.rematerialize();
284284
}
285285

286286
pub fn remove_remote_key(&mut self, key: &str) {
287-
self.remote_keys.remove(key);
287+
self.remote_objects.remove(key);
288288
self.materialized = self.rematerialize();
289289
}
290290

@@ -328,7 +328,7 @@ impl Reference {
328328

329329
/// A list of objects in the bucket
330330
pub fn remote_keys(&self) -> impl ExactSizeIterator<Item = &str> {
331-
self.remote_keys.keys().map(|key| key.as_str())
331+
self.remote_objects.keys().map(|key| key.as_str())
332332
}
333333
}
334334

0 commit comments

Comments
 (0)