Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Dec 5, 2024
1 parent d76585e commit c555b44
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 218 deletions.
213 changes: 0 additions & 213 deletions core/src/layers/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,216 +562,3 @@ where
Ok(())
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;

use http::HeaderMap;
use http::Method as HttpMethod;

use super::*;

#[derive(Debug)]
struct MockService {
capability: Capability,
}

impl Access for MockService {
type Reader = oio::Reader;
type Writer = oio::Writer;
type Lister = oio::Lister;
type Deleter = oio::Deleter;
type BlockingReader = oio::BlockingReader;
type BlockingWriter = oio::BlockingWriter;
type BlockingLister = oio::BlockingLister;
type BlockingDeleter = oio::BlockingDeleter;

fn info(&self) -> Arc<AccessorInfo> {
let mut info = AccessorInfo::default();
info.set_native_capability(self.capability);

info.into()
}

async fn create_dir(&self, _: &str, _: OpCreateDir) -> Result<RpCreateDir> {
Ok(RpCreateDir {})
}

async fn stat(&self, _: &str, _: OpStat) -> Result<RpStat> {
Ok(RpStat::new(Metadata::new(EntryMode::Unknown)))
}

async fn read(&self, _: &str, _: OpRead) -> Result<(RpRead, Self::Reader)> {
Ok((RpRead::new(), Box::new(bytes::Bytes::new())))
}

async fn write(&self, _: &str, _: OpWrite) -> Result<(RpWrite, Self::Writer)> {
Ok((RpWrite::new(), Box::new(())))
}

async fn list(&self, _: &str, _: OpList) -> Result<(RpList, Self::Lister)> {
Ok((RpList {}, Box::new(())))
}

async fn copy(&self, _: &str, _: &str, _: OpCopy) -> Result<RpCopy> {
Ok(RpCopy {})
}

async fn rename(&self, _: &str, _: &str, _: OpRename) -> Result<RpRename> {
Ok(RpRename {})
}

async fn presign(&self, _: &str, _: OpPresign) -> Result<RpPresign> {
Ok(RpPresign::new(PresignedRequest::new(
HttpMethod::POST,
"https://example.com/presign".parse().expect("should parse"),
HeaderMap::new(),
)))
}
}

fn new_test_operator(capability: Capability) -> Operator {
let srv = MockService { capability };

Operator::from_inner(Arc::new(srv)).layer(CompleteLayer)
}

#[tokio::test]
async fn test_read() {
let op = new_test_operator(Capability::default());
let res = op.read("path").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
read: true,
stat: true,
..Default::default()
});
let res = op.read("path").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_stat() {
let op = new_test_operator(Capability::default());
let res = op.stat("path").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
stat: true,
..Default::default()
});
let res = op.stat("path").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_writer() {
let op = new_test_operator(Capability::default());
let bs: Vec<u8> = vec![];
let res = op.write("path", bs).await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
write: true,
..Default::default()
});
let res = op.writer("path").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_create_dir() {
let op = new_test_operator(Capability::default());
let res = op.create_dir("path/").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
create_dir: true,
..Default::default()
});
let res = op.create_dir("path/").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_delete() {
let op = new_test_operator(Capability::default());
let res = op.delete("path").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
delete: true,
..Default::default()
});
let res = op.delete("path").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_copy() {
let op = new_test_operator(Capability::default());
let res = op.copy("path_a", "path_b").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
copy: true,
..Default::default()
});
let res = op.copy("path_a", "path_b").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_rename() {
let op = new_test_operator(Capability::default());
let res = op.rename("path_a", "path_b").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
rename: true,
..Default::default()
});
let res = op.rename("path_a", "path_b").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_list() {
let op = new_test_operator(Capability::default());
let res = op.list("path/").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
list: true,
list_with_recursive: true,
..Default::default()
});
let res = op.list("path/").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_presign() {
let op = new_test_operator(Capability::default());
let res = op.presign_read("path", Duration::from_secs(1)).await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
presign: true,
..Default::default()
});
let res = op.presign_read("path", Duration::from_secs(1)).await;
assert!(res.is_ok())
}
}
78 changes: 73 additions & 5 deletions core/src/layers/correctness_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

use std::fmt::{Debug, Formatter};
use std::future::Future;
use std::sync::Arc;

use crate::raw::*;
Expand Down Expand Up @@ -78,11 +79,11 @@ impl<A: Access> LayeredAccess for CorrectnessAccessor<A> {
type Reader = A::Reader;
type Writer = A::Writer;
type Lister = A::Lister;
type Deleter = A::Deleter;
type Deleter = CheckWrapper<A::Deleter>;
type BlockingReader = A::BlockingReader;
type BlockingWriter = A::BlockingWriter;
type BlockingLister = A::BlockingLister;
type BlockingDeleter = A::BlockingDeleter;
type BlockingDeleter = CheckWrapper<A::BlockingDeleter>;

fn inner(&self) -> &Self::Inner {
&self.inner
Expand Down Expand Up @@ -146,7 +147,10 @@ impl<A: Access> LayeredAccess for CorrectnessAccessor<A> {
}

async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
self.inner.delete().await
self.inner.delete().await.map(|(rp, deleter)| {
let deleter = CheckWrapper::new(deleter, self.info.clone());
(rp, deleter)
})
}

async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
Expand Down Expand Up @@ -207,14 +211,62 @@ impl<A: Access> LayeredAccess for CorrectnessAccessor<A> {
}

fn blocking_delete(&self) -> Result<(RpDelete, Self::BlockingDeleter)> {
self.inner.blocking_delete()
self.inner.blocking_delete().map(|(rp, deleter)| {
let deleter = CheckWrapper::new(deleter, self.info.clone());
(rp, deleter)
})
}

fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingLister)> {
self.inner.blocking_list(path, args)
}
}

pub struct CheckWrapper<T> {
info: Arc<AccessorInfo>,
inner: T,
}

impl<T> CheckWrapper<T> {
fn new(inner: T, info: Arc<AccessorInfo>) -> Self {
Self { inner, info }
}

fn check_delete(&self, args: &OpDelete) -> Result<()> {
if args.version().is_some() && !self.info.full_capability().delete_with_version {
return Err(new_unsupported_error(
&self.info,
Operation::DeleterDelete,
"version",
));
}

Ok(())
}
}

impl<T: oio::Delete> oio::Delete for CheckWrapper<T> {
fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
self.check_delete(&args)?;
self.inner.delete(path, args)
}

fn flush(&mut self) -> impl Future<Output = Result<usize>> + MaybeSend {
self.inner.flush()
}
}

impl<T: oio::BlockingDelete> oio::BlockingDelete for CheckWrapper<T> {
fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
self.check_delete(&args)?;
self.inner.delete(path, args)
}

fn flush(&mut self) -> Result<usize> {
self.inner.flush()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -256,7 +308,23 @@ mod tests {
}

async fn list(&self, _: &str, _: OpList) -> Result<(RpList, Self::Lister)> {
Ok((RpList {}, Box::new(())))
Ok((RpList::default(), Box::new(())))
}

async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
Ok((RpDelete::default(), Box::new(MockDeleter)))
}
}

struct MockDeleter;

impl oio::Delete for MockDeleter {
fn delete(&mut self, _: &str, _: OpDelete) -> Result<()> {
Ok(())
}

async fn flush(&mut self) -> Result<usize> {
Ok(1)
}
}

Expand Down

0 comments on commit c555b44

Please sign in to comment.