Skip to content

Commit

Permalink
Add test for FilesystemStore-to-FilesystemStore migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Dec 12, 2024
1 parent 7bc6a5e commit 877a636
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lightning-persister/src/fs_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,9 @@ impl MigratableKVStore for FilesystemStore {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{do_read_write_remove_list_persist, do_test_store};
use crate::test_utils::{
do_read_write_remove_list_persist, do_test_data_migration, do_test_store,
};

use bitcoin::Txid;

Expand Down Expand Up @@ -508,6 +510,21 @@ mod tests {
do_read_write_remove_list_persist(&fs_store);
}

#[test]
fn test_data_migration() {
for move_data in [true, false] {
let mut source_temp_path = std::env::temp_dir();
source_temp_path.push("test_data_migration_source");
let mut source_store = FilesystemStore::new(source_temp_path);

let mut target_temp_path = std::env::temp_dir();
target_temp_path.push("test_data_migration_target");
let mut target_store = FilesystemStore::new(target_temp_path);

do_test_data_migration(&mut source_store, &mut target_store, move_data);
}
}

#[test]
fn test_if_monitors_is_not_dir() {
let store = FilesystemStore::new("test_monitors_is_not_dir".into());
Expand Down
54 changes: 53 additions & 1 deletion lightning-persister/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use lightning::ln::functional_test_utils::{
connect_block, create_announced_chan_between_nodes, create_chanmon_cfgs, create_dummy_block,
create_network, create_node_cfgs, create_node_chanmgrs, send_payment,
};
use lightning::util::persist::{read_channel_monitors, KVStore, KVSTORE_NAMESPACE_KEY_MAX_LEN};
use lightning::util::persist::{
migrate_kv_store_data, read_channel_monitors, KVStore, MigratableKVStore,
KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN,
};
use lightning::util::test_utils;
use lightning::{check_added_monitors, check_closed_broadcast, check_closed_event};

Expand Down Expand Up @@ -59,6 +62,55 @@ pub(crate) fn do_read_write_remove_list_persist<K: KVStore + RefUnwindSafe>(kv_s
assert_eq!(listed_keys.len(), 0);
}

pub(crate) fn do_test_data_migration<S: MigratableKVStore, T: MigratableKVStore>(
source_store: &mut S, target_store: &mut T, move_data: bool,
) {
// We fill the source with some bogus keys.
let primary_namespace_base = "testspace".to_string();
let secondary_namespace_base = "testsubspace".to_string();
let key_base = "testkey".to_string();
let alphabet_len = KVSTORE_NAMESPACE_KEY_ALPHABET.len();

let dummy_data = [42u8; 32];

let num_primary_namespaces = 5;
let num_secondary_namespaces = 5;
let num_keys = 10;
for i in 0..num_primary_namespaces {
let primary_ext = KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(i % alphabet_len).unwrap();
let mut primary_namespace = primary_namespace_base.clone();
primary_namespace.push(primary_ext);
for j in 0..num_secondary_namespaces {
let secondary_ext =
KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(j % alphabet_len).unwrap();
let mut secondary_namespace = secondary_namespace_base.clone();
secondary_namespace.push(secondary_ext);
for k in 0..num_keys {
let key_ext = KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(k % alphabet_len).unwrap();
let mut key = key_base.clone();
key.push(key_ext);

source_store
.write(&primary_namespace, &secondary_namespace, &key, &dummy_data)
.unwrap();
}
}
}
let total_num_entries = num_primary_namespaces * num_secondary_namespaces * num_keys;
let all_keys = source_store.list_all_keys().unwrap();
assert_eq!(all_keys.len(), total_num_entries);

migrate_kv_store_data(source_store, target_store, move_data).unwrap();
if move_data {
assert_eq!(source_store.list_all_keys().unwrap().len(), 0);
}

assert_eq!(target_store.list_all_keys().unwrap().len(), total_num_entries);
for (p, s, k) in &all_keys {
assert_eq!(target_store.read(p, s, k).unwrap(), dummy_data);
}
}

// Integration-test the given KVStore implementation. Test relaying a few payments and check that
// the persisted data is updated the appropriate number of times.
pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {
Expand Down

0 comments on commit 877a636

Please sign in to comment.