Exemplo n.º 1
0
def init_cluster(num_nodes, num_observers, num_shards, config,
                 genesis_config_changes, client_config_changes):
    """
    Create cluster configuration
    """
    if 'local' not in config and 'nodes' in config:
        logger.critical(
            "Attempt to launch a regular test with a mocknet config")
        sys.exit(1)

    is_local = config['local']
    near_root = config['near_root']
    binary_name = config.get('binary_name', 'neard')

    logger.info("Creating %s cluster configuration with %s nodes" %
                ("LOCAL" if is_local else "REMOTE", num_nodes + num_observers))

    process = subprocess.Popen([
        os.path.join(near_root, binary_name), "localnet", "--v",
        str(num_nodes), "--shards",
        str(num_shards), "--n",
        str(num_observers), "--prefix", "test"
    ],
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    out, err = process.communicate()
    assert 0 == process.returncode, err

    node_dirs = [
        line.split()[-1] for line in err.decode('utf8').split('\n')
        if '/test' in line
    ]
    assert len(
        node_dirs
    ) == num_nodes + num_observers, "node dirs: %s num_nodes: %s num_observers: %s" % (
        len(node_dirs), num_nodes, num_observers)

    logger.info("Search for stdout and stderr in %s" % node_dirs)
    # apply config changes
    for i, node_dir in enumerate(node_dirs):
        apply_genesis_changes(node_dir, genesis_config_changes)
        overrides = client_config_changes.get(i)
        if overrides:
            apply_config_changes(node_dir, overrides)

    return near_root, node_dirs
Exemplo n.º 2
0
    def cleanup(self):
        if self.cleaned:
            return

        try:
            self.kill()
        except:
            logger.critical('Kill failed on cleanup!', exc_info=sys.exc_info())

        # move the node dir to avoid weird interactions with multiple serial test invocations
        target_path = self.node_dir + '_finished'
        if os.path.exists(target_path) and os.path.isdir(target_path):
            shutil.rmtree(target_path)
        os.rename(self.node_dir, target_path)
        self.node_dir = target_path
        self.output_logs()
        self.cleaned = True
Exemplo n.º 3
0
def prepare_ab_test(other_branch):
    # Use NEAR_AB_BINARY_EXISTS to avoid rebuild / re-download when testing locally.
    #if not os.environ.get('NEAR_AB_BINARY_EXISTS'):
    #    compile_current()
    #    uname = os.uname()[0]
    #    if other_branch in ['master', 'beta', 'stable'] and uname in ['Linux', 'Darwin']:
    #        download_binary(uname, other_branch)
    #    else:
    # TODO: re-enable caching
    uname = os.uname()[0]
    if not os.getenv('NAYDUCK'):
        compile_current()
    try:
        download_binary(uname, other_branch)
    except Exception:
        if not os.getenv('NAYDUCK'):
            compile_binary(str(other_branch))
        else:
            logger.critical('RC binary should be downloaded for NayDuck.')
            sys.exit(1)
    return '../target/debug/', [other_branch, escaped(current_branch())]
Exemplo n.º 4
0
def chain_query(node, block_handler, *, block_hash=None, max_blocks=-1):
    """
    Query chain block approvals and chunks preceding of block of block_hash.
    If block_hash is None, it query latest block hash
    It query at most max_blocks, or if it's -1, all blocks back to genesis
    """
    if block_hash is None:
        status = node.get_status()
        block_hash = status['sync_info']['latest_block_hash']

    initial_validators = node.validators()

    if max_blocks == -1:
        while True:
            validators = node.validators()
            if validators != initial_validators:
                logger.critical(
                    f'Fatal: validator set of node {node} changes, from {initial_validators} to {validators}'
                )
                sys.exit(1)
            block = node.get_block(block_hash)['result']
            block_handler(block)
            block_hash = block['header']['prev_hash']
            block_height = block['header']['height']
            if block_height == 0:
                break
    else:
        for _ in range(max_blocks):
            validators = node.validators()
            if validators != initial_validators:
                logger.critical(
                    f'Fatal: validator set of node {node} changes, from {initial_validators} to {validators}'
                )
                sys.exit(1)
            block = node.get_block(block_hash)['result']
            block_handler(block)
            block_hash = block['header']['prev_hash']
            block_height = block['header']['height']
            if block_height == 0:
                break
Exemplo n.º 5
0
def init_network_pillager():
    _run_process(["mkdir", "-p", "/sys/fs/cgroup/net_cls/block"])
    try:
        with open("/sys/fs/cgroup/net_cls/block/net_cls.classid", 'w') as f:
            f.write("42")
    except IOError as e:
        if e[0] == 13:
            logger.critical(
                "Failed to modify `/sys/fs/cgroup/net_cls/block/net_cls.classid`."
            )
            logger.critical(
                "Make sure the current user has access to it, e.g. by changing the owner:\n"
            )
            logger.critical(
                "    chown <group>.<user> /sys/fs/cgroup/net_cls/block/net_cls.classid"
            )
            sys.exit(1)
    _run_process([
        "iptables", "-A", "OUTPUT", "-m", "cgroup", "--cgroup", "42", "-j",
        "DROP"
    ])