iotests: complicate run_job with this one weird trick?
cover iotests: complicate run_job with this one weird trick?
Requires: [email protected]
I'm kidding, but do treat this as an RFC. I doodled a little code change and wasn't sure if it was appropriate because it's not really a simplification.
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.
Letmeknowwhatchathink.
John Snow (2):
iotests: add JobRunner class
iotests: modify test 040 to use JobRunner
tests/qemu-iotests/040 | 51 +++++----- tests/qemu-iotests/255 | 9 +- tests/qemu-iotests/257 | 12 ++- tests/qemu-iotests/287 | 19 +++- tests/qemu-iotests/iotests.py | 176 ++++++++++++++++++++++++---------- 5 files changed, 183 insertions(+), 84 deletions(-)
Signature · 1 line
2.21.1
PATCH 1/2 iotests: add JobRunner class
The 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.
--- tests/qemu-iotests/255 | 9 +- tests/qemu-iotests/257 | 12 ++- tests/qemu-iotests/287 | 19 +++- tests/qemu-iotests/iotests.py | 176 ++++++++++++++++++++++++---------- 4 files changed, 158 insertions(+), 58 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 +9 -3
@@ -265,9 +265,15 @@ def test_bitmap_sync(bsync_mode, msync_mode='bitmap', failure=None): ebitmap.clear() ebitmap.dirty_group(2) - vm.run_job(job, auto_dismiss=True, auto_finalize=False, - pre_finalize=_callback, - cancel=(failure == 'simulated')) + class TestJobRunner(iotests.JobRunner): + def on_pending(self, event): + _callback() + super().on_pending(event) + + runner = TestJobRunner(vm, job, cancel=(failure == 'simulated'), + auto_finalize=False, auto_dismiss=True) + runner.run() + bitmaps = vm.query_bitmaps() log({'bitmaps': bitmaps}, indent=2) log('')
tests/qemu-iotests/287 +14 -5
@@ -165,13 +165,22 @@ def test_bitmap_populate(config): if not config.disabled: ebitmap.dirty_group(2) + + class TestJobRunner(iotests.JobRunner): + def on_pending(self, event): + if config.mid_writes: + perform_writes(drive0, 2) + if not config.disabled: + ebitmap.dirty_group(2) + super().on_pending(event) + job = populate(drive0, 'target', 'bitpop0') assert job['return'] == {'return': {}} - vm.run_job(job['id'], - auto_dismiss=job['auto-dismiss'], - auto_finalize=job['auto-finalize'], - pre_finalize=pre_finalize, - cancel=config.cancel) + job_runner = TestJobRunner(vm, job['id'], + auto_dismiss=job['auto-dismiss'], + auto_finalize=job['auto-finalize'], + cancel=config.cancel) + job_runner.run() log('')
tests/qemu-iotests/iotests.py +128 -48
200 lines not on this page · raw message
Signature · 1 line
2.21.1
PATCH 2/2 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): + result = self._vm.qmp('block-job-resume', device=self._id) + self.test.assert_qmp(result, 'return', {}) + super().on_pause(event) + + 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(iotests.filter_qmp_event(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', use_log=False, 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
Re: [PATCH 1/2] iotests: add JobRunner class
In reply to John SnowQuoted 11 lines · John Snow
The idea is that instead of increasing the arguments to
job_runall the time, create a more general-purpose job runner that can be subclassed to do interesting things with.Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/255 | 9 +- tests/qemu-iotests/257 | 12 ++- tests/qemu-iotests/287 | 19 +++- tests/qemu-iotests/iotests.py | 176 ++++++++++++++++++++++++---------- 4 files changed, 158 insertions(+), 58 deletions(-)
I like it!
[...]
Quoted 5 lines
tests/qemu-iotests/287 +0
-0
tests/qemu-iotests/287 def test_bitmap_populate(config): · line 174
Quoted 10 lines
tests/qemu-iotests/287 +7
-0@@ -165,13 +165,22 @@ def test_bitmap_populate(config): if not config.disabled: ebitmap.dirty_group(2) + + class TestJobRunner(iotests.JobRunner): + def on_pending(self, event): + if config.mid_writes: + perform_writes(drive0, 2) + if not config.disabled: + ebitmap.dirty_group(2)
I actually prefer inlining the pre_finalize() functions (over calling the existing one), but then we can also remove the original function. :)
Quoted 27 lines
tests/qemu-iotests/287 +7
-5+ super().on_pending(event) + job = populate(drive0, 'target', 'bitpop0') assert job['return'] == {'return': {}} - vm.run_job(job['id'], - auto_dismiss=job['auto-dismiss'], - auto_finalize=job['auto-finalize'], - pre_finalize=pre_finalize, - cancel=config.cancel) + job_runner = TestJobRunner(vm, job['id'], + auto_dismiss=job['auto-dismiss'], + auto_finalize=job['auto-finalize'], + cancel=config.cancel) + job_runner.run() log('')tests/qemu-iotests/iotests.py +2
-0@@ -460,6 +460,130 @@ def remote_filename(path): else: raise Exception("Protocol %s not supported" % (imgproto)) + +class JobRunner:
[...]
tests/qemu-iotests/iotests.py def remote_filename(path): · line 519
Quoted 5 lines
tests/qemu-iotests/iotests.py +5
-0+ def on_ready(self, event): + if self.logging: + self._vm.qmp_log('job-complete', id=self._id) + else: + self._vm.qmp('job-complete', id=self._id)
I suppose this is a bug fix. (The old version always called qmp_log.)
But what about adding a do_qmp method to JobRunner that does the “if self.logging { self._vm.qmp_log() } else { self._vm.qmp }” part so we don’t have to inline that everywhere?
Max
Re: [PATCH 2/2] iotests: modify test 040 to use JobRunner
In reply to John SnowQuoted 20 lines · John Snow
Instead of having somewhat reproduced it for itself.
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/040 | 51 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 26 deletions(-)tests/qemu-iotests/040 +5
-0@@ -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')
tests/qemu-iotests/040 class TestErrorHandling(iotests.QMPTestCase): · line 500
Quoted 10 lines
tests/qemu-iotests/040 +10
-0+ + def __init__(self, *args, test, **kwargs): + super().__init__(*args, **kwargs) + self.log = [] + self.test = test + + def on_pause(self, event): + result = self._vm.qmp('block-job-resume', device=self._id) + self.test.assert_qmp(result, 'return', {}) + super().on_pause(event)
Not that it functionally matters, but I suppose I’d call super().on_pause() before resuming (because the job isn’t exactly paused afterwards).
tests/qemu-iotests/040 class TestErrorHandling(iotests.QMPTestCase): · line 506
Quoted 6 lines
tests/qemu-iotests/040 +6
-0+ + 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(iotests.filter_qmp_event(event))
Hasn’t the event been through filter_qmp_event() already?
Max
Quoted 36 lines
tests/qemu-iotests/040 +4
-26+ 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', use_log=False, 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 {
Re: [PATCH 1/2] iotests: add JobRunner class
In reply to Max ReitzQuoted 13 lines · Max Reitz
Quoted 11 lines · John Snow
The idea is that instead of increasing the arguments to
job_runall the time, create a more general-purpose job runner that can be subclassed to do interesting things with.Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/255 | 9 +- tests/qemu-iotests/257 | 12 ++- tests/qemu-iotests/287 | 19 +++- tests/qemu-iotests/iotests.py | 176 ++++++++++++++++++++++++---------- 4 files changed, 158 insertions(+), 58 deletions(-)
Quoted 2 lines
I like it!
High praise!
Quoted 10 lines
[...]
Quoted 8 lines
tests/qemu-iotests/287 +0
-0@@ -165,13 +165,22 @@ def test_bitmap_populate(config): if not config.disabled: ebitmap.dirty_group(2)
Quoted 11 lines
tests/qemu-iotests/287 def test_bitmap_populate(config): · line 174
Quoted 7 lines
tests/qemu-iotests/287 +7
-0+ + class TestJobRunner(iotests.JobRunner): + def on_pending(self, event): + if config.mid_writes: + perform_writes(drive0, 2) + if not config.disabled: + ebitmap.dirty_group(2)I actually prefer inlining the
pre_finalize()functions (over calling the existing one), but then we can also remove the original function. :)
Not sure I understand you correctly. You're saying you prefer this strategy where I inline the logic vs others where I call out to a function?
If so, I agree if only for purity -- the function looks and acts like a callback instead of a callback-that-calls-another-callback.
Quoted 30 lines
Quoted 27 lines
tests/qemu-iotests/287 +7
-5+ super().on_pending(event) + job = populate(drive0, 'target', 'bitpop0') assert job['return'] == {'return': {}} - vm.run_job(job['id'], - auto_dismiss=job['auto-dismiss'], - auto_finalize=job['auto-finalize'], - pre_finalize=pre_finalize, - cancel=config.cancel) + job_runner = TestJobRunner(vm, job['id'], + auto_dismiss=job['auto-dismiss'], + auto_finalize=job['auto-finalize'], + cancel=config.cancel) + job_runner.run() log('')tests/qemu-iotests/iotests.py +2
-0@@ -460,6 +460,130 @@ def remote_filename(path): else: raise Exception("Protocol %s not supported" % (imgproto)) + +class JobRunner:[...]
Quoted 8 lines
tests/qemu-iotests/iotests.py def remote_filename(path): · line 519
Quoted 5 lines
tests/qemu-iotests/iotests.py +5
-0+ def on_ready(self, event): + if self.logging: + self._vm.qmp_log('job-complete', id=self._id) + else: + self._vm.qmp('job-complete', id=self._id)I suppose this is a bug fix. (The old version always called
qmp_log.)
Technically yes. It was needed for 040.
Quoted 6 lines
But what about adding a
do_qmpmethod to JobRunner that does the “if self.logging {self._vm.qmp_log()} else { self._vm.qmp }” part so we don’t have to inline that everywhere?Max
I'll just clean up the logging series I had to do it at a more fundamental level.
Just testing the temperature of the water.
--js
Re: [PATCH 2/2] iotests: modify test 040 to use JobRunner
In reply to Max ReitzQuoted 27 lines · Max Reitz
Quoted 26 lines · John Snow
Instead of having somewhat reproduced it for itself.
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/040 | 51 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 26 deletions(-)tests/qemu-iotests/040 +11
-0@@ -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 +
Quoted 9 lines
tests/qemu-iotests/040 class TestErrorHandling(iotests.QMPTestCase): · line 500
Quoted 4 lines
tests/qemu-iotests/040 +4
-0+ def on_pause(self, event): + result = self._vm.qmp('block-job-resume', device=self._id) + self.test.assert_qmp(result, 'return', {}) + super().on_pause(event)Not that it functionally matters, but I suppose I’d call super().on_pause() before resuming (because the job isn’t exactly paused afterwards).
Reasonable detail to consider.
It's also likely valid to just *omit* calling the base class pause event when overriding behavior -- If we decide to send resume commands in the future, we'll want to avoid sending conflicting/duplicate events.
In this case, the base event is just a NOP so it doesn't matter, but it probably is good hygiene to avoid changing the state *and then* calling the base class.
So I think this is a valid observation that should be worked into the docstring for the JobRunner class on how best to make use of it.
Quoted 9 lines
tests/qemu-iotests/040 class TestErrorHandling(iotests.QMPTestCase): · line 506
Quoted 6 lines
tests/qemu-iotests/040 +6
-0+ + 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(iotests.filter_qmp_event(event))Hasn’t the event been through
filter_qmp_event()already?
Oh, yeah. When I converted 040 here I just kind of shoehorned it onto the new API in a somewhat mechanical fashion, but you're right.
Re: [PATCH 1/2] iotests: add JobRunner class
In reply to John SnowQuoted 43 lines · John Snow
Quoted 13 lines · Max Reitz
Quoted 11 lines · John Snow
The idea is that instead of increasing the arguments to
job_runall the time, create a more general-purpose job runner that can be subclassed to do interesting things with.Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/255 | 9 +- tests/qemu-iotests/257 | 12 ++- tests/qemu-iotests/287 | 19 +++- tests/qemu-iotests/iotests.py | 176 ++++++++++++++++++++++++---------- 4 files changed, 158 insertions(+), 58 deletions(-)Quoted 2 lines
I like it!
High praise!
Quoted 10 lines
[...]
Quoted 8 lines
tests/qemu-iotests/287 +0
-0@@ -165,13 +165,22 @@ def test_bitmap_populate(config): if not config.disabled: ebitmap.dirty_group(2)Quoted 11 lines
tests/qemu-iotests/287 def test_bitmap_populate(config): · line 174
Quoted 7 lines
tests/qemu-iotests/287 +7
-0+ + class TestJobRunner(iotests.JobRunner): + def on_pending(self, event): + if config.mid_writes: + perform_writes(drive0, 2) + if not config.disabled: + ebitmap.dirty_group(2)I actually prefer inlining the
pre_finalize()functions (over calling the existing one), but then we can also remove the original function. :)
Quoted 2 lines
Not sure I understand you correctly. You're saying you prefer this strategy where I inline the logic vs others where I call out to a function?
Yes.
Quoted 2 lines
If so, I agree if only for purity -- the function looks and acts like a callback instead of a callback-that-calls-another-callback.
That was my thinking.
Quoted 45 lines
Quoted 30 lines
Quoted 27 lines
tests/qemu-iotests/287 +7
-5+ super().on_pending(event) + job = populate(drive0, 'target', 'bitpop0') assert job['return'] == {'return': {}} - vm.run_job(job['id'], - auto_dismiss=job['auto-dismiss'], - auto_finalize=job['auto-finalize'], - pre_finalize=pre_finalize, - cancel=config.cancel) + job_runner = TestJobRunner(vm, job['id'], + auto_dismiss=job['auto-dismiss'], + auto_finalize=job['auto-finalize'], + cancel=config.cancel) + job_runner.run() log('')tests/qemu-iotests/iotests.py +2
-0@@ -460,6 +460,130 @@ def remote_filename(path): else: raise Exception("Protocol %s not supported" % (imgproto)) + +class JobRunner:[...]
Quoted 8 lines
tests/qemu-iotests/iotests.py def remote_filename(path): · line 519
Quoted 5 lines
tests/qemu-iotests/iotests.py +5
-0+ def on_ready(self, event): + if self.logging: + self._vm.qmp_log('job-complete', id=self._id) + else: + self._vm.qmp('job-complete', id=self._id)I suppose this is a bug fix. (The old version always called
qmp_log.)Technically yes. It was needed for 040.
Quoted 4 lines
But what about adding a
do_qmpmethod to JobRunner that does the “if self.logging {self._vm.qmp_log()} else { self._vm.qmp }” part so we don’t have to inline that everywhere?
Quoted 5 lines
Quoted 2 lines
Max
I'll just clean up the logging series I had to do it at a more fundamental level.
OK. So you’re looking to basically get VM.qmp() to log automatically if necessary? (or maybe qmp_log() to not log unless necessary)
Max
Re: [PATCH 1/2] iotests: add JobRunner class
Quoted 7 lines · Max Reitz
Quoted 2 lines
I'll just clean up the logging series I had to do it at a more fundamental level.
OK. So you’re looking to basically get
VM.qmp()to log automatically if necessary? (or maybeqmp_log()to not log unless necessary)Max
Yes. If this series looks good I will just rebase it on top of the logging series. Then self.logging goes away and the calls to qmp_log et al just become unconditional.
Then we can enable/disable "all QMP logging" globally instead of individually throughout the iotests shared codebase.
--js