Commit 3c6f4fa4 authored by Matt Traudt's avatar Matt Traudt
Browse files

Test generate command with multiple relays

parent caae5f74
......@@ -141,3 +141,118 @@ def test_generate_single_success_scale(dotsbws_success_result, parser, log,
bw_line = 'node_id={} bw={} nick={} rtt={} time={}'.format(
result.fingerprint, bw, result.nickname, rtt, round(result.time))
assert stdout_lines[2] == bw_line
def test_generate_single_relay_success_noscale(
dotsbws_success_result_one_relay, parser, log, capfd):
dotsbws = dotsbws_success_result_one_relay
args = parser.parse_args(
'-d {} -vvvv generate'.format(dotsbws.name).split())
conf = get_config(args, log.debug)
sbws.commands.generate.main(args, conf, log)
dd = conf['paths']['datadir']
results = load_recent_results_in_datadir(
1, dd, success_only=False, log_fn=log.debug)
assert len(results) == 2, 'There should be two results in the datadir'
for result in results:
assert isinstance(result, ResultSuccess), 'All existing results '\
'should be a success'
captured = capfd.readouterr()
stdout_lines = captured.out.strip().split('\n')
assert len(stdout_lines) == 3
# XXX: after mocking time, make sure first line is the current timestamp
# assert stdout_lines[0] is current timestamp
v = 'version={}'.format(version)
assert stdout_lines[1] == v
speeds = [dl['amount'] / dl['duration'] for r in results
for dl in r.downloads]
speed = round(median(speeds))
rtt = round(median([round(r * 1000) for r in result.rtts]))
bw_line = 'node_id={} bw={} nick={} rtt={} time={}'.format(
result.fingerprint, speed, result.nickname, rtt, round(result.time))
assert stdout_lines[2] == bw_line
def test_generate_single_relay_success_scale(
dotsbws_success_result_one_relay, parser, log, capfd):
dotsbws = dotsbws_success_result_one_relay
args = parser.parse_args(
'-d {} -vvvv generate --scale'.format(dotsbws.name).split())
conf = get_config(args, log.debug)
sbws.commands.generate.main(args, conf, log)
dd = conf['paths']['datadir']
results = load_recent_results_in_datadir(
1, dd, success_only=False, log_fn=log.debug)
assert len(results) == 2, 'There should be two results in the datadir'
for result in results:
assert isinstance(result, ResultSuccess), 'All existing results '\
'should be a success'
captured = capfd.readouterr()
stdout_lines = captured.out.strip().split('\n')
assert len(stdout_lines) == 3
# XXX: after mocking time, make sure first line is the current timestamp
# assert stdout_lines[0] is current timestamp
v = 'version={}'.format(version)
assert stdout_lines[1] == v
speed = 7500
rtt = round(median([round(r * 1000) for r in result.rtts]))
bw_line = 'node_id={} bw={} nick={} rtt={} time={}'.format(
result.fingerprint, speed, result.nickname, rtt, round(result.time))
assert stdout_lines[2] == bw_line
def test_generate_two_relays_success_noscale(
dotsbws_success_result_two_relays, parser, log, capfd):
dotsbws = dotsbws_success_result_two_relays
args = parser.parse_args(
'-d {} -vvvv generate'.format(dotsbws.name).split())
conf = get_config(args, log.debug)
sbws.commands.generate.main(args, conf, log)
dd = conf['paths']['datadir']
results = load_recent_results_in_datadir(
1, dd, success_only=False, log_fn=log.debug)
assert len(results) == 4, 'There should be 4 results in the datadir'
for result in results:
assert isinstance(result, ResultSuccess), 'All existing results '\
'should be a success'
captured = capfd.readouterr()
stdout_lines = captured.out.strip().split('\n')
assert len(stdout_lines) == 4
# XXX: after mocking time, make sure first line is the current timestamp
# assert stdout_lines[0] is current timestamp
v = 'version={}'.format(version)
assert stdout_lines[1] == v
r1_results = [r for r in results if r.fingerprint == 'A' * 40]
r1_time = round(max([r.time for r in r1_results]))
r1_name = r1_results[0].nickname
r1_fingerprint = r1_results[0].fingerprint
r1_speeds = [dl['amount'] / dl['duration'] for r in r1_results
for dl in r.downloads]
r1_speed = round(median(r1_speeds))
r1_rtt = round(median([round(rtt * 1000) for r in r1_results
for rtt in r.rtts]))
bw_line = 'node_id={} bw={} nick={} rtt={} time={}'.format(
r1_fingerprint, r1_speed, r1_name, r1_rtt, r1_time)
assert stdout_lines[3] == bw_line
r2_results = [r for r in results if r.fingerprint == 'B' * 40]
r2_time = round(max([r.time for r in r2_results]))
r2_name = r2_results[0].nickname
r2_fingerprint = r2_results[0].fingerprint
r2_speeds = [dl['amount'] / dl['duration'] for r in r2_results
for dl in r.downloads]
r2_speed = round(median(r2_speeds))
r2_rtt = round(median([round(rtt * 1000) for r in r2_results
for rtt in r.rtts]))
bw_line = 'node_id={} bw={} nick={} rtt={} time={}'.format(
r2_fingerprint, r2_speed, r2_name, r2_rtt, r2_time)
assert stdout_lines[2] == bw_line
......@@ -149,3 +149,88 @@ def dotsbws_success_result(empty_dotsbws_datadir):
dd = conf['paths']['datadir']
write_result_to_datadir(result, dd)
return empty_dotsbws_datadir
@pytest.fixture(scope='function')
def dotsbws_success_result_one_relay(empty_dotsbws_datadir):
'''
Creates an ~/.sbws with a a couple of fresh ResultSuccess for one relay
'''
args = _PseudoArguments(directory=empty_dotsbws_datadir.name)
conf = get_config(args)
dd = conf['paths']['datadir']
fp1 = 'A' * 40
fp2 = 'B' * 40
circ = [fp1, fp2]
nick = 'CowSayWhat'
relay_ip = '169.254.100.1'
server_ip = '169.254.100.2'
client_nick = 'SBWSclient'
rtts = [5, 25]
downloads = [{'duration': 4, 'amount': 40}]
t = time.time()
relay = Result.Relay(fp1, nick, relay_ip)
result = ResultSuccess(rtts, downloads, relay, circ, server_ip,
client_nick, t=t)
write_result_to_datadir(result, dd)
rtts = [10, 20]
downloads = [{'duration': 4, 'amount': 80}]
t = time.time()
result = ResultSuccess(rtts, downloads, relay, circ, server_ip,
client_nick, t=t)
write_result_to_datadir(result, dd)
return empty_dotsbws_datadir
@pytest.fixture(scope='function')
def dotsbws_success_result_two_relays(empty_dotsbws_datadir):
'''
Creates an ~/.sbws with a a couple of fresh ResultSuccess for a couple or
relays
'''
args = _PseudoArguments(directory=empty_dotsbws_datadir.name)
conf = get_config(args)
dd = conf['paths']['datadir']
fp1 = 'A' * 40
fp2 = 'C' * 40
circ = [fp1, fp2]
nick = 'CowSayWhat1'
relay_ip = '169.254.100.1'
server_ip = '169.254.100.3'
client_nick = 'SBWSclient'
rtts = [5, 25]
downloads = [{'duration': 4, 'amount': 40}]
t = time.time()
relay = Result.Relay(fp1, nick, relay_ip)
result = ResultSuccess(rtts, downloads, relay, circ, server_ip,
client_nick, t=t)
write_result_to_datadir(result, dd)
rtts = [10, 20]
downloads = [{'duration': 4, 'amount': 80}]
t = time.time()
result = ResultSuccess(rtts, downloads, relay, circ, server_ip,
client_nick, t=t)
write_result_to_datadir(result, dd)
fp1 = 'B' * 40
circ = [fp1, fp2]
nick = 'CowSayWhat2'
relay_ip = '169.254.100.2'
rtts = [50, 250]
downloads = [{'duration': 4, 'amount': 400}]
t = time.time()
relay = Result.Relay(fp1, nick, relay_ip)
result = ResultSuccess(rtts, downloads, relay, circ, server_ip,
client_nick, t=t)
write_result_to_datadir(result, dd)
rtts = [100, 200]
downloads = [{'duration': 4, 'amount': 800}]
t = time.time()
result = ResultSuccess(rtts, downloads, relay, circ, server_ip,
client_nick, t=t)
write_result_to_datadir(result, dd)
return empty_dotsbws_datadir
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment