You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/04/23 13:30:33 UTC

[incubator-opendal] branch main updated: core: Add behavior test for read_with_if_match & stat_with_if_match (#2088)

This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new c541811c core: Add behavior test for read_with_if_match & stat_with_if_match (#2088)
c541811c is described below

commit c541811cca0104d5a7561c143a8316b4cd7e712a
Author: Suyan <su...@gmail.com>
AuthorDate: Sun Apr 23 21:30:29 2023 +0800

    core: Add behavior test for read_with_if_match & stat_with_if_match (#2088)
    
    * test(core): test for read_if_match & stat_if_match
    
    Signed-off-by: suyanhanx <su...@gmail.com>
    
    * make fmt happy
    
    Signed-off-by: suyanhanx <su...@gmail.com>
    
    ---------
    
    Signed-off-by: suyanhanx <su...@gmail.com>
---
 core/tests/behavior/write.rs | 75 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)

diff --git a/core/tests/behavior/write.rs b/core/tests/behavior/write.rs
index bf2d5c18..4b76128d 100644
--- a/core/tests/behavior/write.rs
+++ b/core/tests/behavior/write.rs
@@ -21,7 +21,7 @@ use futures::AsyncSeekExt;
 use futures::StreamExt;
 use log::debug;
 use log::warn;
-use opendal::ops::OpRead;
+use opendal::ops::{OpRead, OpStat};
 use opendal::EntryMode;
 use opendal::ErrorKind;
 use opendal::Operator;
@@ -80,6 +80,7 @@ macro_rules! behavior_write_tests {
                 test_stat_with_special_chars,
                 test_stat_not_cleaned_path,
                 test_stat_not_exist,
+                test_stat_with_if_match,
                 test_stat_root,
                 test_read_full,
                 test_read_range,
@@ -88,6 +89,7 @@ macro_rules! behavior_write_tests {
                 test_reader_from,
                 test_reader_tail,
                 test_read_not_exist,
+                test_read_with_if_match,
                 test_fuzz_range_reader,
                 test_fuzz_offset_reader,
                 test_fuzz_part_reader,
@@ -245,6 +247,41 @@ pub async fn test_stat_not_exist(op: Operator) -> Result<()> {
     Ok(())
 }
 
+/// Stat with if_match should succeed, else get a 412(PreconditionFailed) error.
+pub async fn test_stat_with_if_match(op: Operator) -> Result<()> {
+    if !op.info().capability().stat_with_if_match {
+        return Ok(());
+    }
+
+    let path = uuid::Uuid::new_v4().to_string();
+    debug!("Generate a random file: {}", &path);
+    let (content, size) = gen_bytes();
+
+    op.write(&path, content.clone())
+        .await
+        .expect("write must succeed");
+
+    let meta = op.stat(&path).await?;
+    assert_eq!(meta.mode(), EntryMode::FILE);
+    assert_eq!(meta.content_length(), size as u64);
+
+    let mut op_stat = OpStat::default();
+    op_stat = op_stat.with_if_match("invalid_etag");
+
+    let res = op.stat_with(&path, op_stat).await;
+    assert!(res.is_err());
+    assert_eq!(res.unwrap_err().kind(), ErrorKind::PreconditionFailed);
+
+    let mut op_stat = OpStat::default();
+    op_stat = op_stat.with_if_match(meta.etag().expect("etag must exist"));
+
+    let result = op.stat_with(&path, op_stat).await;
+    assert!(result.is_ok());
+
+    op.delete(&path).await.expect("delete must succeed");
+    Ok(())
+}
+
 /// Root should be able to stat and returns DIR.
 pub async fn test_stat_root(op: Operator) -> Result<()> {
     let meta = op.stat("").await?;
@@ -433,6 +470,42 @@ pub async fn test_read_not_exist(op: Operator) -> Result<()> {
     Ok(())
 }
 
+// Read with if_match should match, else get a 412(Precondition Failed) error.
+pub async fn test_read_with_if_match(op: Operator) -> Result<()> {
+    if !op.info().capability().read_with_if_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_if_match = OpRead::default();
+    op_if_match = op_if_match.with_if_match("invalid_etag");
+
+    let res = op.read_with(&path, op_if_match).await;
+    assert!(res.is_err());
+    assert_eq!(res.unwrap_err().kind(), ErrorKind::PreconditionFailed);
+
+    let mut op_if_match = OpRead::default();
+    op_if_match = op_if_match.with_if_match(meta.etag().expect("etag must exist"));
+
+    let bs = op
+        .read_with(&path, op_if_match)
+        .await
+        .expect("read must succeed");
+    assert_eq!(bs, content);
+
+    op.delete(&path).await.expect("delete must succeed");
+    Ok(())
+}
+
 pub async fn test_fuzz_range_reader(op: Operator) -> Result<()> {
     let path = uuid::Uuid::new_v4().to_string();
     debug!("Generate a random file: {}", &path);