def smoketest(ctx, debug, eth_client): """ Test, that the raiden installation is sane. """ from raiden.tests.utils.smoketest import setup_testchain_and_raiden, run_smoketest from raiden.tests.utils.transport import make_requests_insecure, matrix_server_starter report_file = mktemp(suffix=".log") configure_logging( logger_level_config={"": "DEBUG"}, log_file=report_file, disable_debug_logfile=ctx.parent.params["disable_debug_logfile"], ) free_port_generator = get_free_port() click.secho(f"Report file: {report_file}", fg="yellow") def append_report(subject: str, data: Optional[AnyStr] = None): with open(report_file, "a", encoding="UTF-8") as handler: handler.write(f'{f" {subject.upper()} ":=^80}{os.linesep}') if data is not None: write_data: str if isinstance(data, bytes): write_data = data.decode() else: write_data = data handler.writelines([write_data + os.linesep]) append_report("Raiden version", json.dumps(get_system_spec())) append_report("Raiden log") step_count = 7 if ctx.parent.params["transport"] == "matrix": step_count = 8 step = 0 stdout = sys.stdout def print_step(description: str, error: bool = False) -> None: nonlocal step step += 1 click.echo( "{} {}".format( click.style(f"[{step}/{step_count}]", fg="blue"), click.style(description, fg="green" if not error else "red"), ), file=stdout, ) print_step("Getting smoketest configuration") contracts_version = environment_type_to_contracts_version( ctx.parent.params["environment_type"]) with setup_testchain_and_raiden( transport=ctx.parent.params["transport"], eth_client=eth_client, matrix_server=ctx.parent.params["matrix_server"], contracts_version=contracts_version, print_step=print_step, free_port_generator=free_port_generator, ) as result: args = result["args"] contract_addresses = result["contract_addresses"] token = result["token"] ethereum_nodes = result["ethereum_nodes"] # Also respect environment type args["environment_type"] = ctx.parent.params["environment_type"] for option_ in run.params: if option_.name in args.keys(): args[option_.name] = option_.process_value( ctx, args[option_.name]) else: args[option_.name] = option_.default port = next(free_port_generator) args["api_address"] = "localhost:" + str(port) if args["transport"] == "udp": with SocketFactory("127.0.0.1", port, strategy="none") as mapped_socket: args["mapped_socket"] = mapped_socket success = run_smoketest( print_step=print_step, append_report=append_report, args=args, contract_addresses=contract_addresses, token=token, debug=debug, ethereum_nodes=ethereum_nodes, ) elif args["transport"] == "matrix": args["mapped_socket"] = None print_step("Starting Matrix transport") try: with matrix_server_starter( free_port_generator=free_port_generator ) as server_urls: # Disable TLS verification so we can connect to the self signed certificate make_requests_insecure() urllib3.disable_warnings(InsecureRequestWarning) args["extra_config"] = { "transport": { "matrix": { "available_servers": server_urls } } } success = run_smoketest( print_step=print_step, append_report=append_report, args=args, contract_addresses=contract_addresses, token=token, debug=debug, ethereum_nodes=ethereum_nodes, ) except (PermissionError, ProcessExitedWithError, FileNotFoundError): append_report("Matrix server start exception", traceback.format_exc()) print_step( f"Error during smoketest setup, report was written to {report_file}", error=True, ) success = False else: # Shouldn't happen raise RuntimeError(f"Invalid transport type '{args['transport']}'") if not success: sys.exit(1)
def smoketest(ctx: Context, debug: bool, eth_client: EthClient, report_path: Optional[str]) -> None: # pragma: no cover """ Test, that the raiden installation is sane. """ from raiden.tests.utils.smoketest import run_smoketest, setup_smoketest, step_printer raiden_stdout = StringIO() assert ctx.parent, MYPY_ANNOTATION environment_type = ctx.parent.params["environment_type"] disable_debug_logfile = ctx.parent.params["disable_debug_logfile"] if report_path is None: report_file = mktemp(suffix=".log") else: report_file = report_path click.secho(f"Report file: {report_file}", fg="yellow") configure_logging( logger_level_config={"": "DEBUG"}, log_file=report_file, disable_debug_logfile=disable_debug_logfile, ) def append_report(subject: str, data: Optional[AnyStr] = None) -> None: with open(report_file, "a", encoding="UTF-8") as handler: handler.write(f'{f" {subject.upper()} ":=^80}{os.linesep}') if data is not None: write_data: str if isinstance(data, bytes): write_data = data.decode() else: write_data = data handler.writelines([write_data + os.linesep]) append_report("Raiden version", json.dumps(get_system_spec())) append_report("Raiden log") free_port_generator = get_free_port() try: with step_printer(step_count=7, stdout=sys.stdout) as print_step: with setup_smoketest( eth_client=eth_client, print_step=print_step, free_port_generator=free_port_generator, debug=debug, stdout=raiden_stdout, append_report=append_report, ) as setup: args = setup.args port = next(free_port_generator) args["api_address"] = f"localhost:{port}" args["environment_type"] = environment_type # Matrix server args["one_to_n_contract_address"] = "0x" + "1" * 40 args["routing_mode"] = RoutingMode.PRIVATE args["flat_fee"] = () args["proportional_fee"] = () args["proportional_imbalance_fee"] = () for option_ in run.params: if option_.name in args.keys(): args[option_.name] = option_.process_value( ctx, args[option_.name]) else: args[option_.name] = option_.default run_smoketest(print_step=print_step, setup=setup) append_report("Raiden Node stdout", raiden_stdout.getvalue()) except: # noqa pylint: disable=bare-except if debug: import pdb pdb.post_mortem() # pylint: disable=no-member error = traceback.format_exc() append_report("Smoketest execution error", error) print_step("Smoketest execution error", error=True) success = False else: print_step("Smoketest successful") success = True if not success: sys.exit(1)
def smoketest( ctx: Context, debug: bool, eth_client: EthClient, report_path: Optional[str] ) -> None: """ Test, that the raiden installation is sane. """ from raiden.tests.utils.smoketest import ( setup_raiden, run_smoketest, setup_matrix_for_smoketest, setup_testchain_for_smoketest, ) from raiden.tests.utils.transport import make_requests_insecure, ParsedURL step_count = 8 step = 0 stdout = sys.stdout raiden_stdout = StringIO() assert ctx.parent, MYPY_ANNOTATION environment_type = ctx.parent.params["environment_type"] transport = ctx.parent.params["transport"] disable_debug_logfile = ctx.parent.params["disable_debug_logfile"] matrix_server = ctx.parent.params["matrix_server"] if transport != "matrix": raise RuntimeError(f"Invalid transport type '{transport}'") if report_path is None: report_file = mktemp(suffix=".log") else: report_file = report_path make_requests_insecure() urllib3.disable_warnings(InsecureRequestWarning) click.secho(f"Report file: {report_file}", fg="yellow") configure_logging( logger_level_config={"": "DEBUG"}, log_file=report_file, disable_debug_logfile=disable_debug_logfile, ) def append_report(subject: str, data: Optional[AnyStr] = None) -> None: with open(report_file, "a", encoding="UTF-8") as handler: handler.write(f'{f" {subject.upper()} ":=^80}{os.linesep}') if data is not None: write_data: str if isinstance(data, bytes): write_data = data.decode() else: write_data = data handler.writelines([write_data + os.linesep]) append_report("Raiden version", json.dumps(get_system_spec())) append_report("Raiden log") def print_step(description: str, error: bool = False) -> None: nonlocal step step += 1 click.echo( "{} {}".format( click.style(f"[{step}/{step_count}]", fg="blue"), click.style(description, fg="green" if not error else "red"), ), file=stdout, ) contracts_version = RAIDEN_CONTRACT_VERSION try: free_port_generator = get_free_port() ethereum_nodes = None datadir = mkdtemp() testchain_manager: ContextManager[Dict[str, Any]] = setup_testchain_for_smoketest( eth_client=eth_client, print_step=print_step, free_port_generator=free_port_generator, base_datadir=datadir, base_logdir=datadir, ) matrix_manager: ContextManager[ List[Tuple[ParsedURL, HTTPExecutor]] ] = setup_matrix_for_smoketest( print_step=print_step, free_port_generator=free_port_generator, broadcast_rooms_aliases=[ make_room_alias(NETWORKNAME_TO_ID["smoketest"], DISCOVERY_DEFAULT_ROOM), make_room_alias(NETWORKNAME_TO_ID["smoketest"], PATH_FINDING_BROADCASTING_ROOM), ], ) # Do not redirect the stdout on a debug session, otherwise the REPL # will also be redirected if debug: stdout_manager = contextlib.nullcontext() else: stdout_manager = contextlib.redirect_stdout(raiden_stdout) with stdout_manager, testchain_manager as testchain, matrix_manager as server_urls: result = setup_raiden( transport=transport, matrix_server=matrix_server, print_step=print_step, contracts_version=contracts_version, eth_client=testchain["eth_client"], eth_rpc_endpoint=testchain["eth_rpc_endpoint"], web3=testchain["web3"], base_datadir=testchain["base_datadir"], keystore=testchain["keystore"], ) args = result["args"] contract_addresses = result["contract_addresses"] ethereum_nodes = testchain["node_executors"] token = result["token"] port = next(free_port_generator) args["api_address"] = f"localhost:{port}" args["environment_type"] = environment_type # Matrix server # TODO: do we need more than one here? first_server = server_urls[0] args["matrix_server"] = first_server[0] args["one_to_n_contract_address"] = "0x" + "1" * 40 args["routing_mode"] = RoutingMode.LOCAL args["flat_fee"] = () args["proportional_fee"] = () args["proportional_imbalance_fee"] = () for option_ in run.params: if option_.name in args.keys(): args[option_.name] = option_.process_value(ctx, args[option_.name]) else: args[option_.name] = option_.default try: run_smoketest( print_step=print_step, args=args, contract_addresses=contract_addresses, token=token, ) finally: if ethereum_nodes: for node_executor in ethereum_nodes: node = node_executor.process node.send_signal(signal.SIGINT) try: node.wait(10) except TimeoutExpired: print_step("Ethereum node shutdown unclean, check log!", error=True) node.kill() if isinstance(node_executor.stdio, tuple): logfile = node_executor.stdio[1] logfile.flush() logfile.seek(0) append_report("Ethereum Node log output", logfile.read()) append_report("Raiden Node stdout", raiden_stdout.getvalue()) except: # noqa pylint: disable=bare-except if debug: import pdb pdb.post_mortem() # pylint: disable=no-member error = traceback.format_exc() append_report("Smoketest execution error", error) print_step("Smoketest execution error", error=True) success = False else: print_step(f"Smoketest successful") success = True if not success: sys.exit(1)
def smoketest(ctx, debug, eth_client): """ Test, that the raiden installation is sane. """ from raiden.tests.utils.smoketest import setup_testchain_and_raiden, run_smoketest from raiden.tests.utils.transport import make_requests_insecure, matrix_server_starter report_file = mktemp(suffix='.log') configure_logging( logger_level_config={'': 'DEBUG'}, log_file=report_file, disable_debug_logfile=ctx.parent.params['disable_debug_logfile'], ) click.secho(f'Report file: {report_file}', fg='yellow') def append_report(subject: str, data: Optional[AnyStr] = None): with open(report_file, 'a', encoding='UTF-8') as handler: handler.write(f'{f" {subject.upper()} ":=^80}{os.linesep}') if data is not None: if isinstance(data, bytes): data = data.decode() handler.writelines([data + os.linesep]) append_report('Raiden version', json.dumps(get_system_spec())) append_report('Raiden log') step_count = 7 if ctx.parent.params['transport'] == 'matrix': step_count = 8 step = 0 stdout = sys.stdout def print_step(description: str, error: bool = False) -> None: nonlocal step step += 1 click.echo( '{} {}'.format( click.style(f'[{step}/{step_count}]', fg='blue'), click.style(description, fg='green' if not error else 'red'), ), file=stdout, ) print_step('Getting smoketest configuration') contracts_version = environment_type_to_contracts_version( ctx.parent.params['environment_type'], ) with setup_testchain_and_raiden( transport=ctx.parent.params['transport'], eth_client=eth_client, matrix_server=ctx.parent.params['matrix_server'], contracts_version=contracts_version, print_step=print_step, ) as result: args = result['args'] contract_addresses = result['contract_addresses'] token = result['token'] ethereum_nodes = result['ethereum_nodes'] # Also respect environment type args['environment_type'] = ctx.parent.params['environment_type'] for option_ in run.params: if option_.name in args.keys(): args[option_.name] = option_.process_value( ctx, args[option_.name]) else: args[option_.name] = option_.default port = next(get_free_port(5001)) args['api_address'] = 'localhost:' + str(port) if args['transport'] == 'udp': with SocketFactory('127.0.0.1', port, strategy='none') as mapped_socket: args['mapped_socket'] = mapped_socket success = run_smoketest( print_step=print_step, append_report=append_report, args=args, contract_addresses=contract_addresses, token=token, debug=debug, ethereum_nodes=ethereum_nodes, ) elif args['transport'] == 'matrix': args['mapped_socket'] = None print_step('Starting Matrix transport') try: with matrix_server_starter() as server_urls: # Disable TLS verification so we can connect to the self signed certificate make_requests_insecure() urllib3.disable_warnings(InsecureRequestWarning) args['extra_config'] = { 'transport': { 'matrix': { 'available_servers': server_urls, }, }, } success = run_smoketest( print_step=print_step, append_report=append_report, args=args, contract_addresses=contract_addresses, token=token, debug=debug, ethereum_nodes=ethereum_nodes, ) except (PermissionError, ProcessExitedWithError, FileNotFoundError): append_report('Matrix server start exception', traceback.format_exc()) print_step( f'Error during smoketest setup, report was written to {report_file}', error=True, ) success = False else: # Shouldn't happen raise RuntimeError(f"Invalid transport type '{args['transport']}'") if not success: sys.exit(1)