Exemple #1
0
def raiden_testchain(testchain_provider, cli_tests_contracts_version):
    import time
    start_time = time.monotonic()

    result = setup_raiden(
        transport='matrix',
        matrix_server='auto',
        print_step=lambda x: None,
        contracts_version=cli_tests_contracts_version,
        testchain_setup=testchain_provider,
    )
    args = result['args']
    # The setup of the testchain returns a TextIOWrapper but
    # for the tests we need a filename
    args['password_file'] = args['password_file'].name
    print('setup_raiden took', time.monotonic() - start_time)
    return args
Exemple #2
0
def raiden_testchain(
    blockchain_type: str, port_generator, cli_tests_contracts_version: str
) -> Iterable[Dict[str, Any]]:
    import time

    start_time = time.monotonic()
    eth_client = EthClient(blockchain_type)

    # The private chain data is always discarded on the CI
    tmpdir = mkdtemp()
    base_datadir = str(tmpdir)

    # Save the Ethereum node's logs, if needed for debugging
    base_logdir = os.path.join(get_artifacts_storage() or str(tmpdir), blockchain_type)
    os.makedirs(base_logdir, exist_ok=True)

    testchain_manager: ContextManager[Dict[str, Any]] = setup_testchain(
        eth_client=eth_client,
        free_port_generator=port_generator,
        base_datadir=base_datadir,
        base_logdir=base_logdir,
    )

    def dont_print_step(
        description: str, error: bool = False  # pylint: disable=unused-argument
    ) -> None:
        pass

    with testchain_manager as testchain:
        result = setup_raiden(
            matrix_server=MATRIX_AUTO_SELECT_SERVER,
            print_step=dont_print_step,
            contracts_version=cli_tests_contracts_version,
            eth_rpc_endpoint=testchain["eth_rpc_endpoint"],
            web3=testchain["web3"],
            base_datadir=testchain["base_datadir"],
            keystore=testchain["keystore"],
        )

        args = result.args
        # The setup of the testchain returns a TextIOWrapper but
        # for the tests we need a filename
        args["password_file"] = args["password_file"].name
        print("setup_raiden took", time.monotonic() - start_time)
        yield args
Exemple #3
0
def raiden_testchain(blockchain_type, port_generator,
                     cli_tests_contracts_version):
    import time

    start_time = time.monotonic()
    eth_client = EthClient(blockchain_type)

    # The private chain data is always discarded on the CI
    tmpdir = mkdtemp()
    base_datadir = str(tmpdir)

    # Save the Ethereum node's logs, if needed for debugging
    base_logdir = os.path.join(get_artifacts_storage() or str(tmpdir),
                               blockchain_type)
    os.makedirs(base_logdir, exist_ok=True)

    testchain_manager: ContextManager[Dict[str, Any]] = setup_testchain(
        eth_client=eth_client,
        free_port_generator=port_generator,
        base_datadir=base_datadir,
        base_logdir=base_logdir,
    )

    with testchain_manager as testchain:
        result = setup_raiden(
            transport="matrix",
            matrix_server="auto",
            print_step=lambda x: None,
            contracts_version=cli_tests_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"],
        )
        result["ethereum_nodes"] = testchain["node_executors"]

        args = result["args"]
        # The setup of the testchain returns a TextIOWrapper but
        # for the tests we need a filename
        args["password_file"] = args["password_file"].name
        print("setup_raiden took", time.monotonic() - start_time)
        yield args
Exemple #4
0
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)