From 674d345a9fa2bd88b1610126f4ed81583b0e2aaf Mon Sep 17 00:00:00 2001 From: aLinChe <140829724+oeasy1412@users.noreply.github.com> Date: Sat, 26 Apr 2025 15:56:38 +0800 Subject: [PATCH] feat(ci): add integration_test for deploy proxy delete in namespace 'default' (#90) --- crates/app/tests/integration_test.rs | 102 +++++++++++++++++++++++++ crates/provider/src/handlers/delete.rs | 2 +- crates/provider/src/handlers/deploy.rs | 2 +- 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 crates/app/tests/integration_test.rs diff --git a/crates/app/tests/integration_test.rs b/crates/app/tests/integration_test.rs new file mode 100644 index 0000000..921a699 --- /dev/null +++ b/crates/app/tests/integration_test.rs @@ -0,0 +1,102 @@ +use actix_web::{App, web}; +use provider::{ + handlers::{delete::delete_handler, deploy::deploy_handler}, + proxy::proxy_handler::proxy_handler, + types::config::FaaSConfig, +}; +use service::containerd_manager::ContainerdManager; + +mod integration_tests { + use super::*; + use actix_web::http::StatusCode; + use actix_web::test; + use serde_json::json; + + #[actix_web::test] + #[ignore] + async fn test_handlers_in_order() { + dotenv::dotenv().ok(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + let socket_path = std::env::var("SOCKET_PATH") + .unwrap_or_else(|_| "/run/containerd/containerd.sock".to_string()); + ContainerdManager::init(&socket_path).await; + + let faas_config = FaaSConfig::new(); + + let app = test::init_service( + App::new() + .app_data(web::Data::new(faas_config)) + .route("/system/functions", web::post().to(deploy_handler)) + .route("/system/functions", web::delete().to(delete_handler)) + .route("/function/{name}{path:/?.*}", web::to(proxy_handler)), + ) + .await; + + // test proxy no-found-function in namespace 'default' + let req = test::TestRequest::get() + .uri("/function/test-no-found-function") + .to_request(); + let resp = test::call_service(&app, req).await; + assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + let response_body = test::read_body(resp).await; + let response_str = std::str::from_utf8(&response_body).unwrap(); + assert!(response_str.contains("Failed to get function")); + + // test delete no-found-function in namespace 'default' + let req = test::TestRequest::delete() + .uri("/system/functions") + .set_json(json!({"function_name": "test-no-found-function"})) + .to_request(); + let resp = test::call_service(&app, req).await; + assert_eq!(resp.status(), StatusCode::NOT_FOUND); + let response_body = test::read_body(resp).await; + let response_str = std::str::from_utf8(&response_body).unwrap(); + assert!( + response_str + .contains("Function 'test-no-found-function' not found in namespace 'default'") + ); + + // test deploy in namespace 'default' + let req = test::TestRequest::post() + .uri("/system/functions") + .set_json(json!({ + "function_name": "test-function", + "image": "docker.io/library/nginx:alpine" + })) + .to_request(); + let resp = test::call_service(&app, req).await; + assert_eq!( + resp.status(), + StatusCode::ACCEPTED, + "check whether the container has been existed" + ); + + let response_body = test::read_body(resp).await; + let response_str = std::str::from_utf8(&response_body).unwrap(); + log::info!("{}", response_str); + assert!(response_str.contains("Function test-function deployment initiated successfully.")); + + // test proxy in namespace 'default' + let req = test::TestRequest::get() + .uri("/function/test-function") + .to_request(); + let resp = test::call_service(&app, req).await; + assert_eq!(resp.status(), StatusCode::OK); + + let response_body = test::read_body(resp).await; + let response_str = std::str::from_utf8(&response_body).unwrap(); + assert!(response_str.contains("Welcome to nginx!")); + + // test delete in namespace 'default' + let req = test::TestRequest::delete() + .uri("/system/functions") + .set_json(json!({"function_name": "test-function"})) + .to_request(); + let resp = test::call_service(&app, req).await; + assert_eq!(resp.status(), StatusCode::OK); + + let response_body = test::read_body(resp).await; + let response_str = std::str::from_utf8(&response_body).unwrap(); + assert!(response_str.contains("Function test-function deleted successfully.")); + } +} diff --git a/crates/provider/src/handlers/delete.rs b/crates/provider/src/handlers/delete.rs index 922d895..b8cd6e1 100644 --- a/crates/provider/src/handlers/delete.rs +++ b/crates/provider/src/handlers/delete.rs @@ -15,7 +15,7 @@ pub async fn delete_handler(info: web::Json) -> impl Respon match delete(&function_name, &namespace).await { Ok(()) => { - HttpResponse::Ok().body(format!("function {} deleted successfully", function_name)) + HttpResponse::Ok().body(format!("Function {} deleted successfully.", function_name)) } Err(e) => e.error_response(), } diff --git a/crates/provider/src/handlers/deploy.rs b/crates/provider/src/handlers/deploy.rs index 5c4f99f..e706ad4 100644 --- a/crates/provider/src/handlers/deploy.rs +++ b/crates/provider/src/handlers/deploy.rs @@ -23,7 +23,7 @@ pub async fn deploy_handler(info: web::Json) -> impl Respond match deploy(&config).await { Ok(()) => HttpResponse::Accepted().body(format!( - "Function {} deployment initiated successfully .", + "Function {} deployment initiated successfully.", config.service )), Err(e) => HttpResponse::InternalServerError().body(format!(