Buildroot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Yann E. MORIN <yann.morin.1998@free.fr>
To: buildroot@busybox.net
Subject: [Buildroot] [PATCH v5 4/5] support/scripts/boot-qemu-image.py: boot Qemu images with Qemu-system.
Date: Mon, 13 Apr 2020 09:15:32 +0200	[thread overview]
Message-ID: <20200413071532.GV29898@scaer> (raw)
In-Reply-To: <20200217205030.8157-5-romain.naour@smile.fr>

Romain, Jugurtha, All,

On 2020-02-17 21:50 +0100, Romain Naour spake thusly:
> From: Jugurtha BELKALEM <jugurtha.belkalem@smile.fr>
> This script is intended to be used by gitlab CI to test
> at runtime Qemu images generated by Buildroot's Qemu defconfigs.
[--SNIP--]
> diff --git a/support/scripts/boot-qemu-image.py b/support/scripts/boot-qemu-image.py
> new file mode 100755
> index 0000000000..abaf88d446
> --- /dev/null
> +++ b/support/scripts/boot-qemu-image.py
> @@ -0,0 +1,105 @@
> +#!/usr/bin/env python3
> +
> +# This script expect to run from the Buildroot top directory.
> +
> +import pexpect
> +import sys
> +import os
> +import re
> +import shlex
> +import shutil
> +import subprocess
> +
> +argc = len(sys.argv)
> +if not (argc == 2):
> +    print("Error: incorrect number of arguments")
> +    print("""Usage: boot-qemu-image.py <qemu_arch_defconfig>""")
> +    sys.exit(1)
> +
> +defconfig_filename = sys.argv[1]
> +
> +# Ignore non Qemu defconfig
> +if defconfig_filename.startswith('qemu_') is False:
> +    sys.exit(0)
> +
> +qemu_start_script_filepath = os.path.join(os.getcwd(), 'output/images/start-qemu.sh')
> +
> +# Ignore if start-qemu.sh is missing, we can't test.
> +if not os.path.exists(qemu_start_script_filepath):
> +    print("Error: " + qemu_start_script_filepath) + " file is missing."
> +    sys.exit(1)
> +
> +qemu_cmd = ""
> +
> +with open(qemu_start_script_filepath, 'r') as script_file:
> +    for line in script_file:
> +        if re.search("qemu-system", line):
> +            qemu_cmd = line
> +            break
> +
> +if not qemu_cmd:
> +    print("Error: No QEMU command line found in " + qemu_start_script_filepath)
> +    sys.exit(1)

Why do you extract the command line, and can't directly run the script?

> +# Replace bashism
> +qemu_cmd = line.replace("${IMAGE_DIR}", "output/images")

This would avoid this dance...

> +# pexpect needs a list, convert a sting to a list and escape quoted substring.
> +qemu_cmd = shlex.split(qemu_cmd)

... as well as this one.

> +# Use host Qemu if provided by Buildroot.
> +os.environ["PATH"] = os.getcwd() + "/output/host/bin" + os.pathsep + os.environ["PATH"]

And this one can be avoided too if the script is called with PATH
properly set to include ${HOST_DIR}/bin. This should be easy, as this
script is only expected to run from the gitlab-ci pipelines.

> +# Ignore when no Qemu emulation is available
> +if not shutil.which(qemu_cmd[0]):
> +    print("No " + qemu_cmd[0] + " binary available, THIS DEFCONFIG CAN NOT BE TESTED!")
> +    sys.exit(0)
> +
> +
> +def main():
> +    global child

Mixing code at the top-level and in a main() is ugly. Mocve everything
to a main (and helper functions if youlike), but don't mix the two.

> +    try:
> +        child.expect(["buildroot login:", pexpect.TIMEOUT], timeout=60)
> +    except pexpect.EOF:
> +        print("Connection problem, exiting.")
> +        sys.exit(1)
> +    except pexpect.TIMEOUT:
> +        print("System did not boot in time, exiting.")
> +        sys.exit(1)
> +
> +    child.sendline("root\r")
> +
> +    try:
> +        child.expect(["# ", pexpect.TIMEOUT], timeout=60)
> +    except pexpect.EOF:
> +        print("Cannot connect to shell")
> +        sys.exit(1)
> +    except pexpect.TIMEOUT:
> +        print("Timeout while waiting for shell")
> +        sys.exit(1)
> +
> +    child.sendline("poweroff\r")
> +
> +    try:
> +        child.expect(["System halted", pexpect.TIMEOUT], timeout=60)
> +        child.expect(pexpect.EOF)
> +    except pexpect.EOF:
> +        sys.exit(0)
> +    except pexpect.TIMEOUT:
> +        print("Cannot halt machine")
> +        # Qemu may not exit properly after "System halted", ignore.
> +        sys.exit(0)
> +
> +
> +# Log the Qemu version
> +subprocess.call([qemu_cmd[0], "--version"])
> +
> +# Log the Qemu command line
> +print(qemu_cmd)

You would not have to do that if you just called the start-qemu.sh script.

Regards,
Yann E. MORIN.

> +child = pexpect.spawn(qemu_cmd[0], qemu_cmd[1:], timeout=5, encoding='utf-8',
> +                      env={"QEMU_AUDIO_DRV": "none", 'PATH': os.environ["PATH"]})
> +# We want only stdout into the log to avoid double echo
> +child.logfile = sys.stdout
> +main()
> -- 
> 2.24.1
> 
> _______________________________________________
> buildroot mailing list
> buildroot at busybox.net
> http://lists.busybox.net/mailman/listinfo/buildroot

-- 
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'

  reply	other threads:[~2020-04-13  7:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-17 20:50 [Buildroot] [PATCH v5 0/5] gitlab Qemu runtime testing Romain Naour
2020-02-17 20:50 ` [Buildroot] [PATCH v5 1/5] board/qemu: add defconfig file name as tag after the qemu command line Romain Naour
2020-02-17 20:50 ` [Buildroot] [PATCH v5 2/5] board/qemu: add post-image script for gitlab qemu testing Romain Naour
2020-04-13  7:25   ` Yann E. MORIN
2020-02-17 20:50 ` [Buildroot] [PATCH v5 3/5] configs/qemu*: use the post-image script with "$(BR2_DEFCONFIG)" as argument Romain Naour
2020-02-17 20:50 ` [Buildroot] [PATCH v5 4/5] support/scripts/boot-qemu-image.py: boot Qemu images with Qemu-system Romain Naour
2020-04-13  7:15   ` Yann E. MORIN [this message]
2020-04-13 21:38     ` Romain Naour
2020-02-17 20:50 ` [Buildroot] [PATCH v5 5/5] gitlab.yml.in*: enable Qemu gitlab testing Romain Naour
2020-04-12 14:11 ` [Buildroot] [PATCH v5 0/5] gitlab Qemu runtime testing Thomas Petazzoni
2020-04-12 20:00   ` Romain Naour
2020-04-13 19:56 ` Yann E. MORIN
2020-04-13 20:03   ` Romain Naour

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=20200413071532.GV29898@scaer \
    --to=yann.morin.1998@free.fr \
    --cc=buildroot@busybox.net \
    /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