From: Uladzimir Bely <ubely@ilbers.de>
To: isar-users@googlegroups.com
Subject: [PATCH v2 6/9] testsuite: Support running custom commands in VM
Date: Wed, 8 Feb 2023 16:45:08 +0100 [thread overview]
Message-ID: <20230208154511.1884-7-ubely@ilbers.de> (raw)
In-Reply-To: <20230208154511.1884-1-ubely@ilbers.de>
By specifying `cmd='<command>'` it's now possible to run VM and
execute the command over SSH connection.
If this is not specified, tests uses previous approach, with
parsing output log and searching some specific fragments.
Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
---
testsuite/cibuilder.py | 81 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 79 insertions(+), 2 deletions(-)
diff --git a/testsuite/cibuilder.py b/testsuite/cibuilder.py
index 247b998b..9b64af90 100755
--- a/testsuite/cibuilder.py
+++ b/testsuite/cibuilder.py
@@ -2,6 +2,7 @@
import logging
import os
+import re
import select
import shutil
import subprocess
@@ -209,13 +210,54 @@ class CIBuilder(Test):
return env['LAYERDIR_' + layer].strip('"')
+ def get_ssh_cmd_prefix(self, port, priv_key):
+ port_args = ''
+ if port:
+ port_args = ' -p ' + str(port)
+
+ cmd_prefix = 'ssh' + port_args + \
+ ' -o ConnectTimeout=5 -o IdentityFile=' + priv_key + \
+ ' -o StrictHostKeyChecking=no ci@localhost '
+
+ return cmd_prefix
+
+
+ def exec_cmd(self, cmd, cmd_prefix):
+ rc = subprocess.call('exec ' + str(cmd_prefix) + ' "' + str(cmd) + '"', shell=True,
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ return rc
+
+
+ def wait_connection(self, proc, cmd_prefix, timeout):
+ self.log.debug('Waiting for SSH server ready...')
+
+ rc = None
+ while time.time() < timeout:
+ if proc.poll() is not None:
+ self.log.error('Machine is not running')
+ return rc
+
+ rc = self.exec_cmd('/bin/true', cmd_prefix)
+ time.sleep(1)
+
+ if rc == 0:
+ self.log.debug('SSH server is ready')
+ break
+
+ if rc != 0:
+ self.log.error('SSH server is not ready')
+
+ return rc
+
+
def vm_start(self, arch='amd64', distro='buster',
enforce_pcbios=False, skip_modulecheck=False,
- image='isar-image-base'):
+ image='isar-image-base', cmd=None):
time_to_wait = self.params.get('time_to_wait', default=60)
self.log.info('===================================================')
self.log.info('Running Isar VM boot test for (' + distro + '-' + arch + ')')
+ self.log.info('Remote command is ' + str(cmd))
self.log.info('Isar build folder is: ' + self.build_dir)
self.log.info('===================================================')
@@ -238,7 +280,7 @@ class CIBuilder(Test):
output_file, None, enforce_pcbios)
cmdline.insert(1, '-nographic')
- self.log.info('QEMU boot line: ' + str(cmdline))
+ self.log.info('QEMU boot line:\n' + ' '.join(cmdline))
login_prompt = b'isar login:'
# the printk of recipes-kernel/example-module
@@ -266,6 +308,41 @@ class CIBuilder(Test):
p1 = subprocess.Popen('exec ' + ' '.join(cmdline), shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
+
+ if cmd is not None:
+ rc = None
+ try:
+ port = None
+ for arg in cmdline:
+ match = re.match(r".*hostfwd=tcp::(\d*).*", arg)
+ if match:
+ port = match.group(1)
+ break
+
+ # copy private key to build directory
+ priv_key = '%s/ci_priv_key' % self.build_dir
+ if not os.path.exists(priv_key):
+ shutil.copy(os.path.dirname(__file__) + '/keys/ssh/id_rsa', priv_key)
+ os.chmod(priv_key, 0o400)
+
+ cmd_prefix = self.get_ssh_cmd_prefix(port, priv_key)
+ self.log.debug('Connect command:\n' + cmd_prefix)
+ rc = self.wait_connection(p1, cmd_prefix, timeout)
+
+ if rc == 0:
+ rc = self.exec_cmd(cmd, cmd_prefix)
+ self.log.debug('`' + cmd + '` returned ' + str(rc))
+ finally:
+ if p1.poll() is None:
+ self.log.debug('Killing qemu...')
+ p1.kill()
+ p1.wait()
+
+ if rc != 0:
+ self.fail('Log ' + output_file)
+
+ return
+
try:
poller = select.poll()
poller.register(p1.stdout, select.POLLIN)
--
2.20.1
next prev parent reply other threads:[~2023-02-08 15:45 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-08 15:45 [PATCH v2 0/9] " Uladzimir Bely
2023-02-08 15:45 ` [PATCH v2 1/9] testsuite: Fix failing hostname service in qemu guest Uladzimir Bely
2023-02-08 15:45 ` [PATCH v2 2/9] testsuite: Add SSH key pair for using in CI Uladzimir Bely
2023-02-08 15:45 ` [PATCH v2 3/9] meta-isar: Add a recipe that configures ci user Uladzimir Bely
2023-02-08 15:45 ` [PATCH v2 4/9] meta-isar: Use a separate image recipe in CI Uladzimir Bely
2023-02-08 15:45 ` [PATCH v2 5/9] testsuite: Allow custom image names in start_vm.py Uladzimir Bely
2023-02-08 15:45 ` Uladzimir Bely [this message]
2023-02-08 15:45 ` [PATCH v2 7/9] testsuite: Support running custom scripts in VM Uladzimir Bely
2023-02-08 15:45 ` [PATCH v2 8/9] testsuite: Run custom commands and scripts on some qemu targets Uladzimir Bely
2023-02-08 15:45 ` [PATCH v2 9/9] testsuite: Update testsuite qemu-related documentation Uladzimir Bely
2023-02-21 10:56 ` [PATCH v2 0/9] Support running custom commands in VM 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=20230208154511.1884-7-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