Skip to content

Commit

Permalink
test(core): test for read_with_if_none_match (#2141)
Browse files Browse the repository at this point in the history
* add test for read_with_if_none_match

Signed-off-by: suyanhanx <[email protected]>

* enable read for webdav

Signed-off-by: suyanhanx <[email protected]>

* Revert "enable read for webdav"

This reverts commit addb518.

---------

Signed-off-by: suyanhanx <[email protected]>
  • Loading branch information
suyanhanx authored Apr 27, 2023
1 parent 3c3463b commit c6a37ad
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 6 deletions.
2 changes: 2 additions & 0 deletions core/src/services/gcs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ impl Accessor for GcsBackend {

read: true,
read_can_next: true,
read_with_if_match: true,
read_with_if_none_match: true,

write: true,
write_with_content_type: true,
Expand Down
2 changes: 2 additions & 0 deletions core/src/services/http/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ impl Accessor for HttpBackend {

read: true,
read_can_next: true,
read_with_if_match: true,
read_with_if_none_match: true,

..Default::default()
});
Expand Down
2 changes: 2 additions & 0 deletions core/src/services/obs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ impl Accessor for ObsBackend {

read: true,
read_can_next: true,
read_with_if_match: true,
read_with_if_none_match: true,

write: true,
write_with_content_type: true,
Expand Down
2 changes: 2 additions & 0 deletions core/src/services/oss/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ impl Accessor for OssBackend {

read: true,
read_can_next: true,
read_with_if_match: true,
read_with_if_none_match: true,

write: true,
write_with_cache_control: true,
Expand Down
71 changes: 71 additions & 0 deletions core/tests/behavior/read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use anyhow::Result;
use futures::AsyncReadExt;
use opendal::ops::OpRead;
use opendal::ops::OpStat;
use opendal::EntryMode;
use opendal::ErrorKind;
Expand Down Expand Up @@ -76,6 +77,8 @@ macro_rules! behavior_read_tests {
test_reader_tail,
test_read_not_exist,
test_read_with_dir_path,
test_read_with_if_match,
test_read_with_if_none_match,
);
)*
};
Expand Down Expand Up @@ -304,3 +307,71 @@ pub async fn test_read_with_dir_path(op: Operator) -> Result<()> {

Ok(())
}

/// Read with if_match should match, else get a ConditionNotMatch error.
pub async fn test_read_with_if_match(op: Operator) -> Result<()> {
if !op.info().capability().read_with_if_match {
return Ok(());
}

let path = "normal_file";

let meta = op.stat(path).await?;

let mut op_read = OpRead::default();
op_read = op_read.with_if_match("invalid_etag");

let res = op.read_with(path, op_read).await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch);

let mut op_read = OpRead::default();
op_read = op_read.with_if_match(meta.etag().expect("etag must exist"));

let bs = op
.read_with(path, op_read)
.await
.expect("read must succeed");
assert_eq!(bs.len(), 262144, "read size");
assert_eq!(
format!("{:x}", Sha256::digest(&bs)),
"e7541d0f50d2d5c79dc41f28ccba8e0cdfbbc8c4b1aa1a0110184ef0ef67689f",
"read content"
);

Ok(())
}

/// Read with if_none_match should match, else get a ConditionNotMatch error.
pub async fn test_read_with_if_none_match(op: Operator) -> Result<()> {
if !op.info().capability().read_with_if_none_match {
return Ok(());
}

let path = "normal_file";

let meta = op.stat(path).await?;

let mut op_read = OpRead::default();
op_read = op_read.with_if_none_match(meta.etag().expect("etag must exist"));

let res = op.read_with(path, op_read).await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch);

let mut op_read = OpRead::default();
op_read = op_read.with_if_none_match("invalid_etag");

let bs = op
.read_with(path, op_read)
.await
.expect("read must succeed");
assert_eq!(bs.len(), 262144, "read size");
assert_eq!(
format!("{:x}", Sha256::digest(&bs)),
"e7541d0f50d2d5c79dc41f28ccba8e0cdfbbc8c4b1aa1a0110184ef0ef67689f",
"read content"
);

Ok(())
}
49 changes: 43 additions & 6 deletions core/tests/behavior/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ macro_rules! behavior_write_tests {
test_reader_tail,
test_read_not_exist,
test_read_with_if_match,
test_read_with_if_none_match,
test_fuzz_range_reader,
test_fuzz_offset_reader,
test_fuzz_part_reader,
Expand Down Expand Up @@ -585,18 +586,54 @@ pub async fn test_read_with_if_match(op: Operator) -> Result<()> {

let meta = op.stat(&path).await?;

let mut op_if_match = OpRead::default();
op_if_match = op_if_match.with_if_match("invalid_etag");
let mut op_read = OpRead::default();
op_read = op_read.with_if_match("invalid_etag");

let res = op.read_with(&path, op_if_match).await;
let res = op.read_with(&path, op_read).await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch);

let mut op_if_match = OpRead::default();
op_if_match = op_if_match.with_if_match(meta.etag().expect("etag must exist"));
let mut op_read = OpRead::default();
op_read = op_read.with_if_match(meta.etag().expect("etag must exist"));

let bs = op
.read_with(&path, op_read)
.await
.expect("read must succeed");
assert_eq!(bs, content);

op.delete(&path).await.expect("delete must succeed");
Ok(())
}

/// Read with if_none_match should match, else get a ConditionNotMatch error.
pub async fn test_read_with_if_none_match(op: Operator) -> Result<()> {
if !op.info().capability().read_with_if_none_match {
return Ok(());
}

let path = uuid::Uuid::new_v4().to_string();
debug!("Generate a random file: {}", &path);
let (content, _) = gen_bytes();

op.write(&path, content.clone())
.await
.expect("write must succeed");

let meta = op.stat(&path).await?;

let mut op_read = OpRead::default();
op_read = op_read.with_if_none_match(meta.etag().expect("etag must exist"));

let res = op.read_with(&path, op_read).await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch);

let mut op_read = OpRead::default();
op_read = op_read.with_if_none_match("invalid_etag");

let bs = op
.read_with(&path, op_if_match)
.read_with(&path, op_read)
.await
.expect("read must succeed");
assert_eq!(bs, content);
Expand Down

0 comments on commit c6a37ad

Please sign in to comment.