Skip to content

Commit 877a636

Browse files
committed
Add test for FilesystemStore-to-FilesystemStore migration
1 parent 7bc6a5e commit 877a636

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ impl MigratableKVStore for FilesystemStore {
475475
#[cfg(test)]
476476
mod tests {
477477
use super::*;
478-
use crate::test_utils::{do_read_write_remove_list_persist, do_test_store};
478+
use crate::test_utils::{
479+
do_read_write_remove_list_persist, do_test_data_migration, do_test_store,
480+
};
479481

480482
use bitcoin::Txid;
481483

@@ -508,6 +510,21 @@ mod tests {
508510
do_read_write_remove_list_persist(&fs_store);
509511
}
510512

513+
#[test]
514+
fn test_data_migration() {
515+
for move_data in [true, false] {
516+
let mut source_temp_path = std::env::temp_dir();
517+
source_temp_path.push("test_data_migration_source");
518+
let mut source_store = FilesystemStore::new(source_temp_path);
519+
520+
let mut target_temp_path = std::env::temp_dir();
521+
target_temp_path.push("test_data_migration_target");
522+
let mut target_store = FilesystemStore::new(target_temp_path);
523+
524+
do_test_data_migration(&mut source_store, &mut target_store, move_data);
525+
}
526+
}
527+
511528
#[test]
512529
fn test_if_monitors_is_not_dir() {
513530
let store = FilesystemStore::new("test_monitors_is_not_dir".into());

lightning-persister/src/test_utils.rs

Lines changed: 53 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,55 @@ 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, move_data: bool,
67+
) {
68+
// We fill the source with some bogus keys.
69+
let primary_namespace_base = "testspace".to_string();
70+
let secondary_namespace_base = "testsubspace".to_string();
71+
let key_base = "testkey".to_string();
72+
let alphabet_len = KVSTORE_NAMESPACE_KEY_ALPHABET.len();
73+
74+
let dummy_data = [42u8; 32];
75+
76+
let num_primary_namespaces = 5;
77+
let num_secondary_namespaces = 5;
78+
let num_keys = 10;
79+
for i in 0..num_primary_namespaces {
80+
let primary_ext = KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(i % alphabet_len).unwrap();
81+
let mut primary_namespace = primary_namespace_base.clone();
82+
primary_namespace.push(primary_ext);
83+
for j in 0..num_secondary_namespaces {
84+
let secondary_ext =
85+
KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(j % alphabet_len).unwrap();
86+
let mut secondary_namespace = secondary_namespace_base.clone();
87+
secondary_namespace.push(secondary_ext);
88+
for k in 0..num_keys {
89+
let key_ext = KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(k % alphabet_len).unwrap();
90+
let mut key = key_base.clone();
91+
key.push(key_ext);
92+
93+
source_store
94+
.write(&primary_namespace, &secondary_namespace, &key, &dummy_data)
95+
.unwrap();
96+
}
97+
}
98+
}
99+
let total_num_entries = num_primary_namespaces * num_secondary_namespaces * num_keys;
100+
let all_keys = source_store.list_all_keys().unwrap();
101+
assert_eq!(all_keys.len(), total_num_entries);
102+
103+
migrate_kv_store_data(source_store, target_store, move_data).unwrap();
104+
if move_data {
105+
assert_eq!(source_store.list_all_keys().unwrap().len(), 0);
106+
}
107+
108+
assert_eq!(target_store.list_all_keys().unwrap().len(), total_num_entries);
109+
for (p, s, k) in &all_keys {
110+
assert_eq!(target_store.read(p, s, k).unwrap(), dummy_data);
111+
}
112+
}
113+
62114
// Integration-test the given KVStore implementation. Test relaying a few payments and check that
63115
// the persisted data is updated the appropriate number of times.
64116
pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {

0 commit comments

Comments
 (0)