[PATCH v3 03/24] tests/functional: Set up logging

Daniel P. Berrangé posted 24 patches 3 months, 3 weeks ago
There is a newer version of this series
[PATCH v3 03/24] tests/functional: Set up logging
Posted by Daniel P. Berrangé 3 months, 3 weeks ago
From: Thomas Huth <thuth@redhat.com>

Create log files for each test separately, one file that contains
the basic logging and one that contains the console output.

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 tests/functional/qemu_test/testcase.py | 27 +++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
index 82cc1d454f..27bbf4a0af 100644
--- a/tests/functional/qemu_test/testcase.py
+++ b/tests/functional/qemu_test/testcase.py
@@ -31,7 +31,8 @@ class QemuBaseTest(unittest.TestCase):
     arch = None
 
     workdir = None
-    log = logging.getLogger('qemu-test')
+    log = None
+    logdir = None
 
     def setUp(self, bin_prefix):
         self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set')
@@ -42,6 +43,20 @@ def setUp(self, bin_prefix):
         if not os.path.exists(self.workdir):
             os.makedirs(self.workdir)
 
+        self.logdir = self.workdir
+        self.log = logging.getLogger('qemu-test')
+        self.log.setLevel(logging.DEBUG)
+        self._log_fh = logging.FileHandler(os.path.join(self.logdir,
+                                                        'base.log'), mode='w')
+        self._log_fh.setLevel(logging.DEBUG)
+        fileFormatter = logging.Formatter(
+            '%(asctime)s - %(levelname)s: %(message)s')
+        self._log_fh.setFormatter(fileFormatter)
+        self.log.addHandler(self._log_fh)
+
+    def tearDown(self):
+        self.log.removeHandler(self._log_fh)
+
     def main():
         tr = pycotap.TAPTestRunner(message_log = pycotap.LogMode.LogToError,
                                    test_output_log = pycotap.LogMode.LogToError)
@@ -61,6 +76,15 @@ def setUp(self):
 
         super().setUp('qemu-system-')
 
+        console_log = logging.getLogger('console')
+        console_log.setLevel(logging.DEBUG)
+        self._console_log_fh = logging.FileHandler(os.path.join(self.workdir,
+                                                   'console.log'), mode='w')
+        self._console_log_fh.setLevel(logging.DEBUG)
+        fileFormatter = logging.Formatter('%(asctime)s: %(message)s')
+        self._console_log_fh.setFormatter(fileFormatter)
+        console_log.addHandler(self._console_log_fh)
+
     def set_machine(self, machinename):
         # TODO: We should use QMP to get the list of available machines
         if not self._machinehelp:
@@ -151,4 +175,5 @@ def set_vm_arg(self, arg, value):
     def tearDown(self):
         for vm in self._vms.values():
             vm.shutdown()
+        logging.getLogger('console').removeHandler(self._console_log_fh)
         super().tearDown()
-- 
2.45.2


Re: [PATCH v3 03/24] tests/functional: Set up logging
Posted by Alex Bennée 3 months, 3 weeks ago
Daniel P. Berrangé <berrange@redhat.com> writes:

> From: Thomas Huth <thuth@redhat.com>
>
> Create log files for each test separately, one file that contains
> the basic logging and one that contains the console output.
>
> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  tests/functional/qemu_test/testcase.py | 27 +++++++++++++++++++++++++-
>  1 file changed, 26 insertions(+), 1 deletion(-)
>
> diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
> index 82cc1d454f..27bbf4a0af 100644
> --- a/tests/functional/qemu_test/testcase.py
> +++ b/tests/functional/qemu_test/testcase.py
> @@ -31,7 +31,8 @@ class QemuBaseTest(unittest.TestCase):
>      arch = None
>  
>      workdir = None
> -    log = logging.getLogger('qemu-test')
> +    log = None
> +    logdir = None
>  
>      def setUp(self, bin_prefix):
>          self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set')
> @@ -42,6 +43,20 @@ def setUp(self, bin_prefix):
>          if not os.path.exists(self.workdir):
>              os.makedirs(self.workdir)
>  
> +        self.logdir = self.workdir
> +        self.log = logging.getLogger('qemu-test')
> +        self.log.setLevel(logging.DEBUG)
> +        self._log_fh = logging.FileHandler(os.path.join(self.logdir,
> +                                                        'base.log'), mode='w')
> +        self._log_fh.setLevel(logging.DEBUG)
> +        fileFormatter = logging.Formatter(
> +            '%(asctime)s - %(levelname)s: %(message)s')
> +        self._log_fh.setFormatter(fileFormatter)
> +        self.log.addHandler(self._log_fh)

This is in the wrong place, we have a unique machine directory for each
run, the console.log should go in there.

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro
Re: [PATCH v3 03/24] tests/functional: Set up logging
Posted by Thomas Huth 3 months, 1 week ago
On 01/08/2024 12.10, Alex Bennée wrote:
> Daniel P. Berrangé <berrange@redhat.com> writes:
> 
>> From: Thomas Huth <thuth@redhat.com>
>>
>> Create log files for each test separately, one file that contains
>> the basic logging and one that contains the console output.
>>
>> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
>> Signed-off-by: Thomas Huth <thuth@redhat.com>
>> ---
>>   tests/functional/qemu_test/testcase.py | 27 +++++++++++++++++++++++++-
>>   1 file changed, 26 insertions(+), 1 deletion(-)
>>
>> diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
>> index 82cc1d454f..27bbf4a0af 100644
>> --- a/tests/functional/qemu_test/testcase.py
>> +++ b/tests/functional/qemu_test/testcase.py
>> @@ -31,7 +31,8 @@ class QemuBaseTest(unittest.TestCase):
>>       arch = None
>>   
>>       workdir = None
>> -    log = logging.getLogger('qemu-test')
>> +    log = None
>> +    logdir = None
>>   
>>       def setUp(self, bin_prefix):
>>           self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set')
>> @@ -42,6 +43,20 @@ def setUp(self, bin_prefix):
>>           if not os.path.exists(self.workdir):
>>               os.makedirs(self.workdir)
>>   
>> +        self.logdir = self.workdir
>> +        self.log = logging.getLogger('qemu-test')
>> +        self.log.setLevel(logging.DEBUG)
>> +        self._log_fh = logging.FileHandler(os.path.join(self.logdir,
>> +                                                        'base.log'), mode='w')
>> +        self._log_fh.setLevel(logging.DEBUG)
>> +        fileFormatter = logging.Formatter(
>> +            '%(asctime)s - %(levelname)s: %(message)s')
>> +        self._log_fh.setFormatter(fileFormatter)
>> +        self.log.addHandler(self._log_fh)
> 
> This is in the wrong place, we have a unique machine directory for each
> run, the console.log should go in there.

self.workdir already points to the unique directory of the test (see second 
patch), so setting logdir = workdir should be fine, I think?

  Thomas