Skip to content

Commit

Permalink
move test_handle_s3_event to unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
daidokoro committed Dec 22, 2023
1 parent 341e714 commit 7c3c781
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
71 changes: 71 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,74 @@ pub async fn handle_s3_event(s3_event: S3Event) -> Result<(String, String), Erro
// .to_owned();
// Ok((bucket, key))
// }

#[cfg(test)]
mod test {
use aws_lambda_events::event::s3::S3Event;
use super::*;

// Note: we test the s3_event handler directly here, since the integration tests will bypass it
// using the mock s3 client. The [handle_cloudwatch_logs_event] is however invoked as part of the
// integration test workflow.
#[tokio::test]
async fn test_handle_s3_event() {
let s3_event_str = |bucket: &str, key: &str| -> String {
format!(
r#"{{
"Records": [
{{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "eu-west-1",
"eventTime": "1970-01-01T00:00:00.000Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {{
"principalId": "EXAMPLE"
}},
"requestParameters": {{
"sourceIPAddress": "127.0.0.1"
}},
"responseElements": {{
"x-amz-request-id": "EXAMPLE123456789",
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
}},
"s3": {{
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {{
"name": "{}",
"ownerIdentity": {{
"principalId": "EXAMPLE"
}},
"arn": "arn:aws:s3:::{}"
}},
"object": {{
"key": "{}",
"size": 311000048,
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901"
}}
}}
}}
]
}}"#,
bucket, bucket, key
)
};

// test normal s3 event
let s3_event = s3_event_str("coralogix-serverless-repo", "coralogix-aws-shipper/s3.log");
let evt: S3Event = serde_json::from_str(s3_event.as_str()).expect("failed to parse s3_event");
let (bucket, key) = handle_s3_event(evt).await.unwrap();
assert_eq!(bucket, "coralogix-serverless-repo");
assert_eq!(key, "coralogix-aws-shipper/s3.log");

// test s3 event with spaces in key name (note: aws event replaces spaces with +)
let s3_event = s3_event_str("coralogix-serverless-repo", "coralogix-aws-shipper/s3+with+spaces.log");
let evt: S3Event = serde_json::from_str(s3_event.as_str()).expect("failed to parse s3_event");
let (bucket, key) = handle_s3_event(evt).await.unwrap();
assert_eq!(bucket, "coralogix-serverless-repo");
assert_eq!(key, "coralogix-aws-shipper/s3 with spaces.log");
}

}
24 changes: 1 addition & 23 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::string::String;
use std::sync::Arc;
use std::sync::Mutex;

fn s3event_string(bucket: &str, key: &str) -> String {
pub fn s3event_string(bucket: &str, key: &str) -> String {
format!(
r#"{{
"Records": [
Expand Down Expand Up @@ -583,8 +583,6 @@ async fn run_blocking_and_newline_pattern() {
let bulks = exporter.take_bulks();
assert!(bulks.is_empty());

println!("{:?}", exporter);

let singles = exporter.take_singles();
assert_eq!(singles.len(), 1);
assert_eq!(singles[0].entries.len(), 1);
Expand Down Expand Up @@ -629,23 +627,3 @@ async fn test_blocking_and_newline_pattern() {
.await;
}


// Note: we test the s3_event handler directly here, since the integration tests above will bypass it
// using the mock s3 client. The cloudwatch and sns events are not bypassed, since they don't use
// the s3client, as such, they are covered by the integration tests above.
#[tokio::test]
async fn test_s3_event_handler() {
// test normal s3 event
let s3_event = s3event_string("coralogix-serverless-repo", "coralogix-aws-shipper/s3.log");
let evt: S3Event = serde_json::from_str(s3_event.as_str()).expect("failed to parse s3_event");
let (bucket, key) = coralogix_aws_shipper::handle_s3_event(evt).await.unwrap();
assert_eq!(bucket, "coralogix-serverless-repo");
assert_eq!(key, "coralogix-aws-shipper/s3.log");

// test s3 event with spaces in key name (note: aws event replaces spaces with +)
let s3_event = s3event_string("coralogix-serverless-repo", "coralogix-aws-shipper/s3+with+spaces.log");
let evt: S3Event = serde_json::from_str(s3_event.as_str()).expect("failed to parse s3_event");
let (bucket, key) = coralogix_aws_shipper::handle_s3_event(evt).await.unwrap();
assert_eq!(bucket, "coralogix-serverless-repo");
assert_eq!(key, "coralogix-aws-shipper/s3 with spaces.log");
}

0 comments on commit 7c3c781

Please sign in to comment.