Blame tests/readme.md

Packit 534379
# OPAE Testing Framework #
Packit 534379
Packit 534379
## Objective ##
Packit 534379
Packit 534379
The main goal of the testing framework used by OPAE is to enable (Google test)
Packit 534379
tests that don't require the presence of either an OPAE kernel driver or an
Packit 534379
OPAE compatible FPGA. An additional goal is to allow authoring tests that can
Packit 534379
easily ported from a mock environment to a real system.
Packit 534379
Packit 534379
Packit 534379
## Mocking the Kernel Driver ##
Packit 534379
Packit 534379
The two interfaces to the OPAE Linux driver are `ioctl` calls and `sysfs`
Packit 534379
files. The OPAE C library uses both of these interfaces in its implementation
Packit 534379
to control and/or query the device. To test the API functions (including
Packit 534379
internal functions used within), a `mock` driver has been developed that can
Packit 534379
allow for calling these functions without calling the kernel driver through
Packit 534379
these interfaces. At a high level, system calls related to `ioctl` and `sysfs`
Packit 534379
files (eg. `ioctl`, `open`) are implemented in a C file called `mock.c` which
Packit 534379
in turn calls into a C++ singleton class called `test_system` using its C
Packit 534379
interface. This C++ singleton can also be used by any google test code to call
Packit 534379
any necessary setup and teardown routines. When calls such as `open` are called
Packit 534379
and handled by `test_system`, paths used by the OPAE kernel driver
Packit 534379
(`/dev/intel-fpga-port.0` or `/sys/class/fpga/intel-fpga-port.0`) can be
Packit 534379
re-rooted to point to files in a mock sysfs subtree.
Packit 534379
Packit 534379
The following diagram illustrate a general flow of a google test using the mock
Packit 534379
driver and the test_system.
Packit 534379
![OPAE mock test](mock_driver.png "OPAE mock test") 
Packit 534379
Packit 534379
### test_system ###
Packit 534379
Packit 534379
As mentioned above, `test_system` is a singleton class that is designed to be
Packit 534379
called from either C code (by using its C interface) or by using its C++
Packit 534379
`instance()` function. This allows it to be called from both google test tests
Packit 534379
as well as from C functions that are being mocked. Tests that depend on any
Packit 534379
sysfs paths handled by the OPAE kernel driver should be implemented as part of
Packit 534379
a test fixture. This fixture should call `test_system::initialize` in its setup
Packit 534379
and should also call `test_system::finalize` in its teardown. If a unit test
Packit 534379
does not depend on any sysfs paths used by the OPAE kernel driver, then there
Packit 534379
is no need to call these two functions and those functions should be
Packit 534379
implemented without a fixture. The class `test_system` is compiled into a
Packit 534379
shared library (`libtest_system.so`) and should be linked with any OPAE test
Packit 534379
that uses it. Additionally, the file `mock.c` is not included in this library
Packit 534379
and should be included into any test executable that uses `test_system`.
Packit 534379
Packit 534379
### ioctl handlers ###
Packit 534379
Packit 534379
The file `ioctl_handlers.cpp` includes some default ioctl handler functions for
Packit 534379
most of the ioctl requests (as identified by the request constant in
Packit 534379
`intel-fpga.h`). This file calls the function
Packit 534379
`test_system::register_ioctl_handler` which maps a request constant to a
Packit 534379
function that should be called when the request is made. A templated function,
Packit 534379
`dummy_ioctl` is provided to allow registering an ioctl handler that uses its
Packit 534379
template arguments to define what to return from ioctl and also to specify what
Packit 534379
`errno` should be set to. This is intended for authoring negative tests that
Packit 534379
depend on `ioctl` calls.
Packit 534379