[PATCH 08/22] tests/functional: add helpers for building file paths

Daniel P. Berrangé posted 22 patches 3 weeks, 5 days ago
There is a newer version of this series
[PATCH 08/22] tests/functional: add helpers for building file paths
Posted by Daniel P. Berrangé 3 weeks, 5 days ago
Add helper methods that construct paths for

 * log files - to be preserved at the end of a test
 * scratch files - to be purged at the end of a test
 * build files - anything relative to the build root
 * data files - anything relative to the functional test source root
 * socket files - a short temporary dir to avoid UNIX socket limits

These are to be used instead of direct access to the self.workdir,
or self.logdir variables, or any other place where paths are built
manually.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 tests/functional/qemu_test/testcase.py | 86 ++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
index 90ae59eb54..fb62052817 100644
--- a/tests/functional/qemu_test/testcase.py
+++ b/tests/functional/qemu_test/testcase.py
@@ -13,10 +13,12 @@
 
 import logging
 import os
+from pathlib import Path
 import pycotap
 import shutil
 import subprocess
 import sys
+import tempfile
 import unittest
 import uuid
 
@@ -37,9 +39,93 @@ class QemuBaseTest(unittest.TestCase):
     log = None
     logdir = None
 
+    def socket_dir(self):
+        if self.socketdir is None:
+            self.socketdir = tempfile.TemporaryDirectory(
+                prefix="qemu_func_test_sock_")
+        return self.socketdir
+
+    '''
+    @params args list of zero or more subdirectories or file
+
+    Construct a path for accessing a data file located
+    relative to the source directory that is the root for
+    functional tests.
+
+    @args may be an empty list to reference the root dir
+    itself, may be a single element to reference a file in
+    the root directory, or may be multiple elements to
+    reference a file nested below. The path components
+    will be joined using the platform appropriate path
+    separator.
+
+    Returns: a qualified path
+    '''
+    def data_file(self, *args):
+        return str(Path(Path(__file__).parent.parent, *args))
+
+    '''
+    @params args list of zero or more subdirectories or file
+
+    Construct a path for accessing a data file located
+    relative to the build directory root.
+
+    @args may be an empty list to reference the build dir
+    itself, may be a single element to reference a file in
+    the build directory, or may be multiple elements to
+    reference a file nested below. The path components
+    will be joined using the platform appropriate path
+    separator.
+
+    Returns: a qualified path
+    '''
+    def build_file(self, *args):
+        return str(Path(BUILD_DIR, *args))
+
+    '''
+    @params args list of zero or more subdirectories or file
+
+    Construct a path for accessing/creating a scratch file
+    located relative to a temporary directory dedicated to
+    this test case. The directory and its contents will be
+    purged upon completion of the test.
+
+    @args may be an empty list to reference the scratch dir
+    itself, may be a single element to reference a file in
+    the scratch directory, or may be multiple elements to
+    reference a file nested below. The path components
+    will be joined using the platform appropriate path
+    separator.
+
+    Returns: a qualified path
+    '''
+    def scratch_file(self, *args):
+        return str(Path(self.workdir, *args))
+
+    '''
+    @params args list of zero or more subdirectories or file
+
+    Construct a path for accessing/creating a log file
+    located relative to a temporary directory dedicated to
+    this test case. The directory and its log files will be
+    preserved upon completion of the test.
+
+    @args may be an empty list to reference the log dir
+    itself, may be a single element to reference a file in
+    the log directory, or may be multiple elements to
+    reference a file nested below. The path components
+    will be joined using the platform appropriate path
+    separator.
+
+    Returns: a pathlib.Path object
+    '''
+    def log_file(self, *args):
+        return str(Path(self.logdir, *args))
+
     def setUp(self, bin_prefix):
         self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set')
         self.arch = self.qemu_bin.split('-')[-1]
+        self.socketdir = None
 
         self.outputdir = os.path.join(BUILD_DIR, 'tests', 'functional',
                                       self.arch, self.id())
-- 
2.46.0


Re: [PATCH 08/22] tests/functional: add helpers for building file paths
Posted by Thomas Huth 3 weeks, 3 days ago
On 29/11/2024 18.31, Daniel P. Berrangé wrote:
> Add helper methods that construct paths for
> 
>   * log files - to be preserved at the end of a test
>   * scratch files - to be purged at the end of a test
>   * build files - anything relative to the build root
>   * data files - anything relative to the functional test source root
>   * socket files - a short temporary dir to avoid UNIX socket limits
> 
> These are to be used instead of direct access to the self.workdir,
> or self.logdir variables, or any other place where paths are built
> manually.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   tests/functional/qemu_test/testcase.py | 86 ++++++++++++++++++++++++++
>   1 file changed, 86 insertions(+)
> 
> diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
> index 90ae59eb54..fb62052817 100644
> --- a/tests/functional/qemu_test/testcase.py
> +++ b/tests/functional/qemu_test/testcase.py
> @@ -13,10 +13,12 @@
>   
>   import logging
>   import os
> +from pathlib import Path
>   import pycotap
>   import shutil
>   import subprocess
>   import sys
> +import tempfile
>   import unittest
>   import uuid
>   
> @@ -37,9 +39,93 @@ class QemuBaseTest(unittest.TestCase):
>       log = None
>       logdir = None
>   
> +    def socket_dir(self):
> +        if self.socketdir is None:
> +            self.socketdir = tempfile.TemporaryDirectory(
> +                prefix="qemu_func_test_sock_")
> +        return self.socketdir
> +
> +    '''
> +    @params args list of zero or more subdirectories or file
> +
> +    Construct a path for accessing a data file located
> +    relative to the source directory that is the root for
> +    functional tests.
> +
> +    @args may be an empty list to reference the root dir
> +    itself, may be a single element to reference a file in
> +    the root directory, or may be multiple elements to
> +    reference a file nested below. The path components
> +    will be joined using the platform appropriate path
> +    separator.
> +
> +    Returns: a qualified path
> +    '''
> +    def data_file(self, *args):
> +        return str(Path(Path(__file__).parent.parent, *args))
> +
> +    '''
> +    @params args list of zero or more subdirectories or file
> +
> +    Construct a path for accessing a data file located
> +    relative to the build directory root.
> +
> +    @args may be an empty list to reference the build dir
> +    itself, may be a single element to reference a file in
> +    the build directory, or may be multiple elements to
> +    reference a file nested below. The path components
> +    will be joined using the platform appropriate path
> +    separator.
> +
> +    Returns: a qualified path
> +    '''
> +    def build_file(self, *args):
> +        return str(Path(BUILD_DIR, *args))
> +
> +    '''
> +    @params args list of zero or more subdirectories or file
> +
> +    Construct a path for accessing/creating a scratch file
> +    located relative to a temporary directory dedicated to
> +    this test case. The directory and its contents will be
> +    purged upon completion of the test.
> +
> +    @args may be an empty list to reference the scratch dir
> +    itself, may be a single element to reference a file in
> +    the scratch directory, or may be multiple elements to
> +    reference a file nested below. The path components
> +    will be joined using the platform appropriate path
> +    separator.
> +
> +    Returns: a qualified path
> +    '''
> +    def scratch_file(self, *args):
> +        return str(Path(self.workdir, *args))
> +
> +    '''
> +    @params args list of zero or more subdirectories or file
> +
> +    Construct a path for accessing/creating a log file
> +    located relative to a temporary directory dedicated to
> +    this test case. The directory and its log files will be
> +    preserved upon completion of the test.
> +
> +    @args may be an empty list to reference the log dir
> +    itself, may be a single element to reference a file in
> +    the log directory, or may be multiple elements to
> +    reference a file nested below. The path components
> +    will be joined using the platform appropriate path
> +    separator.
> +
> +    Returns: a pathlib.Path object

Looks like it is rather returning a string?

> +    '''
> +    def log_file(self, *args):
> +        return str(Path(self.logdir, *args))
> +
>       def setUp(self, bin_prefix):
>           self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set')
>           self.arch = self.qemu_bin.split('-')[-1]
> +        self.socketdir = None

Should we also delete the socketdir during teardown again?

  Thomas


Re: [PATCH 08/22] tests/functional: add helpers for building file paths
Posted by Daniel P. Berrangé 3 weeks, 1 day ago
On Mon, Dec 02, 2024 at 10:19:47AM +0100, Thomas Huth wrote:
> On 29/11/2024 18.31, Daniel P. Berrangé wrote:
> > Add helper methods that construct paths for
> > 
> >   * log files - to be preserved at the end of a test
> >   * scratch files - to be purged at the end of a test
> >   * build files - anything relative to the build root
> >   * data files - anything relative to the functional test source root
> >   * socket files - a short temporary dir to avoid UNIX socket limits
> > 
> > These are to be used instead of direct access to the self.workdir,
> > or self.logdir variables, or any other place where paths are built
> > manually.
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> >   tests/functional/qemu_test/testcase.py | 86 ++++++++++++++++++++++++++
> >   1 file changed, 86 insertions(+)

> > +    '''
> > +    def log_file(self, *args):
> > +        return str(Path(self.logdir, *args))
> > +
> >       def setUp(self, bin_prefix):
> >           self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set')
> >           self.arch = self.qemu_bin.split('-')[-1]
> > +        self.socketdir = None
> 
> Should we also delete the socketdir during teardown again?

Yes, we should be purging this object and deleting the dir


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|