Python / Acceptance Tests: improve logging
cover Python / Acceptance Tests: improve logging
The location and amount of information kept while using QEMUMachine in Acceptance Tests is currently not optimal.
This improves the situation by using the Test's log directory (an Avocado standard feature) as the default location to keep logs, instead of the temporary directory currently used.
Users will be able to find "qemu-$PID.log" files under the test log directories, containing all the stdout/stderr generated by the QEMU binary.
Cleber Rosa (6):
Python: close the log file kept by QEMUMachine before reading it
Python: expose QEMUMachine's temporary directory Acceptance Tests: use the job work directory for created VMs Acceptance Tests: log information when creating QEMUMachine Acceptance Tests: distinguish between temp and logs dir tests/acceptance/virtio-gpu.py: preserve virtio-user-gpu log
python/qemu/machine.py | 42 +++++++++++++++++------ python/qemu/qtest.py | 6 ++-- tests/acceptance/avocado_qemu/__init__.py | 10 ++++-- tests/acceptance/virtio-gpu.py | 5 +-- tests/qemu-iotests/iotests.py | 2 +- 5 files changed, 45 insertions(+), 20 deletions(-)
Signature · 1 line
2.25.4
PATCH 1/6 Python: close the log file kept by QEMUMachine before reading it
Closing a file that is open for writing, and then reading from it sounds like a better idea than the opposite, given that the content will be flushed.
Reference: https://docs.python.org/3/library/io.html#io.IOBase.close
--- python/qemu/machine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
python/qemu/machine.py +2 -2
@@ -337,12 +337,12 @@ class QEMUMachine: self._qmp.close() self._qmp_connection = None - self._load_io_log() - if self._qemu_log_file is not None: self._qemu_log_file.close() self._qemu_log_file = None + self._load_io_log() + self._qemu_log_path = None if self._temp_dir is not None:
Signature · 1 line
2.25.4
Re: [PATCH 1/6] Python: close the log file kept by QEMUMachine before reading it
Hi,
Quoted 16 lines · Cleber Rosa
Closing a file that is open for writing, and then reading from it sounds like a better idea than the opposite, given that the content will be flushed.
Reference: https://docs.python.org/3/library/io.html#io.IOBase.close
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)python/qemu/machine.py +0
-0@@ -337,12 +337,12 @@ class QEMUMachine: self._qmp.close()
python/qemu/machine.py class QEMUMachine: · line 345
Quoted 10 lines
python/qemu/machine.py +2
-2self._qmp_connection = None - self._load_io_log() - if self._qemu_log_file is not None: self._qemu_log_file.close() self._qemu_log_file = None + self._load_io_log() +
IMO it's a too fragile fix. It needs the self._qemu_log_file.close() being called before self._load_io_log() but a future change can make this condition unmet again. Maybe you could document that in the code. Or change the _load_io_log() to close the log file if it is open (also document it in code).
- Wainer
Quoted 3 lines
python/qemu/machine.py +0
-0self._qemu_log_path = None if self._temp_dir is not None:
Re: [PATCH 1/6] Python: close the log file kept by QEMUMachine before reading it
Quoted 35 lines · Wainer dos Santos Moschetta
Hi,
Quoted 15 lines · Cleber Rosa
Closing a file that is open for writing, and then reading from it sounds like a better idea than the opposite, given that the content will be flushed.
Reference: https://docs.python.org/3/library/io.html#io.IOBase.close
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)python/qemu/machine.py +0
-0python/qemu/machine.py class QEMUMachine: · line 343
Quoted 9 lines
python/qemu/machine.py +2
-2@@ -337,12 +337,12 @@ class QEMUMachine: self._qmp.close() self._qmp_connection = None - self._load_io_log() - if self._qemu_log_file is not None: self._qemu_log_file.close() self._qemu_log_file = None + self._load_io_log() +IMO it's a too fragile fix. It needs the
self._qemu_log_file.close()being called beforeself._load_io_log()but a future change can make this condition unmet again. Maybe you could document that in the code. Or change the_load_io_log()to close the log file if it is open (also document it in code).
Quoted 2 lines
- Wainer
I'm glad you see this is needed... and then something else. I'll investigate making this more robust as time allows it.
Question is: do you ack/nack this change?
Cheers,
- Cleber.
Wainer dos Santos Moschetta
Acked-byRe: [PATCH 1/6] Python: close the log file kept by QEMUMachine before reading it
Quoted 35 lines · Cleber Rosa
Quoted 34 lines · Wainer dos Santos Moschetta
Hi,
Quoted 15 lines · Cleber Rosa
Closing a file that is open for writing, and then reading from it sounds like a better idea than the opposite, given that the content will be flushed.
Reference: https://docs.python.org/3/library/io.html#io.IOBase.close
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)python/qemu/machine.py +0
-0python/qemu/machine.py class QEMUMachine: · line 343
Quoted 9 lines
python/qemu/machine.py +2
-2@@ -337,12 +337,12 @@ class QEMUMachine: self._qmp.close() self._qmp_connection = None - self._load_io_log() - if self._qemu_log_file is not None: self._qemu_log_file.close() self._qemu_log_file = None + self._load_io_log() +IMO it's a too fragile fix. It needs the
self._qemu_log_file.close()being called beforeself._load_io_log()but a future change can make this condition unmet again. Maybe you could document that in the code. Or change the_load_io_log()to close the log file if it is open (also document it in code).
Quoted 6 lines
Quoted 2 lines
- Wainer
I'm glad you see this is needed... and then something else. I'll investigate making this more robust as time allows it.
Question is: do you ack/nack this change?
hmm... /me thinking hmmm... okay, good deal. :)
Quoted 3 lines
Cheers,
- Cleber.
Re: [PATCH 1/6] Python: close the log file kept by QEMUMachine before reading it
In reply to Cleber Rosapython/qemu/machine.py class QEMUMachine:
Quoted 15 lines · Cleber Rosa
Closing a file that is open for writing, and then reading from it sounds like a better idea than the opposite, given that the content will be flushed.
Reference: https://docs.python.org/3/library/io.html#io.IOBase.close
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)python/qemu/machine.py +0
-0@@ -337,12 +337,12 @@ class QEMUMachine:
Is there a way to improve context for python functions? What method is this in? etc.
Quoted 5 lines
python/qemu/machine.py +0
-1self._qmp.close() self._qmp_connection = None - self._load_io_log()
python/qemu/machine.py class QEMUMachine: · line 348
Quoted 10 lines
python/qemu/machine.py +2
-1- if self._qemu_log_file is not None: self._qemu_log_file.close() self._qemu_log_file = None + self._load_io_log() + self._qemu_log_path = None if self._temp_dir is not None:
Yeh, seems fine, though as wainer points out the interdependencies between _load_io_log, _qemu_log_file and _qemu_log_path are not all strictly clear, so it seems fragile.
But, this is more correct than it was, so:
Re: [PATCH 1/6] Python: close the log file kept by QEMUMachine before reading it
Quoted 11 lines · John Snow
Quoted 10 lines · Cleber Rosa
Closing a file that is open for writing, and then reading from it sounds like a better idea than the opposite, given that the content will be flushed.
Reference: https://docs.python.org/3/library/io.html#io.IOBase.close
Signed-off-by Cleber Rosa <[email protected]>---
python/qemu/machine.py| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 8 lines
python/qemu/machine.py class QEMUMachine:
Quoted 5 lines
python/qemu/machine.py +0
-0@@ -337,12 +337,12 @@ class QEMUMachine:Is there a way to improve context for python functions? What method is this in? etc.
'man gitattributes' suggests that having this line in .gitattributes would help:
*.py diff=python
/me goes to play with it...
Does this look better to you? It certainly does to me! I'll go ahead and propose the .gitattributes change as a formal patch...
--- a/python/qemu/machine.py
+++ b/python/qemu/machine.py
-@@ -337,12 +337,12 @@ class QEMUMachine:
+@@ -337,12 +337,12 @@ def _post_shutdown(self) -> None:
self._qmp.close()
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [PATCH 1/6] Python: close the log file kept by QEMUMachine before reading it
In reply to John SnowQuoted 20 lines · John Snow
python/qemu/machine.py class QEMUMachine:
Quoted 15 lines · Cleber Rosa
Closing a file that is open for writing, and then reading from it sounds like a better idea than the opposite, given that the content will be flushed.
Reference: https://docs.python.org/3/library/io.html#io.IOBase.close
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)python/qemu/machine.py +0
-0@@ -337,12 +337,12 @@ class QEMUMachine:Is there a way to improve context for python functions? What method is this in? etc.
Quoted 17 lines
python/qemu/machine.py class QEMUMachine:
Quoted 12 lines
python/qemu/machine.py +2
-2self._qmp.close() self._qmp_connection = None - self._load_io_log() - if self._qemu_log_file is not None: self._qemu_log_file.close() self._qemu_log_file = None + self._load_io_log() + self._qemu_log_path = None if self._temp_dir is not None:Yeh, seems fine, though as wainer points out the interdependencies between
_load_io_log,_qemu_log_fileand_qemu_log_pathare not all strictly clear, so it seems fragile.
Yep, agreed. This was a first, conservative change. Expect more later.
Quoted 3 lines
But, this is more correct than it was, so:
Reviewed-by John Snow <[email protected]>
Thanks,
- Cleber
PATCH 2/6 Python: expose QEMUMachine's temporary directory
In reply to Cleber RosaEach instance of qemu.machine.QEMUMachine currently has a "test directory", which may not have any relation to a "test", and it's really a temporary directory.
Users instantiating the QEMUMachine class will be able to set the location of the directory that will *contain* the QEMUMachine unique temporary directory, so that parameter name has been changed from test_dir to base_temp_dir.
A property has been added to allow users to access it without using private attributes, and with that, the directory is created on first use of the property.
--- python/qemu/machine.py | 24 ++++++++++++++++-------- python/qemu/qtest.py | 6 +++--- tests/acceptance/virtio-gpu.py | 2 +- tests/qemu-iotests/iotests.py | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-)
python/qemu/machine.py +16 -8
@@ -84,7 +84,7 @@ class QEMUMachine: args: Sequence[str] = (), wrapper: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp", monitor_address: Optional[SocketAddrT] = None, socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None,
@@ -97,10 +97,10 @@ class QEMUMachine: @param args: list of extra arguments @param wrapper: list of arguments used as prefix to qemu binary @param name: prefix for socket and log file names (default: qemu-PID) - @param test_dir: where to create socket and log file + @param base_temp_dir: default location where temporary files are created @param monitor_address: address for QMP monitor @param socket_scm_helper: helper program, required for send_fd_scm() - @param sock_dir: where to create socket (overrides test_dir for sock) + @param sock_dir: where to create socket (defaults to base_temp_dir) @param drain_console: (optional) True to drain console socket to buffer @param console_log: (optional) path to console log file @note: Qemu process is not started until launch() is used.
@@ -112,8 +112,8 @@ class QEMUMachine: self._wrapper = wrapper self._name = name or "qemu-%d" % os.getpid() - self._test_dir = test_dir - self._sock_dir = sock_dir or self._test_dir + self._base_temp_dir = base_temp_dir + self._sock_dir = sock_dir or self._base_temp_dir self._socket_scm_helper = socket_scm_helper if monitor_address is not None:
@@ -303,9 +303,7 @@ class QEMUMachine: return args def _pre_launch(self) -> None: - self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", - dir=self._test_dir) - self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log") + self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") self._qemu_log_file = open(self._qemu_log_path, 'wb') if self._console_set:
@@ -744,3 +742,13 @@ class QEMUMachine: file=self._console_log_path, drain=self._drain_console) return self._console_socket + + @property + def temp_dir(self) -> str: + """ + Returns a temporary directory to be used for this machine + """ + if self._temp_dir is None: + self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", + dir=self._base_temp_dir) + return self._temp_dir
python/qemu/qtest.py +3 -3
@@ -112,14 +112,14 @@ class QEMUQtestMachine(QEMUMachine): binary: str, args: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp", socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None): if name is None: name = "qemu-%d" % os.getpid() if sock_dir is None: - sock_dir = test_dir - super().__init__(binary, args, name=name, test_dir=test_dir, + sock_dir = base_temp_dir + super().__init__(binary, args, name=name, base_temp_dir=base_temp_dir, socket_scm_helper=socket_scm_helper, sock_dir=sock_dir) self._qtest: Optional[QEMUQtestProtocol] = None
tests/acceptance/virtio-gpu.py +1 -1
@@ -119,7 +119,7 @@ class VirtioGPUx86(Test): os.set_inheritable(vug_sock.fileno(), True) self._vug_log_path = os.path.join( - self.vm._test_dir, "vhost-user-gpu.log" + self.vm.temp_dir, "vhost-user-gpu.log" ) self._vug_log_file = open(self._vug_log_path, "wb") print(self._vug_log_path)
tests/qemu-iotests/iotests.py +1 -1
@@ -562,7 +562,7 @@ class VM(qtest.QEMUQtestMachine): def __init__(self, path_suffix=''): name = "qemu%s-%d" % (path_suffix, os.getpid()) super().__init__(qemu_prog, qemu_opts, name=name, - test_dir=test_dir, + base_temp_dir=test_dir, socket_scm_helper=socket_scm_helper, sock_dir=sock_dir) self._num_drives = 0
Signature · 1 line
2.25.4
Re: [PATCH 2/6] Python: expose QEMUMachine's temporary directory
Quoted 26 lines · Cleber Rosa
Each instance of qemu.machine.QEMUMachine currently has a "test directory", which may not have any relation to a "test", and it's really a temporary directory.
Users instantiating the QEMUMachine class will be able to set the location of the directory that will *contain* the QEMUMachine unique temporary directory, so that parameter name has been changed from
test_dirtobase_temp_dir.A property has been added to allow users to access it without using private attributes, and with that, the directory is created on first use of the property.
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 24 ++++++++++++++++-------- python/qemu/qtest.py | 6 +++--- tests/acceptance/virtio-gpu.py | 2 +- tests/qemu-iotests/iotests.py | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-)python/qemu/machine.py +0
-0
python/qemu/machine.py class QEMUMachine: · line 87
Quoted 5 lines
python/qemu/machine.py +1
-1@@ -84,7 +84,7 @@ class QEMUMachine: args: Sequence[str] = (), wrapper: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp",
Not this patch fault, but I see we use /var/tmp since commit 66613974468 ("scripts: refactor the VM class in iotests for reuse"). Can we use an OS agnostic method to get temp storage directory instead?
Re: [PATCH 2/6] Python: expose QEMUMachine's temporary directory
Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Cleber Rosa
Each instance of qemu.machine.QEMUMachine currently has a "test directory", which may not have any relation to a "test", and it's really a temporary directory.
Users instantiating the QEMUMachine class will be able to set the location of the directory that will *contain* the QEMUMachine unique temporary directory, so that parameter name has been changed from
test_dirtobase_temp_dir.A property has been added to allow users to access it without using private attributes, and with that, the directory is created on first use of the property.
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 24 ++++++++++++++++-------- python/qemu/qtest.py | 6 +++--- tests/acceptance/virtio-gpu.py | 2 +- tests/qemu-iotests/iotests.py | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-)
Quoted 15 lines
python/qemu/machine.py class QEMUMachine: · line 87
Quoted 10 lines
python/qemu/machine.py +1
-1@@ -84,7 +84,7 @@ class QEMUMachine: args: Sequence[str] = (), wrapper: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp",Not this patch fault, but I see we use
/var/tmpsince commit 66613974468 ("scripts: refactor the VM class in iotests for reuse"). Can we use an OS agnostic method to get temp storage directory instead?
Sounds like a reasonable thing to do. I do remember a few issues with Python's tempfile.gettempdir() returning '/tmp' on most Linux/Unix, dur to the fact that '/tmp' is a tmpfs filesystem on most modern Linux systems.
Anyway, I agree that hardcoding '/var/tmp' needs to be reconsidered.
Cheers,
- Cleber.
Re: [PATCH 2/6] Python: expose QEMUMachine's temporary directory
In reply to Philippe Mathieu-DaudéQuoted 4 lines · Philippe Mathieu-Daudé
Not this patch fault, but I see we use
/var/tmpsince commit 66613974468 ("scripts: refactor the VM class in iotests for reuse"). Can we use an OS agnostic method to get temp storage directory instead?
As a follow-up.
Feel free to add wish-list things and other small nuisances to https://gitlab.com/jsnow/qemu/-/issues where I will, some day, migrate them to the real tracker when we get it set up.
For now, that's just where I'm trying to keep track of my own python to-dos.
--js
Re: [PATCH 2/6] Python: expose QEMUMachine's temporary directory
In reply to Cleber RosaHi,
Quoted 88 lines · Cleber Rosa
Each instance of qemu.machine.QEMUMachine currently has a "test directory", which may not have any relation to a "test", and it's really a temporary directory.
Users instantiating the QEMUMachine class will be able to set the location of the directory that will *contain* the QEMUMachine unique temporary directory, so that parameter name has been changed from
test_dirtobase_temp_dir.A property has been added to allow users to access it without using private attributes, and with that, the directory is created on first use of the property.
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 24 ++++++++++++++++-------- python/qemu/qtest.py | 6 +++--- tests/acceptance/virtio-gpu.py | 2 +- tests/qemu-iotests/iotests.py | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-)python/qemu/machine.py +16
-8@@ -84,7 +84,7 @@ class QEMUMachine: args: Sequence[str] = (), wrapper: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp", monitor_address: Optional[SocketAddrT] = None, socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None,@@ -97,10 +97,10 @@ class QEMUMachine: @param args: list of extra arguments @param wrapper: list of arguments used as prefix to qemu binary @param name: prefix for socket and log file names (default: qemu-PID) - @param test_dir: where to create socket and log file + @param base_temp_dir: default location where temporary files are created @param monitor_address: address for QMP monitor @param socket_scm_helper: helper program, required for send_fd_scm() - @param sock_dir: where to create socket (overrides test_dir for sock) + @param sock_dir: where to create socket (defaults to base_temp_dir) @param drain_console: (optional) True to drain console socket to buffer @param console_log: (optional) path to console log file @note: Qemu process is not started until launch() is used.@@ -112,8 +112,8 @@ class QEMUMachine: self._wrapper = wrapper self._name = name or "qemu-%d" % os.getpid() - self._test_dir = test_dir - self._sock_dir = sock_dir or self._test_dir + self._base_temp_dir = base_temp_dir + self._sock_dir = sock_dir or self._base_temp_dir self._socket_scm_helper = socket_scm_helper if monitor_address is not None:@@ -303,9 +303,7 @@ class QEMUMachine: return args def _pre_launch(self) -> None: - self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", - dir=self._test_dir) - self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log") + self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") self._qemu_log_file = open(self._qemu_log_path, 'wb') if self._console_set:@@ -744,3 +742,13 @@ class QEMUMachine: file=self._console_log_path, drain=self._drain_console) return self._console_socket + + @property + def temp_dir(self) -> str: + """ + Returns a temporary directory to be used for this machine + """ + if self._temp_dir is None: + self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", + dir=self._base_temp_dir) + return self._temp_dirpython/qemu/qtest.py +0
-0
python/qemu/qtest.py class QEMUQtestMachine(QEMUMachine): · line 115
Quoted 5 lines
python/qemu/qtest.py +1
-1@@ -112,14 +112,14 @@ class QEMUQtestMachine(QEMUMachine): binary: str, args: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp",
In qtest.QEMUQtestMachine.__init__(), the argument named 'test_dir' still make sense, right?
- Wainer
Quoted 38 lines
python/qemu/qtest.py +2
-2socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None): if name is None: name = "qemu-%d" % os.getpid() if sock_dir is None: - sock_dir = test_dir - super().__init__(binary, args, name=name, test_dir=test_dir, + sock_dir = base_temp_dir + super().__init__(binary, args, name=name, base_temp_dir=base_temp_dir, socket_scm_helper=socket_scm_helper, sock_dir=sock_dir) self._qtest: Optional[QEMUQtestProtocol] = Nonetests/acceptance/virtio-gpu.py +1
-1@@ -119,7 +119,7 @@ class VirtioGPUx86(Test): os.set_inheritable(vug_sock.fileno(), True) self._vug_log_path = os.path.join( - self.vm._test_dir, "vhost-user-gpu.log" + self.vm.temp_dir, "vhost-user-gpu.log" ) self._vug_log_file = open(self._vug_log_path, "wb") print(self._vug_log_path)tests/qemu-iotests/iotests.py +1
-1@@ -562,7 +562,7 @@ class VM(qtest.QEMUQtestMachine): def __init__(self, path_suffix=''): name = "qemu%s-%d" % (path_suffix, os.getpid()) super().__init__(qemu_prog, qemu_opts, name=name, - test_dir=test_dir, + base_temp_dir=test_dir, socket_scm_helper=socket_scm_helper, sock_dir=sock_dir) self._num_drives = 0
Re: [PATCH 2/6] Python: expose QEMUMachine's temporary directory
Quoted 4 lines · Wainer dos Santos Moschetta
In
qtest.QEMUQtestMachine.__init__(), the argument named 'test_dir' still make sense, right?- Wainer
It might upset pylint/mypy to rename parameters in the initializer for a parent class. If we rename it in the base class, we should rename it in the descendants, too.
(I say "might" because I have not yet worked out under the exact conditions that mypy will give you LSP warnings for initializer methods. It definitely doesn't always seem to, but I have run afoul of it enough times that I try to avoid it as a matter of habit now.)
Wainer dos Santos Moschetta
Reviewed-byRe: [PATCH 2/6] Python: expose QEMUMachine's temporary directory
Quoted 9 lines · John Snow
Quoted 4 lines · Wainer dos Santos Moschetta
In
qtest.QEMUQtestMachine.__init__(), the argument named 'test_dir' still make sense, right?- Wainer
It might upset pylint/mypy to rename parameters in the initializer for a parent class. If we rename it in the base class, we should rename it in the descendants, too.
(I say "might" because I have not yet worked out under the exact conditions that mypy will give you LSP warnings for initializer methods. It definitely doesn't always seem to, but I have run afoul of it enough times that I try to avoid it as a matter of habit now.)
Thanks for the explanation, John!
So Cleber just got another:
Quoted 3 lines
Re: [PATCH 2/6] Python: expose QEMUMachine's temporary directory
In reply to Cleber RosaQuoted 9 lines · Cleber Rosa
Each instance of qemu.machine.QEMUMachine currently has a "test directory", which may not have any relation to a "test", and it's really a temporary directory.
Users instantiating the QEMUMachine class will be able to set the location of the directory that will *contain* the QEMUMachine unique temporary directory, so that parameter name has been changed from
test_dirtobase_temp_dir.
Yeah, makes sense. It's a bad name.
Quoted 2 lines
A property has been added to allow users to access it without using private attributes,
OK
Quoted 3 lines
and with that, the directory is created on first
use of the property.
Less sure if I want this.
Quoted 60 lines
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 24 ++++++++++++++++-------- python/qemu/qtest.py | 6 +++--- tests/acceptance/virtio-gpu.py | 2 +- tests/qemu-iotests/iotests.py | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-)python/qemu/machine.py +6
-8@@ -84,7 +84,7 @@ class QEMUMachine: args: Sequence[str] = (), wrapper: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp", monitor_address: Optional[SocketAddrT] = None, socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None,@@ -97,10 +97,10 @@ class QEMUMachine: @param args: list of extra arguments @param wrapper: list of arguments used as prefix to qemu binary @param name: prefix for socket and log file names (default: qemu-PID) - @param test_dir: where to create socket and log file + @param base_temp_dir: default location where temporary files are created @param monitor_address: address for QMP monitor @param socket_scm_helper: helper program, required for send_fd_scm() - @param sock_dir: where to create socket (overrides test_dir for sock) + @param sock_dir: where to create socket (defaults to base_temp_dir) @param drain_console: (optional) True to drain console socket to buffer @param console_log: (optional) path to console log file @note: Qemu process is not started until launch() is used.@@ -112,8 +112,8 @@ class QEMUMachine: self._wrapper = wrapper self._name = name or "qemu-%d" % os.getpid() - self._test_dir = test_dir - self._sock_dir = sock_dir or self._test_dir + self._base_temp_dir = base_temp_dir + self._sock_dir = sock_dir or self._base_temp_dir self._socket_scm_helper = socket_scm_helper if monitor_address is not None:@@ -303,9 +303,7 @@ class QEMUMachine: return args def _pre_launch(self) -> None: - self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", - dir=self._test_dir) - self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log") + self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") self._qemu_log_file = open(self._qemu_log_path, 'wb') if self._console_set:@@ -744,3 +742,13 @@ class QEMUMachine: file=self._console_log_path, drain=self._drain_console) return self._console_socket
python/qemu/machine.py class QEMUMachine: · line 754
Quoted 10 lines
python/qemu/machine.py +10
-0+ + @property + def temp_dir(self) -> str: + """ + Returns a temporary directory to be used for this machine + """ + if self._temp_dir is None: + self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", + dir=self._base_temp_dir) + return self._temp_dir
Since this property has a side effect that will outlive the process, I think it ought to be a function (or ditch the side-effect.) The docstring should state that the directory will be created when this property is first accessed.
(I realize you do it here because until we create the directory, we cannot return any particular value. Internal cleanup routines will need to be careful NOT to call temp_dir, though!)
Quoted 41 lines
python/qemu/qtest.py +3
-3@@ -112,14 +112,14 @@ class QEMUQtestMachine(QEMUMachine): binary: str, args: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp", socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None): if name is None: name = "qemu-%d" % os.getpid() if sock_dir is None: - sock_dir = test_dir - super().__init__(binary, args, name=name, test_dir=test_dir, + sock_dir = base_temp_dir + super().__init__(binary, args, name=name, base_temp_dir=base_temp_dir, socket_scm_helper=socket_scm_helper, sock_dir=sock_dir) self._qtest: Optional[QEMUQtestProtocol] = Nonetests/acceptance/virtio-gpu.py +1
-1@@ -119,7 +119,7 @@ class VirtioGPUx86(Test): os.set_inheritable(vug_sock.fileno(), True) self._vug_log_path = os.path.join( - self.vm._test_dir, "vhost-user-gpu.log" + self.vm.temp_dir, "vhost-user-gpu.log" ) self._vug_log_file = open(self._vug_log_path, "wb") print(self._vug_log_path)tests/qemu-iotests/iotests.py +0
-0
tests/qemu-iotests/iotests.py class VM(qtest.QEMUQtestMachine): · line 568
Quoted 8 lines
tests/qemu-iotests/iotests.py +1
-1@@ -562,7 +562,7 @@ class VM(qtest.QEMUQtestMachine): def __init__(self, path_suffix=''): name = "qemu%s-%d" % (path_suffix, os.getpid()) super().__init__(qemu_prog, qemu_opts, name=name, - test_dir=test_dir, + base_temp_dir=test_dir, socket_scm_helper=socket_scm_helper, sock_dir=sock_dir) self._num_drives = 0
Seems OK otherwise, at a glance.
PATCH 3/6 Acceptance Tests: use the job work directory for created VMs
In reply to Cleber RosaThe QEMUMachine uses a base temporary directory for all temporary needs. By setting it to the Avocado's workdir, it's possible to keep the temporary files during debugging sessions much more easily by setting the "--keep-tmp" command line option.
Reference: https://avocado-framework.readthedocs.io/en/85.0/api/test/avocado.html#avocado.Test.workdir
Reference: https://avocado-framework.readthedocs.io/en/85.0/config/index.html#run-keep-tmp
--- tests/acceptance/avocado_qemu/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tests/acceptance/avocado_qemu/__init__.py +2 -1
@@ -172,7 +172,8 @@ class Test(avocado.Test): def _new_vm(self, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") - vm = QEMUMachine(self.qemu_bin, sock_dir=self._sd.name) + vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, + sock_dir=self._sd.name) if args: vm.add_args(*args) return vm
Signature · 1 line
2.25.4
Re: [PATCH 3/6] Acceptance Tests: use the job work directory for created VMs
Quoted 11 lines · Cleber Rosa
The QEMUMachine uses a base temporary directory for all temporary needs. By setting it to the Avocado's workdir, it's possible to keep the temporary files during debugging sessions much more easily by setting the "--keep-tmp" command line option.
Reference: https://avocado-framework.readthedocs.io/en/85.0/api/test/avocado.html#avocado.Test.workdir
Reference: https://avocado-framework.readthedocs.io/en/85.0/config/index.html#run-keep-tmpSigned-off-by Cleber Rosa <[email protected]>--- tests/acceptance/avocado_qemu/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
The changes look good to me, so:
But I got confused, the patch's subject states "use the job work directory" while the documentation of workdir says it is a "writable directory that exists during the entire test execution (...)". In the end is it a job or test work directory?
- Wainer
Quoted 15 lines
tests/acceptance/avocado_qemu/__init__.py +2
-1@@ -172,7 +172,8 @@ class Test(avocado.Test): def _new_vm(self, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") - vm = QEMUMachine(self.qemu_bin, sock_dir=self._sd.name) + vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, + sock_dir=self._sd.name) if args: vm.add_args(*args) return vm
Re: [PATCH 3/6] Acceptance Tests: use the job work directory for created VMs
In reply to Cleber RosaQuoted 5 lines · Cleber Rosa
The QEMUMachine uses a base temporary directory for all temporary needs. By setting it to the Avocado's workdir, it's possible to keep the temporary files during debugging sessions much more easily by setting the "--keep-tmp" command line option.
Makes sense, kinda-sorta.
Is this a band-aid?
QEMUMachine has this gem in _post_shutdown():
if self._temp_dir is not None:
shutil.rmtree(self._temp_dir)
self._temp_dir = None
we probably do want a way to adjust the deletion policy that QEMUMachine has. Kevin and I discussed this (extremely briefly) in January. One note is that the QEMU logs are read into memory when the process closes, so iotests et al have a chance to show you those logs on failure cases. Not relevant here for your purposes.
Meanwhile, we could also change the behavior of QEMUMachine, and create a temp dir deletion policy tunable:
(1) Always delete
(2) Never delete
(3) Delete on success (keep on failure)
(4) Delete on success and anticipated failures.
(About #4: QEMUMachine has a condition where it will not report AbornalShutdown if .kill() is called and the retcode is observed to be -SIGKILL. We treat this as a kind of success.)
These avocado tests could then just use a "never delete" policy, use the avocado runner's working dir, and then never worry about the cleanup.
iotests could do something similar with a temporary directory established by the top-level runner that we could modify the behavior of with --keep-files[ <on-failure|always>] or similar.
It might be time to just make this less stupidly annoying for all users of the library once and for all.
Quoted 13 lines
Reference: https://avocado-framework.readthedocs.io/en/85.0/api/test/avocado.html#avocado.Test.workdir
Reference: https://avocado-framework.readthedocs.io/en/85.0/config/index.html#run-keep-tmpSigned-off-by Cleber Rosa <[email protected]>--- tests/acceptance/avocado_qemu/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tests/acceptance/avocado_qemu/__init__.py +0
-0
tests/acceptance/avocado_qemu/__init__.py class Test(avocado.Test): · line 179
Quoted 9 lines
tests/acceptance/avocado_qemu/__init__.py +2
-1@@ -172,7 +172,8 @@ class Test(avocado.Test): def _new_vm(self, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") - vm = QEMUMachine(self.qemu_bin, sock_dir=self._sd.name) + vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, + sock_dir=self._sd.name) if args: vm.add_args(*args) return vm
But, you know, absent all that extra work, sure:
PATCH 4/6 Acceptance Tests: log information when creating QEMUMachine
In reply to Cleber RosaIncluding its base temporary directory, given that information useful for debugging can be put there.
--- tests/acceptance/avocado_qemu/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tests/acceptance/avocado_qemu/__init__.py +4 -2
@@ -170,10 +170,12 @@ class Test(avocado.Test): if self.qemu_bin is None: self.cancel("No QEMU binary defined or found in the build tree") - def _new_vm(self, *args): + def _new_vm(self, name, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, sock_dir=self._sd.name) + self.log.debug('QEMUMachine "%s" created', name) + self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) if args: vm.add_args(*args) return vm
@@ -186,7 +188,7 @@ class Test(avocado.Test): if not name: name = str(uuid.uuid4()) if self._vms.get(name) is None: - self._vms[name] = self._new_vm(*args) + self._vms[name] = self._new_vm(name, *args) if self.machine is not None: self._vms[name].set_machine(self.machine) return self._vms[name]
Signature · 1 line
2.25.4
Re: [PATCH 4/6] Acceptance Tests: log information when creating QEMUMachine
Quoted 7 lines · Cleber Rosa
Including its base temporary directory, given that information useful for debugging can be put there.
Signed-off-by Cleber Rosa <[email protected]>--- tests/acceptance/avocado_qemu/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 28 lines
tests/acceptance/avocado_qemu/__init__.py +4
-2@@ -170,10 +170,12 @@ class Test(avocado.Test): if self.qemu_bin is None: self.cancel("No QEMU binary defined or found in the build tree") - def _new_vm(self, *args): + def _new_vm(self, name, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, sock_dir=self._sd.name) + self.log.debug('QEMUMachine "%s" created', name) + self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) if args: vm.add_args(*args) return vm@@ -186,7 +188,7 @@ class Test(avocado.Test): if not name: name = str(uuid.uuid4()) if self._vms.get(name) is None: - self._vms[name] = self._new_vm(*args) + self._vms[name] = self._new_vm(name, *args) if self.machine is not None: self._vms[name].set_machine(self.machine) return self._vms[name]
PATCH 5/6 Acceptance Tests: distinguish between temp and logs dir
In reply to Cleber RosaLogs can be very important to debug issues, and currently QEMUMachine instances will remove logs that are created under the temporary directories.
With this change, the stdout and stderr generated by the QEMU process started by QEMUMachine will always be kept along the test results directory.
--- python/qemu/machine.py | 16 ++++++++++++++-- tests/acceptance/avocado_qemu/__init__.py | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-)
python/qemu/machine.py +14 -2
@@ -89,7 +89,8 @@ class QEMUMachine: socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None, drain_console: bool = False, - console_log: Optional[str] = None): + console_log: Optional[str] = None, + log_dir: Optional[str] = None): ''' Initialize a QEMUMachine
@@ -103,6 +104,7 @@ class QEMUMachine: @param sock_dir: where to create socket (defaults to base_temp_dir) @param drain_console: (optional) True to drain console socket to buffer @param console_log: (optional) path to console log file + @param log_dir: where to create and keep log files @note: Qemu process is not started until launch() is used. ''' # Direct user configuration
@@ -114,6 +116,7 @@ class QEMUMachine: self._name = name or "qemu-%d" % os.getpid() self._base_temp_dir = base_temp_dir self._sock_dir = sock_dir or self._base_temp_dir + self._log_dir = log_dir self._socket_scm_helper = socket_scm_helper if monitor_address is not None:
@@ -303,7 +306,7 @@ class QEMUMachine: return args def _pre_launch(self) -> None: - self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") + self._qemu_log_path = os.path.join(self.log_dir, self._name + ".log") self._qemu_log_file = open(self._qemu_log_path, 'wb') if self._console_set:
@@ -752,3 +755,12 @@ class QEMUMachine: self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", dir=self._base_temp_dir) return self._temp_dir + + @property + def log_dir(self) -> str: + """ + Returns a directory to be used for writing logs + """ + if self._log_dir is None: + return self.temp_dir + return self._log_dir
tests/acceptance/avocado_qemu/__init__.py +2 -1
@@ -173,9 +173,10 @@ class Test(avocado.Test): def _new_vm(self, name, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, - sock_dir=self._sd.name) + sock_dir=self._sd.name, log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name) self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) + self.log.debug('QEMUMachine "%s" log_dir: %s', name, vm.log_dir) if args: vm.add_args(*args) return vm
Signature · 1 line
2.25.4
Re: [PATCH 5/6] Acceptance Tests: distinguish between temp and logs dir
Hi,
Quoted 29 lines · Cleber Rosa
Logs can be very important to debug issues, and currently QEMUMachine instances will remove logs that are created under the temporary directories.
With this change, the stdout and stderr generated by the QEMU process started by QEMUMachine will always be kept along the test results directory.
Signed-off-by Cleber Rosa <[email protected]>--- python/qemu/machine.py | 16 ++++++++++++++-- tests/acceptance/avocado_qemu/__init__.py | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-)python/qemu/machine.py +2
-1@@ -89,7 +89,8 @@ class QEMUMachine: socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None, drain_console: bool = False, - console_log: Optional[str] = None): + console_log: Optional[str] = None, + log_dir: Optional[str] = None): ''' Initialize a QEMUMachine
python/qemu/machine.py class QEMUMachine: · line 107
Quoted 4 lines
python/qemu/machine.py +1
-0@@ -103,6 +104,7 @@ class QEMUMachine: @param sock_dir: where to create socket (defaults to base_temp_dir) @param drain_console: (optional) True to drain console socket to buffer @param console_log: (optional) path to console log file + @param log_dir: where to create and keep log files
"(optional) where to create and keep (...)".
You could also say it defaults to the temp dir, thus logs aren't kept guaranteed.
Otherwise,
Quoted 49 lines
python/qemu/machine.py +11
-1@note: Qemu process is not started until launch() is used. ''' # Direct user configuration@@ -114,6 +116,7 @@ class QEMUMachine: self._name = name or "qemu-%d" % os.getpid() self._base_temp_dir = base_temp_dir self._sock_dir = sock_dir or self._base_temp_dir + self._log_dir = log_dir self._socket_scm_helper = socket_scm_helper if monitor_address is not None:@@ -303,7 +306,7 @@ class QEMUMachine: return args def _pre_launch(self) -> None: - self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") + self._qemu_log_path = os.path.join(self.log_dir, self._name + ".log") self._qemu_log_file = open(self._qemu_log_path, 'wb') if self._console_set:@@ -752,3 +755,12 @@ class QEMUMachine: self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", dir=self._base_temp_dir) return self._temp_dir + + @property + def log_dir(self) -> str: + """ + Returns a directory to be used for writing logs + """ + if self._log_dir is None: + return self.temp_dir + return self._log_dirtests/acceptance/avocado_qemu/__init__.py +2
-1@@ -173,9 +173,10 @@ class Test(avocado.Test): def _new_vm(self, name, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, - sock_dir=self._sd.name) + sock_dir=self._sd.name, log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name) self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) + self.log.debug('QEMUMachine "%s" log_dir: %s', name, vm.log_dir) if args: vm.add_args(*args) return vm
PATCH 6/6 tests/acceptance/virtio-gpu.py: preserve virtio-user-gpu log
In reply to Cleber RosaAt location already prepared for keeping the test's log files.
While at it, log info about its location (in the main test log file), instead of printing it out.
Reference: https://avocado-framework.readthedocs.io/en/85.0/api/test/avocado.html#avocado.Test.logdir
--- tests/acceptance/virtio-gpu.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tests/acceptance/virtio-gpu.py +3 -2
@@ -119,10 +119,11 @@ class VirtioGPUx86(Test): os.set_inheritable(vug_sock.fileno(), True) self._vug_log_path = os.path.join( - self.vm.temp_dir, "vhost-user-gpu.log" + self.logdir, "vhost-user-gpu.log" ) self._vug_log_file = open(self._vug_log_path, "wb") - print(self._vug_log_path) + self.log.info('Complete vhost-user-gpu.log file can be ' + 'found at %s', self._vug_log_path) vugp = subprocess.Popen( [vug, "--virgl", "--fd=%d" % vug_sock.fileno()],
Signature · 1 line
2.25.4
Re: [PATCH 6/6] tests/acceptance/virtio-gpu.py: preserve virtio-user-gpu log
On 2/11/21 11:01 PM, Cleber Rosa wrote:
"Preserve log" ...
Quoted 10 lines
At location already prepared for keeping the test's log files.
While at it, log info about its location (in the main test log file), instead of printing it out.
Reference: https://avocado-framework.readthedocs.io/en/85.0/api/test/avocado.html#avocado.Test.logdir
Signed-off-by Cleber Rosa <[email protected]>--- tests/acceptance/virtio-gpu.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Using full sentence:
Re: [PATCH 6/6] tests/acceptance/virtio-gpu.py: preserve virtio-user-gpu log
In reply to Cleber RosaQuoted 10 lines · Cleber Rosa
At location already prepared for keeping the test's log files.
While at it, log info about its location (in the main test log file), instead of printing it out.
Reference: https://avocado-framework.readthedocs.io/en/85.0/api/test/avocado.html#avocado.Test.logdir
Signed-off-by Cleber Rosa <[email protected]>--- tests/acceptance/virtio-gpu.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 19 lines
tests/acceptance/virtio-gpu.py +3
-2@@ -119,10 +119,11 @@ class VirtioGPUx86(Test): os.set_inheritable(vug_sock.fileno(), True) self._vug_log_path = os.path.join( - self.vm.temp_dir, "vhost-user-gpu.log" + self.logdir, "vhost-user-gpu.log" ) self._vug_log_file = open(self._vug_log_path, "wb") - print(self._vug_log_path) + self.log.info('Complete vhost-user-gpu.log file can be ' + 'found at %s', self._vug_log_path) vugp = subprocess.Popen( [vug, "--virgl", "--fd=%d" % vug_sock.fileno()],