The MagAO-X ecosystem is largely based on C++. Type-safe logging requirements play a key role in the ecosystem's robustness. The MagAO-X logging system is based on Flatfile (created by Facebook), and is controlled through the Flatlog classes within MagAO-X. Individual entries within Flatlogs may each contain different data and be of a different type. The types are defined as C++ classes generated from the Flatfile configuration. Lastly, MagAO-X installations may each utilize different classes for their logging needs.
While Flatlogs is an excellent method of logging variadic data of multiple types, testing the flatlog classes is difficult. Entry classes need to be tested singularly with generated data, and also need to be tested together in longer sequences of logs.
To this end, the Catch2 Test Generator will automatically review the class definitions in the Flatfile .fbs
schema files. Once analyzed, the generator will create singular C++ tests using Catch2, compile, run, and report their results. In addition, the generator will also generate a combined test of multiple "random" types and compile, run and report their results.
The test generator is composed of two files, generateTemplatedCatch2Tests.py
and catch2TestTemplate.jinja2
. When run, Catch2 test files for each flatlog type will be created.
jinja2 must be installed. The script will check for jinja2 and automatically install it if not present. It can be installed manually with the following command:
python3 -m pip install Jinja2
It is assumed that any libraries and dependencies for the flatlog types have already been installed.
The generator can be run by either running the python script directly, or calling the Makefile target. They are functionally equivalent.
To run the generator, do:
python3 ./generateTemplatedCatch2Tests.py
or
make generated_tests
Upon success, a folder titled generated_tests
will be created, containing generated tests with the title <log_type>_generated_tests.cpp
.
To build and run the tests, do:
make do_catch2_test
If the generated test files are not present, they will be generated by the Makefile.
At the start of the python script, the previous generated test files will be deleted. Because random values are used for test values, the values will change between runs of the generator. A random seed can be provided as a command line option with -s
. For example:
python3 ./generateTemplatedCatch2Tests.py -s <seed>
To have incrementing test values, use the -i
option:
python3 ./generateTemplatedCatch2Tests.py -i
If no option is provided, the generator defaults to randomly generated values.
This script detects a 'base' type if it does not have eventCode and defaultLevel in the .hpp file. It is noted in these log types that they cannot be used directly. The base types found are:
It is assumed that these base types do not explicitly require tests, but will be tested through their children types.
**software_log is a special case since the base type is contained in the same file as its child types. The template uses LOG_NOTICE for all software logs.
generated_tests
folder will be deleted in addition to the default clean
target with make really_clean
.In order to test flatlogs further, 'entropy' tests can be generated. These tests consist of a number of flatlogs being created with random values, and then each field is verified one-by-one.
The entropy generator is composed of entropyTestTemplate.jinja2
and generateEntropyTests.py
. Both files should be in the same directory as types/
.
The Catch2 Test Generator described above must be run before entropy tests can be generated, since entropy tests include these test files.
The easiest way to generate, compile, and run the entropy test is through the makefile target:
make do_entropy_test <OPTIONS>
The generated file is written to gen_entropy_tests/generated_test_e<ENTROPY>_n<NTYPES>.cpp
For example, if e=3 and n=12, the cpp file is titled generated_test_e3_n12.cpp
and the test executable is generated_test_e3_n12
.
s
: random seedn
: number of distinct flatlog types to use.e
: entropy level. The number of total flatlogs that will be tested is e * nt
: specific types of flatlogs to use, in quotes, comma-separated. The types will be randomly selected if this option is not used.Note: It is not required to provide these options on the commandline. Their default values are stored as variables in the makefile, and can be set there as well.
make do_entropy_test
make do_entropy_test s=123 n=25 e=2
make do_entropy_test n=5 e=5 t="config_log, git_state"
e
and n
that already exist in gen_entropy_tests/
, the existing file will be overwritten.The makefile creates the compiled executable generated_entropy_test_e<E>_n<N>
. Because this file includes other Catch2 tests, they all will be run when the executable is run. In order to run just the entropy test, you must specify the scenario:
./generated_entropy_test_e<E>_n<N> "Scenario: test_e<E>_n<N>"