Add basic integration tests for process endpoint
Some checks failed
/ deny (push) Waiting to run
/ tests (push) Waiting to run
/ check (aarch64-unknown-linux-musl) (push) Waiting to run
/ check (armv7-unknown-linux-musleabihf) (push) Waiting to run
/ check (x86_64-unknown-linux-musl) (push) Waiting to run
/ clippy (push) Has been cancelled

This commit is contained in:
asonix 2025-03-29 16:57:07 -05:00
parent b81eb306c0
commit 5c9d419cc8
2 changed files with 375 additions and 9 deletions

View file

@ -1,6 +1,12 @@
#![allow(dead_code)]
use std::future::Future;
use std::{future::Future, str::FromStr};
use opentelemetry_sdk::propagation::TraceContextPropagator;
use tracing::subscriber::set_global_default;
use tracing_error::ErrorLayer;
use tracing_log::LogTracer;
use tracing_subscriber::{fmt::format::FmtSpan, layer::SubscriberExt, EnvFilter, Layer, Registry};
pub fn with_pict_rs<F, Fut>(config: serde_json::Value, callback: F) -> color_eyre::Result<()>
where
@ -13,6 +19,10 @@ where
.block_on(async move {
tokio::task::LocalSet::new()
.run_until(async move {
if init_tracing().is_err() {
tracing::info!("tracing already enabled");
};
let mut pict_rs_handle = spawn_pict_rs(config);
// give time to spin up
@ -79,6 +89,25 @@ fn spawn_pict_rs(config: serde_json::Value) -> tokio::task::JoinHandle<()> {
})
}
fn init_tracing() -> color_eyre::Result<()> {
LogTracer::init()?;
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
let format_layer = tracing_subscriber::fmt::layer()
.with_test_writer()
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_filter(EnvFilter::from_str("info")?);
let subscriber = Registry::default()
.with(format_layer)
.with(ErrorLayer::default());
set_global_default(subscriber)?;
Ok(())
}
#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
pub enum PictRsResult<T> {
@ -115,14 +144,6 @@ pub struct File {
pub details: Details,
}
#[derive(Debug, serde::Deserialize)]
pub struct DetailsResponse {
pub msg: OkString,
#[serde(flatten)]
pub details: Details,
}
#[derive(Debug, serde::Deserialize)]
pub struct Details {
pub width: u16,

345
tests/process.rs Normal file
View file

@ -0,0 +1,345 @@
#![cfg(system_deps)]
use common::{
pict_rs_test_config, upload_form, with_pict_rs, Details, PictRsResult, UploadResponse,
};
mod common;
#[test]
fn can_identity_process_image() {
let address = "127.0.0.1:9400";
let config = pict_rs_test_config(address);
with_pict_rs(config, || async {
let client = reqwest::Client::new();
let form = upload_form(["./client-examples/cat.jpg"]).await;
let response = client
.post(format!("http://{address}/image"))
.multipart(form)
.send()
.await
.expect("send request");
let upload = response
.json::<PictRsResult<UploadResponse>>()
.await
.expect("valid response")
.unwrap();
let alias = &upload.files[0].file;
let original_details = &upload.files[0].details;
let response = client
.get(format!(
"http://{address}/image/process.jpg?alias={alias}&identity=true"
))
.send()
.await
.expect("send request");
assert!(response.status().is_success());
let bytes = response.bytes().await.expect("response body");
assert!(!bytes.is_empty());
let details_response = client
.get(format!(
"http://{address}/image/details/process.jpg?alias={alias}&identity=true"
))
.send()
.await
.expect("send request");
assert!(details_response.status().is_success());
let process_details = details_response
.json::<Details>()
.await
.expect("valid response");
assert_eq!(original_details.width, process_details.width);
assert_eq!(original_details.height, process_details.height);
assert_eq!(original_details.frames, process_details.frames);
})
.unwrap();
}
#[test]
fn can_crop_process_image() {
let address = "127.0.0.1:9401";
let config = pict_rs_test_config(address);
with_pict_rs(config, || async {
let client = reqwest::Client::new();
let form = upload_form(["./client-examples/cat.jpg"]).await;
let response = client
.post(format!("http://{address}/image"))
.multipart(form)
.send()
.await
.expect("send request");
let upload = response
.json::<PictRsResult<UploadResponse>>()
.await
.expect("valid response")
.unwrap();
let alias = &upload.files[0].file;
let original_details = &upload.files[0].details;
let response = client
.get(format!(
"http://{address}/image/process.jpg?alias={alias}&crop=1x1"
))
.send()
.await
.expect("send request");
assert!(response.status().is_success());
let bytes = response.bytes().await.expect("response body");
assert!(!bytes.is_empty());
let details_response = client
.get(format!(
"http://{address}/image/details/process.jpg?alias={alias}&crop=1x1"
))
.send()
.await
.expect("send request");
assert!(details_response.status().is_success());
let process_details = details_response
.json::<Details>()
.await
.expect("valid response");
assert_ne!(original_details.width, original_details.height);
assert_eq!(process_details.width, process_details.height);
})
.unwrap();
}
#[test]
fn can_resize_process_image() {
let address = "127.0.0.1:9402";
let config = pict_rs_test_config(address);
with_pict_rs(config, || async {
let client = reqwest::Client::new();
let form = upload_form(["./client-examples/cat.jpg"]).await;
let response = client
.post(format!("http://{address}/image"))
.multipart(form)
.send()
.await
.expect("send request");
let upload = response
.json::<PictRsResult<UploadResponse>>()
.await
.expect("valid response")
.unwrap();
let alias = &upload.files[0].file;
let original_details = &upload.files[0].details;
let response = client
.get(format!(
"http://{address}/image/process.jpg?alias={alias}&resize=512"
))
.send()
.await
.expect("send request");
assert!(response.status().is_success());
let bytes = response.bytes().await.expect("response body");
assert!(!bytes.is_empty());
let details_response = client
.get(format!(
"http://{address}/image/details/process.jpg?alias={alias}&resize=512"
))
.send()
.await
.expect("send request");
assert!(details_response.status().is_success());
let process_details = details_response
.json::<Details>()
.await
.expect("valid response");
assert!(original_details.width > 512);
assert!(original_details.height > 512);
assert!(process_details.width <= 512);
assert!(process_details.height <= 512);
// check aspect ratios
assert!(
(original_details.height as f64 / original_details.width as f64)
- (process_details.height as f64 / process_details.width as f64)
<= 0.001
)
})
.unwrap();
}
#[test]
fn can_resize_area_process_image() {
let address = "127.0.0.1:9403";
let config = pict_rs_test_config(address);
with_pict_rs(config, || async {
let client = reqwest::Client::new();
let form = upload_form(["./client-examples/cat.jpg"]).await;
let response = client
.post(format!("http://{address}/image"))
.multipart(form)
.send()
.await
.expect("send request");
let upload = response
.json::<PictRsResult<UploadResponse>>()
.await
.expect("valid response")
.unwrap();
let alias = &upload.files[0].file;
let original_details = &upload.files[0].details;
let response = client
.get(format!(
"http://{address}/image/process.jpg?alias={alias}&resize=.a625"
))
.send()
.await
.expect("send request");
assert!(response.status().is_success());
let bytes = response.bytes().await.expect("response body");
assert!(!bytes.is_empty());
let details_response = client
.get(format!(
"http://{address}/image/details/process.jpg?alias={alias}&resize=.a625"
))
.send()
.await
.expect("send request");
assert!(details_response.status().is_success());
let process_details = details_response
.json::<Details>()
.await
.expect("valid response");
assert!(original_details.width > 40);
assert!(original_details.height > 40);
assert!(process_details.width <= 40);
assert!(process_details.height <= 40);
// check aspect ratios
assert!(
(original_details.height as f64 / original_details.width as f64)
- (process_details.height as f64 / process_details.width as f64)
<= 0.001
)
})
.unwrap();
}
#[test]
fn can_thumbnail_process_image() {
let address = "127.0.0.1:9404";
let config = pict_rs_test_config(address);
with_pict_rs(config, || async {
let client = reqwest::Client::new();
let form = upload_form(["./client-examples/cat.jpg"]).await;
let response = client
.post(format!("http://{address}/image"))
.multipart(form)
.send()
.await
.expect("send request");
let upload = response
.json::<PictRsResult<UploadResponse>>()
.await
.expect("valid response")
.unwrap();
let alias = &upload.files[0].file;
let original_details = &upload.files[0].details;
let response = client
.get(format!(
"http://{address}/image/process.jpg?alias={alias}&thumbnail=512"
))
.send()
.await
.expect("send request");
assert!(response.status().is_success());
let bytes = response.bytes().await.expect("response body");
assert!(!bytes.is_empty());
let details_response = client
.get(format!(
"http://{address}/image/details/process.jpg?alias={alias}&thumbnail=512"
))
.send()
.await
.expect("send request");
assert!(details_response.status().is_success());
let process_details = details_response
.json::<Details>()
.await
.expect("valid response");
assert!(original_details.width > 512);
assert!(original_details.height > 512);
assert!(process_details.width <= 512);
assert!(process_details.height <= 512);
// check aspect ratios
assert!(
(original_details.height as f64 / original_details.width as f64)
- (process_details.height as f64 / process_details.width as f64)
<= 0.001
)
})
.unwrap();
}