/* * Copyright (2024) Volcengine * * Licensed 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 ve_tos_rust_sdk::asynchronous::bucket::BucketAPI; use ve_tos_rust_sdk::bucket::{CreateBucketInput, DeleteBucketInput, HeadBucketInput, ListBucketsInput}; use ve_tos_rust_sdk::enumeration::{ACLType, AzRedundancyType, StorageClassType}; use crate::common::asynchronous::create_async_context; use crate::common::gen_random_string; mod common; #[cfg(feature = "asynchronous")] #[test] fn test_main() { tokio_test::block_on(async { let context = create_async_context().await; let client = context.client(); let o = client.list_buckets(&ListBucketsInput::new()).await.unwrap(); assert!(o.request_id().len() > 0); assert!(o.buckets().len() >= 0); let bucket1 = gen_random_string(10); let bucket1 = bucket1.as_str(); let o = client.create_bucket(&CreateBucketInput::new(bucket1)).await.unwrap(); assert!(o.request_id().len() > 0); context.add_bucket(bucket1).await; let o = client.head_bucket(&HeadBucketInput::new(bucket1)).await.unwrap(); assert_eq!(o.storage_class().as_ref().unwrap(), &StorageClassType::StorageClassStandard); if let Some(az) = o.az_redundancy().as_ref() { assert_eq!(az, &AzRedundancyType::AzRedundancySingleAz); } let bucket2 = gen_random_string(40); let bucket2 = bucket2.as_str(); let mut input = CreateBucketInput::new(bucket2); input.set_acl(ACLType::ACLPublicRead); input.set_storage_class(StorageClassType::StorageClassIa); input.set_az_redundancy(AzRedundancyType::AzRedundancySingleAz); let o = client.create_bucket(&input).await.unwrap(); assert!(o.request_id().len() > 0); context.add_bucket(bucket2).await; match client.head_bucket(&HeadBucketInput::new(bucket2)).await { Ok(o) => { assert_eq!(o.storage_class().as_ref().unwrap(), &StorageClassType::StorageClassIa); if let Some(az) = o.az_redundancy().as_ref() { assert_eq!(az, &AzRedundancyType::AzRedundancySingleAz); } } Err(_) => { assert!(false); } } let bucket1 = self::gen_random_string(20); let o = client.create_bucket(&CreateBucketInput::new(bucket1.as_str())).await.unwrap(); assert!(o.request_id().len() > 0); let bucket2 = self::gen_random_string(20); let o = client.create_bucket(&CreateBucketInput::new(bucket2.as_str())).await.unwrap(); assert!(o.request_id().len() > 0); let mut count = 0; let o = client.list_buckets(&ListBucketsInput::new()).await.unwrap(); for bucket in o.buckets() { if bucket.name() == bucket1 || bucket.name() == bucket2 { count += 1; assert!(bucket.location().len() > 0); } } assert_eq!(count, 2); let o = client.delete_bucket(&DeleteBucketInput::new(bucket1.as_str())).await.unwrap(); assert!(o.request_id().len() > 0); let o = client.delete_bucket(&DeleteBucketInput::new(bucket2.as_str())).await.unwrap(); assert!(o.request_id().len() > 0); let mut count = 0; let o = client.list_buckets(&ListBucketsInput::new()).await.unwrap(); for bucket in o.buckets() { if bucket.name() == bucket1 || bucket.name() == bucket2 { count += 1; assert!(bucket.location().len() > 0); } } assert_eq!(count, 0); let e = client.head_bucket(&HeadBucketInput::new(context.non_exists_bucket())).await.expect_err(""); assert!(e.is_server_error()); let ex = e.as_server_error().unwrap(); assert_eq!(ex.status_code(), 404); context.tear_down().await; }) }