
Convert nova from using nosetests to testr for its test runner. Some tests had to be modified to get them to run properly under testr. run_tests.sh has been updated to run testr instead of nosetests. Coverage is collected by running subunit.run under coverage.py when the coverage environment is selected. Note that you will need to rebuild your virtualenvs as nose is being removed from the dependency lists and is being replaced by testr. Tests will run in different processes once this test is merged so you cannot use test classes to pass information between tests. Each test should be a proper independent unit. Additionally the -x and -d flags to run_tests.sh have been removed as there are currently no decent approximations for those functions. Change-Id: I019ca098972ca749b195f59968cf21edd5ba9109
OpenStack Hyper-V Nova Testing Architecture |
The Hyper-V Nova Compute plugin uses Windows Management Instrumentation (WMI) as the main API for hypervisor related operations. WMI has a database / procedural oriented nature that can become difficult to test with a traditional static mock / stub based unit testing approach.
The included Hyper-V testing framework has been developed with the following goals:
- Dynamic mock generation.
- Decoupling. No dependencies on WMI or any other module.
-
The tests are designed to work with mocked objects in all cases, including OS-dependent (e.g. wmi, os, subprocess) and non-deterministic (e.g. time, uuid) modules
- Transparency. Mocks and real objects can be swapped via DI
-
or monkey patching.
- Platform independence.
- Tests need to be executed against the real object or against the mocks
-
with a simple configuration switch. Development efforts can highly benefit from this feature.
- It must be possible to change a mock's behavior without running the tests
-
against the hypervisor (e.g. by manually adding a value / return value).
The tests included in this package include dynamically generated mock objects, based on the recording of the attribute values and invocations on the real WMI objects and other OS dependent features. The generated mock objects are serialized in the nova/tests/hyperv/stubs directory as gzipped pickled objects.
An environment variable controls the execution mode of the tests.
Recording mode:
NOVA_GENERATE_TEST_MOCKS=True Tests are executed on the hypervisor (without mocks), and mock objects are generated.
Replay mode:
NOVA_GENERATE_TEST_MOCKS= Tests are executed with the existing mock objects (default).
Mock generation is performed by nova.tests.hyperv.mockproxy.MockProxy. Instances of this class wrap objects that need to be mocked and act as a delegate on the wrapped object by leveraging Python's __getattr__ feature. Attribute values and method call return values are recorded at each access. Objects returned by attributes and method invocations are wrapped in a MockProxy consistently. From a caller perspective, the MockProxy is completely transparent, with the exception of calls to the type(...) builtin function.
At the end of the test, a mock is generated by each MockProxy by calling the get_mock() method. A mock is represented by an instance of the nova.tests.hyperv.mockproxy.Mock class.
The Mock class task consists of replicating the behaviour of the mocked objects / modules by returning the same values in the same order, for example:
- def check_path(path):
-
- if not os.path.exists(path):
-
os.makedirs(path)
check_path(path) # The second time os.path.exists returns True check_path(path)
The injection of MockProxy / Mock instances is performed by the nova.tests.hyperv.basetestcase.BaseTestCase class in the setUp() method via selective monkey patching. Mocks are serialized in tearDown() during recording.
The actual Hyper-V test case inherits from BaseTestCase: nova.tests.hyperv.test_hypervapi.HyperVAPITestCase
Future directions:
- Replace the pickled files with a more generic serialization option (e.g. json)
- Add methods to statically extend the mocks (e.g. method call return values)
- Extend an existing framework, e.g. mox