Ejemplo n.º 1
0
def network_fault(chain_file, num_steps, delay_step, interval):
    chain = Chain.deserialize(json.loads(chain_file.read()))

    result = Chainmanager.get_network_fault(chain, num_steps, delay_step,
                                            interval)

    print(json.dumps(result))
def test_network_fault(requests_mock, chain, mock_ethermint_requests,
                       mocksubprocess):
    # FIXME: test-all smoke test for now, consider splitting into separate logical assertions
    ip = chain.instances[0].public_ip_address
    t = datetime.now(tz=pytz.UTC)

    mock_ethermint_requests(1, t, "hash", [ip])
    mock_ethermint_requests(
        10, t + timedelta(seconds=10), "hash2",
        [inst.public_ip_address for inst in chain.instances])
    requests_mock.add(
        requests_mock.GET,
        re.compile(r'http://' + ip +
                   r':46657/blockchain\?minHeight=1\&maxHeight=10'),
        json={
            "result": [
                0, {
                    'block_metas':
                    10 * [{
                        'header': {
                            'app_hash': "",
                            'height': "",
                            'time': t.isoformat()
                        }
                    }]
                }
            ]
        },
        status=200)

    # fake time when a remote machine should report having run it's tc command
    delay_step_time1 = (t + timedelta(seconds=5)).isoformat()
    delay_step_time2 = (t + timedelta(seconds=7)).isoformat()
    delay_step_time3 = (t + timedelta(seconds=7)).isoformat()

    def _side_effect(*args, **kwargs):
        if "get_datetime.sh" in args[0]:
            assert ip in args[0]  # only instance 0 is queried for date
            return t.isoformat()
        elif "run_tcs.sh" in args[0]:
            assert "run_tcs.sh 2 123 1 eth0 {}".format((t + timedelta(
                seconds=(NETWORK_FAULT_PREPARATION_TIME_PER_INSTANCE *
                         len(chain.instances)))).isoformat()) in args[0]
            assert any([
                inst.public_ip_address in args[0] for inst in chain.instances
            ])
            return "{}\n{}\n{}".format(delay_step_time1, delay_step_time2,
                                       delay_step_time3)

        assert False

    mocksubprocess.side_effect = _side_effect

    result = Chainmanager.get_network_fault(chain, 2, 123, 1)

    check_history(result["blocktimes"], 9, 0, "", t)
    assert len(result["delay_steps"]) == 3
    assert result["delay_steps"] == [(123, delay_step_time1),
                                     (246, delay_step_time2),
                                     (0, delay_step_time3)]