Skip to content

Commit

Permalink
feat(test-env): Preserve test env logs on crash (#4959)
Browse files Browse the repository at this point in the history
* 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

* feat(test-env): Rename log files to not be hidden
* feat(test-env): Put both stdout and stderr into the same file

---------

Signed-off-by: ⭐️NINIKA⭐️ <[email protected]>
  • Loading branch information
DCNick3 authored and nxsaken committed Aug 21, 2024
1 parent 2d4121f commit 3cf5081
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions scripts/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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...")
Expand All @@ -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)



Expand Down

0 comments on commit 3cf5081

Please sign in to comment.