commit 4e2ccf3c34df54139954256528813305b48596c0 Author: juga0 juga@riseup.net Date: Sun Mar 22 16:28:16 2020 +0000
fix: tests: Check the files generated in test net
Test that the results, state and bandwidth file generated by running the scanner and the generator in the test network are correct. --- tests/integration/conftest.py | 11 ++++----- tests/integration/test_files.py | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-)
diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index b1312d9..170ea51 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -36,15 +36,15 @@ def tmpdir(tmpdir_factory, request):
@pytest.fixture(scope='session') def sbwshome_empty(tmpdir): - """Create sbws home inside of the tests tmp dir without initializing.""" - home = tmpdir.join('.sbws') - os.makedirs(home.strpath, exist_ok=True) - return home.strpath + """Create sbws home inside of the test net tmp dir without initializing.""" + home = "/tmp/.sbws" + os.makedirs(home, exist_ok=True) + return home
@pytest.fixture(scope='session') def sbwshome_dir(sbwshome_empty): - """Create sbws home inside of the tests tmp dir without initializing.""" + """Create sbws home inside of the test net tmp dir without initializing.""" os.makedirs(os.path.join(sbwshome_empty, 'datadir'), exist_ok=True) return sbwshome_empty
@@ -69,6 +69,7 @@ def conf(sbwshome_dir): """Default configuration with sbws home in the tmp test dir.""" conf = _get_default_config() conf['paths']['sbws_home'] = sbwshome_dir + conf["paths"]["state_fpath"] = os.path.join(sbwshome_dir, "state.dat") conf['tor']['run_dpath'] = os.path.join(sbwshome_dir, 'tor', 'run') conf['destinations']['foo'] = 'on' conf['destinations.foo'] = {} diff --git a/tests/integration/test_files.py b/tests/integration/test_files.py new file mode 100644 index 0000000..aa7973d --- /dev/null +++ b/tests/integration/test_files.py @@ -0,0 +1,49 @@ +""" +Integration tests for the files with data to be used by the bandwidth file. + +""" +from sbws.lib.resultdump import load_recent_results_in_datadir +from sbws.lib.v3bwfile import V3BWFile +from sbws.util.state import State + + +def test_results(conf): + results = load_recent_results_in_datadir(5, conf["paths"]["datadir"]) + for fp, values in results.items(): + count = max( + [ + len(getattr(r, "relay_recent_measurement_attempt", [])) + for r in values + ] + ) + assert count == 1 + count = max( + [len(getattr(r, "relay_in_recent_consensus", [])) for r in values] + ) + assert count == 1 + count = max( + [len(getattr(r, "relay_recent_priority_list", [])) for r in values] + ) + assert count == 1 + + +def test_state(conf): + state = State(conf["paths"]["state_fpath"]) + assert 1 == state.count("recent_consensus") + assert 1 == state.count("recent_priority_list") + assert 15 == state.count("recent_priority_relay") + assert 15 == state.count("recent_measurement_attempt") + + +def test_v3bwfile(conf): + bwfile = V3BWFile.from_v1_fpath( + conf["paths"]["v3bw_fname"].format("latest") + ) + assert "1" == bwfile.header.recent_consensus_count + assert "1" == bwfile.header.recent_priority_list_count + assert "15" == bwfile.header.recent_priority_relay_count + assert "15" == bwfile.header.recent_measurement_attempt_count + for bwline in bwfile.bw_lines: + assert 1 == bwline.relay_in_recent_consensus_count + assert 1 == bwline.relay_recent_priority_list_count + assert 1 == bwline.relay_recent_measurement_attempt_count