Skip to content

Commit 0b59447

Browse files
jackstar12rustyrussell
authored andcommitted
tests: adjust cln-grpc tests for new default start
1 parent bc9834c commit 0b59447

File tree

4 files changed

+46
-74
lines changed

4 files changed

+46
-74
lines changed

contrib/pyln-testing/pyln/testing/utils.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,6 @@ def __init__(
595595
port=9735,
596596
random_hsm=False,
597597
node_id=0,
598-
grpc_port=None
599598
):
600599
# We handle our own version of verbose, below.
601600
TailableProc.__init__(self, lightning_dir, verbose=False)
@@ -613,6 +612,7 @@ def __init__(
613612
'addr': '127.0.0.1:{}'.format(port),
614613
'allow-deprecated-apis': '{}'.format("true" if DEPRECATED_APIS
615614
else "false"),
615+
616616
'network': TEST_NETWORK,
617617
'ignore-fee-limits': 'false',
618618
'bitcoin-rpcuser': BITCOIND_CONFIG['rpcuser'],
@@ -622,9 +622,6 @@ def __init__(
622622
'bitcoin-datadir': lightning_dir,
623623
}
624624

625-
if grpc_port is not None:
626-
opts['grpc-port'] = grpc_port
627-
628625
for k, v in opts.items():
629626
self.opts[k] = v
630627

@@ -758,7 +755,7 @@ def __init__(self, node_id, lightning_dir, bitcoind, executor, valgrind, may_fai
758755
broken_log=None,
759756
allow_warning=False,
760757
allow_bad_gossip=False,
761-
db=None, port=None, disconnect=None, random_hsm=None, options=None,
758+
db=None, port=None, grpc_port=None, disconnect=None, random_hsm=None, options=None,
762759
jsonschemas={},
763760
valgrind_plugins=True,
764761
**kwargs):
@@ -783,7 +780,6 @@ def __init__(self, node_id, lightning_dir, bitcoind, executor, valgrind, may_fai
783780
self.daemon = LightningD(
784781
lightning_dir, bitcoindproxy=bitcoind.get_proxy(),
785782
port=port, random_hsm=random_hsm, node_id=node_id,
786-
grpc_port=self.grpc_port,
787783
)
788784

789785
self.disconnect = disconnect
@@ -834,6 +830,13 @@ def __init__(self, node_id, lightning_dir, bitcoind, executor, valgrind, may_fai
834830
if SLOW_MACHINE:
835831
self.daemon.cmd_prefix += ['--read-inline-info=no']
836832

833+
if self.daemon.opts.get('disable-plugin') == 'cln-grpc':
834+
self.grpc_port = None
835+
else:
836+
if grpc_port:
837+
self.daemon.opts['grpc-port'] = grpc_port
838+
self.grpc_port = grpc_port or 9736
839+
837840
def _create_rpc(self, jsonschemas):
838841
"""Prepares anything related to the RPC.
839842
"""
@@ -845,7 +848,7 @@ def _create_rpc(self, jsonschemas):
845848

846849
def _create_grpc_rpc(self):
847850
from pyln.testing import grpc
848-
self.grpc_port = reserve_unused_port()
851+
self.grpc_port = self.grpc_port or reserve_unused_port()
849852
d = self.lightning_dir / TEST_NETWORK
850853
d.mkdir(parents=True, exist_ok=True)
851854

@@ -868,7 +871,6 @@ def _create_grpc_rpc(self):
868871

869872
def _create_jsonrpc_rpc(self, jsonschemas):
870873
socket_path = self.lightning_dir / TEST_NETWORK / "lightning-rpc"
871-
self.grpc_port = None
872874

873875
self.rpc = PrettyPrintingLightningRpc(
874876
str(socket_path),
@@ -881,12 +883,7 @@ def grpc(self):
881883
"""Tiny helper to return a grpc stub if grpc was configured.
882884
"""
883885
# Before doing anything let's see if we have a grpc-port at all
884-
try:
885-
grpc_port = int(filter(
886-
lambda v: v[0] == 'grpc-port',
887-
self.daemon.opts.items()
888-
).__next__()[1])
889-
except Exception:
886+
if not self.grpc_port:
890887
raise ValueError("grpc-port is not specified, can't connect over grpc")
891888

892889
import grpc
@@ -902,7 +899,7 @@ def grpc(self):
902899
)
903900

904901
channel = grpc.secure_channel(
905-
f"localhost:{grpc_port}",
902+
f"localhost:{self.grpc_port}",
906903
creds,
907904
options=(('grpc.ssl_target_name_override', 'cln'),)
908905
)
@@ -1591,9 +1588,10 @@ def get_nodes(self, num_nodes, opts=None):
15911588
def get_node(self, node_id=None, options=None, dbfile=None,
15921589
bkpr_dbfile=None, feerates=(15000, 11000, 7500, 3750),
15931590
start=True, wait_for_bitcoind_sync=True, may_fail=False,
1594-
expect_fail=False, cleandir=True, gossip_store_file=None, **kwargs):
1591+
expect_fail=False, cleandir=True, gossip_store_file=None, unused_grpc_port=True, **kwargs):
15951592
node_id = self.get_node_id() if not node_id else node_id
15961593
port = reserve_unused_port()
1594+
grpc_port = self.get_unused_port() if unused_grpc_port else None
15971595

15981596
lightning_dir = os.path.join(
15991597
self.directory, "lightning-{}/".format(node_id))
@@ -1607,7 +1605,7 @@ def get_node(self, node_id=None, options=None, dbfile=None,
16071605
db.provider = self.db_provider
16081606
node = self.node_cls(
16091607
node_id, lightning_dir, self.bitcoind, self.executor, self.valgrind, db=db,
1610-
port=port, options=options, may_fail=may_fail or expect_fail,
1608+
port=port, grpc_port=grpc_port, options=options, may_fail=may_fail or expect_fail,
16111609
jsonschemas=self.jsonschemas,
16121610
**kwargs
16131611
)

tests/test_cln_rs.py

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
def wait_for_grpc_start(node):
2222
"""This can happen before "public key" which start() swallows"""
23-
wait_for(lambda: node.daemon.is_in_log(r'serving grpc on 0.0.0.0:'))
23+
wait_for(lambda: node.daemon.is_in_log(r'serving grpc'))
2424

2525

2626
def test_rpc_client(node_factory):
@@ -35,8 +35,9 @@ def test_plugin_start(node_factory):
3535
"""Start a minimal plugin and ensure it is well-behaved
3636
"""
3737
bin_path = Path.cwd() / "target" / RUST_PROFILE / "examples" / "cln-plugin-startup"
38-
l1 = node_factory.get_node(options={"plugin": str(bin_path), 'test-option': 31337})
39-
l2 = node_factory.get_node()
38+
l1, l2 = node_factory.get_nodes(2, opts=[
39+
{"plugin": str(bin_path), 'test-option': 31337}, {}
40+
])
4041

4142
# The plugin should be in the list of active plugins
4243
plugins = l1.rpc.plugin('list')['plugins']
@@ -107,9 +108,7 @@ def test_plugin_options_handle_defaults(node_factory):
107108
def test_grpc_connect(node_factory):
108109
"""Attempts to connect to the grpc interface and call getinfo"""
109110
# These only exist if we have rust!
110-
111-
grpc_port = node_factory.get_unused_port()
112-
l1 = node_factory.get_node(options={"grpc-port": str(grpc_port)})
111+
l1 = node_factory.get_node()
113112

114113
p = Path(l1.daemon.lightning_dir) / TEST_NETWORK
115114
cert_path = p / "client.pem"
@@ -123,7 +122,7 @@ def test_grpc_connect(node_factory):
123122

124123
wait_for_grpc_start(l1)
125124
channel = grpc.secure_channel(
126-
f"localhost:{grpc_port}",
125+
f"localhost:{l1.grpc_port}",
127126
creds,
128127
options=(('grpc.ssl_target_name_override', 'cln'),)
129128
)
@@ -164,10 +163,7 @@ def test_grpc_generate_certificate(node_factory):
164163
- If we have certs, we they should just get loaded
165164
- If we delete one cert or its key it should get regenerated.
166165
"""
167-
grpc_port = node_factory.get_unused_port()
168-
l1 = node_factory.get_node(options={
169-
"grpc-port": str(grpc_port),
170-
}, start=False)
166+
l1 = node_factory.get_node(start=False)
171167

172168
p = Path(l1.daemon.lightning_dir) / TEST_NETWORK
173169
files = [p / f for f in [
@@ -202,18 +198,20 @@ def test_grpc_generate_certificate(node_factory):
202198
assert all(private)
203199

204200

205-
def test_grpc_no_auto_start(node_factory):
206-
"""Ensure that we do not start cln-grpc unless a port is configured.
207-
Also check that we do not generate certificates.
208-
"""
209-
l1 = node_factory.get_node()
201+
def test_grpc_default_port_auto_starts(node_factory):
202+
"""Ensure that we start cln-grpc on default port. Also check that certificates are generated."""
203+
l1 = node_factory.get_node(unused_grpc_port=False)
210204

211-
wait_for(lambda: [p for p in l1.rpc.plugin('list')['plugins'] if 'cln-grpc' in p['name']] == [])
212-
assert l1.daemon.is_in_log(r'plugin-cln-grpc: Killing plugin: disabled itself at init')
213-
p = Path(l1.daemon.lightning_dir) / TEST_NETWORK
214-
files = os.listdir(p)
215-
pem_files = [f for f in files if re.match(r".*\.pem$", f)]
216-
assert pem_files == []
205+
grpcplugin = next((p for p in l1.rpc.plugin('list')['plugins'] if 'cln-grpc' in p['name'] and p['active']), None)
206+
# Check that the plugin is active
207+
assert grpcplugin is not None
208+
# Check that the plugin is listening on the default port
209+
assert l1.daemon.is_in_log(f'plugin-cln-grpc: Plugin logging initialized')
210+
# Check that the certificates are generated
211+
assert len([f for f in os.listdir(Path(l1.daemon.lightning_dir) / TEST_NETWORK) if re.match(r".*\.pem$", f)]) >= 6
212+
213+
# Check server connection
214+
l1.grpc.Getinfo(clnpb.GetinfoRequest())
217215

218216

219217
def test_grpc_wrong_auth(node_factory):
@@ -223,12 +221,7 @@ def test_grpc_wrong_auth(node_factory):
223221
and then we try to cross the wires.
224222
"""
225223
# These only exist if we have rust!
226-
227-
grpc_port = node_factory.get_unused_port()
228-
l1, l2 = node_factory.get_nodes(2, opts={
229-
"start": False,
230-
"grpc-port": str(grpc_port),
231-
})
224+
l1, l2 = node_factory.get_nodes(2, opts=[{"start": False}, {"start": False}])
232225
l1.start()
233226
wait_for_grpc_start(l1)
234227

@@ -246,7 +239,7 @@ def connect(node):
246239
)
247240

248241
channel = grpc.secure_channel(
249-
f"localhost:{grpc_port}",
242+
f"localhost:{node.grpc_port}",
250243
creds,
251244
options=(('grpc.ssl_target_name_override', 'cln'),)
252245
)
@@ -282,8 +275,7 @@ def test_cln_plugin_reentrant(node_factory, executor):
282275
283276
"""
284277
bin_path = Path.cwd() / "target" / RUST_PROFILE / "examples" / "cln-plugin-reentrant"
285-
l1 = node_factory.get_node(options={"plugin": str(bin_path)})
286-
l2 = node_factory.get_node()
278+
l1, l2 = node_factory.get_nodes(2, opts=[{"plugin": str(bin_path)}, {}])
287279
l2.connect(l1)
288280
l2.fundchannel(l1)
289281

@@ -311,18 +303,13 @@ def test_grpc_keysend_routehint(bitcoind, node_factory):
311303
recipient.
312304
313305
"""
314-
grpc_port = node_factory.get_unused_port()
315306
l1, l2, l3 = node_factory.line_graph(
316307
3,
317-
opts=[
318-
{"grpc-port": str(grpc_port)}, {}, {}
319-
],
320308
announce_channels=True, # Do not enforce scid-alias
321309
)
322310
bitcoind.generate_block(3)
323311
sync_blockheight(bitcoind, [l1, l2, l3])
324312

325-
stub = l1.grpc
326313
chan = l2.rpc.listpeerchannels(l3.info['id'])
327314

328315
routehint = clnpb.RoutehintList(hints=[
@@ -348,19 +335,15 @@ def test_grpc_keysend_routehint(bitcoind, node_factory):
348335
routehints=routehint,
349336
)
350337

351-
res = stub.KeySend(call)
338+
res = l1.grpc.KeySend(call)
352339
print(res)
353340

354341

355342
def test_grpc_listpeerchannels(bitcoind, node_factory):
356343
""" Check that conversions of this rather complex type work.
357344
"""
358-
grpc_port = node_factory.get_unused_port()
359345
l1, l2 = node_factory.line_graph(
360346
2,
361-
opts=[
362-
{"grpc-port": str(grpc_port)}, {}
363-
],
364347
announce_channels=True, # Do not enforce scid-alias
365348
)
366349

@@ -385,8 +368,7 @@ def test_grpc_listpeerchannels(bitcoind, node_factory):
385368

386369

387370
def test_grpc_decode(node_factory):
388-
grpc_port = node_factory.get_unused_port()
389-
l1 = node_factory.get_node(options={'grpc-port': str(grpc_port)})
371+
l1 = node_factory.get_node()
390372
inv = l1.grpc.Invoice(clnpb.InvoiceRequest(
391373
amount_msat=clnpb.AmountOrAny(any=True),
392374
description="desc",
@@ -418,9 +400,7 @@ def test_rust_plugin_subscribe_wildcard(node_factory):
418400

419401

420402
def test_grpc_block_added_notifications(node_factory, bitcoind):
421-
grpc_port = node_factory.get_unused_port()
422-
423-
l1 = node_factory.get_node(options={"grpc-port": str(grpc_port)})
403+
l1 = node_factory.get_node()
424404

425405
# Test the block_added notification
426406
# Start listening to block added events over grpc
@@ -436,10 +416,7 @@ def test_grpc_block_added_notifications(node_factory, bitcoind):
436416

437417

438418
def test_grpc_connect_notification(node_factory):
439-
grpc_port = node_factory.get_unused_port()
440-
441-
l1 = node_factory.get_node(options={"grpc-port": str(grpc_port)})
442-
l2 = node_factory.get_node()
419+
l1, l2 = node_factory.get_nodes(2)
443420

444421
# Test the connect notification
445422
connect_stream = l1.grpc.SubscribeConnect(clnpb.StreamConnectRequest())
@@ -451,10 +428,7 @@ def test_grpc_connect_notification(node_factory):
451428

452429

453430
def test_grpc_custommsg_notification(node_factory):
454-
grpc_port = node_factory.get_unused_port()
455-
456-
l1 = node_factory.get_node(options={"grpc-port": str(grpc_port)})
457-
l2 = node_factory.get_node()
431+
l1, l2 = node_factory.get_nodes(2)
458432

459433
# Test the connect notification
460434
custommsg_stream = l1.grpc.SubscribeCustomMsg(clnpb.StreamCustomMsgRequest())

tests/test_clnrest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_clnrest_uses_grpc_plugin_certificates(node_factory):
6363
base_url = f'https://{rest_host}:{rest_port}'
6464
# This might happen really early!
6565
l1.daemon.logsearch_start = 0
66-
l1.daemon.wait_for_logs([r'serving grpc on 0.0.0.0:',
66+
l1.daemon.wait_for_logs([r'serving grpc on 127.0.0.1:',
6767
r'plugin-clnrest: REST server running at ' + base_url])
6868
ca_cert = Path(l1.daemon.lightning_dir) / TEST_NETWORK / 'ca.pem'
6969
http_session = http_session_with_retry()

tests/test_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ def test_channel_state_changed_bilateral(node_factory, bitcoind):
813813

814814
# a helper that gives us the next channel_state_changed log entry
815815
def wait_for_event(node):
816-
msg = node.daemon.wait_for_log("channel_state_changed.*new_state.*")
816+
msg = node.daemon.wait_for_log("plugin-misc_notifications.py: channel_state_changed.*new_state.*")
817817
event = ast.literal_eval(re.findall(".*({.*}).*", msg)[0])
818818
return event
819819

@@ -981,7 +981,7 @@ def test_channel_state_changed_unilateral(node_factory, bitcoind):
981981

982982
# a helper that gives us the next channel_state_changed log entry
983983
def wait_for_event(node):
984-
msg = node.daemon.wait_for_log("channel_state_changed.*new_state.*")
984+
msg = node.daemon.wait_for_log("plugin-misc_notifications.py: channel_state_changed.*new_state.*")
985985
event = ast.literal_eval(re.findall(".*({.*}).*", msg)[0])
986986
return event
987987

0 commit comments

Comments
 (0)