object_store/
test_util.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::env;
16
17use crate::{ObjectStore, Result};
18
19/// Temp folder for object store test
20pub struct TempFolder {
21    store: ObjectStore,
22    // The path under root.
23    path: String,
24}
25
26impl TempFolder {
27    pub fn new(store: &ObjectStore, path: &str) -> Self {
28        Self {
29            store: store.clone(),
30            path: path.to_string(),
31        }
32    }
33
34    pub async fn remove_all(&self) -> Result<()> {
35        self.store.remove_all(&self.path).await
36    }
37}
38
39/// Test s3 config from environment variables
40#[derive(Debug)]
41pub struct TestS3Config {
42    pub root: String,
43    pub access_key_id: String,
44    pub secret_access_key: String,
45    pub bucket: String,
46    pub region: Option<String>,
47}
48
49/// Returns s3 test config, return None if not found.
50pub fn s3_test_config() -> Option<TestS3Config> {
51    if let Ok(b) = env::var("GT_S3_BUCKET") {
52        if !b.is_empty() {
53            return Some(TestS3Config {
54                root: uuid::Uuid::new_v4().to_string(),
55                access_key_id: env::var("GT_S3_ACCESS_KEY_ID").ok()?,
56                secret_access_key: env::var("GT_S3_ACCESS_KEY").ok()?,
57                bucket: env::var("GT_S3_BUCKET").ok()?,
58                region: Some(env::var("GT_S3_REGION").ok()?),
59            });
60        }
61    }
62
63    None
64}