Refactor state tests generators

This commit is contained in:
Hsiao-Wei Wang 2021-02-15 22:25:58 +08:00
parent 600a4daddf
commit c7d975981c
No known key found for this signature in database
GPG Key ID: 1111A8A81778319E
6 changed files with 69 additions and 181 deletions

View File

@ -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,
))

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)