From 33752a757bf98f95250d76e4bfc15e968788b959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=AD=90=EF=B8=8FNINIKA=E2=AD=90=EF=B8=8F?= Date: Wed, 21 Aug 2024 15:50:18 +0300 Subject: [PATCH] feat(test-env): Preserve test env logs on crash (#4959) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(test-env): leave the environment directory intact on wait-for-genesis failure This allows you to inspect logs in case of problems with starting iroha, instead of just deleting them Signed-off-by: ⭐️NINIKA⭐️ * feat(test-env): Rename log files to not be hidden Signed-off-by: ⭐️NINIKA⭐️ * feat(test-env): Put both stdout and stderr into the same file Signed-off-by: ⭐️NINIKA⭐️ --------- Signed-off-by: ⭐️NINIKA⭐️ --- scripts/test_env.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/scripts/test_env.py b/scripts/test_env.py index 9ab3270135e..748aac96254 100755 --- a/scripts/test_env.py +++ b/scripts/test_env.py @@ -77,7 +77,9 @@ def wait_for_genesis(self, n_tries: int): logging.info(f"Error connecting to genesis peer: {e}. Sleeping 1 second...") time.sleep(1) logging.critical(f"Genesis block wasn't created within {n_tries} seconds. Aborting...") - cleanup(self.out_dir) + cleanup(self.out_dir, True) + logging.critical(f"Test environment directory `{self.out_dir}` was left intact. " + f"Inspect it or use `cleanup` subcommand to remove it.") sys.exit(2) def run(self): @@ -148,11 +150,10 @@ def run(self): logging.info(f"Running peer {self.name}...") # FD never gets closed - stdout_file = open(self.peer_dir / ".stdout", "w") - stderr_file = open(self.peer_dir / ".stderr", "w") + log_file = open(self.peer_dir / "log.txt", "w") # These processes are created detached from the parent process already subprocess.Popen([self.name, "--config", self.config_path], - executable=self.out_dir / "peers/irohad", stdout=stdout_file, stderr=stderr_file) + executable=self.out_dir / "peers/irohad", stdout=log_file, stderr=log_file) def pos_int(arg): if int(arg) > 0: @@ -241,7 +242,7 @@ def main(args: argparse.Namespace): if args.command == "setup": setup(args) elif args.command == "cleanup": - cleanup(args.out_dir) + cleanup(args.out_dir, False) def setup(args: argparse.Namespace): logging.info(f"Starting Iroha network with {args.n_peers} peers...") @@ -254,11 +255,14 @@ def setup(args: argparse.Namespace): Network(args).run() -def cleanup(out_dir: pathlib.Path): +def cleanup(out_dir: pathlib.Path, keep_out_dir: bool): logging.info("Killing peer processes...") subprocess.run(["pkill", "-9", "iroha"]) - logging.info(f"Cleaning up test directory `{out_dir}`...") - shutil.rmtree(out_dir) + if keep_out_dir: + logging.info(f"Leaving test directory `{out_dir}` as-is...") + else: + logging.info(f"Cleaning up test directory `{out_dir}`...") + shutil.rmtree(out_dir)