feat: add variables

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-05-20 23:05:20 +02:00
parent f71b68cd89
commit bbe630e822
14 changed files with 429 additions and 22 deletions

View File

@@ -1,9 +1,12 @@
use std::{cmp::Ordering, path::Path};
use cuddle_clusters::process::ProcessOpts;
use cuddle_clusters::{process::ProcessOpts, ConcreteComponent, IntoComponent};
use walkdir::DirEntry;
pub(crate) async fn run_test(name: &str) -> anyhow::Result<()> {
pub(crate) async fn run_test_with_components(
name: &str,
components: Vec<impl IntoComponent>,
) -> anyhow::Result<()> {
let _ = tracing_subscriber::fmt::try_init();
println!("running for: {name}");
@@ -17,17 +20,25 @@ pub(crate) async fn run_test(name: &str) -> anyhow::Result<()> {
let expected = test_folder.join("expected");
tokio::fs::create_dir_all(&expected).await?;
cuddle_clusters::process_opts(ProcessOpts {
path: test_folder.clone(),
output: actual.clone(),
})
let components: Vec<_> = components.into_iter().map(|p| p.into_component()).collect();
cuddle_clusters::process_opts(
components.clone(),
ProcessOpts {
path: test_folder.clone(),
output: actual.clone(),
},
)
.await?;
if std::env::var("TEST_OVERRIDE") == Ok("true".to_string()) {
cuddle_clusters::process_opts(ProcessOpts {
path: test_folder,
output: expected.clone(),
})
cuddle_clusters::process_opts(
components,
ProcessOpts {
path: test_folder,
output: expected.clone(),
},
)
.await?;
}
@@ -36,6 +47,10 @@ pub(crate) async fn run_test(name: &str) -> anyhow::Result<()> {
Ok(())
}
pub(crate) async fn run_test(name: &str) -> anyhow::Result<()> {
run_test_with_components(name, Vec::<ConcreteComponent>::new()).await
}
async fn compare(expected: &Path, actual: &Path) -> anyhow::Result<()> {
let mut exp = walk_dir(expected)?;
let mut act = walk_dir(actual)?;

View File

@@ -0,0 +1,49 @@
use std::collections::HashMap;
use cuddle_clusters::catalog::cuddle_vars::{CuddleVariable, CuddleVariables, CuddleVars};
use similar_asserts::assert_eq;
#[tokio::test]
async fn can_load_cuddle_file() -> anyhow::Result<()> {
let current_dir = std::env::current_dir()?.join("tests");
let vars = CuddleVars::new(&current_dir.join("cuddle_vars/basic/")).await?;
assert_eq!(
CuddleVars {
variables: CuddleVariables(HashMap::from([
("service".into(), CuddleVariable::String("basic".into())),
(
"some".into(),
CuddleVariable::Object(Box::new(CuddleVariables(HashMap::from([
("other".into(), CuddleVariable::String("item".into())),
(
"nested".into(),
CuddleVariable::Object(Box::new(CuddleVariables(HashMap::from([(
"item".into(),
CuddleVariable::String("item".into())
)]))))
),
(
"array".into(),
CuddleVariable::Array(vec![CuddleVariable::Object(Box::new(
CuddleVariables(HashMap::from([(
"item".into(),
CuddleVariable::Object(Box::new(CuddleVariables(
HashMap::from([(
"item".into(),
CuddleVariable::String("item".into()),
)])
)))
)]))
))])
)
]))))
)
]))
},
vars
);
Ok(())
}

View File

@@ -0,0 +1,10 @@
vars:
service: basic
some:
other: item
nested:
item: item
array:
- item:
item: item

View File

@@ -1,8 +1,11 @@
pub mod common;
mod can_run_for_env;
mod cuddle_vars;
use crate::common::run_test;
use cuddle_clusters::catalog::cuddle_vars::CuddleVars;
use crate::common::{run_test, run_test_with_components};
#[tokio::test]
async fn raw_files() -> anyhow::Result<()> {
@@ -37,3 +40,16 @@ async fn environment() -> anyhow::Result<()> {
Ok(())
}
#[tokio::test]
async fn with_cuddle_vars() -> anyhow::Result<()> {
let current_dir = std::env::current_dir()?.join("tests/with_cuddle_vars");
run_test_with_components(
"with_cuddle_vars",
vec![CuddleVars::new(&current_dir).await?],
)
.await?;
Ok(())
}

View File

@@ -0,0 +1,5 @@
vars:
service: service
cuddle/clusters:
dev:

View File

@@ -0,0 +1,2 @@
service: service

View File

@@ -0,0 +1,3 @@
service: {{ vars.cuddle_vars.service }}