You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by "Xuanwo (via GitHub)" <gi...@apache.org> on 2023/05/22 13:53:12 UTC
[GitHub] [incubator-opendal] Xuanwo commented on a diff in pull request #2286: test(core): test for append
Xuanwo commented on code in PR #2286:
URL: https://github.com/apache/incubator-opendal/pull/2286#discussion_r1200549228
##########
core/tests/behavior/append.rs:
##########
@@ -0,0 +1,240 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use anyhow::Result;
+use opendal::ops::OpAppend;
+use opendal::EntryMode;
+use opendal::ErrorKind;
+use opendal::Operator;
+
+use super::utils::*;
+
+/// Test services that meet the following capability:
+///
+/// - can_read
+/// - can_write
+/// - can_append
+macro_rules! behavior_append_test {
+ ($service:ident, $($(#[$meta:meta])* $test:ident),*,) => {
+ paste::item! {
+ $(
+ #[test]
+ $(
+ #[$meta]
+ )*
+ fn [<append_ $test >]() -> anyhow::Result<()> {
+ match OPERATOR.as_ref() {
+ Some(op) if op.info().can_read()
+ && op.info().can_write()
+ && op.info().can_append() => RUNTIME.block_on($crate::append::$test(op.clone())),
+ Some(_) => {
+ log::warn!("service {} doesn't support append, ignored", opendal::Scheme::$service);
+ Ok(())
+ },
+ None => {
+ Ok(())
+ }
+ }
+ }
+ )*
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! behavior_append_tests {
+ ($($service:ident),*) => {
+ $(
+ behavior_append_test!(
+ $service,
+
+ test_append,
+ test_append_to_non_existing_target,
+ test_append_with_dir_path,
+ test_append_with_cache_control,
+ test_append_with_content_type,
+ test_append_with_content_disposition,
+
+ test_fuzz_appender,
+ );
+ )*
+ };
+}
+
+/// Test append to a file must success.
+pub async fn test_append(op: Operator) -> Result<()> {
+ let path = uuid::Uuid::new_v4().to_string();
+ let (content_one, size_one) = gen_bytes();
+ let (content_two, size_two) = gen_bytes();
+
+ op.write(&path, content_one.clone())
Review Comment:
This will fail on most object storage services. A path is only appendable while it's created by `append`.
##########
core/tests/behavior/append.rs:
##########
@@ -0,0 +1,240 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use anyhow::Result;
+use opendal::ops::OpAppend;
+use opendal::EntryMode;
+use opendal::ErrorKind;
+use opendal::Operator;
+
+use super::utils::*;
+
+/// Test services that meet the following capability:
+///
+/// - can_read
+/// - can_write
+/// - can_append
+macro_rules! behavior_append_test {
+ ($service:ident, $($(#[$meta:meta])* $test:ident),*,) => {
+ paste::item! {
+ $(
+ #[test]
+ $(
+ #[$meta]
+ )*
+ fn [<append_ $test >]() -> anyhow::Result<()> {
+ match OPERATOR.as_ref() {
+ Some(op) if op.info().can_read()
+ && op.info().can_write()
+ && op.info().can_append() => RUNTIME.block_on($crate::append::$test(op.clone())),
+ Some(_) => {
+ log::warn!("service {} doesn't support append, ignored", opendal::Scheme::$service);
+ Ok(())
+ },
+ None => {
+ Ok(())
+ }
+ }
+ }
+ )*
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! behavior_append_tests {
+ ($($service:ident),*) => {
+ $(
+ behavior_append_test!(
+ $service,
+
+ test_append,
+ test_append_to_non_existing_target,
+ test_append_with_dir_path,
+ test_append_with_cache_control,
+ test_append_with_content_type,
+ test_append_with_content_disposition,
+
+ test_fuzz_appender,
+ );
+ )*
+ };
+}
+
+/// Test append to a file must success.
+pub async fn test_append(op: Operator) -> Result<()> {
+ let path = uuid::Uuid::new_v4().to_string();
+ let (content_one, size_one) = gen_bytes();
+ let (content_two, size_two) = gen_bytes();
+
+ op.write(&path, content_one.clone())
+ .await
+ .expect("write file must success");
+
+ op.append(&path, content_two.clone())
+ .await
+ .expect("append file must success");
+
+ let bs = op.read(&path).await.expect("read file must success");
+
+ assert_eq!(bs.len(), size_one + size_two);
+ assert_eq!(bs[..size_one], content_one);
+ assert_eq!(bs[size_one..], content_two);
+
+ op.delete(&path).await.expect("delete file must success");
+
+ Ok(())
+}
+
+/// Test append to a non existing target must success.
+pub async fn test_append_to_non_existing_target(op: Operator) -> Result<()> {
+ let path = uuid::Uuid::new_v4().to_string();
+ let (content, size) = gen_bytes();
+
+ op.append(&path, content.clone())
+ .await
+ .expect_err("append to non existing target must success");
+
+ let bs = op.read(&path).await?;
+ assert_eq!(bs.len(), size);
+ assert_eq!(bs, content);
+
+ Ok(())
+}
+
+/// Test append to a directory path must fail.
+pub async fn test_append_with_dir_path(op: Operator) -> Result<()> {
Review Comment:
We don't need to test this. It doesn't worth.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org