Skip to content

Commit

Permalink
tests: Adjust cln-grpc tests for cln-grpc default start
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahanaFarooqui committed Aug 13, 2024
1 parent 103420c commit 7a64585
Showing 1 changed file with 32 additions and 51 deletions.
83 changes: 32 additions & 51 deletions tests/test_cln_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def test_plugin_start(node_factory):
"""Start a minimal plugin and ensure it is well-behaved
"""
bin_path = Path.cwd() / "target" / RUST_PROFILE / "examples" / "cln-plugin-startup"
l1 = node_factory.get_node(options={"plugin": str(bin_path), 'test-option': 31337})
l2 = node_factory.get_node()
l1, l2 = node_factory.get_nodes(2, opts=[
{"grpc-port": str(node_factory.get_unused_port()), "plugin": str(bin_path), 'test-option': 31337},
{"grpc-port": str(node_factory.get_unused_port())}])

# The plugin should be in the list of active plugins
plugins = l1.rpc.plugin('list')['plugins']
Expand Down Expand Up @@ -76,6 +77,7 @@ def test_plugin_options_handle_defaults(node_factory):
bin_path = Path.cwd() / "target" / RUST_PROFILE / "examples" / "cln-plugin-startup"
l1 = node_factory.get_node(
options={
"grpc-port": str(node_factory.get_unused_port()),
"plugin": str(bin_path),
"opt-option": 31337,
"test-option": 31338,
Expand All @@ -94,7 +96,7 @@ def test_plugin_options_handle_defaults(node_factory):
assert opts["multi-i64-option-default"] == [5, 6]

# Do not set any value, should be None now
l1 = node_factory.get_node(options={"plugin": str(bin_path)})
l1 = node_factory.get_node(options={"grpc-port": str(node_factory.get_unused_port()), "plugin": str(bin_path)})
opts = l1.rpc.testoptions()
assert opts["opt-option"] is None, "opt-option has no default"
assert opts["test-option"] == 42, "test-option has a default of 42"
Expand All @@ -107,9 +109,8 @@ def test_plugin_options_handle_defaults(node_factory):
def test_grpc_connect(node_factory):
"""Attempts to connect to the grpc interface and call getinfo"""
# These only exist if we have rust!

grpc_port = node_factory.get_unused_port()
l1 = node_factory.get_node(options={"grpc-port": str(grpc_port)})
l1 = node_factory.get_node(options={"grpc-port": grpc_port})

p = Path(l1.daemon.lightning_dir) / TEST_NETWORK
cert_path = p / "client.pem"
Expand Down Expand Up @@ -164,9 +165,8 @@ def test_grpc_generate_certificate(node_factory):
- If we have certs, we they should just get loaded
- If we delete one cert or its key it should get regenerated.
"""
grpc_port = node_factory.get_unused_port()
l1 = node_factory.get_node(options={
"grpc-port": str(grpc_port),
"grpc-port": str(node_factory.get_unused_port()),
}, start=False)

p = Path(l1.daemon.lightning_dir) / TEST_NETWORK
Expand Down Expand Up @@ -202,18 +202,17 @@ def test_grpc_generate_certificate(node_factory):
assert all(private)


def test_grpc_no_auto_start(node_factory):
"""Ensure that we do not start cln-grpc unless a port is configured.
Also check that we do not generate certificates.
"""
def test_grpc_default_port_auto_starts(node_factory):
"""Ensure that we start cln-grpc on default port. Also check that certificates are generated."""
l1 = node_factory.get_node()

wait_for(lambda: [p for p in l1.rpc.plugin('list')['plugins'] if 'cln-grpc' in p['name']] == [])
assert l1.daemon.is_in_log(r'plugin-cln-grpc: Killing plugin: disabled itself at init')
p = Path(l1.daemon.lightning_dir) / TEST_NETWORK
files = os.listdir(p)
pem_files = [f for f in files if re.match(r".*\.pem$", f)]
assert pem_files == []
grpcplugin = next((p for p in l1.rpc.plugin('list')['plugins'] if 'cln-grpc' in p['name'] and p['active']), None)
# Check that the plugin is active
assert grpcplugin is not None
# Check that the plugin is listening on the default port
assert l1.daemon.is_in_log(f'plugin-cln-grpc: Plugin logging initialized')
# Check that the certificates are generated
assert len([f for f in os.listdir(Path(l1.daemon.lightning_dir) / TEST_NETWORK) if re.match(r".*\.pem$", f)]) >= 6


def test_grpc_wrong_auth(node_factory):
Expand All @@ -223,16 +222,13 @@ def test_grpc_wrong_auth(node_factory):
and then we try to cross the wires.
"""
# These only exist if we have rust!

grpc_port = node_factory.get_unused_port()
l1, l2 = node_factory.get_nodes(2, opts={
"start": False,
"grpc-port": str(grpc_port),
})
grpc_port_1 = node_factory.get_unused_port()
grpc_port_2 = node_factory.get_unused_port()
l1, l2 = node_factory.get_nodes(2, opts=[{"start": False, "grpc-port": str(grpc_port_1)}, {"start": False, "grpc-port": str(grpc_port_2)}])
l1.start()
wait_for_grpc_start(l1)

def connect(node):
def connect(node, grpcport):
p = Path(node.daemon.lightning_dir) / TEST_NETWORK
cert, key, ca = [f.open('rb').read() for f in [
p / 'client.pem',
Expand All @@ -246,13 +242,13 @@ def connect(node):
)

channel = grpc.secure_channel(
f"localhost:{grpc_port}",
f"localhost:{grpcport}",
creds,
options=(('grpc.ssl_target_name_override', 'cln'),)
)
return clnpb.NodeStub(channel)

stub = connect(l1)
stub = connect(l1, grpc_port_1)
# This should work, it's the correct node
stub.Getinfo(clnpb.GetinfoRequest())

Expand All @@ -265,7 +261,7 @@ def connect(node):
stub.Getinfo(clnpb.GetinfoRequest())

# Now load the correct ones and we should be good to go
stub = connect(l2)
stub = connect(l2, grpc_port_2)
stub.Getinfo(clnpb.GetinfoRequest())


Expand All @@ -282,8 +278,7 @@ def test_cln_plugin_reentrant(node_factory, executor):
"""
bin_path = Path.cwd() / "target" / RUST_PROFILE / "examples" / "cln-plugin-reentrant"
l1 = node_factory.get_node(options={"plugin": str(bin_path)})
l2 = node_factory.get_node()
l1, l2 = node_factory.get_nodes(2, opts=[{"plugin": str(bin_path)}, {"grpc-port": str(node_factory.get_unused_port())}])
l2.connect(l1)
l2.fundchannel(l1)

Expand Down Expand Up @@ -311,18 +306,14 @@ def test_grpc_keysend_routehint(bitcoind, node_factory):
recipient.
"""
grpc_port = node_factory.get_unused_port()
l1, l2, l3 = node_factory.line_graph(
3,
opts=[
{"grpc-port": str(grpc_port)}, {}, {}
],
opts=[{"grpc-port": str(node_factory.get_unused_port())} for _ in range(3)],
announce_channels=True, # Do not enforce scid-alias
)
bitcoind.generate_block(3)
sync_blockheight(bitcoind, [l1, l2, l3])

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

routehint = clnpb.RoutehintList(hints=[
Expand All @@ -348,18 +339,17 @@ def test_grpc_keysend_routehint(bitcoind, node_factory):
routehints=routehint,
)

res = stub.KeySend(call)
res = l1.grpc.KeySend(call)
print(res)


def test_grpc_listpeerchannels(bitcoind, node_factory):
""" Check that conversions of this rather complex type work.
"""
grpc_port = node_factory.get_unused_port()
l1, l2 = node_factory.line_graph(
2,
opts=[
{"grpc-port": str(grpc_port)}, {}
{"grpc-port": str(node_factory.get_unused_port())}, {}
],
announce_channels=True, # Do not enforce scid-alias
)
Expand All @@ -385,8 +375,7 @@ def test_grpc_listpeerchannels(bitcoind, node_factory):


def test_grpc_decode(node_factory):
grpc_port = node_factory.get_unused_port()
l1 = node_factory.get_node(options={'grpc-port': str(grpc_port)})
l1 = node_factory.get_node(options={'grpc-port': str(node_factory.get_unused_port())})
inv = l1.grpc.Invoice(clnpb.InvoiceRequest(
amount_msat=clnpb.AmountOrAny(any=True),
description="desc",
Expand All @@ -410,17 +399,15 @@ def test_rust_plugin_subscribe_wildcard(node_factory):
"""
bin_path = Path.cwd() / "target" / RUST_PROFILE / "examples" / "cln-subscribe-wildcard"
l1 = node_factory.get_node(options={"plugin": bin_path})
l2 = node_factory.get_node()
l2 = node_factory.get_node(options={"grpc-port": str(node_factory.get_unused_port())})

l2.connect(l1)

l1.daemon.wait_for_log("Received notification connect")


def test_grpc_block_added_notifications(node_factory, bitcoind):
grpc_port = node_factory.get_unused_port()

l1 = node_factory.get_node(options={"grpc-port": str(grpc_port)})
l1 = node_factory.get_node(options={"grpc-port": str(node_factory.get_unused_port())})

# Test the block_added notification
# Start listening to block added events over grpc
Expand All @@ -436,10 +423,7 @@ def test_grpc_block_added_notifications(node_factory, bitcoind):


def test_grpc_connect_notification(node_factory):
grpc_port = node_factory.get_unused_port()

l1 = node_factory.get_node(options={"grpc-port": str(grpc_port)})
l2 = node_factory.get_node()
l1, l2 = node_factory.get_nodes(2, opts=[{"grpc-port": str(node_factory.get_unused_port())} for _ in range(2)])

# Test the connect notification
connect_stream = l1.grpc.SubscribeConnect(clnpb.StreamConnectRequest())
Expand All @@ -451,10 +435,7 @@ def test_grpc_connect_notification(node_factory):


def test_grpc_custommsg_notification(node_factory):
grpc_port = node_factory.get_unused_port()

l1 = node_factory.get_node(options={"grpc-port": str(grpc_port)})
l2 = node_factory.get_node()
l1, l2 = node_factory.get_nodes(2, opts=[{"grpc-port": str(node_factory.get_unused_port())} for _ in range(2)])

# Test the connect notification
custommsg_stream = l1.grpc.SubscribeCustomMsg(clnpb.StreamCustomMsgRequest())
Expand Down

0 comments on commit 7a64585

Please sign in to comment.