[v2,4/5] CI: Allow external usage for some APIs

Message ID 20240617150901.55853-5-amikan@ilbers.de
State Superseded, archived
Headers show
Series Remove code duplications for start_vm | expand

Commit Message

Anton Mikanovich June 17, 2024, 3:09 p.m. UTC
Provide utilities class for external usage to be imported from outside
of Avocado framework.

Signed-off-by: Anton Mikanovich <amikan@ilbers.de>
---
 testsuite/cibase.py    |  6 +++--
 testsuite/cibuilder.py | 50 ++++----------------------------------
 testsuite/citest.py    |  4 +++-
 testsuite/utils.py     | 54 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 66 insertions(+), 48 deletions(-)
 create mode 100755 testsuite/utils.py

Patch

diff --git a/testsuite/cibase.py b/testsuite/cibase.py
index 0f6997af..c257a465 100755
--- a/testsuite/cibase.py
+++ b/testsuite/cibase.py
@@ -8,6 +8,8 @@  import tempfile
 import time
 
 from cibuilder import CIBuilder, isar_root
+from utils import CIUtils
+
 from avocado.utils import process
 
 class CIBaseTest(CIBuilder):
@@ -238,13 +240,13 @@  class CIBaseTest(CIBuilder):
             for target in targets:
                 sfiles[target] = dict()
                 package = target.rsplit(':', 1)[-1]
-                isar_apt = self.getVars('REPO_ISAR_DB_DIR', target=target)
+                isar_apt = CIUtils.getVars('REPO_ISAR_DB_DIR', target=target)
                 fpath = f'{package}/{package}*.tar.gz'
                 targz = set(glob.glob(f'{isar_apt}/../apt/*/pool/*/*/{fpath}'))
                 if len(targz) < 1:
                     self.fail('No source packages found')
                 for filename in targz:
-                    sfiles[target][filename] = self.get_tar_content(filename)
+                    sfiles[target][filename] = CIUtils.get_tar_content(filename)
             return sfiles
 
         self.configure(**kwargs)
diff --git a/testsuite/cibuilder.py b/testsuite/cibuilder.py
index a51d6f7e..4aa06ffb 100755
--- a/testsuite/cibuilder.py
+++ b/testsuite/cibuilder.py
@@ -9,11 +9,11 @@  import shutil
 import signal
 import subprocess
 import sys
-import tarfile
 import time
 import tempfile
 
 import start_vm
+from utils import CIUtils
 
 from avocado import Test
 from avocado.utils import path
@@ -22,7 +22,6 @@  from avocado.utils import process
 sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/../bitbake/lib')
 
 import bb
-import bb.tinfoil
 
 DEF_VM_TO_SEC = 600
 
@@ -253,38 +252,6 @@  class CIBuilder(Test):
         except FileNotFoundError:
             self.log.warn(path + backup_prefix + ' not exist')
 
-    def getVars(self, *vars, target=None):
-        self.check_init()
-        def fixStream(stream):
-            # fix stream objects to emulate _io.TextIOWrapper
-            stream.isatty = lambda: False
-            stream.fileno = lambda: False
-            stream.encoding = sys.getdefaultencoding()
-
-        sl = target is not None
-        fixStream(sys.stdout)
-        fixStream(sys.stderr)
-
-        # wait until previous bitbake will be finished
-        lockfile = os.path.join(self.build_dir, 'bitbake.lock')
-        checks = 0
-        while os.path.exists(lockfile) and checks < 5:
-            time.sleep(1)
-            checks += 1
-
-        with bb.tinfoil.Tinfoil(setup_logging=sl) as tinfoil:
-            values = ()
-            if target:
-                tinfoil.prepare(quiet=2)
-                d = tinfoil.parse_recipe(target)
-                for var in vars:
-                    values = values + (d.getVar(var, True) or 'None',)
-            else:
-                tinfoil.prepare(config_only=True, quiet=2)
-                for var in vars:
-                    values = values + (tinfoil.config_data.getVar(var, True) or 'None',)
-            return values if len(values) > 1 else values[0]
-
     def create_tmp_layer(self):
         tmp_layer_dir = os.path.join(isar_root, 'meta-tmp')
 
@@ -314,13 +281,6 @@  BBPATH .= ":${LAYERDIR}"\
         bb.utils.edit_bblayers_conf(bblayersconf_file, None, tmp_layer_dir)
         bb.utils.prunedir(tmp_layer_dir)
 
-    def get_tar_content(self, filename):
-        try:
-            tar = tarfile.open(filename)
-            return tar.getnames()
-        except Exception:
-            return []
-
     def get_ssh_cmd_prefix(self, user, host, port, priv_key):
         cmd_prefix = 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no '\
                      '-p %s -o IdentityFile=%s %s@%s ' \
@@ -526,10 +486,10 @@  BBPATH .= ":${LAYERDIR}"\
         resize_output = None
         image_fstypes, \
         wks_file, \
-        bbdistro = self.getVars('IMAGE_FSTYPES', \
-                                'WKS_FILE', \
-                                'DISTRO', \
-                                target=multiconfig)
+        bbdistro = CIUtils.getVars('IMAGE_FSTYPES', \
+                                   'WKS_FILE', \
+                                   'DISTRO', \
+                                   target=multiconfig)
 
         # only the first type will be tested in start_vm
         if image_fstypes.split()[0] == 'wic':
diff --git a/testsuite/citest.py b/testsuite/citest.py
index 42d44f6a..b84ae0e1 100755
--- a/testsuite/citest.py
+++ b/testsuite/citest.py
@@ -5,6 +5,7 @@  import os
 from avocado import skipUnless
 from avocado.utils import path
 from cibase import CIBaseTest
+from utils import CIUtils
 
 UMOCI_AVAILABLE = True
 SKOPEO_AVAILABLE = True
@@ -17,6 +18,7 @@  try:
 except path.CmdNotFoundError:
     SKOPEO_AVAILABLE = False
 
+
 class DevTest(CIBaseTest):
 
     """
@@ -46,7 +48,7 @@  class DevTest(CIBaseTest):
 
     def test_dev_rebuild(self):
         self.init()
-        layerdir_core = self.getVars('LAYERDIR_core')
+        layerdir_core = CIUtils.getVars('LAYERDIR_core')
 
         dpkgbase_file = layerdir_core + '/classes/dpkg-base.bbclass'
 
diff --git a/testsuite/utils.py b/testsuite/utils.py
new file mode 100755
index 00000000..a3f643fa
--- /dev/null
+++ b/testsuite/utils.py
@@ -0,0 +1,54 @@ 
+#!/usr/bin/env python3
+
+import os
+import sys
+import tarfile
+import time
+
+sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/../bitbake/lib')
+
+import bb
+import bb.tinfoil
+
+class CIUtils():
+    @staticmethod
+    def getVars(*vars, target=None):
+        def fixStream(stream):
+            # fix stream objects to emulate _io.TextIOWrapper
+            stream.isatty = lambda: False
+            stream.fileno = lambda: False
+            stream.encoding = sys.getdefaultencoding()
+
+        sl = target is not None
+        if not hasattr(sys.stdout, 'isatty'):
+            fixStream(sys.stdout)
+        if not hasattr(sys.stderr, 'isatty'):
+            fixStream(sys.stderr)
+
+        # wait until previous bitbake will be finished
+        lockfile = os.path.join(os.getcwd(), 'bitbake.lock')
+        checks = 0
+        while os.path.exists(lockfile) and checks < 5:
+            time.sleep(1)
+            checks += 1
+
+        with bb.tinfoil.Tinfoil(setup_logging=sl) as tinfoil:
+            values = ()
+            if target:
+                tinfoil.prepare(quiet=2)
+                d = tinfoil.parse_recipe(target)
+                for var in vars:
+                    values = values + (d.getVar(var, True) or '',)
+            else:
+                tinfoil.prepare(config_only=True, quiet=2)
+                for var in vars:
+                    values = values + (tinfoil.config_data.getVar(var, True) or '',)
+            return values if len(values) > 1 else values[0]
+
+    @staticmethod
+    def get_tar_content(filename):
+        try:
+            tar = tarfile.open(filename)
+            return tar.getnames()
+        except Exception:
+            return []