Skip to content

Commit

Permalink
fix:add ut for write option if_none_match
Browse files Browse the repository at this point in the history
Signed-off-by: LeeHao <HaozzaLi@gmail.com>
  • Loading branch information
ForestLH committed Sep 21, 2024
1 parent bfb4fea commit 77a98cc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ impl Access for S3Backend {
write_can_multi: true,
write_with_cache_control: true,
write_with_content_type: true,
write_with_if_none_match: true,
write_with_user_metadata: true,

// The min multipart size of S3 is 5 MiB.
Expand Down
2 changes: 2 additions & 0 deletions core/src/types/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub struct Capability {
pub write_with_content_disposition: bool,
/// If operator supports write with cache control.
pub write_with_cache_control: bool,
/// If operator supports write with if none match.
pub write_with_if_none_match: bool,
/// If operator supports write with user defined metadata
pub write_with_user_metadata: bool,
/// write_multi_max_size is the max size that services support in write_multi.
Expand Down
11 changes: 7 additions & 4 deletions core/src/types/operator/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,16 +1207,19 @@ impl Operator {
///
/// This feature can be used to check if the file already exists.
/// This prevents overwriting of existing objects with identical key names.
/// And must use the * (asterisk) value with this parameter
/// Users can use *(asterisk) to verify if a file already exists by matching with any ETag.
/// Note: S3 only support use *(asterisk).
///
/// If file exists, an error with kind [`ErrorKind::ConditionNotMatch`] will be returned.
///
/// ```no_run
/// # use opendal::Result;
/// # use opendal::{ErrorKind, Result};
/// use opendal::Operator;
/// # async fn test(op: Operator, etag: &str) -> Result<()> {
/// let bs = b"hello, world!".to_vec();
/// let mut metadata = op.write_with("path/to/file", bs).if_none_match("*").await?;
/// let res = op.write_with("path/to/file", bs).if_none_match("*").await;
/// assert!(res.is_err());
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch);
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1264,7 +1267,7 @@ impl Operator {
}

let context = WriteContext::new(inner, path, args, options);
let mut w = Writer::new(context).await?; // 这个函数就会调用S3的write函数
let mut w = Writer::new(context).await?;
w.write(bs).await?;
w.close().await?;
Ok(())
Expand Down
22 changes: 22 additions & 0 deletions core/tests/behavior/async_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn tests(op: &Operator, tests: &mut Vec<Trial>) {
test_write_with_cache_control,
test_write_with_content_type,
test_write_with_content_disposition,
test_write_with_if_none_match,
test_write_with_user_metadata,
test_writer_write,
test_writer_write_with_overwrite,
Expand Down Expand Up @@ -624,3 +625,24 @@ pub async fn test_writer_write_with_overwrite(op: Operator) -> Result<()> {
op.delete(&path).await.expect("delete must succeed");
Ok(())
}

/// Write an exists file with if_none_match should match, else get a ConditionNotMatch error.
pub async fn test_write_with_if_none_match(op: Operator) -> Result<()> {
if !op.info().full_capability().write_with_if_none_match {
return Ok(());
}

let (path, content, _) = TEST_FIXTURE.new_file(op.clone());

op.write(&path, content.clone())
.await
.expect("write must succeed");
let res = op
.write_with(&path, content.clone())
.if_none_match("*")
.await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch);

Ok(())
}

0 comments on commit 77a98cc

Please sign in to comment.