|
1 | | -//! Persistency tests. |
2 | | -//! |
3 | | -//! Requirements verified: |
4 | | -//! - Default Values (feat_req__persistency__default_values) |
5 | | -//! The KVS system shall support predefined default values for keys. |
6 | | -//! - Default Values Retrieval (feat_req__persistency__default_value_get) |
7 | | -//! The KVS system shall support retrieving the default value associated with a key. |
8 | | -//! - Set default key values via file (feat_req__persistency__default_value_file) |
9 | | -//! The KVS shall support the configuration of default key values using an external file. |
10 | | -//! |
| 1 | +// Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 2 | +// |
| 3 | +// See the NOTICE file(s) distributed with this work for additional |
| 4 | +// information regarding copyright ownership. |
| 5 | +// |
| 6 | +// This program and the accompanying materials are made available under the |
| 7 | +// terms of the Apache License Version 2.0 which is available at |
| 8 | +// <https://www.apache.org/licenses/LICENSE-2.0> |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: Apache-2.0 |
| 11 | + |
11 | 12 | use rust_kvs::prelude::*; |
12 | 13 | use std::collections::HashMap; |
13 | 14 | use std::path::Path; |
@@ -48,6 +49,11 @@ fn write_defaults_file( |
48 | 49 | } |
49 | 50 |
|
50 | 51 | #[test] |
| 52 | +// #[record_property("PartiallyVerifies", ["comp_req__persistency__value_default", "comp_req__persistency__default_value_config", "comp_req__persistency__default_value_types", "comp_req__persistency__default_value_query"])] |
| 53 | +// #[record_property("FullyVerifies", [])] |
| 54 | +// #[record_property("Description", "Verifies default value loading, querying, and override behavior for KVS instances with and without defaults.")] |
| 55 | +// #[record_property("TestType", "requirements-based")] |
| 56 | +// #[record_property("DerivationTechnique", "requirements-based")] |
51 | 57 | fn cit_persistency_default_values() -> Result<(), ErrorCode> { |
52 | 58 | // Temp directory. |
53 | 59 | let dir = tempdir()?; |
@@ -88,11 +94,23 @@ fn cit_persistency_default_values() -> Result<(), ErrorCode> { |
88 | 94 | kvs_with_defaults.is_value_default(&keyname)?, |
89 | 95 | "kvs_with_defaults: key '{keyname}' should be default" |
90 | 96 | ); |
| 97 | + assert_eq!( |
| 98 | + kvs_with_defaults.get_default_value(&keyname)?, |
| 99 | + KvsValue::from(default_value), |
| 100 | + "kvs_with_defaults: key '{keyname}' should have default value {default_value}" |
| 101 | + ); |
91 | 102 | assert_eq!( |
92 | 103 | kvs_without_defaults.is_value_default(&keyname).unwrap_err(), |
93 | 104 | ErrorCode::KeyNotFound, |
94 | 105 | "kvs_without_defaults: key '{keyname}' should not exist and return KeyNotFound" |
95 | 106 | ); |
| 107 | + assert_eq!( |
| 108 | + kvs_without_defaults |
| 109 | + .get_default_value(&keyname) |
| 110 | + .unwrap_err(), |
| 111 | + ErrorCode::KeyNotFound, |
| 112 | + "kvs_without_defaults: key '{keyname}' should not exist and return KeyNotFound" |
| 113 | + ); |
96 | 114 |
|
97 | 115 | // Check values. |
98 | 116 | assert_eq!( |
@@ -153,6 +171,11 @@ fn cit_persistency_default_values() -> Result<(), ErrorCode> { |
153 | 171 | } |
154 | 172 |
|
155 | 173 | #[test] |
| 174 | +// #[record_property("PartiallyVerifies", ["comp_req__persistency__value_default", "comp_req__persistency__default_value_config", "comp_req__persistency__default_value_types"])] |
| 175 | +// #[record_property("FullyVerifies", [""])] |
| 176 | +// #[record_property("Description", "Checks that KVS loads default values when defaults are present and OpenNeedDefaults is set to Optional.")] |
| 177 | +// #[record_property("TestType", "requirements-based")] |
| 178 | +// #[record_property("DerivationTechnique", "requirements-based")] |
156 | 179 | fn cit_persistency_default_values_optional() -> Result<(), ErrorCode> { |
157 | 180 | // Temp directory. |
158 | 181 | let dir = tempdir().unwrap(); |
@@ -198,6 +221,11 @@ fn cit_persistency_default_values_optional() -> Result<(), ErrorCode> { |
198 | 221 | } |
199 | 222 |
|
200 | 223 | #[test] |
| 224 | +// #[record_property("PartiallyVerifies", ["comp_req__persistency__value_default", "comp_req__persistency__default_value_config", "comp_req__persistency__default_value_types"])] |
| 225 | +// #[record_property("FullyVerifies", [""])] |
| 226 | +// #[record_property("Description", "Tests removal of values in KVS with defaults enabled, ensuring keys revert to their default values.")] |
| 227 | +// #[record_property("TestType", "requirements-based")] |
| 228 | +// #[record_property("DerivationTechnique", "requirements-based")] |
201 | 229 | fn cit_persistency_defaults_enabled_values_removal() -> Result<(), ErrorCode> { |
202 | 230 | // Temp directory. |
203 | 231 | let dir = tempdir()?; |
@@ -257,6 +285,11 @@ fn cit_persistency_defaults_enabled_values_removal() -> Result<(), ErrorCode> { |
257 | 285 | } |
258 | 286 |
|
259 | 287 | #[test] |
| 288 | +// #[record_property("PartiallyVerifies", ["comp_req__persistency__value_default", "comp_req__persistency__default_value_types"])] |
| 289 | +// #[record_property("FullyVerifies", [""])] |
| 290 | +// #[record_property("Description", "Tests removal of values in KVS without defaults, ensuring removed keys are not found and return KeyNotFound.")] |
| 291 | +// #[record_property("TestType", "requirements-based")] |
| 292 | +// #[record_property("DerivationTechnique", "requirements-based")] |
260 | 293 | fn cit_persistency_defaults_disabled_values_removal() -> Result<(), ErrorCode> { |
261 | 294 | // Temp directory. |
262 | 295 | let dir = tempdir()?; |
@@ -296,6 +329,11 @@ fn cit_persistency_defaults_disabled_values_removal() -> Result<(), ErrorCode> { |
296 | 329 | } |
297 | 330 |
|
298 | 331 | #[test] |
| 332 | +// #[record_property("PartiallyVerifies", ["comp_req__persistency__value_default", "comp_req__persistency__default_value_config", "comp_req__persistency__default_value_types"])] |
| 333 | +// #[record_property("FullyVerifies", [""])] |
| 334 | +// #[record_property("Description", "Verifies that KVS fails to open when the defaults file contains invalid JSON.")] |
| 335 | +// #[record_property("TestType", "requirements-based")] |
| 336 | +// #[record_property("DerivationTechnique", "requirements-based")] |
299 | 337 | fn cit_persistency_invalid_default_values() -> Result<(), ErrorCode> { |
300 | 338 | // Temp directory. |
301 | 339 | let dir = tempdir()?; |
@@ -324,6 +362,11 @@ fn cit_persistency_invalid_default_values() -> Result<(), ErrorCode> { |
324 | 362 | } |
325 | 363 |
|
326 | 364 | #[test] |
| 365 | +// #[record_property("PartiallyVerifies", ["comp_req__persistency__value_default", "comp_req__persistency__default_value_config", "comp_req__persistency__default_value_types"])] |
| 366 | +// #[record_property("FullyVerifies", ["comp_req__persistency__value_reset"])] |
| 367 | +// #[record_property("Description", "Checks that resetting KVS restores all keys to their default values.")] |
| 368 | +// #[record_property("TestType", "requirements-based")] |
| 369 | +// #[record_property("DerivationTechnique", "requirements-based")] |
327 | 370 | fn cit_persistency_reset_all_default_values() -> Result<(), ErrorCode> { |
328 | 371 | // Temp directory. |
329 | 372 | let dir = tempdir()?; |
@@ -396,7 +439,58 @@ fn cit_persistency_reset_all_default_values() -> Result<(), ErrorCode> { |
396 | 439 | } |
397 | 440 |
|
398 | 441 | #[test] |
399 | | -#[ignore] |
| 442 | +// #[record_property("PartiallyVerifies", ["comp_req__persistency__value_default", "comp_req__persistency__default_value_config"])] |
| 443 | +// #[record_property("FullyVerifies", ["comp_req__persistency__default_value_checksum"])] |
| 444 | +// #[record_property("Description", "Ensures that a checksum file is created when opening KVS with defaults.")] |
| 445 | +// #[record_property("TestType", "requirements-based")] |
| 446 | +// #[record_property("DerivationTechnique", "requirements-based")] |
| 447 | +fn cit_persistency_defaults_checksum() -> Result<(), ErrorCode> { |
| 448 | + // Temp directory. |
| 449 | + let dir = tempdir()?; |
| 450 | + let dir_string = dir.path().to_string_lossy().to_string(); |
| 451 | + |
| 452 | + // Values. |
| 453 | + let keyname = "test_number".to_string(); |
| 454 | + let default_value = 111.1; |
| 455 | + // Create defaults file for instance 0. |
| 456 | + let default_id = InstanceId(0); |
| 457 | + write_defaults_file( |
| 458 | + dir.path(), |
| 459 | + HashMap::from([(keyname.clone(), JsonValue::from(default_value))]), |
| 460 | + default_id, |
| 461 | + )?; |
| 462 | + |
| 463 | + let hash_file = dir.path().join(format!("kvs_{default_id}_0.hash")); |
| 464 | + assert!( |
| 465 | + !hash_file.exists(), |
| 466 | + "Hash file should NOT exist before opening KVS with defaults" |
| 467 | + ); |
| 468 | + |
| 469 | + { |
| 470 | + // KVS instance with defaults. |
| 471 | + let kvs_with_defaults = Kvs::open( |
| 472 | + default_id.clone(), |
| 473 | + OpenNeedDefaults::Required, |
| 474 | + OpenNeedKvs::Optional, |
| 475 | + Some(dir_string.clone()), |
| 476 | + )?; |
| 477 | + } |
| 478 | + |
| 479 | + assert!( |
| 480 | + hash_file.exists(), |
| 481 | + "Hash file should exist after opening KVS with defaults" |
| 482 | + ); |
| 483 | + |
| 484 | + Ok(()) |
| 485 | +} |
| 486 | + |
| 487 | +#[test] |
| 488 | +// #[record_property("PartiallyVerifies", ["comp_req__persistency__value_default", "comp_req__persistency__default_value_config"])] |
| 489 | +// #[record_property("FullyVerifies", [""])] |
| 490 | +// #[record_property("Description", "Placeholder for future test: should verify resetting a single key to its default value.")] |
| 491 | +// #[record_property("TestType", "requirements-based")] |
| 492 | +// #[record_property("DerivationTechnique", "requirements-based")] |
| 493 | +#[ignore = "not implemented yet"] |
400 | 494 | fn cit_persistency_reset_single_default_value() -> Result<(), ErrorCode> { |
401 | 495 | // TODO: This test is not implemented yet. |
402 | 496 | // API supports resetting only all keys. |
|
0 commit comments