Last active
June 22, 2021 22:13
-
-
Save fabiand/10448153 to your computer and use it in GitHub Desktop.
How to auto-test an image using pexpect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pexpect | |
image = "fedora-20.img" | |
user = "root" | |
password = "changeme" | |
# Define the qemu cmd to run | |
# The important bit is to redirect the serial to stdio | |
cmd = "qemu-kvm" | |
cmd += " -m 1024 -serial stdio -net user -net nic" | |
cmd += " -snapshot -hda %s" % image | |
cmd += " -watchdog-action poweroff" | |
# Spawn the qemu process and log to stdout | |
child = pexpect.spawn(cmd) | |
child.logfile = sys.stdout | |
# Now wait for the login | |
child.expect('(?i)login:') | |
# And login with the credentials from above | |
child.sendline(user) | |
child.expect('(?i)password:') | |
child.sendline(password) | |
child.expect('# ') | |
# Now shutdown the machine and end the process | |
if child.isalive(): | |
child.sendline('init 0') | |
child.close() | |
if child.isalive(): | |
print('Child did not exit gracefully.') | |
else: | |
print('Child exited gracefully.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment