iotests: add JobRunner framework
v2 cover iotests: add JobRunner framework
Requires: [email protected]
(This requires the iotests pylint & logging series.)
The basic idea is to make a generic job runtime manager and allow callers to subclass the manager. Then, instead of adding callback arguments to the function all the time, we have à la carte customization of the loop.
To showcase this a little bit, I removed the pre_finalization argument and made existing callers use a custom JobRunner; and then converted test 040 to use this style of job runner.
Is it a simplification? No. Is it cool? Maybe. Did it remove the duplicated job-running code in 040? yes.
V2:
- Rebased on logging series; logging conditionals are pretty now.
- Inlined callback login in 257
- No longer based on bitmap-populate job (no test 287)
- Moved super() call to the beginning of test 040's callback
- Added docstrings and type annotations
John Snow (3):
qmp.py: change event_wait to use a dict
iotests: add JobRunner class
iotests: modify test 040 to use JobRunner
python/qemu/machine.py | 10 +- tests/qemu-iotests/040 | 51 +++++---- tests/qemu-iotests/255 | 9 +- tests/qemu-iotests/257 | 54 +++++---- tests/qemu-iotests/260 | 5 +- tests/qemu-iotests/iotests.py | 201 +++++++++++++++++++++++++--------- tests/qemu-iotests/pylintrc | 11 ++ 7 files changed, 233 insertions(+), 108 deletions(-)
Signature · 1 line
2.21.1
PATCH v2 1/3 qmp.py: change event_wait to use a dict
It's easier to work with than a list of tuples, because we can check the keys for membership.
--- python/qemu/machine.py | 10 +++++----- tests/qemu-iotests/040 | 12 ++++++------ tests/qemu-iotests/260 | 5 +++-- tests/qemu-iotests/iotests.py | 16 ++++++++-------- 4 files changed, 22 insertions(+), 21 deletions(-)
python/qemu/machine.py +5 -5
@@ -476,21 +476,21 @@ def event_wait(self, name, timeout=60.0, match=None): timeout: QEMUMonitorProtocol.pull_event timeout parameter. match: Optional match criteria. See event_match for details. """ - return self.events_wait([(name, match)], timeout) + return self.events_wait({name: match}, timeout) def events_wait(self, events, timeout=60.0): """ events_wait waits for and returns a named event from QMP with a timeout. - events: a sequence of (name, match_criteria) tuples. + events: a mapping containing {name: match_criteria}. The match criteria are optional and may be None. See event_match for details. timeout: QEMUMonitorProtocol.pull_event timeout parameter. """ def _match(event): - for name, match in events: - if event['event'] == name and self.event_match(event, match): - return True + name = event['event'] + if name in events: + return self.event_match(event, events[name]) return False # Search cached events
tests/qemu-iotests/040 +6 -6
@@ -485,12 +485,12 @@ class TestErrorHandling(iotests.QMPTestCase): def run_job(self, expected_events, error_pauses_job=False): match_device = {'data': {'device': 'job0'}} - events = [ - ('BLOCK_JOB_COMPLETED', match_device), - ('BLOCK_JOB_CANCELLED', match_device), - ('BLOCK_JOB_ERROR', match_device), - ('BLOCK_JOB_READY', match_device), - ] + events = { + 'BLOCK_JOB_COMPLETED': match_device, + 'BLOCK_JOB_CANCELLED': match_device, + 'BLOCK_JOB_ERROR': match_device, + 'BLOCK_JOB_READY': match_device, + } completed = False log = []
tests/qemu-iotests/260 +3 -2
@@ -67,8 +67,9 @@ def test(persistent, restart): vm.qmp_log('block-commit', device='drive0', top=top, filters=[iotests.filter_qmp_testfiles]) - ev = vm.events_wait((('BLOCK_JOB_READY', None), - ('BLOCK_JOB_COMPLETED', None))) + ev = vm.events_wait({ + 'BLOCK_JOB_READY': None, + 'BLOCK_JOB_COMPLETED': None }) log(filter_qmp_event(ev)) if (ev['event'] == 'BLOCK_JOB_COMPLETED'): vm.shutdown()
tests/qemu-iotests/iotests.py +8 -8
@@ -614,14 +614,14 @@ def run_job(self, job, auto_finalize=True, auto_dismiss=False, """ match_device = {'data': {'device': job}} match_id = {'data': {'id': job}} - events = [ - ('BLOCK_JOB_COMPLETED', match_device), - ('BLOCK_JOB_CANCELLED', match_device), - ('BLOCK_JOB_ERROR', match_device), - ('BLOCK_JOB_READY', match_device), - ('BLOCK_JOB_PENDING', match_id), - ('JOB_STATUS_CHANGE', match_id) - ] + events = { + 'BLOCK_JOB_COMPLETED': match_device, + 'BLOCK_JOB_CANCELLED': match_device, + 'BLOCK_JOB_ERROR': match_device, + 'BLOCK_JOB_READY': match_device, + 'BLOCK_JOB_PENDING': match_id, + 'JOB_STATUS_CHANGE': match_id, + } error = None while True: ev = filter_qmp_event(self.events_wait(events, timeout=wait))
Signature · 1 line
2.21.1
PATCH v2 2/3 iotests: add JobRunner class
In reply to John SnowThe idea is that instead of increasing the arguments to job_run all the time, create a more general-purpose job runner that can be subclassed to do interesting things with.
pylint note: the 'callbacks' option guards against unused warning arguments in functions designated as callbacks. It does not currently guard against "no-self-use" though; hence a once-off ignore.
mypy note: QapiEvent is only a weak alias; it's fully interchangable with the type it's declared as. In the future, we may wish to tighten these types. For now, this communicates the rough shape of the type and (more importantly) the intent.
--- tests/qemu-iotests/255 | 9 +- tests/qemu-iotests/257 | 54 +++++---- tests/qemu-iotests/iotests.py | 201 +++++++++++++++++++++++++--------- tests/qemu-iotests/pylintrc | 11 ++ 4 files changed, 200 insertions(+), 75 deletions(-)
tests/qemu-iotests/255 +7 -2
@@ -71,8 +71,13 @@ with iotests.FilePath('t.qcow2') as disk_path, \ result = vm.qmp_log('block-commit', job_id='job0', auto_finalize=False, device='overlay', top_node='mid') - vm.run_job('job0', auto_finalize=False, pre_finalize=start_requests, - auto_dismiss=True) + class TestJobRunner(iotests.JobRunner): + def on_pending(self, event): + start_requests() + super().on_pending(event) + + runner = TestJobRunner(vm, 'job0', auto_finalize=False, auto_dismiss=True) + runner.run() vm.shutdown()
tests/qemu-iotests/257 +32 -22
62 lines not on this page · raw message
tests/qemu-iotests/iotests.py +150 -51
229 lines not on this page · raw message
tests/qemu-iotests/pylintrc +11 -0
@@ -17,9 +17,20 @@ disable=invalid-name, too-many-locals, too-many-branches, too-many-public-methods, + too-many-instance-attributes, # These are temporary, and should be removed: missing-docstring, + +[VARIABLES] + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb, + on_, + + [FORMAT] # Maximum number of characters on a single line.
Signature · 1 line
2.21.1
PATCH v2 3/3 iotests: modify test 040 to use JobRunner
In reply to John SnowInstead of having somewhat reproduced it for itself.
--- tests/qemu-iotests/040 | 51 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 26 deletions(-)
tests/qemu-iotests/040 +25 -26
@@ -483,34 +483,33 @@ class TestErrorHandling(iotests.QMPTestCase): file=('top-dbg' if top_debug else 'top-file'), backing='mid-fmt') + + class TestJobRunner(iotests.JobRunner): + expected_events = ('BLOCK_JOB_COMPLETED', + 'BLOCK_JOB_ERROR', + 'BLOCK_JOB_READY') + + def __init__(self, *args, test, **kwargs): + super().__init__(*args, **kwargs) + self.log = [] + self.test = test + + def on_pause(self, event): + super().on_pause(event) + result = self._vm.qmp('block-job-resume', device=self._id) + self.test.assert_qmp(result, 'return', {}) + + def on_block_job_event(self, event): + if event['event'] not in self.expected_events: + self.test.fail("Unexpected event: %s" % event) + super().on_block_job_event(event) + self.log.append(event) + def run_job(self, expected_events, error_pauses_job=False): - match_device = {'data': {'device': 'job0'}} - events = { - 'BLOCK_JOB_COMPLETED': match_device, - 'BLOCK_JOB_CANCELLED': match_device, - 'BLOCK_JOB_ERROR': match_device, - 'BLOCK_JOB_READY': match_device, - } - - completed = False - log = [] - while not completed: - ev = self.vm.events_wait(events, timeout=5.0) - if ev['event'] == 'BLOCK_JOB_COMPLETED': - completed = True - elif ev['event'] == 'BLOCK_JOB_ERROR': - if error_pauses_job: - result = self.vm.qmp('block-job-resume', device='job0') - self.assert_qmp(result, 'return', {}) - elif ev['event'] == 'BLOCK_JOB_READY': - result = self.vm.qmp('block-job-complete', device='job0') - self.assert_qmp(result, 'return', {}) - else: - self.fail("Unexpected event: %s" % ev) - log.append(iotests.filter_qmp_event(ev)) - + job = self.TestJobRunner(self.vm, 'job0', test=self) + job.run() self.maxDiff = None - self.assertEqual(expected_events, log) + self.assertEqual(expected_events, job.log) def event_error(self, op, action): return {
Signature · 1 line
2.21.1