From af207dd9caf1f0a720a601262edfe20470d3b212 Mon Sep 17 00:00:00 2001 From: Howard <139238193+howard-oc@users.noreply.github.com> Date: Wed, 1 May 2024 23:11:04 +0100 Subject: [PATCH] fix: enabled simple tests Introduce a promise test which is to copy files --- .env | 12 ++--- .rustfmt.toml | 6 +++ Cargo.toml | 3 +- src/lib.rs | 43 ++++++++---------- src/pipeline/mod.rs | 15 ++++-- src/promise/mod.rs | 14 ++++-- tests/mod.rs | 93 ++++++++++++++++++++++---------------- tests/test-output/.gitkeep | 1 + 8 files changed, 107 insertions(+), 80 deletions(-) create mode 100644 .rustfmt.toml create mode 100644 tests/test-output/.gitkeep diff --git a/.env b/.env index 24852bf..4527a79 100644 --- a/.env +++ b/.env @@ -1,13 +1,11 @@ -RUST_LOG=debug -WORKSPACE=/Users/hhill/work/oc -BASE_INSTANCE="$WORKSPACE/promise-flink/internal/configure-pipeline/resources/minimal-flinkdep-manifest.yaml" +BASE_INSTANCE="$WORKSPACE/resources/minimal-flinkdep-manifest.yaml" -DEPENDENCIES_DIR="$WORKSPACE/promise-flink/internal/configure-pipeline/dependencies" +DEPENDENCIES_DIR="$WORKSPACE/dependencies" -RESOURCES_DIR="$WORKSPACE/promise-flink/internal/configure-pipeline/resources" +RESOURCES_DIR="$WORKSPACE/resources" -KRATIX_INPUT="$WORKSPACE/promise-flink/internal/configure-pipeline/tests/test-input" +KRATIX_INPUT="$WORKSPACE/tests/test-input" -KRATIX_OUTPUT="$WORKSPACE/promise-flink/internal/configure-pipeline/tests/test-output" +KRATIX_OUTPUT="$WORKSPACE/tests/test-output" diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..87a648f --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,6 @@ +max_width = 100 +tab_spaces = 4 +format_code_in_doc_comments = true +imports_granularity = "Crate" +imports_layout = "Vertical" +wrap_comments = true \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index c690d04..1cd8614 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ dotenv = "0.15.0" log = "0.4.21" serde_yaml = "0.9.34" toml = "0.5" -log4rs = "1.3.0" \ No newline at end of file +pretty_env_logger = "0.5.0" +env_logger = "0.11.3" diff --git a/src/lib.rs b/src/lib.rs index 4e32cce..6449d3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,12 +3,10 @@ pub mod promise; use crate::pipeline::PipelineConfig; use dotenv::dotenv; use log; -use std::{env, process}; - -use log::LevelFilter; -use log4rs::append::console::ConsoleAppender; -use log4rs::config::{Appender, Root}; -use log4rs::Config; +use std::{ + env, + process, +}; // Structure to hold potential errors #[derive(Debug)] @@ -20,6 +18,20 @@ pub trait ResourceRequest { fn transform(&self, conf: &PipelineConfig) -> String; } +/// Runs the Kratix pipeline based on the specified workflow type. +/// +/// This function checks the KRATIX_WORKFLOW_TYPE environment variable and +/// executes the appropriate workflow, such as "promise", "resource", or +/// "request". It handles file copying, resource transformation, and other tasks +/// required for the pipeline execution. +/// +/// # Example +/// ``` +/// use std::env; +/// env::set_var("KRATIX_WORKFLOW_TYPE", "promise"); +/// let result = kratix_utils::run_pipeline(); +/// assert_eq!(result.workflow_type(), "promise"); +/// ``` pub fn run_pipeline() -> PipelineConfig { #[derive(Clone)] pub struct MyPromise { @@ -42,14 +54,6 @@ pub fn run_pipeline() -> PipelineConfig { pub fn run_custom_pipeline(_request: Option) -> PipelineConfig { dotenv().ok(); - let stdout = ConsoleAppender::builder().build(); - let config = Config::builder() - .appender(Appender::builder().build("stdout", Box::new(stdout))) - .build(Root::builder().appender("stdout").build(LevelFilter::Trace)) - .unwrap(); - - let _handle = log4rs::init_config(config).unwrap(); - // Validate environment variables up front match validate_env_vars() { Ok(()) => (), // Everything is good, proceed @@ -110,18 +114,9 @@ pub fn run_custom_pipeline(_request: Option) -> PipelineCo config.kratix_input_dir(), ); } - "request" => { - log::debug!(" 1. transform request"); - // Fullfil resource_request.yaml - promise::transform( - config.res_dir(), - config.base_instance(), - config.kratix_output_dir(), - config.kratix_input_dir(), - ); - } _ => { log::error!("No workflow_type"); + panic!("No workflow_type"); } } diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index 961a6b6..f0e64ca 100644 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -1,9 +1,13 @@ use log; -use std::fs; -use std::io::Result; -use std::path::Path; -use yaml_rust2::Yaml; -use yaml_rust2::YamlLoader; +use std::{ + fs, + io::Result, + path::Path, +}; +use yaml_rust2::{ + Yaml, + YamlLoader, +}; mod pipeline_config; pub use pipeline_config::PipelineConfig; // Re-export PipelineConfig @@ -42,6 +46,7 @@ pub fn copy_files(source_dir: &str, destination_dir: &str) -> Result<()> { #[allow(dead_code)] pub fn list_files_recursively(path: &str) { + log::debug!("list_files_recursively for {}", path); if let Ok(entries) = fs::read_dir(path) { for entry in entries { if let Ok(entry) = entry { diff --git a/src/promise/mod.rs b/src/promise/mod.rs index 7d0e0cd..0f7f4f9 100644 --- a/src/promise/mod.rs +++ b/src/promise/mod.rs @@ -1,4 +1,7 @@ -use serde_yaml::{to_string, Value}; +use serde_yaml::{ + to_string, + Value, +}; use std::fs::read_to_string; use std::fs::write; // Import 'write' for generic write operations, File for opening use std::io::Error; // Import ErrorKind @@ -45,11 +48,14 @@ pub fn transform(_res_dir: &str, _res_path: &str, _kout_dir: &str, _kin_dir: &st // let new_name = kin_doc["spec"]["name"].as_str().unwrap().to_string(); - // log::debug!("Template Instance Name {}",base_instance["metadata"]["name"].as_str().unwrap().to_string()); + // log::debug!("Template Instance Name + // {}",base_instance["metadata"]["name"].as_str().unwrap().to_string()); - // base_instance["metadata"]["name"] = serde_yaml::Value::String(new_name.clone()); + // base_instance["metadata"]["name"] = + // serde_yaml::Value::String(new_name.clone()); - // log::debug!("Promise Request Name {}",base_instance["metadata"]["name"].as_str().unwrap().to_string()); + // log::debug!("Promise Request Name + // {}",base_instance["metadata"]["name"].as_str().unwrap().to_string()); let new_kout_path = format!("{}/flink-instance.yaml", _kout_dir); log::debug!("kratix output {}", new_kout_path); diff --git a/tests/mod.rs b/tests/mod.rs index 62247a4..e788bfd 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,48 +1,63 @@ -// use kratix_utils::pipeline::PipelineConfig; -// use kratix_utils::promise; -// use std::env; - -fn sqrt(number: f64) -> Result { - if number >= 0.0 { - Ok(number.powf(0.5)) - } else { - Err("negative floats don't have square roots".to_owned()) - } -} +use kratix_utils::{pipeline::PipelineConfig, run_custom_pipeline, ResourceRequest}; +use std::{env, path::Path}; #[cfg(test)] mod tests { use super::*; + const WORKSPACE: &str = "WORKSPACE"; + const KRATIX_WORKFLOW_TYPE: &str = "KRATIX_WORKFLOW_TYPE"; + + #[derive(Clone)] + pub struct MyPromise { + pub params: String, + } + + impl ResourceRequest for MyPromise { + fn transform(&self, _conf: &PipelineConfig) -> String { + let new_kin_path = format!("{}/object.yaml", _conf.kratix_input_dir()); + + format!("{} modify {:?}", self.params, new_kin_path) + } + } + + #[test] + fn test_list_files_recursively() -> Result<(), String> { + let current_dir = env::current_dir().unwrap(); + env::set_var(WORKSPACE, current_dir); + let x = "tests/test-input"; + kratix_utils::pipeline::list_files_recursively(x); + + Ok(()) + } + #[test] - fn test_resource_request() -> Result<(), String> { - let x = 4.0; - - // Extract validated environment variables - // let workflow_type = env::var("KRATIX_WORKFLOW_TYPE").unwrap(); - // let base_instance = env::var("BASE_INSTANCE").unwrap(); - // let dep_dir = env::var("DEPENDENCIES_DIR").unwrap(); - // let res_dir = env::var("RESOURCES_DIR").unwrap(); - // let kratix_input_dir = env::var("KRATIX_INPUT").unwrap(); - // let kratix_output_dir = env::var("KRATIX_OUTPUT").unwrap(); - - // let config = PipelineConfig::new( - // &base_instance, - // &res_dir, - // &dep_dir, - // &kratix_output_dir, - // &kratix_input_dir, - // &workflow_type, - // ); - - // promise::transform( - // config.res_dir(), - // config.base_instance(), - // config.kratix_output_dir(), - // config.kratix_input_dir(), - // ); - - assert_eq!(sqrt(x)?.powf(2.0), x); + fn test_promise_request() -> Result<(), String> { + init_logger(); + + let current_dir = env::current_dir().unwrap(); + env::set_var(WORKSPACE, current_dir); + env::set_var(KRATIX_WORKFLOW_TYPE, "promise"); + + let request = MyPromise { + params: String::from("(custom)"), + }; + + let _result = run_custom_pipeline(Some(request)); + + let new_kout_path = format!("{}/.gitkeep", _result.kratix_output_dir()); + assert_eq!(Path::new(&new_kout_path).exists(), true); + Ok(()) } + + fn init_logger() { + let _ = env_logger::builder() + // Include all events in tests + .filter_level(log::LevelFilter::max()) + // Ensure events are captured by `cargo test` + .is_test(true) + // Ignore errors initializing the logger if tests race to configure it + .try_init(); + } } diff --git a/tests/test-output/.gitkeep b/tests/test-output/.gitkeep new file mode 100644 index 0000000..45adbb2 --- /dev/null +++ b/tests/test-output/.gitkeep @@ -0,0 +1 @@ +.gitkeep \ No newline at end of file