From c7d975981c04626b72cdc3566914af0aa552b7bb Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 15 Feb 2021 22:25:58 +0800 Subject: [PATCH] Refactor state tests generators --- tests/core/gen_helpers/gen_from_tests/gen.py | 53 ++++++++++++++++++-- tests/generators/epoch_processing/main.py | 39 ++------------ tests/generators/finality/main.py | 41 ++------------- tests/generators/operations/main.py | 39 ++------------ tests/generators/rewards/main.py | 39 ++------------ tests/generators/sanity/main.py | 39 ++------------ 6 files changed, 69 insertions(+), 181 deletions(-) diff --git a/tests/core/gen_helpers/gen_from_tests/gen.py b/tests/core/gen_helpers/gen_from_tests/gen.py index c09b477bb..3ec8904fc 100644 --- a/tests/core/gen_helpers/gen_from_tests/gen.py +++ b/tests/core/gen_helpers/gen_from_tests/gen.py @@ -1,11 +1,17 @@ +from importlib import reload, import_module from inspect import getmembers, isfunction -from typing import Any, Iterable, Dict +from typing import Any, Callable, Dict, Iterable -from gen_base.gen_typing import TestCase +from eth2spec.config import config_util +from eth2spec.utils import bls + +from eth2spec.test.context import ALL_CONFIGS, TESTGEN_FORKS, SpecForkName, ConfigName +from gen_base import gen_runner +from gen_base.gen_typing import TestCase, TestProvider def generate_from_tests(runner_name: str, handler_name: str, src: Any, - fork_name: str, bls_active: bool = True) -> Iterable[TestCase]: + fork_name: SpecForkName, bls_active: bool = True) -> Iterable[TestCase]: """ Generate a list of test cases by running tests from the given src in generator-mode. :param runner_name: to categorize the test in general as. @@ -40,7 +46,10 @@ def generate_from_tests(runner_name: str, handler_name: str, src: Any, ) -def get_provider(create_provider_fn, config_name, fork_name, all_mods): +def get_provider(create_provider_fn: Callable[[SpecForkName, str, str, ConfigName], TestProvider], + config_name: ConfigName, + fork_name: SpecForkName, + all_mods: Dict[str, Dict[str, str]]) -> Iterable[TestProvider]: for key, mod_name in all_mods[fork_name].items(): yield create_provider_fn( fork_name=fork_name, @@ -48,3 +57,39 @@ def get_provider(create_provider_fn, config_name, fork_name, all_mods): tests_src_mod_name=mod_name, config_name=config_name, ) + + +def get_create_provider_fn(runner_name: str, config_name: ConfigName, specs: Iterable[Any] + ) -> Callable[[SpecForkName, str, str, ConfigName], TestProvider]: + def prepare_fn(configs_path: str) -> str: + config_util.prepare_config(configs_path, config_name) + for spec in specs: + reload(spec) + bls.use_milagro() + return config_name + + def create_provider(fork_name: SpecForkName, handler_name: str, + tests_src_mod_name: str, config_name: ConfigName) -> TestProvider: + def cases_fn() -> Iterable[TestCase]: + tests_src = import_module(tests_src_mod_name) + return generate_from_tests( + runner_name=runner_name, + handler_name=handler_name, + src=tests_src, + fork_name=fork_name, + ) + + return TestProvider(prepare=prepare_fn, make_cases=cases_fn) + return create_provider + + +def run_state_test_generators(runner_name: str, specs: Iterable[Any], all_mods: Dict[str, Dict[str, str]]) -> None: + for config_name in ALL_CONFIGS: + for fork_name in TESTGEN_FORKS: + if fork_name in all_mods: + gen_runner.run_generator(runner_name, get_provider( + create_provider_fn=get_create_provider_fn(runner_name, config_name, specs), + config_name=config_name, + fork_name=fork_name, + all_mods=all_mods, + )) diff --git a/tests/generators/epoch_processing/main.py b/tests/generators/epoch_processing/main.py index 835fe0087..1306da523 100644 --- a/tests/generators/epoch_processing/main.py +++ b/tests/generators/epoch_processing/main.py @@ -1,36 +1,11 @@ -from typing import Iterable - -from gen_base import gen_runner, gen_typing -from gen_from_tests.gen import generate_from_tests, get_provider -from importlib import reload, import_module -from eth2spec.config import config_util +from gen_from_tests.gen import run_state_test_generators from eth2spec.phase0 import spec as spec_phase0 from eth2spec.lightclient_patch import spec as spec_lightclient_patch from eth2spec.phase1 import spec as spec_phase1 -from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH, TESTGEN_FORKS, ALL_CONFIGS -from eth2spec.utils import bls +from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH -def create_provider(fork_name: str, handler_name: str, - tests_src_mod_name: str, config_name: str) -> gen_typing.TestProvider: - def prepare_fn(configs_path: str) -> str: - config_util.prepare_config(configs_path, config_name) - reload(spec_phase0) - reload(spec_lightclient_patch) - reload(spec_phase1) - bls.use_milagro() - return config_name - - def cases_fn() -> Iterable[gen_typing.TestCase]: - tests_src = import_module(tests_src_mod_name) - return generate_from_tests( - runner_name='epoch_processing', - handler_name=handler_name, - src=tests_src, - fork_name=fork_name, - ) - - return gen_typing.TestProvider(prepare=prepare_fn, make_cases=cases_fn) +specs = (spec_phase0, spec_lightclient_patch, spec_phase1) if __name__ == "__main__": @@ -64,10 +39,4 @@ if __name__ == "__main__": PHASE1: phase_1_mods, } - for config_name in ALL_CONFIGS: - for fork_name in TESTGEN_FORKS: - if fork_name in all_mods: - gen_runner.run_generator(f"epoch_processing", get_provider( - create_provider_fn=create_provider, config_name=config_name, - fork_name=fork_name, all_mods=all_mods, - )) + run_state_test_generators(runner_name="epoch_processing", specs=specs, all_mods=all_mods) diff --git a/tests/generators/finality/main.py b/tests/generators/finality/main.py index 6ea3d82c8..0a62b1aa5 100644 --- a/tests/generators/finality/main.py +++ b/tests/generators/finality/main.py @@ -1,38 +1,11 @@ -from typing import Iterable -from importlib import reload, import_module - -from gen_base import gen_runner, gen_typing -from gen_from_tests.gen import generate_from_tests, get_provider - -from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH, TESTGEN_FORKS, ALL_CONFIGS -from eth2spec.config import config_util +from gen_from_tests.gen import run_state_test_generators from eth2spec.phase0 import spec as spec_phase0 from eth2spec.lightclient_patch import spec as spec_lightclient_patch from eth2spec.phase1 import spec as spec_phase1 -from eth2spec.utils import bls +from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH -def create_provider(fork_name: str, handler_name: str, - tests_src_mod_name: str, config_name: str) -> gen_typing.TestProvider: - - def prepare_fn(configs_path: str) -> str: - config_util.prepare_config(configs_path, config_name) - reload(spec_phase0) - reload(spec_lightclient_patch) - reload(spec_phase1) - bls.use_milagro() - return config_name - - def cases_fn() -> Iterable[gen_typing.TestCase]: - tests_src = import_module(tests_src_mod_name) - return generate_from_tests( - runner_name='finality', - handler_name=handler_name, - src=tests_src, - fork_name=fork_name, - ) - - return gen_typing.TestProvider(prepare=prepare_fn, make_cases=cases_fn) +specs = (spec_phase0, spec_lightclient_patch, spec_phase1) if __name__ == "__main__": @@ -47,10 +20,4 @@ if __name__ == "__main__": PHASE1: phase_1_mods, } - for config_name in ALL_CONFIGS: - for fork_name in TESTGEN_FORKS: - if fork_name in all_mods: - gen_runner.run_generator(f"finality", get_provider( - create_provider_fn=create_provider, config_name=config_name, - fork_name=fork_name, all_mods=all_mods, - )) + run_state_test_generators(runner_name="finality", specs=specs, all_mods=all_mods) diff --git a/tests/generators/operations/main.py b/tests/generators/operations/main.py index 20643f8d7..d40dbe9cd 100644 --- a/tests/generators/operations/main.py +++ b/tests/generators/operations/main.py @@ -1,36 +1,11 @@ -from typing import Iterable - -from gen_base import gen_runner, gen_typing -from gen_from_tests.gen import generate_from_tests, get_provider -from importlib import reload, import_module -from eth2spec.config import config_util +from gen_from_tests.gen import run_state_test_generators from eth2spec.phase0 import spec as spec_phase0 from eth2spec.lightclient_patch import spec as spec_lightclient_patch from eth2spec.phase1 import spec as spec_phase1 -from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH, TESTGEN_FORKS, ALL_CONFIGS -from eth2spec.utils import bls +from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH -def create_provider(fork_name: str, handler_name: str, - tests_src_mod_name: str, config_name: str) -> gen_typing.TestProvider: - def prepare_fn(configs_path: str) -> str: - config_util.prepare_config(configs_path, config_name) - reload(spec_phase0) - reload(spec_lightclient_patch) - reload(spec_phase1) - bls.use_milagro() - return config_name - - def cases_fn() -> Iterable[gen_typing.TestCase]: - tests_src = import_module(tests_src_mod_name) - return generate_from_tests( - runner_name='operations', - handler_name=handler_name, - src=tests_src, - fork_name=fork_name, - ) - - return gen_typing.TestProvider(prepare=prepare_fn, make_cases=cases_fn) +specs = (spec_phase0, spec_lightclient_patch, spec_phase1) if __name__ == "__main__": @@ -63,10 +38,4 @@ if __name__ == "__main__": PHASE1: phase_1_mods, } - for config_name in ALL_CONFIGS: - for fork_name in TESTGEN_FORKS: - if fork_name in all_mods: - gen_runner.run_generator(f"operations", get_provider( - create_provider_fn=create_provider, config_name=config_name, - fork_name=fork_name, all_mods=all_mods, - )) + run_state_test_generators(runner_name="operations", specs=specs, all_mods=all_mods) diff --git a/tests/generators/rewards/main.py b/tests/generators/rewards/main.py index cd3206a3f..addb6aef0 100644 --- a/tests/generators/rewards/main.py +++ b/tests/generators/rewards/main.py @@ -1,36 +1,11 @@ -from typing import Iterable - -from gen_base import gen_runner, gen_typing -from gen_from_tests.gen import generate_from_tests, get_provider -from importlib import reload, import_module -from eth2spec.config import config_util +from gen_from_tests.gen import run_state_test_generators from eth2spec.phase0 import spec as spec_phase0 from eth2spec.lightclient_patch import spec as spec_lightclient_patch from eth2spec.phase1 import spec as spec_phase1 -from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH, TESTGEN_FORKS, ALL_CONFIGS -from eth2spec.utils import bls +from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH -def create_provider(fork_name: str, handler_name: str, - tests_src_mod_name: str, config_name: str) -> gen_typing.TestProvider: - def prepare_fn(configs_path: str) -> str: - config_util.prepare_config(configs_path, config_name) - reload(spec_phase0) - reload(spec_lightclient_patch) - reload(spec_phase1) - bls.use_milagro() - return config_name - - def cases_fn() -> Iterable[gen_typing.TestCase]: - tests_src = import_module(tests_src_mod_name) - return generate_from_tests( - runner_name='rewards', - handler_name=handler_name, - src=tests_src, - fork_name=fork_name, - ) - - return gen_typing.TestProvider(prepare=prepare_fn, make_cases=cases_fn) +specs = (spec_phase0, spec_lightclient_patch, spec_phase1) if __name__ == "__main__": @@ -49,10 +24,4 @@ if __name__ == "__main__": PHASE1: phase_1_mods, } - for config_name in ALL_CONFIGS: - for fork_name in TESTGEN_FORKS: - if fork_name in all_mods: - gen_runner.run_generator(f"rewards", get_provider( - create_provider_fn=create_provider, config_name=config_name, - fork_name=fork_name, all_mods=all_mods, - )) + run_state_test_generators(runner_name="rewards", specs=specs, all_mods=all_mods) diff --git a/tests/generators/sanity/main.py b/tests/generators/sanity/main.py index 0aabf5d4c..fc5227a53 100644 --- a/tests/generators/sanity/main.py +++ b/tests/generators/sanity/main.py @@ -1,36 +1,11 @@ -from typing import Iterable - -from gen_base import gen_runner, gen_typing -from gen_from_tests.gen import generate_from_tests, get_provider -from importlib import reload, import_module -from eth2spec.config import config_util +from gen_from_tests.gen import run_state_test_generators from eth2spec.phase0 import spec as spec_phase0 from eth2spec.lightclient_patch import spec as spec_lightclient_patch from eth2spec.phase1 import spec as spec_phase1 -from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH, TESTGEN_FORKS, ALL_CONFIGS -from eth2spec.utils import bls +from eth2spec.test.context import PHASE0, PHASE1, LIGHTCLIENT_PATCH -def create_provider(fork_name: str, handler_name: str, - tests_src_mod_name: str, config_name: str) -> gen_typing.TestProvider: - def prepare_fn(configs_path: str) -> str: - config_util.prepare_config(configs_path, config_name) - reload(spec_phase0) - reload(spec_lightclient_patch) - reload(spec_phase1) - bls.use_milagro() - return config_name - - def cases_fn() -> Iterable[gen_typing.TestCase]: - tests_src = import_module(tests_src_mod_name) - return generate_from_tests( - runner_name='sanity', - handler_name=handler_name, - src=tests_src, - fork_name=fork_name, - ) - - return gen_typing.TestProvider(prepare=prepare_fn, make_cases=cases_fn) +specs = (spec_phase0, spec_lightclient_patch, spec_phase1) if __name__ == "__main__": @@ -52,10 +27,4 @@ if __name__ == "__main__": PHASE1: phase_1_mods, } - for config_name in ALL_CONFIGS: - for fork_name in TESTGEN_FORKS: - if fork_name in all_mods: - gen_runner.run_generator(f"sanity", get_provider( - create_provider_fn=create_provider, config_name=config_name, - fork_name=fork_name, all_mods=all_mods, - )) + run_state_test_generators(runner_name="sanity", specs=specs, all_mods=all_mods)