Skip to content

Commit b0af39f

Browse files
committed
Add test for FilesystemStore-to-FilesystemStore migration
1 parent 4c1f6bd commit b0af39f

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,9 @@ impl MigratableKVStore for FilesystemStore {
494494
#[cfg(test)]
495495
mod tests {
496496
use super::*;
497-
use crate::test_utils::{do_read_write_remove_list_persist, do_test_store};
497+
use crate::test_utils::{
498+
do_read_write_remove_list_persist, do_test_data_migration, do_test_store,
499+
};
498500

499501
use bitcoin::Txid;
500502

@@ -527,6 +529,19 @@ mod tests {
527529
do_read_write_remove_list_persist(&fs_store);
528530
}
529531

532+
#[test]
533+
fn test_data_migration() {
534+
let mut source_temp_path = std::env::temp_dir();
535+
source_temp_path.push("test_data_migration_source");
536+
let mut source_store = FilesystemStore::new(source_temp_path);
537+
538+
let mut target_temp_path = std::env::temp_dir();
539+
target_temp_path.push("test_data_migration_target");
540+
let mut target_store = FilesystemStore::new(target_temp_path);
541+
542+
do_test_data_migration(&mut source_store, &mut target_store);
543+
}
544+
530545
#[test]
531546
fn test_if_monitors_is_not_dir() {
532547
let store = FilesystemStore::new("test_monitors_is_not_dir".into());

lightning-persister/src/test_utils.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use lightning::ln::functional_test_utils::{
33
connect_block, create_announced_chan_between_nodes, create_chanmon_cfgs, create_dummy_block,
44
create_network, create_node_cfgs, create_node_chanmgrs, send_payment,
55
};
6-
use lightning::util::persist::{read_channel_monitors, KVStore, KVSTORE_NAMESPACE_KEY_MAX_LEN};
6+
use lightning::util::persist::{
7+
migrate_kv_store_data, read_channel_monitors, KVStore, MigratableKVStore,
8+
KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN,
9+
};
710
use lightning::util::test_utils;
811
use lightning::{check_added_monitors, check_closed_broadcast, check_closed_event};
912

@@ -59,6 +62,41 @@ pub(crate) fn do_read_write_remove_list_persist<K: KVStore + RefUnwindSafe>(kv_s
5962
assert_eq!(listed_keys.len(), 0);
6063
}
6164

65+
pub(crate) fn do_test_data_migration<S: MigratableKVStore, T: MigratableKVStore>(
66+
source_store: &mut S, target_store: &mut T,
67+
) {
68+
// We fill the source with some bogus keys.
69+
let dummy_data = [42u8; 32];
70+
let num_primary_namespaces = 2;
71+
let num_secondary_namespaces = 2;
72+
let num_keys = 3;
73+
for i in 0..num_primary_namespaces {
74+
let primary_namespace =
75+
format!("testspace{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(i).unwrap());
76+
for j in 0..num_secondary_namespaces {
77+
let secondary_namespace =
78+
format!("testsubspace{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(j).unwrap());
79+
for k in 0..num_keys {
80+
let key =
81+
format!("testkey{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(k).unwrap());
82+
source_store
83+
.write(&primary_namespace, &secondary_namespace, &key, &dummy_data)
84+
.unwrap();
85+
}
86+
}
87+
}
88+
let total_num_entries = num_primary_namespaces * num_secondary_namespaces * num_keys;
89+
let all_keys = source_store.list_all_keys().unwrap();
90+
assert_eq!(all_keys.len(), total_num_entries);
91+
92+
migrate_kv_store_data(source_store, target_store).unwrap();
93+
94+
assert_eq!(target_store.list_all_keys().unwrap().len(), total_num_entries);
95+
for (p, s, k) in &all_keys {
96+
assert_eq!(target_store.read(p, s, k).unwrap(), dummy_data);
97+
}
98+
}
99+
62100
// Integration-test the given KVStore implementation. Test relaying a few payments and check that
63101
// the persisted data is updated the appropriate number of times.
64102
pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {

0 commit comments

Comments
 (0)