-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jsonrpc: added functional tests for floresta-cli commands (#340)
* getmemoryinfo (linux only) * getrpcinfo * uptime fix: florestad getrpcinfo command and florestad binary on tests * florestad/src/florestad.rs: bug fix on start method when send 'logpath' from 'floresta-cli getrpcinfo' command; fix: check for florestad binaries * search for binaries in /tmp/floresta-integration-tests or ./target/release * tests/test_framework/test_framework: added full path of 'florestad' binary to avoid break tests
- Loading branch information
Showing
7 changed files
with
359 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
""" | ||
floresta_cli_getmemoryinfo.py | ||
This functional test cli utility to interact with a Floresta node with `getmemoryinfo` | ||
""" | ||
|
||
import os | ||
import re | ||
import sys | ||
import tempfile | ||
from test_framework.test_framework import FlorestaTestFramework | ||
from test_framework.floresta_rpc import REGTEST_RPC_SERVER | ||
|
||
|
||
class GetMemoryInfoTest(FlorestaTestFramework): | ||
""" | ||
Test `getmemoryinfo` rpc call, by creating a node and get | ||
some memory stats with `stats` and `mallocinfo` modes | ||
""" | ||
|
||
nodes = [-1] | ||
|
||
# pylint: disable=duplicate-code | ||
data_dir = os.path.normpath( | ||
os.path.join( | ||
tempfile.gettempdir(), | ||
"floresta-func-tests", | ||
"florestacli-getmemoryinfo-test", | ||
"node-0", | ||
) | ||
) | ||
|
||
def set_test_params(self): | ||
""" | ||
Setup the two node florestad process with different data-dirs, electrum-addresses | ||
and rpc-addresses in the same regtest network | ||
""" | ||
GetMemoryInfoTest.nodes[0] = self.add_node_settings( | ||
chain="regtest", | ||
extra_args=[ | ||
f"--data-dir={GetMemoryInfoTest.data_dir}", | ||
], | ||
rpcserver=REGTEST_RPC_SERVER, | ||
) | ||
|
||
def test_mode_stats_ibd(self, node): | ||
""" | ||
Test `getmemoryinfo stats` when node is in IBD. | ||
It should return a dictionary with key(str)/value(int). | ||
""" | ||
if sys.platform == "linux": | ||
result = node.get_memoryinfo("stats") | ||
assert "locked" in result | ||
assert "chunks_free" in result["locked"] | ||
assert "chunks_used" in result["locked"] | ||
assert "free" in result["locked"] | ||
assert "locked" in result["locked"] | ||
assert "total" in result["locked"] | ||
assert "used" in result["locked"] | ||
assert result["locked"]["chunks_free"] >= 0 | ||
assert result["locked"]["chunks_used"] >= 0 | ||
assert result["locked"]["free"] >= 0 | ||
assert result["locked"]["locked"] >= 0 | ||
assert result["locked"]["total"] >= 0 | ||
assert result["locked"]["used"] >= 0 | ||
else: | ||
print( | ||
f"Skiping test: 'getmemoryinfo stats' not implemented for '{sys.platform}'" | ||
) | ||
|
||
def test_mode_mallocinfo_ibd(self, node): | ||
""" | ||
Test `getmemoryinfo mallocinfo` when node is in IBD | ||
It should return a malloc xml string. | ||
""" | ||
if sys.platform == "linux": | ||
pattern = ( | ||
r'<malloc version="[^"]+">' | ||
r'<heap nr="\d+">' | ||
r"<allocated>\d+</allocated>" | ||
r"<free>\d+</free>" | ||
r"<total>\d+</total>" | ||
r"<locked>\d+</locked>" | ||
r'<chunks nr="\d+">' | ||
r"<used>\d+</used>" | ||
r"<free>\d+</free>" | ||
r"</chunks>" | ||
r"</heap>" | ||
r"</malloc>" | ||
) | ||
result = node.get_memoryinfo("mallocinfo") | ||
assert re.fullmatch(pattern, result) | ||
else: | ||
print( | ||
f"Skiping test: 'getmemoryinfo malloc' not implemented for '{sys.platform}'" | ||
) | ||
|
||
def run_test(self): | ||
""" | ||
Run JSONRPC server on first, wait to connect, then call `addnode ip[:port]` | ||
""" | ||
# Start node | ||
self.run_node(GetMemoryInfoTest.nodes[0]) | ||
self.wait_for_rpc_connection(GetMemoryInfoTest.nodes[0]) | ||
|
||
# Test assertions | ||
node = self.get_node(GetMemoryInfoTest.nodes[0]) | ||
self.test_mode_stats_ibd(node) | ||
self.test_mode_mallocinfo_ibd(node) | ||
|
||
# Stop node | ||
self.stop_node(GetMemoryInfoTest.nodes[0]) | ||
|
||
|
||
if __name__ == "__main__": | ||
GetMemoryInfoTest().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
floresta_cli_getrpcinfo.py | ||
This functional test cli utility to interact with a Floresta node with `getrpcinfo` | ||
""" | ||
|
||
import os | ||
import tempfile | ||
from test_framework.test_framework import FlorestaTestFramework | ||
from test_framework.floresta_rpc import REGTEST_RPC_SERVER | ||
|
||
|
||
class GetRpcInfoTest(FlorestaTestFramework): | ||
""" | ||
Test `getrpcinfo` rpc call, by creating a node | ||
""" | ||
|
||
nodes = [-1] | ||
data_dir = os.path.normpath( | ||
os.path.join( | ||
tempfile.gettempdir(), | ||
"floresta-func-tests", | ||
"florestacli-getrpcinfo-test", | ||
"node-0", | ||
) | ||
) | ||
|
||
def set_test_params(self): | ||
""" | ||
Setup the two node florestad process with different data-dirs, electrum-addresses | ||
and rpc-addresses in the same regtest network | ||
""" | ||
GetRpcInfoTest.nodes[0] = self.add_node_settings( | ||
chain="regtest", | ||
extra_args=[ | ||
f"--data-dir={GetRpcInfoTest.data_dir}", | ||
], | ||
rpcserver=REGTEST_RPC_SERVER, | ||
) | ||
|
||
def test_rpcinfo_result(self, node): | ||
""" | ||
Test if the 'getrpcinfo' result was built correctly | ||
""" | ||
result = node.get_rpcinfo() | ||
assert "active_commands" in result | ||
assert "logpath" in result | ||
assert len(result["active_commands"]) == 1 | ||
assert "duration" in result["active_commands"][0] | ||
assert "method" in result["active_commands"][0] | ||
assert result["active_commands"][0]["duration"] == 0 | ||
assert result["active_commands"][0]["method"] == "getrpcinfo" | ||
assert result["logpath"] == os.path.normpath( | ||
os.path.join(GetRpcInfoTest.data_dir, "regtest", "output.log") | ||
) | ||
|
||
def run_test(self): | ||
""" | ||
Run JSONRPC server on first, wait to connect, then call `addnode ip[:port]` | ||
""" | ||
# Start node | ||
self.run_node(GetRpcInfoTest.nodes[0]) | ||
self.wait_for_rpc_connection(GetRpcInfoTest.nodes[0]) | ||
|
||
# Test assertions | ||
node = self.get_node(GetRpcInfoTest.nodes[0]) | ||
self.test_rpcinfo_result(node) | ||
|
||
# Stop node | ||
self.stop_node(GetRpcInfoTest.nodes[0]) | ||
|
||
|
||
if __name__ == "__main__": | ||
GetRpcInfoTest().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
floresta_cli_uptime.py | ||
This functional test cli utility to interact with a Floresta node with `uptime` | ||
""" | ||
|
||
import os | ||
import time | ||
import tempfile | ||
from test_framework.test_framework import FlorestaTestFramework | ||
from test_framework.floresta_rpc import REGTEST_RPC_SERVER | ||
|
||
|
||
class UptimeTest(FlorestaTestFramework): | ||
""" | ||
Test `uptime` rpc call, by creating a node, wait for | ||
some time (10 seconds) and assert that this wait time is | ||
equal to how long florestad has been running | ||
""" | ||
|
||
nodes = [-1] | ||
|
||
# pylint: disable=duplicate-code | ||
data_dir = os.path.normpath( | ||
os.path.join( | ||
tempfile.gettempdir(), | ||
"floresta-func-tests", | ||
"florestacli-uptime-test", | ||
"node-0", | ||
) | ||
) | ||
|
||
def set_test_params(self): | ||
""" | ||
Setup the two node florestad process with different data-dirs, electrum-addresses | ||
and rpc-addresses in the same regtest network | ||
""" | ||
UptimeTest.nodes[0] = self.add_node_settings( | ||
chain="regtest", | ||
extra_args=[ | ||
f"--data-dir={UptimeTest.data_dir}", | ||
], | ||
rpcserver=REGTEST_RPC_SERVER, | ||
) | ||
|
||
def run_test(self): | ||
""" | ||
Run JSONRPC server on first, wait to connect, then call `addnode ip[:port]` | ||
""" | ||
# Start node | ||
self.run_node(UptimeTest.nodes[0]) | ||
self.wait_for_rpc_connection(UptimeTest.nodes[0]) | ||
|
||
# wait for some seconds before get the uptime | ||
# and get the current time | ||
before = time.time() | ||
time.sleep(5) | ||
|
||
# Test assertions | ||
node = self.get_node(UptimeTest.nodes[0]) | ||
uptime = node.uptime() | ||
|
||
# calculate the elapsed time | ||
after = time.time() | ||
elapsed = int(after - before) | ||
|
||
assert uptime == elapsed | ||
|
||
# Stop node | ||
self.stop_node(UptimeTest.nodes[0]) | ||
|
||
|
||
if __name__ == "__main__": | ||
UptimeTest().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.