Tester: fix sending uppercase letters to QEMU
[ci.git] / htest / vm / qemu.py
blob062c3626a409f39cd7431a497e29bfb6317ddcac
1 #!/usr/bin/env python3
4 # Copyright (c) 2018 Vojtech Horky
5 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
11 # - Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # - Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # - The name of the author may not be used to endorse or promote products
17 # derived from this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 import subprocess
32 import socket
33 import logging
34 import os
35 import sys
37 from PIL import Image
39 from htest.utils import retries, format_command, format_command_pipe
40 from htest.vm.controller import VMController
42 class QemuVMController(VMController):
43 """
44 QEMU VM controller.
45 """
47 config = {
48 'amd64': [
49 'qemu-system-x86_64',
50 '-cdrom', '{BOOT}',
51 '-m', '{MEMORY}',
52 '-usb',
54 'arm32/integratorcp': [
55 'qemu-system-arm',
56 '-M', 'integratorcp',
57 '-usb',
58 '-kernel', '{BOOT}',
59 '-m', '{MEMORY}',
61 'ia32': [
62 'qemu-system-i386',
63 '-cdrom', '{BOOT}',
64 '-m', '{MEMORY}',
65 '-usb',
67 'ppc32': [
68 'qemu-system-ppc',
69 '-usb',
70 '-boot', 'd',
71 '-cdrom', '{BOOT}',
72 '-m', '{MEMORY}',
76 ocr_sed = os.path.join(
77 os.path.dirname(os.path.realpath(sys.argv[0])),
78 'ocr.sed'
81 def __init__(self, arch, name, boot_image):
82 VMController.__init__(self, 'QEMU-' + arch)
83 self.arch = arch
84 self.booted = False
85 self.name = name
86 self.boot_image = boot_image
88 def is_supported(arch):
89 return arch in QemuVMController.config
91 def _get_image_dimensions(self, filename):
92 im = Image.open(filename)
93 width, height = im.size
94 im.close()
95 return ( width, height )
97 def _check_is_up(self):
98 if not self.booted:
99 raise Exception("Machine not launched")
101 def _send_command(self, command):
102 self._check_is_up()
103 self.logger.debug("Sending command '{}'".format(command))
104 command = command + '\n'
105 self.monitor.sendall(command.encode('utf-8'))
107 def _run_command(self, command):
108 proc = subprocess.Popen(command)
109 proc.wait()
110 if proc.returncode != 0:
111 raise Exception("Command {} failed.".format(command))
113 def _run_pipe(self, commands):
114 self.logger.debug("Running pipe {}".format(format_command_pipe(commands)))
115 procs = []
116 for command in commands:
117 inp = None
118 if len(procs) > 0:
119 inp = procs[-1].stdout
120 proc = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=inp)
121 procs.append(proc)
122 procs[-1].communicate()
125 def boot(self, **kwargs):
126 self.monitor_file = self.get_temp('monitor')
127 cmd = []
128 for opt in QemuVMController.config[self.arch]:
129 if opt == '{BOOT}':
130 opt = self.boot_image
131 elif opt == '{MEMORY}':
132 opt = '{}'.format(self.memory)
133 cmd.append(opt)
134 if self.is_headless:
135 cmd.append('-display')
136 cmd.append('none')
137 cmd.append('-monitor')
138 cmd.append('unix:{},server,nowait'.format(self.monitor_file))
139 for opt in self.extra_options:
140 cmd.append(opt)
141 self.logger.debug("Starting QEMU: {}".format(format_command(cmd)))
143 self.proc = subprocess.Popen(cmd)
144 self.monitor = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
145 for xxx in retries(timeout=30, interval=2, name="ctl-socket", message="Failed to connect to QEMU control socket."):
146 try:
147 self.monitor.connect(self.monitor_file)
148 break
149 except FileNotFoundError:
150 pass
151 except ConnectionRefusedError:
152 pass
153 if self.proc.poll():
154 raise Exception("QEMU not started, aborting.")
156 self.booted = True
157 self.logger.info("Machine started.")
159 # Skip past GRUB
160 self.type('\n')
162 uspace_booted = False
163 for xxx in retries(timeout=3*60, interval=5, name="vterm", message="Failed to boot into userspace"):
164 self.vterm = []
165 self.capture_vterm()
166 for l in self.vterm:
167 if l.find('to see a few survival tips') != -1:
168 uspace_booted = True
169 break
170 if uspace_booted:
171 break
173 assert uspace_booted
174 self.full_vterm = self.vterm
176 self.logger.info("Machine booted into userspace.")
178 return
180 def capture_vterm_impl(self):
181 screenshot_full = self.get_temp('screen-full.ppm')
182 screenshot_term = self.get_temp('screen-term.png')
183 screenshot_text = self.get_temp('screen-term.txt')
185 self._send_command('screendump ' + screenshot_full)
187 for xxx in retries(timeout=5, interval=1, name="scrdump", message="Failed to capture screen"):
188 try:
189 self._run_command([
190 'convert',
191 screenshot_full,
192 '-crop', '640x480+4+24',
193 '+repage',
194 '-colors', '2',
195 '-monochrome',
196 screenshot_term
198 break
199 except:
200 pass
202 width, height = self._get_image_dimensions(screenshot_term)
203 cols = width // 8
204 rows = height // 16
205 self._run_pipe([
207 'convert',
208 screenshot_term,
209 '-crop', '{}x{}'.format(cols * 8, rows * 16),
210 '+repage',
211 '-crop', '8x16',
212 '+repage',
213 '+adjoin',
214 'txt:-',
217 'sed',
218 '-e', 's|[0-9]*,[0-9]*: ([^)]*)[ ]*#\\([0-9A-Fa-f]\\{6\\}\\).*|\\1|',
219 '-e', 's:^#.*:@:',
220 '-e', 's#000000#0#g',
221 '-e', 's#FFFFFF#F#',
223 [ 'tee', self.get_temp('1.txt') ],
225 'sed',
226 '-e', ':a',
227 '-e', 'N;s#\\n##;s#^@##;/@$/{s#@$##p;d}',
228 '-e', 't a',
230 [ 'tee', self.get_temp('2.txt') ],
232 'sed',
233 '-f', QemuVMController.ocr_sed,
236 'sed',
237 '/../s#.*#?#',
239 [ 'tee', self.get_temp('3.txt') ],
241 'paste',
242 '-sd', '',
245 'fold',
246 '-w', '{}'.format(cols),
248 [ 'tee', self.get_temp('4.txt') ],
250 'head',
251 '-n', '{}'.format(rows),
254 'tee',
255 screenshot_text,
259 self.screenshot_filename = screenshot_full
261 with open(screenshot_text, 'r') as f:
262 lines = [ l.strip('\n') for l in f.readlines() ]
263 self.logger.debug("Captured text:")
264 for l in lines:
265 self.logger.debug("| " + l)
266 return lines
268 def terminate(self):
269 if not self.booted:
270 return
271 self._send_command('quit')
272 VMController.terminate(self)
274 def type(self, what):
275 translations = {
276 ' ': 'spc',
277 '.': 'dot',
278 '-': 'minus',
279 '/': 'slash',
280 '\n': 'ret',
281 '_': 'shift-minus',
283 for letter in what:
284 if letter.isupper():
285 letter = 'shift-' + letter.lower()
286 if letter in translations:
287 letter = translations[letter]
288 self._send_command('sendkey ' + letter)
289 pass