244 lines
15 KiB
Rust
244 lines
15 KiB
Rust
// file: crates/ksp-config-lib/tests/public_api.rs
|
|
// version: 16
|
|
|
|
//! Integration tests for the public `ksp-config-lib` bootstrap, registry, JSON/profile/composite, environment-resolution, sensitivity, Logging-adapter and
|
|
//! management contracts.
|
|
|
|
#[test]
|
|
fn bootstrap_contract_is_available_from_crate_root() {
|
|
let result = ksp_config_lib::ConfigBootstrapOptions::defaults();
|
|
assert!(result.is_ok(), "default bootstrap options should be available: {result:?}");
|
|
if let std::result::Result::Ok(options) = result {
|
|
assert_eq!(options.cfg_path(), std::path::Path::new(ksp_config_lib::DEFAULT_CFG_PATH));
|
|
assert_eq!(options.schema_path(), std::path::Path::new(ksp_config_lib::DEFAULT_SCHEMA_PATH));
|
|
assert_eq!(ksp_config_lib::ARG_CFG_PATH, "--cfgpath");
|
|
assert_eq!(ksp_config_lib::ARG_SCHEMA_PATH, "--schemapath");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn programmatic_bootstrap_paths_are_independent() {
|
|
let result = ksp_config_lib::ConfigBootstrapOptions::from_paths("runtime-config", "runtime-schemas");
|
|
assert!(result.is_ok(), "programmatic bootstrap paths should be valid: {result:?}");
|
|
if let std::result::Result::Ok(options) = result {
|
|
assert_eq!(options.cfg_path(), std::path::Path::new("runtime-config"));
|
|
assert_eq!(options.schema_path(), std::path::Path::new("runtime-schemas"));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn cli_bootstrap_parser_is_available_from_crate_root() {
|
|
let args = [
|
|
std::ffi::OsString::from("consumer"),
|
|
std::ffi::OsString::from("--cfgpath=consumer-config"),
|
|
std::ffi::OsString::from("--schemapath"),
|
|
std::ffi::OsString::from("consumer-schemas"),
|
|
];
|
|
let result = ksp_config_lib::ConfigBootstrapOptions::from_args(&args);
|
|
assert!(result.is_ok(), "public bootstrap parser should accept KSP path arguments: {result:?}");
|
|
if let std::result::Result::Ok(options) = result {
|
|
assert_eq!(options.cfg_path(), std::path::Path::new("consumer-config"));
|
|
assert_eq!(options.schema_path(), std::path::Path::new("consumer-schemas"));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn logical_file_registry_is_available_from_crate_root() {
|
|
let args = [
|
|
std::ffi::OsString::from("consumer"),
|
|
std::ffi::OsString::from("--filemap=cfg.std.logging=consumer.logging.json"),
|
|
std::ffi::OsString::from("--filemap=schema.std.logging=consumer.logging.schema.json"),
|
|
];
|
|
let registry = ksp_config_lib::ConfigFileRegistry::from_args(&args);
|
|
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths("consumer-config", "consumer-schemas");
|
|
let logging_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_STD_LOGGING);
|
|
let schema_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_SCHEMA_STD_LOGGING);
|
|
assert!(registry.is_ok(), "public registry should accept known filemap overrides: {registry:?}");
|
|
assert!(bootstrap.is_ok(), "bootstrap paths should remain available: {bootstrap:?}");
|
|
assert!(logging_id.is_ok(), "public logging file_id should be valid: {logging_id:?}");
|
|
assert!(schema_id.is_ok(), "public schema file_id should be valid: {schema_id:?}");
|
|
if let (std::result::Result::Ok(registry), std::result::Result::Ok(bootstrap), std::result::Result::Ok(logging_id), std::result::Result::Ok(schema_id)) =
|
|
(registry, bootstrap, logging_id, schema_id)
|
|
{
|
|
let logging = registry.resolve_path(&bootstrap, &logging_id);
|
|
let schema = registry.resolve_path(&bootstrap, &schema_id);
|
|
assert!(logging.is_ok(), "public logging path should resolve: {logging:?}");
|
|
assert!(schema.is_ok(), "public schema path should resolve: {schema:?}");
|
|
if let std::result::Result::Ok(logging) = logging {
|
|
assert_eq!(logging, std::path::PathBuf::from("consumer-config/consumer.logging.json"));
|
|
}
|
|
if let std::result::Result::Ok(schema) = schema {
|
|
assert_eq!(schema, std::path::PathBuf::from("consumer-schemas/consumer.logging.schema.json"));
|
|
}
|
|
assert_eq!(ksp_config_lib::ARG_FILE_MAP, "--filemap");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn registry_descriptor_inventory_is_available_from_crate_root() {
|
|
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
|
|
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
|
|
if let std::result::Result::Ok(registry) = registry {
|
|
let descriptors: std::vec::Vec<&ksp_config_lib::ConfigFileDescriptor> = registry.descriptors().collect();
|
|
assert_eq!(descriptors.len(), 3);
|
|
assert_eq!(descriptors[0].file_id().as_str(), ksp_config_lib::FILE_ID_STD_LOGGING);
|
|
assert_eq!(descriptors[0].kind(), ksp_config_lib::ConfigFileKind::Config);
|
|
assert_eq!(descriptors[1].file_id().as_str(), ksp_config_lib::FILE_ID_SCHEMA_COMPOSITE);
|
|
assert_eq!(descriptors[2].file_id().as_str(), ksp_config_lib::FILE_ID_SCHEMA_STD_LOGGING);
|
|
let schema_file_id = descriptors[0].schema_file_id();
|
|
assert!(schema_file_id.is_some(), "public descriptor inventory should preserve schema association");
|
|
if let std::option::Option::Some(schema_file_id) = schema_file_id {
|
|
assert_eq!(schema_file_id.as_str(), ksp_config_lib::FILE_ID_SCHEMA_STD_LOGGING);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn validated_json_document_engine_is_available_from_crate_root() {
|
|
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
|
|
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths(workspace.join("config"), workspace.join("config/schemas"));
|
|
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
|
|
let file_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_STD_LOGGING);
|
|
assert!(bootstrap.is_ok(), "public bootstrap should accept committed Config roots: {bootstrap:?}");
|
|
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
|
|
assert!(file_id.is_ok(), "public Logging file_id should remain constructible: {file_id:?}");
|
|
if let (std::result::Result::Ok(bootstrap), std::result::Result::Ok(registry), std::result::Result::Ok(file_id)) = (bootstrap, registry, file_id) {
|
|
let engine = ksp_config_lib::ConfigDocumentEngine::new(bootstrap, registry);
|
|
let document = engine.load_validated_document(&file_id);
|
|
assert!(document.is_ok(), "public document engine should validate committed Logging configuration: {document:?}");
|
|
if let std::result::Result::Ok(document) = document {
|
|
assert_eq!(document.file_id(), &file_id);
|
|
assert_eq!(document.value().get("format_version"), std::option::Option::Some(&serde_json::Value::from(1)));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn resolved_profile_contract_is_available_from_crate_root() {
|
|
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
|
|
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths(workspace.join("config"), workspace.join("config/schemas"));
|
|
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
|
|
let file_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_STD_LOGGING);
|
|
assert!(bootstrap.is_ok(), "public bootstrap should accept committed Config roots: {bootstrap:?}");
|
|
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
|
|
assert!(file_id.is_ok(), "public Logging file_id should remain constructible: {file_id:?}");
|
|
if let (std::result::Result::Ok(bootstrap), std::result::Result::Ok(registry), std::result::Result::Ok(file_id)) = (bootstrap, registry, file_id) {
|
|
let engine = ksp_config_lib::ConfigDocumentEngine::new(bootstrap, registry);
|
|
let document = engine.load_validated_document(&file_id);
|
|
let resolved = engine.load_resolved_profile(&file_id, std::option::Option::None);
|
|
assert!(document.is_ok(), "public profile resolver should read committed source: {document:?}");
|
|
assert!(resolved.is_ok(), "public profile resolver should resolve committed default profile: {resolved:?}");
|
|
if let (std::result::Result::Ok(document), std::result::Result::Ok(resolved)) = (document, resolved) {
|
|
let default_profile = document.value().get("default_profile").and_then(serde_json::Value::as_str);
|
|
assert_eq!(default_profile, std::option::Option::Some(resolved.profile_id()));
|
|
assert_eq!(resolved.selection_source(), ksp_config_lib::ConfigProfileSelectionSource::DefaultProfile);
|
|
assert_eq!(resolved.origin("logs_directory"), std::option::Option::Some(ksp_config_lib::ConfigValueOrigin::Global));
|
|
assert_eq!(resolved.origin("default_filter"), std::option::Option::Some(ksp_config_lib::ConfigValueOrigin::Profile));
|
|
assert!(!resolved.effective().contains_key("default_profile"));
|
|
assert!(!resolved.effective().contains_key("profiles"));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn composite_schema_and_provenance_contracts_are_available_from_crate_root() {
|
|
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
|
|
let schema_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_SCHEMA_COMPOSITE);
|
|
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
|
|
assert!(schema_id.is_ok(), "public composite schema file_id should be valid: {schema_id:?}");
|
|
if let (std::result::Result::Ok(registry), std::result::Result::Ok(schema_id)) = (registry, schema_id) {
|
|
let descriptor = registry.descriptor(&schema_id);
|
|
assert!(descriptor.is_ok(), "public composite schema descriptor should exist: {descriptor:?}");
|
|
if let std::result::Result::Ok(descriptor) = descriptor {
|
|
assert_eq!(descriptor.filename(), std::path::Path::new(ksp_config_lib::DEFAULT_COMPOSITE_SCHEMA_FILENAME));
|
|
assert_eq!(descriptor.kind(), ksp_config_lib::ConfigFileKind::Schema);
|
|
}
|
|
}
|
|
assert_ne!(ksp_config_lib::ConfigProfileSelectionSource::Composite, ksp_config_lib::ConfigProfileSelectionSource::Explicit);
|
|
}
|
|
|
|
#[test]
|
|
fn environment_resolution_contract_is_available_from_crate_root() {
|
|
let loader: fn() -> ksp_core_lib::Result<ksp_config_lib::ConfigEnvironment> = ksp_config_lib::ConfigEnvironment::load;
|
|
let _ = loader;
|
|
assert_eq!(ksp_config_lib::DEFAULT_DOTENV_PATH, ".env");
|
|
assert_eq!(ksp_config_lib::DEFAULT_DOTENV_EXAMPLE_PATH, ".env.example");
|
|
assert_ne!(ksp_config_lib::ConfigEnvironmentSource::Process, ksp_config_lib::ConfigEnvironmentSource::DotEnv);
|
|
assert_ne!(ksp_config_lib::ConfigEnvironmentSource::DotEnv, ksp_config_lib::ConfigEnvironmentSource::Fallback);
|
|
}
|
|
|
|
#[test]
|
|
fn sensitivity_and_safe_resolution_contracts_are_available_from_crate_root() {
|
|
assert_eq!(
|
|
ksp_config_lib::ConfigSensitivity::from_variable_name("KSP_PUBLIC_HOST").ok(),
|
|
std::option::Option::Some(ksp_config_lib::ConfigSensitivity::Public),
|
|
);
|
|
assert_eq!(
|
|
ksp_config_lib::ConfigSensitivity::from_variable_name("KSP_MODE").ok(),
|
|
std::option::Option::Some(ksp_config_lib::ConfigSensitivity::Internal)
|
|
);
|
|
assert_eq!(
|
|
ksp_config_lib::ConfigSensitivity::from_variable_name("KSP_SECRET_TOKEN").ok(),
|
|
std::option::Option::Some(ksp_config_lib::ConfigSensitivity::Secret),
|
|
);
|
|
assert_eq!(ksp_config_lib::REDACTED_CONFIG_VALUE, "********");
|
|
let detailed_text: fn(&ksp_config_lib::ConfigEnvironment, &str) -> ksp_core_lib::Result<ksp_config_lib::ResolvedConfigText> =
|
|
ksp_config_lib::ConfigEnvironment::resolve_text_detailed;
|
|
let detailed_json: fn(&ksp_config_lib::ConfigEnvironment, &serde_json::Value) -> ksp_core_lib::Result<ksp_config_lib::ResolvedConfigJson> =
|
|
ksp_config_lib::ConfigEnvironment::resolve_json_detailed;
|
|
let _ = (detailed_text, detailed_json);
|
|
let provenance = ksp_config_lib::ConfigValueProvenance::EnvironmentFallback { variable_name: "KSP_SECRET_TOKEN".to_owned() };
|
|
assert_eq!(provenance.variable_name(), std::option::Option::Some("KSP_SECRET_TOKEN"));
|
|
assert_eq!(provenance.environment_source(), std::option::Option::Some(ksp_config_lib::ConfigEnvironmentSource::Fallback));
|
|
}
|
|
|
|
#[test]
|
|
fn logging_adapter_contract_is_available_from_crate_root() {
|
|
let adapter = ksp_config_lib::ConfigDocumentEngine::load_resolved_logging_config;
|
|
let _ = adapter;
|
|
assert_eq!(ksp_config_lib::ERROR_CODE_EFFECTIVE_CONFIG_INVALID.domain(), "config");
|
|
assert_eq!(ksp_config_lib::ERROR_CODE_EFFECTIVE_CONFIG_INVALID.code(), "effective_config_invalid");
|
|
assert!(std::mem::size_of::<ksp_config_lib::ResolvedLoggingConfig>() > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn management_contracts_are_available_from_crate_root() {
|
|
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
|
|
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths(workspace.join("config"), workspace.join("config/schemas"));
|
|
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
|
|
assert!(bootstrap.is_ok(), "public bootstrap should accept committed Config roots: {bootstrap:?}");
|
|
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
|
|
if let (std::result::Result::Ok(bootstrap), std::result::Result::Ok(registry)) = (bootstrap, registry) {
|
|
let engine = ksp_config_lib::ConfigDocumentEngine::new(bootstrap, registry);
|
|
let management = ksp_config_lib::ConfigManagement::new(engine);
|
|
let logging = management.load_logging_document();
|
|
assert!(logging.is_ok(), "public typed Logging management contract should load committed source: {logging:?}");
|
|
if let std::result::Result::Ok(mut logging) = logging {
|
|
assert_eq!(logging.format_version(), 1);
|
|
let default_profile = logging.default_profile().to_owned();
|
|
assert!(!default_profile.is_empty());
|
|
assert!(!logging.profiles().is_empty());
|
|
assert!(logging.profiles().iter().any(|profile| return profile.profile_id() == default_profile.as_str()));
|
|
logging.set_logs_directory("public-api-management-test");
|
|
assert_eq!(logging.logs_directory(), "public-api-management-test");
|
|
if let std::option::Option::Some(profile) = logging.profiles_mut().first_mut()
|
|
&& let std::option::Option::Some(file) = profile.files_mut().first_mut()
|
|
{
|
|
file.set_ansi(false);
|
|
assert!(!file.ansi());
|
|
}
|
|
}
|
|
}
|
|
let save_source_candidate: fn(
|
|
&ksp_config_lib::ConfigManagement,
|
|
&ksp_config_lib::ConfigFileId,
|
|
&str,
|
|
) -> ksp_core_lib::Result<ksp_config_lib::ConfigDocumentChangeReport> = ksp_config_lib::ConfigManagement::save_source_candidate;
|
|
let reveal_effective: fn(&ksp_config_lib::ConfigManagement, &str) -> ksp_core_lib::Result<std::option::Option<String>> =
|
|
ksp_config_lib::ConfigManagement::reveal_effective_environment_value;
|
|
let reveal_dotenv: fn(&ksp_config_lib::ConfigManagement, &str) -> ksp_core_lib::Result<std::option::Option<String>> =
|
|
ksp_config_lib::ConfigManagement::reveal_dotenv_value;
|
|
let _ = (save_source_candidate, reveal_effective, reveal_dotenv);
|
|
assert_ne!(ksp_config_lib::ERROR_CODE_MANAGEMENT_OPERATION_INVALID, ksp_config_lib::ERROR_CODE_PERSISTENCE_WRITE_FAILED);
|
|
}
|