Skip to content

Commit 3804d4c

Browse files
committed
Add test for FilesystemStore-to-FilesystemStore migration
1 parent ff024f7 commit 3804d4c

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
@@ -488,7 +488,9 @@ impl MigratableKVStore for FilesystemStore {
488488
#[cfg(test)]
489489
mod tests {
490490
use super::*;
491-
use crate::test_utils::{do_read_write_remove_list_persist, do_test_store};
491+
use crate::test_utils::{
492+
do_read_write_remove_list_persist, do_test_data_migration, do_test_store,
493+
};
492494

493495
use bitcoin::Txid;
494496

@@ -521,6 +523,19 @@ mod tests {
521523
do_read_write_remove_list_persist(&fs_store);
522524
}
523525

526+
#[test]
527+
fn test_data_migration() {
528+
let mut source_temp_path = std::env::temp_dir();
529+
source_temp_path.push("test_data_migration_source");
530+
let mut source_store = FilesystemStore::new(source_temp_path);
531+
532+
let mut target_temp_path = std::env::temp_dir();
533+
target_temp_path.push("test_data_migration_target");
534+
let mut target_store = FilesystemStore::new(target_temp_path);
535+
536+
do_test_data_migration(&mut source_store, &mut target_store);
537+
}
538+
524539
#[test]
525540
fn test_if_monitors_is_not_dir() {
526541
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)