Skip to content

Commit

Permalink
backup: Remove temporary node_factory shim
Browse files Browse the repository at this point in the history
This was required in order to avoid cleaning the datadir on creation.
  • Loading branch information
cdecker committed Aug 28, 2020
1 parent 3b0d7a0 commit b9a8622
Showing 1 changed file with 14 additions and 69 deletions.
83 changes: 14 additions & 69 deletions backup/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,7 @@
cli_path = os.path.join(os.path.dirname(__file__), "backup-cli")


class NodeFactoryWrapper(NodeFactory):
def get_node(self, node_id=None, options=None, dbfile=None,
feerates=(15000, 11000, 7500, 3750), start=True,
wait_for_bitcoind_sync=True, expect_fail=False,
cleandir=True, **kwargs):

node_id = self.get_node_id() if not node_id else node_id
port = self.get_next_port()

lightning_dir = os.path.join(
self.directory, "lightning-{}/".format(node_id))

if cleandir and os.path.exists(lightning_dir):
shutil.rmtree(lightning_dir)

# Get the DB backend DSN we should be using for this test and this
# node.
db = self.db_provider.get_db(os.path.join(lightning_dir, 'regtest'), self.testname, node_id)
node = self.node_cls(
node_id, lightning_dir, self.bitcoind, self.executor, db=db,
port=port, options=options, **kwargs
)

# Regtest estimatefee are unusable, so override.
node.set_feerates(feerates, False)

self.nodes.append(node)
if start:
try:
node.start(wait_for_bitcoind_sync)
except Exception:
if expect_fail:
return node
node.daemon.stop()
raise
return node


@pytest.fixture
def nf(request, directory, test_name, bitcoind, executor, db_provider, node_cls):
"""Temporarily patch the node_factory to not always clean the node directory.
"""
nf = NodeFactoryWrapper(
test_name,
bitcoind,
executor,
directory=directory,
db_provider=db_provider,
node_cls=node_cls
)

yield nf
ok, errs = nf.killall([not n.may_fail for n in nf.nodes])


def test_start(nf, directory):
def test_start(node_factory, directory):
bpath = os.path.join(directory, 'lightning-1', 'regtest')
bdest = 'file://' + os.path.join(bpath, 'backup.dbak')
os.makedirs(bpath)
Expand All @@ -76,7 +21,7 @@ def test_start(nf, directory):
'plugin': plugin_path,
'backup-destination': bdest,
}
l1 = nf.get_node(options=opts, cleandir=False)
l1 = node_factory.get_node(options=opts, cleandir=False)

l1.daemon.wait_for_log(r'backup.py')

Expand All @@ -86,7 +31,7 @@ def test_start(nf, directory):
l1.daemon.wait_for_log(r'Versions match up')


def test_start_no_init(nf, directory):
def test_start_no_init(node_factory, directory):
"""The plugin should refuse to start if we haven't initialized the backup
"""
bpath = os.path.join(directory, 'lightning-1', 'regtest')
Expand All @@ -96,7 +41,7 @@ def test_start_no_init(nf, directory):
'plugin': plugin_path,
'backup-destination': bdest,
}
l1 = nf.get_node(
l1 = node_factory.get_node(
options=opts, cleandir=False, may_fail=True, start=False
)

Expand All @@ -109,14 +54,14 @@ def test_start_no_init(nf, directory):
))


def test_init_not_empty(nf, directory):
def test_init_not_empty(node_factory, directory):
"""We want to add backups to an existing lightning node.
backup-cli init should start the backup with an initial snapshot.
"""
bpath = os.path.join(directory, 'lightning-1', 'regtest')
bdest = 'file://' + os.path.join(bpath, 'backup.dbak')
l1 = nf.get_node()
l1 = node_factory.get_node()
l1.stop()

out = subprocess.check_output([cli_path, "init", bpath, bdest])
Expand All @@ -129,7 +74,7 @@ def test_init_not_empty(nf, directory):
l1.daemon.wait_for_log(r'plugin-backup.py: Versions match up')


def test_tx_abort(nf, directory):
def test_tx_abort(node_factory, directory):
"""Simulate a crash between hook call and DB commit.
We simulate this by updating the data_version var in the database before
Expand All @@ -147,7 +92,7 @@ def test_tx_abort(nf, directory):
'plugin': plugin_path,
'backup-destination': bdest,
}
l1 = nf.get_node(options=opts, cleandir=False)
l1 = node_factory.get_node(options=opts, cleandir=False)
l1.stop()

print(l1.db.query("SELECT * FROM vars;"))
Expand All @@ -162,7 +107,7 @@ def test_tx_abort(nf, directory):


@flaky
def test_failing_restore(nf, directory):
def test_failing_restore(node_factory, directory):
"""The node database is having memory loss, make sure we abort.
We simulate a loss of transactions by manually resetting the data_version
Expand All @@ -182,7 +127,7 @@ def section(comment):
print("="*25, comment, "="*25)

section("Starting node for the first time")
l1 = nf.get_node(options=opts, cleandir=False)
l1 = node_factory.get_node(options=opts, cleandir=False, may_fail=True)
l1.stop()

# Now fudge the data_version:
Expand All @@ -198,7 +143,7 @@ def section(comment):
assert(l1.daemon.is_in_log(r'lost some state') is not None)


def test_intermittent_backup(nf, directory):
def test_intermittent_backup(node_factory, directory):
"""Simulate intermittent use of the backup, or an old file backup.
"""
Expand All @@ -210,7 +155,7 @@ def test_intermittent_backup(nf, directory):
'plugin': plugin_path,
'backup-destination': bdest,
}
l1 = nf.get_node(options=opts, cleandir=False)
l1 = node_factory.get_node(options=opts, cleandir=False, may_fail=True)

# Now start without the plugin. This should work fine.
del l1.daemon.opts['plugin']
Expand All @@ -228,7 +173,7 @@ def test_intermittent_backup(nf, directory):
assert(l1.daemon.is_in_log(r'Backup is out of date') is not None)


def test_restore(nf, directory):
def test_restore(node_factory, directory):
bpath = os.path.join(directory, 'lightning-1', 'regtest')
bdest = 'file://' + os.path.join(bpath, 'backup.dbak')
os.makedirs(bpath)
Expand All @@ -237,7 +182,7 @@ def test_restore(nf, directory):
'plugin': plugin_path,
'backup-destination': bdest,
}
l1 = nf.get_node(options=opts, cleandir=False)
l1 = node_factory.get_node(options=opts, cleandir=False)
l1.stop()

rdest = os.path.join(bpath, 'lightningd.sqlite.restore')
Expand Down

0 comments on commit b9a8622

Please sign in to comment.