Skip to content

Commit

Permalink
Add integration style test for runtime config
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Sep 18, 2024
1 parent 449ad5a commit 2bc860d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/factor-key-value/src/runtime_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ impl RuntimeConfig {
pub fn has_store_manager(&self, label: &str) -> bool {
self.store_managers.contains_key(label)
}

/// Returns the store manager for the store with the given label.
pub fn get_store_manager(&self, label: &str) -> Option<Arc<dyn StoreManager>> {
self.store_managers.get(label).cloned()
}
}

impl IntoIterator for RuntimeConfig {
Expand Down
8 changes: 4 additions & 4 deletions crates/factor-key-value/tests/factor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ struct TestFactors {
key_value: KeyValueFactor,
}

impl Into<TestFactorsRuntimeConfig> for RuntimeConfig {
fn into(self) -> TestFactorsRuntimeConfig {
TestFactorsRuntimeConfig {
key_value: Some(self),
impl From<RuntimeConfig> for TestFactorsRuntimeConfig {
fn from(value: RuntimeConfig) -> Self {
Self {
key_value: Some(value),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions crates/runtime-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ spin-sqlite = { path = "../sqlite" }
spin-trigger = { path = "../trigger" }
toml = "0.8"

[dev-dependencies]
spin-factors-test = { path = "../factors-test" }
spin-world = { path = "../world" }
tokio = { version = "1", features = ["macros"] }
tempfile = "3.2"
toml = "0.8"

[lints]
workspace = true
65 changes: 59 additions & 6 deletions crates/runtime-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ mod tests {
use std::{collections::HashMap, sync::Arc};

use spin_factors::RuntimeFactors;
use spin_factors_test::TestEnvironment;

use super::*;

Expand All @@ -458,10 +459,13 @@ mod tests {
Self::from_source(value)
}
}
fn resolve_toml(toml: toml::Table) -> ResolvedRuntimeConfig<TestFactorsRuntimeConfig> {
fn resolve_toml(
toml: toml::Table,
path: impl AsRef<std::path::Path>,
) -> ResolvedRuntimeConfig<TestFactorsRuntimeConfig> {
ResolvedRuntimeConfig::<TestFactorsRuntimeConfig>::new(
toml_resolver(&toml),
None,
Some(path.as_ref()),
false,
)
.unwrap()
Expand Down Expand Up @@ -500,13 +504,13 @@ mod tests {
type = "spin"
};
assert_eq!(
resolve_toml(toml).runtime_config.configured_labels(),
resolve_toml(toml, ".").runtime_config.configured_labels(),
vec!["default", "foo"]
);

// Test that the default label is added with an empty toml config.
let toml = toml::Table::new();
let runtime_config = resolve_toml(toml).runtime_config;
let runtime_config = resolve_toml(toml, "config.toml").runtime_config;
assert_eq!(runtime_config.configured_labels(), vec!["default"]);
}

Expand All @@ -525,10 +529,59 @@ mod tests {
[key_value_store.foo]
type = "spin"
};
let runtime_config = resolve_toml(toml).runtime_config;
let runtime_config = resolve_toml(toml, "config.toml").runtime_config;
assert!(["default", "foo"]
.iter()
.all(|label| { runtime_config.has_store_manager(label) }));
.all(|label| runtime_config.has_store_manager(label)));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn custom_spin_key_value_works_with_custom_paths() -> anyhow::Result<()> {
use spin_world::v2::key_value::HostStore;
define_test_factor!(key_value: KeyValueFactor);
let tmp_dir = tempfile::TempDir::with_prefix("example")?;
let absolute_path = tmp_dir.path().join("foo/custom.db");
let relative_path = tmp_dir.path().join("custom.db");
// Check that the dbs do not exist yet - they will exist by the end of the test
assert!(!absolute_path.exists());
assert!(!relative_path.exists());

let path_str = absolute_path.to_str().unwrap();
let runtime_config = toml::toml! {
[key_value_store.absolute]
type = "spin"
path = path_str

[key_value_store.relative]
type = "spin"
path = "custom.db"
};
let factors = TestFactors {
key_value: KeyValueFactor::new(),
};
let env = TestEnvironment::new(factors)
.extend_manifest(toml::toml! {
[component.test-component]
source = "does-not-exist.wasm"
key_value_stores = ["absolute", "relative"]
})
.runtime_config(
resolve_toml(runtime_config, tmp_dir.path().join("runtime-config.toml"))
.runtime_config,
)?;
let mut state = env.build_instance_state().await?;

// Actually get a key since store creation is lazy
let store = state.key_value.open("absolute".to_owned()).await??;
let _ = state.key_value.get(store, "foo".to_owned()).await??;

let store = state.key_value.open("relative".to_owned()).await??;
let _ = state.key_value.get(store, "foo".to_owned()).await??;

// Check that the dbs have been created
assert!(absolute_path.exists());
assert!(relative_path.exists());
Ok(())
}

fn toml_resolver(toml: &toml::Table) -> TomlResolver<'_> {
Expand Down

0 comments on commit 2bc860d

Please sign in to comment.