2019-04-28 05:42:04 +00:00
# Eth 2.0 Test Generators
2019-03-27 16:32:13 +00:00
2019-04-15 12:29:25 +00:00
This directory contains all the generators for YAML tests, consumed by Eth 2.0 client implementations.
2019-03-27 16:32:13 +00:00
2019-04-28 05:42:04 +00:00
Any issues with the generators and/or generated tests should be filed in the repository that hosts the generator outputs, here: [ethereum/eth2.0-tests ](https://github.com/ethereum/eth2.0-tests ).
2019-03-27 16:32:13 +00:00
2019-04-28 05:42:04 +00:00
Whenever a release is made, the new tests are automatically built, and
2019-03-27 16:32:13 +00:00
[eth2TestGenBot ](https://github.com/eth2TestGenBot ) commits the changes to the test repository.
## How to run generators
2019-04-28 05:42:04 +00:00
Prerequisites:
2019-03-27 16:32:13 +00:00
- Python 3 installed
- PIP 3
- GNU make
### Cleaning
2019-04-28 05:42:04 +00:00
This removes the existing virtual environments (`/test_generators/< generator > /venv`) and generated tests (`/yaml_tests/`).
2019-03-27 16:32:13 +00:00
```bash
make clean
```
### Running all test generators
2019-04-28 05:42:04 +00:00
This runs all of the generators.
2019-03-27 16:32:13 +00:00
```bash
2019-04-20 02:25:24 +00:00
make -j 4 gen_yaml_tests
2019-03-27 16:32:13 +00:00
```
2019-04-20 02:25:24 +00:00
The `-j N` flag makes the generators run in parallel, with `N` being the amount of cores.
2019-03-27 16:32:13 +00:00
### Running a single generator
2019-04-28 05:48:57 +00:00
The makefile auto-detects generators in the `test_generators` directory and provides a tests-gen target for each generator. See example:
2019-03-27 16:32:13 +00:00
```bash
2019-03-28 16:24:18 +00:00
make ./yaml_tests/shuffling/
2019-03-27 16:32:13 +00:00
```
## Developing a generator
2019-04-28 05:42:04 +00:00
Simply open up the generator (not all at once) of choice in your favorite IDE/editor and run:
2019-03-27 16:32:13 +00:00
```bash
2019-03-28 16:24:18 +00:00
# From the root of the generator directory:
2019-03-27 16:32:13 +00:00
# Create a virtual environment (any venv/.venv/.venvs is git-ignored)
2019-03-28 15:10:16 +00:00
python3 -m venv venv
2019-03-27 16:32:13 +00:00
# Activate the venv, this is where dependencies are installed for the generator
2019-03-28 15:10:16 +00:00
. venv/bin/activate
2019-03-27 16:32:13 +00:00
```
Now that you have a virtual environment, write your generator.
It's recommended to extend the base-generator.
Create a `requirements.txt` in the root of your generator directory:
```
eth-utils==1.4.1
2019-03-28 15:10:16 +00:00
../../test_libs/gen_helpers
2019-04-07 07:26:24 +00:00
../../test_libs/config_helpers
2019-03-28 15:10:16 +00:00
../../test_libs/pyspec
```
2019-04-28 05:42:04 +00:00
The config helper and pyspec is optional, but preferred. We encourage generators to derive tests from the spec itself in order to prevent code duplication and outdated tests.
Applying configurations to the spec is simple and enables you to create test suites with different contexts.
2019-04-07 07:26:24 +00:00
2019-04-28 05:42:04 +00:00
Note: make sure to run `make pyspec` from the root of the specs repository in order to build the pyspec requirement.
2019-03-27 16:32:13 +00:00
Install all the necessary requirements (re-run when you add more):
```bash
2019-04-24 17:59:13 +00:00
pip3 install -r requirements.txt
2019-03-27 16:32:13 +00:00
```
And write your initial test generator, extending the base generator:
2019-04-28 05:42:04 +00:00
Write a `main.py` file. See example:
2019-03-27 16:32:13 +00:00
```python
from gen_base import gen_runner, gen_suite, gen_typing
from eth_utils import (
to_dict, to_tuple
)
2019-04-07 07:26:24 +00:00
from preset_loader import loader
from eth2spec.phase0 import spec
2019-03-27 16:32:13 +00:00
@to_dict
2019-04-07 07:26:24 +00:00
def example_test_case(v: int):
yield "spec_SHARD_COUNT", spec.SHARD_COUNT
yield "example", v
2019-03-27 16:32:13 +00:00
@to_tuple
2019-04-07 07:26:24 +00:00
def generate_example_test_cases():
2019-03-27 16:32:13 +00:00
for i in range(10):
2019-04-07 07:26:24 +00:00
yield example_test_case(i)
2019-03-27 16:32:13 +00:00
2019-04-07 08:28:32 +00:00
def example_minimal_suite(configs_path: str) -> gen_typing.TestSuiteOutput:
2019-04-07 07:26:24 +00:00
presets = loader.load_presets(configs_path, 'minimal')
spec.apply_constants_preset(presets)
2019-03-27 16:32:13 +00:00
2019-04-07 08:28:32 +00:00
return ("mini", "core", gen_suite.render_suite(
2019-04-07 07:26:24 +00:00
title="example_minimal",
2019-03-27 16:32:13 +00:00
summary="Minimal example suite, testing bar.",
2019-04-07 07:26:24 +00:00
forks_timeline="testing",
forks=["phase0"],
2019-03-27 16:32:13 +00:00
config="minimal",
2019-04-07 07:26:24 +00:00
handler="main",
2019-04-07 08:28:32 +00:00
test_cases=generate_example_test_cases()))
2019-03-27 16:32:13 +00:00
2019-04-07 08:28:32 +00:00
def example_mainnet_suite(configs_path: str) -> gen_typing.TestSuiteOutput:
2019-04-07 07:26:24 +00:00
presets = loader.load_presets(configs_path, 'mainnet')
spec.apply_constants_preset(presets)
2019-03-27 16:32:13 +00:00
2019-04-07 08:28:32 +00:00
return ("full", "core", gen_suite.render_suite(
2019-04-07 07:26:24 +00:00
title="example_main_net",
summary="Main net based example suite.",
forks_timeline= "mainnet",
forks=["phase0"],
config="testing",
handler="main",
2019-04-07 08:28:32 +00:00
test_cases=generate_example_test_cases()))
2019-03-27 16:32:13 +00:00
2019-03-28 15:27:28 +00:00
2019-04-07 07:26:24 +00:00
if __name__ == "__main__":
gen_runner.run_generator("example", [example_minimal_suite, example_mainnet_suite])
2019-03-28 15:27:28 +00:00
```
2019-03-27 16:32:13 +00:00
Recommendations:
2019-04-28 05:42:04 +00:00
- You can have more than just one suite creator, e.g. ` gen_runner.run_generator("foo", [bar_test_suite, abc_test_suite, example_test_suite])` .
- You can concatenate lists of test cases if you don't want to split it up in suites, however, make sure they can be run with one handler.
- You can split your suite creators into different python files/packages; this is good for code organization.
- Use config "minimal" for performance, but also implement a suite with the default config where necessary.
- You may be able to write your test suite creator in a way where it does not make assumptions on constants.
2019-04-07 07:26:24 +00:00
If so, you can generate test suites with different configurations for the same scenario (see example).
2019-04-28 05:42:04 +00:00
- The test-generator accepts `--output` and `--force` (overwrite output).
2019-03-27 16:32:13 +00:00
## How to add a new test generator
2019-04-28 05:42:04 +00:00
To add a new test generator that builds `New Tests` :
2019-03-27 16:32:13 +00:00
2019-04-28 05:42:04 +00:00
1. Create a new directory `new_tests` within the `test_generators` directory.
2019-03-27 16:32:13 +00:00
Note that `new_tests` is also the name of the directory in which the tests will appear in the tests repository later.
2. Your generator is assumed to have a `requirements.txt` file,
with any dependencies it may need. Leave it empty if your generator has none.
3. Your generator is assumed to have a `main.py` file in its root.
By adding the base generator to your requirements, you can make a generator really easily. See docs below.
2019-04-07 07:26:24 +00:00
4. Your generator is called with `-o some/file/path/for_testing/can/be_anything -c some/other/path/to_configs/` .
2019-04-28 05:42:04 +00:00
The base generator helps you handle this; you only have to define suite headers
2019-03-27 16:32:13 +00:00
and a list of tests for each suite you generate.
5. Finally, add any linting or testing commands to the
[circleci config file ](https://github.com/ethereum/eth2.0-test-generators/blob/master/.circleci/config.yml )
if desired to increase code quality.
2019-04-07 07:26:24 +00:00
2019-03-27 16:32:13 +00:00
Note: you do not have to change the makefile.
However, if necessary (e.g. not using python, or mixing in other languages), submit an issue, and it can be a special case.
Do note that generators should be easy to maintain, lean, and based on the spec.
## How to remove a test generator
If a test generator is not needed anymore, undo the steps described above and make a new release:
2019-04-28 05:42:04 +00:00
1. Remove the generator directory.
2. Remove the generated tests in the [`eth2.0-tests` ](https://github.com/ethereum/eth2.0-tests ) repository by opening a PR there.
3. Make a new release.