def cleanup(self): with log_box(logging.info, f"Logs for node {self.container.name}:"): logs = self.logs().splitlines() for log_line in logs: logging.info(f"{self.container.name}: {log_line}") logging.info(f"Remove container {self.container.name}") self.container.remove(force=True, v=True)
def log_prof_data(): with log_box(logging.info, "Profiling information:"): for fname, (count, calls) in PROF_DATA.items(): max_time = max(calls) avg_time = sum(calls) / len(calls) logging.info( "Function %s called %d times. Execution time max: %.3fs, average: %.3fs", fname, count, max_time, avg_time)
def wait_for(condition, timeout, error_message): """ Waits for a condition to be fulfilled. It retries until the timeout expires. :param condition: the condition. Has to be a function 'Unit -> Boolean' :param timeout: the total time to wait :return: true if the condition was met in the given timeout """ __tracebackhide__ = True with log_box(logging.info, f"Waiting maximum timeout={timeout}. Patience please!", "."): logging.info(f"Wait condition `{condition.__doc__}`") elapsed = 0 current_ex = None while elapsed < timeout: start_time = time.time() try: value = condition() logging.info( f"Condition satisfied after {elapsed}s. Returning {value}") return value except Exception as ex: condition_evaluation_duration = time.time() - start_time elapsed = int(elapsed + condition_evaluation_duration) time_left = timeout - elapsed iteration_duration = int( max(1, int(0.15 * time_left) )) # iteration duration is 15% of remaining timeout if str(ex) == current_ex: details = "same as above" else: details = str(ex) current_ex = str(ex) logging.info( f"Condition not fulfilled yet ({details}). Time left: {time_left}s. Sleeping {iteration_duration}s..." ) time.sleep(iteration_duration) elapsed = elapsed + iteration_duration logging.warning(f"Giving up after {elapsed}s.") pytest.fail(error_message)
def test_casper_propose_and_deploy(config, converged_network): """ This test represents an integration test that deploys a contract and then checks if all the nodes have received the block containing the contract. """ token_size = 20 contract_name = 'contract.rho' for node in converged_network.nodes: with log_box(logging.info, f"Run test on node '{node.name}'"): expected_string = f"<{node.container.name}:{random_string(token_size)}>" logging.info(f"Expected string: {expected_string}") copyfile(resources.file_path(contract_name, __name__), f"{node.local_deploy_dir}/{contract_name}") exit_code, output = node.exec_run( f"sed -i -e 's/@placeholder@/{expected_string}/g' {node.remote_deploy_dir}/{contract_name}" ) logging.debug(f"Sed result: {exit_code}, output: {output}") exit_code, output = node.deploy(contract_name) logging.debug(f"Deploy result: {exit_code}, output: {output}") logging.info( "Propose to blockchain previously deployed smart contracts.") exit_code, output = node.propose() logging.debug(f"Propose result: {exit_code}, output: {output}") logging.info( f"Check all peer logs for blocks containing {expected_string}") other_nodes = [ n for n in converged_network.nodes if n.container.name != node.container.name ] for node in other_nodes: wait_for( string_contains(show_blocks(node), expected_string), config.receive_timeout, f"Container: {node.container.name}: String {expected_string} NOT found in blocks added." ) logging.info(f"Container: {node.container.name}: SUCCESS!")
def parse_config(request): peer_count = int(request.config.getoption("--peer-count")) start_timeout = int(request.config.getoption("--start-timeout")) converge_timeout = int(request.config.getoption("--converge-timeout")) receive_timeout = int(request.config.getoption("--receive-timeout")) rnode_timeout = int(request.config.getoption("--rnode-timeout")) def make_timeout(value, base, peer_factor=10): return value if value > 0 else base + peer_count * peer_factor config = Config(peer_count=peer_count, node_startup_timeout=make_timeout(start_timeout, 30, 10), network_converge_timeout=make_timeout( converge_timeout, 200, 10), receive_timeout=make_timeout(receive_timeout, 10, 10), rnode_timeout=rnode_timeout) with log_box(logging.info): s = pprint.pformat(dict(config._asdict()), indent=4) logging.info(f"Running with test configuration: {s}") return config