From 9afcbe36a40ac4d28b1d3fe14c0777a7d39a9bab Mon Sep 17 00:00:00 2001 From: Davidson Souza Date: Fri, 10 Feb 2023 16:48:27 -0300 Subject: [PATCH] Add a restart test --- Cargo.toml | 5 ++--- tests/example_test.py | 12 +++++----- tests/{basic => }/restart.py | 6 ++--- tests/run_tests.py | 31 ++++++++++++++++++++++++++ tests/test_framework/test_framework.py | 8 ++++--- 5 files changed, 47 insertions(+), 15 deletions(-) rename tests/{basic => }/restart.py (82%) create mode 100644 tests/run_tests.py diff --git a/Cargo.toml b/Cargo.toml index a77bb43e..031aaff0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,9 +5,8 @@ edition = "2021" [dependencies] rustreexo = { git = "https://www.github.com/mit-dci/rustreexo" } -# btcd-rpc = { git = "https://github.com/Davidson-Souza/rust-btcd-rpc", features = ["utreexod"], branch = "use-reqwest"} -btcd-rpc = { path = "../utreexod-cli/client/", features = ["utreexod"] } -clap = {version = "4.0.29", features = ["derive"]} +btcd-rpc = { git = "https://github.com/Davidson-Souza/rust-btcd-rpc", features = ["utreexod"], branch = "use-reqwest"} +clap = { version = "4.0.29", features = ["derive"] } sha2 = "^0.10.6" async-std = "1.12.0" log = "0.4" diff --git a/tests/example_test.py b/tests/example_test.py index 3c4ea79f..3d24c1a6 100644 --- a/tests/example_test.py +++ b/tests/example_test.py @@ -8,10 +8,10 @@ from test_framework.electrum_client import ElectrumClient from test_framework.mock_rpc import MockUtreexod -# Tests should be a child class from TestFramework - class ExampleTest(TestFramework): + """ Tests should be a child class from TestFramework """ + # All tests should override the run_test method def run_test(self): # This creates a dummy rpc listening on port 8080 @@ -28,8 +28,10 @@ def run_test(self): print(electrum.get_version()) # .... cleanup .... - os.rmdir("./data") - # Stop any rpc server that is running - self.stop_rpc() + # Stop the node that is running self.stop_node(0) + + +if __name__ == '__main__': + ExampleTest().main() diff --git a/tests/basic/restart.py b/tests/restart.py similarity index 82% rename from tests/basic/restart.py rename to tests/restart.py index ca077567..fd1598ce 100644 --- a/tests/basic/restart.py +++ b/tests/restart.py @@ -11,13 +11,11 @@ def run_test(self): Tests if we don't corrupt our data dir between restarts. This would have caught, the error fixed in #9 """ - print("Running restart test") base_testdir = "data/TestRestart/" self.run_node(base_testdir + "1/") - time.sleep(10) + time.sleep(5) self.stop_node(0) self.run_node(base_testdir + "2/") - time.sleep(10) + time.sleep(5) self.stop_node(0) assert (filecmp.dircmp(base_testdir + "2/", base_testdir + "1/")) - print("restart test passed!") diff --git a/tests/run_tests.py b/tests/run_tests.py new file mode 100644 index 00000000..303b6e96 --- /dev/null +++ b/tests/run_tests.py @@ -0,0 +1,31 @@ +import os +import subprocess +import time +from tqdm import tqdm + +BASE_DIR = "/tmp/data" + +tests = ["example_test", "restart"] + + +def main(): + print("Creating work dir") + data_dir = BASE_DIR + f"run-{time.time()}/" + if not os.path.isdir(data_dir): + os.makedirs(data_dir) + print("Running tests") + for test_name in tqdm(tests): + test_dir = "./tests/" + test_name + log_dir = data_dir + test_name.replace(".py", ".log") + log_file = open(log_dir, "wt") + test = subprocess.Popen(["python", test_dir + ".py"], + stdout=log_file, stderr=log_file) + test.wait() + if test.returncode != 0: + print(f"Test {test_name} not passed") + else: + print(f"Test {test_name} passed") + + +if __name__ == '__main__': + main() diff --git a/tests/test_framework/test_framework.py b/tests/test_framework/test_framework.py index 0c4c6f32..951d4359 100644 --- a/tests/test_framework/test_framework.py +++ b/tests/test_framework/test_framework.py @@ -23,12 +23,14 @@ def run_rpc(self): self.rpc.daemon = True self.rpc.start() - def stop_rpc(self): - self.rpc._stop() - def stop_node(self, idx: int): self.nodes[idx].send_signal(15) + self.nodes[idx].wait() # Should be overrided by individual tests + def run_test(self): raise NotImplemented + + def main(self): + self.run_test()