v0.1.4-pre.015-fix.002
This commit is contained in:
@@ -1,36 +1,31 @@
|
||||
// file: crates/ksp-app-config-desk/unit_tests/logging_editor.rs
|
||||
// version: 2
|
||||
// version: 3
|
||||
|
||||
#[test]
|
||||
fn committed_logging_document_maps_complete_read_only_editor_contract() {
|
||||
let management = committed_management();
|
||||
assert!(management.is_ok(), "committed management should construct: {management:?}");
|
||||
if let std::result::Result::Ok(management) = management {
|
||||
let source = management.load_logging_document();
|
||||
assert!(source.is_ok(), "typed Logging source should load: {source:?}");
|
||||
let document = super::document_from_management(&management);
|
||||
assert!(document.is_ok(), "Logging editor document should load: {document:?}");
|
||||
if let std::result::Result::Ok(document) = document {
|
||||
if let (std::result::Result::Ok(source), std::result::Result::Ok(document)) = (source, document) {
|
||||
assert_eq!(document.file_id, ksp_config_lib::FILE_ID_STD_LOGGING);
|
||||
assert_eq!(document.format_version, 1);
|
||||
assert_eq!(document.logs_directory, "${KSP_LOGS_DIRECTORY:-logs}");
|
||||
assert_eq!(document.default_profile, "local_dev");
|
||||
assert_eq!(document.profiles.len(), 1);
|
||||
let profile = &document.profiles[0];
|
||||
assert_eq!(profile.profile_id, "local_dev");
|
||||
assert_eq!(profile.default_filter, "warn");
|
||||
assert_eq!(profile.span_events, "new_and_close");
|
||||
assert!(profile.console.enabled);
|
||||
assert_eq!(profile.console.output, "stderr");
|
||||
assert_eq!(profile.console.format, "compact");
|
||||
assert_eq!(profile.console.filter.level, "debug");
|
||||
assert_eq!(profile.console.filter.targets, std::vec!["*".to_owned()]);
|
||||
assert_eq!(profile.console.filter.domains, std::vec!["*".to_owned()]);
|
||||
assert_eq!(profile.files.len(), 2);
|
||||
assert_eq!(profile.files[0].output_id, "file.all.debug");
|
||||
assert_eq!(profile.files[0].filter.targets, std::vec!["*".to_owned()]);
|
||||
assert_eq!(profile.files[0].filter.domains, std::vec!["*".to_owned()]);
|
||||
assert_eq!(profile.target_filters.len(), 3);
|
||||
assert_eq!(profile.target_filters[2].target_prefix, "ksp-app-config-desk");
|
||||
assert_eq!(profile.target_filters[2].level, "debug");
|
||||
assert_eq!(document.format_version, source.format_version());
|
||||
assert_eq!(document.logs_directory, source.logs_directory());
|
||||
assert_eq!(document.default_profile, source.default_profile());
|
||||
assert_eq!(document.profiles.len(), source.profiles().len());
|
||||
for (profile, source_profile) in document.profiles.iter().zip(source.profiles()) {
|
||||
assert_eq!(profile.profile_id, source_profile.profile_id());
|
||||
assert_eq!(profile.default_filter, source_profile.default_filter());
|
||||
assert_eq!(profile.span_events, source_profile.span_events());
|
||||
assert_eq!(profile.console.enabled, source_profile.console().enabled());
|
||||
assert_eq!(profile.console.output, source_profile.console().output());
|
||||
assert_eq!(profile.console.format, source_profile.console().format());
|
||||
assert_eq!(profile.files.len(), source_profile.files().len());
|
||||
assert_eq!(profile.target_filters.len(), source_profile.target_filters().len());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,7 +75,14 @@ fn editor_candidate_round_trips_through_typed_config_contract() {
|
||||
assert_eq!(config.logs_directory(), document.logs_directory);
|
||||
assert_eq!(config.default_profile(), document.default_profile);
|
||||
assert_eq!(config.profiles().len(), document.profiles.len());
|
||||
assert_eq!(config.profiles()[0].files()[0].ansi(), document.profiles[0].files[0].ansi);
|
||||
for (config_profile, document_profile) in config.profiles().iter().zip(document.profiles.iter()) {
|
||||
assert_eq!(config_profile.profile_id(), document_profile.profile_id);
|
||||
assert_eq!(config_profile.files().len(), document_profile.files.len());
|
||||
for (config_file, document_file) in config_profile.files().iter().zip(document_profile.files.iter()) {
|
||||
assert_eq!(config_file.output_id(), document_file.output_id);
|
||||
assert_eq!(config_file.ansi(), document_file.ansi);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,3 +133,92 @@ fn profile_candidate_preserves_multi_file_and_target_filter_shape() {
|
||||
assert_eq!(config.target_filters().len(), 1);
|
||||
assert_eq!(config.target_filters()[0].level(), "trace");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_failure_rollback_restores_previous_logging_source() {
|
||||
let fixture = rollback_fixture();
|
||||
let fixture = match fixture {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(_) => return,
|
||||
};
|
||||
let (root, management, file_id, previous) = fixture;
|
||||
let mut candidate = management.load_logging_document();
|
||||
assert!(candidate.is_ok(), "rollback fixture should load: {candidate:?}");
|
||||
if let std::result::Result::Ok(candidate) = candidate.as_mut() {
|
||||
candidate.set_logs_directory(format!("temporary-runtime-failure-{}", std::process::id()));
|
||||
}
|
||||
if let std::result::Result::Ok(candidate) = candidate {
|
||||
let saved = management.save_logging_document(&candidate);
|
||||
assert!(saved.is_ok(), "rollback fixture mutation should persist: {saved:?}");
|
||||
if let std::result::Result::Ok(saved) = saved {
|
||||
let runtime_error = ksp_core_lib::Error::new(ksp_logging_lib::ERROR_CODE_RELOAD_FAILED, "synthetic runtime reload failure");
|
||||
let rolled_back = super::rollback_after_runtime_failure(&management, &file_id, previous.as_str(), saved.source_changed(), runtime_error);
|
||||
assert!(rolled_back.is_err(), "runtime failure should remain visible after successful source rollback");
|
||||
if let std::result::Result::Err(error) = rolled_back {
|
||||
assert_eq!(error.code(), ksp_logging_lib::ERROR_CODE_RELOAD_FAILED);
|
||||
}
|
||||
assert_eq!(std::fs::read_to_string(root.join("config/std.logging.json")).ok(), std::option::Option::Some(previous));
|
||||
}
|
||||
}
|
||||
let cleanup = std::fs::remove_dir_all(root.as_path());
|
||||
assert!(cleanup.is_ok(), "rollback fixture should cleanup: {cleanup:?}");
|
||||
}
|
||||
|
||||
fn rollback_fixture() -> ksp_core_lib::Result<(std::path::PathBuf, ksp_config_lib::ConfigManagement, ksp_config_lib::ConfigFileId, String)> {
|
||||
let root = std::env::temp_dir().join(format!("ksp-config-desk-logging-rollback-{}", std::process::id()));
|
||||
let cleanup = std::fs::remove_dir_all(root.as_path());
|
||||
if let std::result::Result::Err(error) = cleanup
|
||||
&& error.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
return std::result::Result::Err(
|
||||
ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "cannot cleanup previous rollback fixture").with_source(error),
|
||||
);
|
||||
}
|
||||
let config_root = root.join("config");
|
||||
let schema_root = config_root.join("schemas");
|
||||
let create = std::fs::create_dir_all(schema_root.as_path());
|
||||
if let std::result::Result::Err(error) = create {
|
||||
return std::result::Result::Err(ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "cannot create rollback fixture").with_source(error));
|
||||
}
|
||||
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
|
||||
let source_config = workspace.join("config/std.logging.json");
|
||||
let source_schema = workspace.join("config/schemas/std.logging.schema.json");
|
||||
let config_path = config_root.join("std.logging.json");
|
||||
let schema_path = schema_root.join("std.logging.schema.json");
|
||||
let copied = std::fs::copy(source_config.as_path(), config_path.as_path());
|
||||
if let std::result::Result::Err(error) = copied {
|
||||
return std::result::Result::Err(
|
||||
ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "cannot copy rollback Config fixture").with_source(error),
|
||||
);
|
||||
}
|
||||
let copied = std::fs::copy(source_schema.as_path(), schema_path.as_path());
|
||||
if let std::result::Result::Err(error) = copied {
|
||||
return std::result::Result::Err(
|
||||
ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "cannot copy rollback schema fixture").with_source(error),
|
||||
);
|
||||
}
|
||||
let previous = std::fs::read_to_string(config_path.as_path());
|
||||
let previous = match previous {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
return std::result::Result::Err(ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "cannot read rollback fixture").with_source(error));
|
||||
},
|
||||
};
|
||||
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths(config_root.as_path(), schema_root.as_path());
|
||||
let bootstrap = match bootstrap {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
|
||||
let registry = match registry {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let file_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_STD_LOGGING);
|
||||
let file_id = match file_id {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let management = ksp_config_lib::ConfigManagement::new(ksp_config_lib::ConfigDocumentEngine::new(bootstrap, registry));
|
||||
return std::result::Result::Ok((root, management, file_id, previous));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user