Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testcontainer refactoring #4

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ reqwest = { version = "0.12", default-features = false, features = [
axum = "0.7"
serde = "1.0"
serde_json = "1.0"
tokio = { version = "1", features = ["macros"] }
tokio = { version = "1", features = ["macros", "sync"] }
image = { version = "0.25", features = ["rayon"] }

miniaturs_shared = { path = "../shared" }
Expand All @@ -36,5 +36,6 @@ bytes = "1.7"
tower-http = { version = "0.6.1", features = ["catch-panic"] }

[dev-dependencies]
ctor = "0.2.8"
testcontainers = { version = "0.23" }
testcontainers-modules = { version = "0.11", features = ["localstack"] }
30 changes: 8 additions & 22 deletions server/src/infra/image_caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,18 @@ mod tests {

use aws_config::{meta::region::RegionProviderChain, BehaviorVersion};
use aws_sdk_s3::{self as s3, primitives::ByteStream};
use testcontainers::{runners::AsyncRunner, ContainerAsync, ImageExt};
use testcontainers_modules::localstack::LocalStack;
use tokio::sync::OnceCell;

use super::*;
use crate::test_utils::{localstack_node, TestResult};

// Holds the shared Localstack container; if it's not held here, the container ref gets dropped and the _actual_ container
// gets stopped
static LOCALSTACK_NODE: OnceCell<ContainerAsync<LocalStack>> = OnceCell::const_new();
// Holds the shared S3 client that refers to the above; use `s3_client().await` to get it
static S3_CLIENT: OnceCell<s3::Client> = OnceCell::const_new();
// Bucket, static because we assume the app is passed a created one.
static S3_BUCKET: OnceCell<String> = OnceCell::const_new();

type TestResult = Result<(), Box<dyn std::error::Error + 'static>>;

#[tokio::test]
async fn test_s3() -> TestResult {
async fn test_s3() -> TestResult<()> {
let client = s3_client().await;

let bucket = "mybucket";
Expand Down Expand Up @@ -282,7 +276,7 @@ mod tests {
}

#[test]
fn test_cache_key() -> TestResult {
fn test_cache_key() -> TestResult<()> {
let req = ImageResizeRequest {
requested_image_url: "https://beachape.com/images/something.png".to_string(),
operations: Operations::build(&Some(ImageResize {
Expand All @@ -296,7 +290,7 @@ mod tests {
}

#[test]
fn test_metadata() -> TestResult {
fn test_metadata() -> TestResult<()> {
let req = ImageResizedCacheRequest {
request: ImageResizeRequest {
requested_image_url: "https://beachape.com/images/something.png".to_string(),
Expand All @@ -318,7 +312,7 @@ mod tests {
}

#[tokio::test]
async fn test_s3_image_cacher_get_does_not_exist() -> TestResult {
async fn test_s3_image_cacher_get_does_not_exist() -> TestResult<()> {
let client = s3_client().await.clone();
let bucket_name = s3_bucket().await.clone();
let s3_image_cacher = S3ImageCacher {
Expand All @@ -339,7 +333,7 @@ mod tests {
}

#[tokio::test]
async fn test_s3_image_cacher_set_get() -> TestResult {
async fn test_s3_image_cacher_set_get() -> TestResult<()> {
let client = s3_client().await.clone();
let bucket_name = s3_bucket().await.clone();
let s3_image_cacher = S3ImageCacher {
Expand Down Expand Up @@ -370,7 +364,7 @@ mod tests {
}

#[tokio::test]
async fn test_s3_image_cacher_setting_metadata_in_s3() -> TestResult {
async fn test_s3_image_cacher_setting_metadata_in_s3() -> TestResult<()> {
let client = s3_client().await.clone();
let bucket_name = s3_bucket().await.clone();
let s3_image_cacher = S3ImageCacher {
Expand Down Expand Up @@ -430,15 +424,7 @@ mod tests {
async fn s3_client() -> &'static s3::Client {
S3_CLIENT
.get_or_init(|| async {
let node = LOCALSTACK_NODE
.get_or_init(|| async {
LocalStack::default()
.with_env_var("SERVICES", "s3")
.start()
.await
.expect("Localstack to start properly")
})
.await;
let node = localstack_node().await;
let host_port = node
.get_host_port_ipv4(4566)
.await
Expand Down
Loading