Skip to content

Commit

Permalink
fix: enabled simple tests (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
howard-oc authored May 2, 2024
1 parent fd7ab70 commit 7f21e73
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 106 deletions.
18 changes: 5 additions & 13 deletions .env
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
RUST_LOG=debug

WORKSPACE=/Users/hhill/work/oc

BASE_INSTANCE="$WORKSPACE/promise-flink/internal/configure-pipeline/resources/minimal-flinkdep-manifest.yaml"

DEPENDENCIES_DIR="$WORKSPACE/promise-flink/internal/configure-pipeline/dependencies"

RESOURCES_DIR="$WORKSPACE/promise-flink/internal/configure-pipeline/resources"

KRATIX_INPUT="$WORKSPACE/promise-flink/internal/configure-pipeline/tests/test-input"

KRATIX_OUTPUT="$WORKSPACE/promise-flink/internal/configure-pipeline/tests/test-output"
BASE_INSTANCE="$WORKSPACE/resources/minimal-flinkdep-manifest.yaml"
DEPENDENCIES_DIR="$WORKSPACE/dependencies"
KRATIX_INPUT="$WORKSPACE/tests/test-input"
KRATIX_OUTPUT="$WORKSPACE/tests/test-output"
RESOURCES_DIR="$WORKSPACE/resources"
3 changes: 2 additions & 1 deletion .github/linters/.checkov.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
skip-check:
- CKV_DOCKER_2
- CKV2_GHA_1
- CKV2_GHA_1
13 changes: 7 additions & 6 deletions .github/linters/.hadolint.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
failure-threshold: warning
format: tty
ignored:
- DL3045
- DL3018
- DL3045
- DL3018
trustedRegistries:
- docker.io
- ghcr.io
- "*.gcr.io"
- quay.io
- docker.io
- ghcr.io
- "*.gcr.io"
- quay.io
6 changes: 4 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ jobs:
env:
DEFAULT_BRANCH: main
VALIDATE_KUBERNETES_KUBECONFORM: false
# To report GitHub Actions status checks
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_RUST_2015: false
VALIDATE_RUST_2018: false
VALIDATE_RUST_2021: false
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
16 changes: 8 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
---
name: Rust

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
6 changes: 6 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ dotenv = "0.15.0"
log = "0.4.21"
serde_yaml = "0.9.34"
toml = "0.5"
log4rs = "1.3.0"
pretty_env_logger = "0.5.0"
env_logger = "0.11.3"
49 changes: 22 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ pub mod pipeline;
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)]
Expand All @@ -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 {
Expand All @@ -28,28 +40,20 @@ pub fn run_pipeline() -> PipelineConfig {

impl ResourceRequest for MyPromise {
fn transform(&self, _conf: &PipelineConfig) -> String {
format!("{}", self.params)
self.params.to_string()
}
}

let request = MyPromise {
params: String::from("(default)"),
};

return run_custom_pipeline(Some(request));
run_custom_pipeline(Some(request))
}

pub fn run_custom_pipeline(_request: Option<impl ResourceRequest>) -> 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
Expand Down Expand Up @@ -110,18 +114,9 @@ pub fn run_custom_pipeline(_request: Option<impl ResourceRequest>) -> 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");
}
}

Expand All @@ -130,7 +125,7 @@ pub fn run_custom_pipeline(_request: Option<impl ResourceRequest>) -> PipelineCo
//pipeline::list_files_recursively(_kratix_output_dir);

log::debug!("<- End Pipeline ->");
return config;
config
}

// validation function
Expand Down
15 changes: 10 additions & 5 deletions src/pipeline/mod.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions src/promise/mod.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
93 changes: 54 additions & 39 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,63 @@
// use kratix_utils::pipeline::PipelineConfig;
// use kratix_utils::promise;
// use std::env;

fn sqrt(number: f64) -> Result<f64, String> {
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();
}
}
1 change: 1 addition & 0 deletions tests/test-output/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.gitkeep

0 comments on commit 7f21e73

Please sign in to comment.