Skip to content

Commit

Permalink
Update test runner infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Oct 7, 2024
1 parent 22e7e26 commit d230283
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 300 deletions.
1 change: 0 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ divan.workspace = true
rand.workspace = true
tempfile.workspace = true
tempdir.workspace = true
testcontainers.workspace = true
297 changes: 3 additions & 294 deletions common/src/object_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,16 @@ where
}
}

#[cfg(test)]
mod testing {
pub mod testing {
use aws_config::{meta::region::RegionProviderChain, BehaviorVersion};
use aws_sdk_s3::config::Credentials;
use futures::Future;
use testcontainers::{core::WaitFor, ContainerAsync, Image};

pub struct MinIO;

pub trait MinIOExt {
async fn s3_config(&self) -> aws_sdk_s3::Config;
fn s3_config(&self) -> impl Future<Output = aws_sdk_s3::Config> + Send;
}

impl Image for MinIO {
Expand Down Expand Up @@ -388,294 +388,3 @@ mod testing {
config.to_builder().force_path_style(true).build()
}
}

#[cfg(test)]
mod tests {
use testcontainers::runners::AsyncRunner;

use super::{
testing::{minio_container, MinIOExt},
DeleteOptions, GetOptions, ObjectETag, ObjectStore, ObjectStoreOptions,
ObjectStoreResultExt, PutMode, PutOptions,
};

#[tokio::test]
async fn test_put_and_get_no_prefix_no_precondition() {
let minio = minio_container().start().await.unwrap();
let config = minio.s3_config().await;

let client = ObjectStore::new_from_config(
config,
ObjectStoreOptions {
bucket: "test".to_string(),
..Default::default()
},
);

client.ensure_bucket().await.unwrap();

let put_res = client
.put("test", "Hello, World".into(), PutOptions::default())
.await
.unwrap();

assert_eq!(
put_res.etag,
ObjectETag("\"600335e986d6c8ce1e348d20d6d16045\"".to_string())
);

let get_res = client.get("test", GetOptions::default()).await.unwrap();
assert_eq!(get_res.etag, put_res.etag);
assert_eq!(get_res.body, "Hello, World".as_bytes());
}

#[tokio::test]
async fn test_put_and_get_with_prefix_no_precondition() {
let minio = minio_container().start().await.unwrap();
let config = minio.s3_config().await;

// Put an object in the bucket with prefix.
// Put an object with the same filename in the bucket without prefix.
// Check that they are indeed different.
let client = ObjectStore::new_from_config(
config.clone(),
ObjectStoreOptions {
bucket: "test".to_string(),
prefix: Some("my-prefix".to_string()),
},
);

client.ensure_bucket().await.unwrap();

client
.put("test", "With my-prefix".into(), PutOptions::default())
.await
.unwrap();

{
let client = ObjectStore::new_from_config(
config,
ObjectStoreOptions {
bucket: "test".to_string(),
prefix: None,
},
);
client
.put("test", "Without prefix".into(), PutOptions::default())
.await
.unwrap();
}

let get_res = client.get("test", GetOptions::default()).await.unwrap();
assert_eq!(get_res.body, "With my-prefix".as_bytes());
}

#[tokio::test]
async fn test_get_with_etag() {
let minio = minio_container().start().await.unwrap();
let config = minio.s3_config().await;

let client = ObjectStore::new_from_config(
config.clone(),
ObjectStoreOptions {
bucket: "test".to_string(),
..Default::default()
},
);

client.ensure_bucket().await.unwrap();

let put_res = client
.put("test", "Hello, World".into(), PutOptions::default())
.await
.unwrap();

client
.get(
"test",
GetOptions {
etag: Some(put_res.etag),
},
)
.await
.unwrap();

let response = client
.get(
"test",
GetOptions {
etag: Some(ObjectETag("bad etag".to_string())),
},
)
.await;

assert!(response.is_err());
assert!(response.unwrap_err().is_precondition());
}

#[tokio::test]
async fn test_put_with_overwrite() {
let minio = minio_container().start().await.unwrap();
let config = minio.s3_config().await;

let client = ObjectStore::new_from_config(
config.clone(),
ObjectStoreOptions {
bucket: "test".to_string(),
..Default::default()
},
);

client.ensure_bucket().await.unwrap();

let put_res = client
.put("test", "Hello, World".into(), PutOptions::default())
.await
.unwrap();

let original_etag = put_res.etag;

let put_res = client
.put(
"test",
"Something else".into(),
PutOptions {
mode: PutMode::Overwrite,
..Default::default()
},
)
.await
.unwrap();

assert_ne!(put_res.etag, original_etag);
}

#[tokio::test]
async fn test_put_with_create() {
let minio = minio_container().start().await.unwrap();
let config = minio.s3_config().await;

let client = ObjectStore::new_from_config(
config.clone(),
ObjectStoreOptions {
bucket: "test".to_string(),
..Default::default()
},
);

client.ensure_bucket().await.unwrap();

client
.put(
"test",
"Hello, World".into(),
PutOptions {
mode: PutMode::Create,
..Default::default()
},
)
.await
.unwrap();

let response = client
.put(
"test",
"Something else".into(),
PutOptions {
mode: PutMode::Create,
..Default::default()
},
)
.await;

assert!(response.is_err());
assert!(response.unwrap_err().is_precondition());
}

#[tokio::test]
async fn test_put_with_update() {
let minio = minio_container().start().await.unwrap();
let config = minio.s3_config().await;

let client = ObjectStore::new_from_config(
config.clone(),
ObjectStoreOptions {
bucket: "test".to_string(),
..Default::default()
},
);

client.ensure_bucket().await.unwrap();

let response = client
.put(
"test",
"Hello, World".into(),
PutOptions {
mode: PutMode::Create,
..Default::default()
},
)
.await
.unwrap();

let original_etag = response.etag;

let response = client
.put(
"test",
"Something else".into(),
PutOptions {
mode: PutMode::Update("bad etag".to_string().into()),
..Default::default()
},
)
.await;
assert!(response.is_err());
assert!(response.unwrap_err().is_precondition());

let response = client
.put(
"test",
"Something else".into(),
PutOptions {
mode: PutMode::Update(original_etag.clone()),
..Default::default()
},
)
.await
.unwrap();

assert_ne!(response.etag, original_etag);
}

#[tokio::test]
async fn test_delete() {
let minio = minio_container().start().await.unwrap();
let config = minio.s3_config().await;

let client = ObjectStore::new_from_config(
config.clone(),
ObjectStoreOptions {
bucket: "test".to_string(),
..Default::default()
},
);

client.ensure_bucket().await.unwrap();

client
.put("test", "Hello, World".into(), PutOptions::default())
.await
.unwrap();

client
.delete("test", DeleteOptions::default())
.await
.unwrap();

let response = client.get("test", GetOptions::default()).await;
assert!(response.is_err());
assert!(response.unwrap_err().is_not_found());
}
}
Loading

0 comments on commit d230283

Please sign in to comment.