public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: Uladzimir Bely <ubely@ilbers.de>
To: isar-users@googlegroups.com
Subject: [PATCH 5/8] cibuilder.py: Reuse the same qemu machine in ssh-based tests
Date: Fri, 18 Aug 2023 09:07:03 +0200	[thread overview]
Message-ID: <20230818070706.27913-6-ubely@ilbers.de> (raw)
In-Reply-To: <20230818070706.27913-1-ubely@ilbers.de>

Every ssh-based test starts new qemu machine, runs remote command
and closes the machine.

This patch allows not to close (kill) the machine started by first
test and reuse it to run remote commands in the following tests and
save some time.

Since qemu machine now remains running by default, the last test for
this particular machine should care about closing it by passing
additonal `stop_vm=True` parameter to `vm_start()` function.

Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
---
 testsuite/cibuilder.py | 75 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 14 deletions(-)

diff --git a/testsuite/cibuilder.py b/testsuite/cibuilder.py
index c0bfb75c..73c8ee2c 100755
--- a/testsuite/cibuilder.py
+++ b/testsuite/cibuilder.py
@@ -2,9 +2,11 @@
 
 import logging
 import os
+import pickle
 import re
 import select
 import shutil
+import signal
 import subprocess
 import time
 import tempfile
@@ -52,6 +54,15 @@ class CIBuilder(Test):
                     for x in output.splitlines() if x != ''))
         os.environ.update(env)
 
+        self.vm_dict = {}
+        self.vm_dict_file = '%s/vm_dict_file' % self.build_dir
+
+        if os.path.isfile(self.vm_dict_file):
+            with open(self.vm_dict_file, "rb") as f:
+                data = f.read()
+                if data:
+                    self.vm_dict = pickle.loads(data)
+
     def check_init(self):
         if not hasattr(self, 'build_dir'):
             self.error("Broken test implementation: need to call init().")
@@ -432,17 +443,26 @@ class CIBuilder(Test):
         return rc
 
 
-    def vm_turn_off(self, p1):
-        if p1.poll() is None:
-            p1.kill()
-        p1.wait()
+    def vm_dump_dict(self, vm):
+        f = open(self.vm_dict_file, "wb")
+        pickle.dump(self.vm_dict, f)
+        f.close()
+
 
-        self.log.debug("Stopped VM with pid %s" % (p1.pid))
+    def vm_turn_off(self, vm):
+        pid = self.vm_dict[vm][0]
+        os.kill(pid, signal.SIGKILL)
+
+        del(self.vm_dict[vm])
+        self.vm_dump_dict(vm)
+
+        self.log.debug("Stopped VM with pid %s" % (pid))
 
 
     def vm_start(self, arch='amd64', distro='buster',
                  enforce_pcbios=False, skip_modulecheck=False,
-                 image='isar-image-base', cmd=None, script=None):
+                 image='isar-image-base', cmd=None, script=None,
+                 stop_vm=False):
         time_to_wait = self.params.get('time_to_wait', default=DEF_VM_TO_SEC)
 
         self.log.info('===================================================')
@@ -456,12 +476,36 @@ class CIBuilder(Test):
 
         timeout = time.time() + int(time_to_wait)
 
-        p1, cmdline, boot_log = self.vm_turn_on(arch, distro, image, enforce_pcbios)
+        vm = "%s_%s_%s_%d" % (arch, distro, image, enforce_pcbios)
+
+        p1 = None
+        pid = None
+        cmdline = ""
+        boot_log = ""
 
-        rc = self.vm_wait_boot(p1, timeout)
-        if rc != 0:
-            self.vm_turn_off(p1)
-            self.fail('Failed to boot qemu machine')
+        run_qemu = True
+
+        if vm in self.vm_dict:
+            pid, cmdline, boot_log = self.vm_dict[vm]
+
+            # Check that corresponding process exists
+            proc = subprocess.run("ps -o cmd= %d" % (pid), shell=True, text=True,
+                                  stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+            if cmdline[0] in proc.stdout:
+                self.log.debug("Found '%s' process with pid '%d', use it" % (cmdline[0], pid))
+                run_qemu = False
+
+        if run_qemu:
+            self.log.debug("No qemu-system process for `%s` found, run new VM" % (vm))
+
+            p1, cmdline, boot_log = self.vm_turn_on(arch, distro, image, enforce_pcbios)
+            self.vm_dict[vm] = p1.pid, cmdline, boot_log
+            self.vm_dump_dict(vm)
+
+            rc = self.vm_wait_boot(p1, timeout)
+            if rc != 0:
+                self.vm_turn_off(vm)
+                self.fail('Failed to boot qemu machine')
 
         if cmd is not None or script is not None:
             user='ci'
@@ -474,13 +518,16 @@ class CIBuilder(Test):
                     break
             rc = self.remote_run(user, host, port, cmd, script, timeout)
             if rc != 0:
-                self.vm_turn_off(p1)
+                if stop_vm:
+                    self.vm_turn_off(vm)
                 self.fail('Failed to run test over ssh')
         else:
             bb_output = start_vm.get_bitbake_env(arch, distro, image).decode()
             rc = self.vm_parse_output(boot_log, bb_output, skip_modulecheck)
             if rc != 0:
-                self.vm_turn_off(p1)
+                if stop_vm:
+                    self.vm_turn_off(vm)
                 self.fail('Failed to parse output')
 
-        self.vm_turn_off(p1)
+        if stop_vm:
+            self.vm_turn_off(vm)
-- 
2.20.1


  parent reply	other threads:[~2023-08-18  7:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-18  7:06 [PATCH 0/8] Testsuite improvements for SSH-based tests Uladzimir Bely
2023-08-18  7:06 ` [PATCH 1/8] cibuilder.py: Support custom arguments passing to CI scripts Uladzimir Bely
2023-08-18  7:07 ` [PATCH 2/8] meta-isar: Add more extra space to qemu ext4 images Uladzimir Bely
2023-08-18  7:07 ` [PATCH 3/8] cibuilder.py: Split vm_start function to smaller subfunctions Uladzimir Bely
2023-08-18  7:07 ` [PATCH 4/8] cibuilder.py: Simplify remote_run command Uladzimir Bely
2023-08-18  7:07 ` Uladzimir Bely [this message]
2023-08-18  7:07 ` [PATCH 6/8] citest.py: Adapt tests to qemu reuse Uladzimir Bely
2023-08-18  7:07 ` [PATCH 7/8] cibuilder.py: enable output from remote scripts Uladzimir Bely
2023-08-18  7:07 ` [PATCH 8/8] testsuite: Switch to remote scripts with arguments Uladzimir Bely
2023-08-24 15:34 ` [PATCH 0/8] Testsuite improvements for SSH-based tests Uladzimir Bely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230818070706.27913-6-ubely@ilbers.de \
    --to=ubely@ilbers.de \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox