Add --pass option
[ci.git] / vm-test.py
blob2c2ea06ac8956773a7621b64ff024fbeb8364137
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.
32 import argparse
33 import yaml
34 import logging
35 import sys
37 from htest.vm.controller import VMManager
38 from htest.vm.qemu import QemuVMController
39 from htest.tasks import *
41 args = argparse.ArgumentParser(description='HelenOS VM tests')
42 args.add_argument('--scenario',
43 metavar='FILENAME.yml',
44 dest='scenario',
45 default='scenarios/base/pcut.yml',
46 help='Scenario file'
48 args.add_argument('--arch',
49 metavar='ARCHITECTURE',
50 dest='architecture',
51 required=True,
52 help='Emulated architecture identification.'
54 args.add_argument('--memory',
55 metavar='MB',
56 dest='memory',
57 type=int,
58 required=False,
59 default=256,
60 help='Amount of memory for the virtual machine.'
62 args.add_argument('--image',
63 metavar='FILENAME',
64 dest='boot_image',
65 required=True,
66 help='HelenOS boot image (e.g. ISO file).'
68 args.add_argument('--pass',
69 metavar='OPTION',
70 dest='pass_thru_options',
71 default=[],
72 action='append',
73 help='Extra options to pass through to the emulator'
75 args.add_argument('--vterm-dump',
76 metavar='FILENAME.txt',
77 dest='vterm_dump',
78 default=None,
79 help='Where to store full vterm dump.'
81 args.add_argument('--last-screenshot',
82 metavar='FILENAME.png',
83 dest='last_screenshot',
84 default=None,
85 help='Where to store last screenshot.'
87 args.add_argument('--debug',
88 dest='debug',
89 default=False,
90 action='store_true',
91 help='Print debugging messages'
94 config = args.parse_args()
96 if config.debug:
97 config.logging_level = logging.DEBUG
98 else:
99 config.logging_level = logging.INFO
101 logging.basicConfig(
102 format='[%(asctime)s %(name)-16s %(levelname)7s] %(message)s',
103 level=config.logging_level
106 logger = logging.getLogger('main')
108 with open(config.scenario, 'r') as f:
109 try:
110 scenario = yaml.load(f)
111 except yaml.YAMLError as ex:
112 logger.error(ex)
113 sys.exit(1)
115 if config.memory < 8:
116 logger.error("Specify at least 8MB of memory.")
117 sys.exit(1)
119 controller = None
120 for ctl in [ QemuVMController ]:
121 if ctl.is_supported(config.architecture):
122 controller = ctl
124 if controller is None:
125 logger.error("Unsupported architecture {}.".format(config.architecture))
126 sys.exit(1)
128 vmm = VMManager(controller, config.architecture, config.boot_image, config.memory, config.pass_thru_options)
130 scenario_tasks = []
131 for t in scenario['tasks']:
132 task_name = None
133 if type(t) is dict:
134 k = list(set(t.keys()) - set(['name', 'machine']))
135 if len(k) != 1:
136 raise Exception("Unknown task ({})!".format(k))
137 task_name = k[0]
138 elif type(t) is str:
139 task_name = t
140 t = {
141 task_name: {}
143 else:
144 raise Exception("Unknown task!")
145 task_classname = 'ScenarioTask' + task_name.title().replace('-', '_')
146 task_class = globals()[task_classname]
147 task_inst = task_class(t[task_name])
148 if not ('machine' in t):
149 t['machine'] = None
150 machine = vmm.get(t['machine'])
151 if machine is None:
152 if t['machine'] is None:
153 t['machine'] = 'default'
154 logger.debug("Creating new machine {}.".format(t['machine']))
155 machine = vmm.create(t['machine'])
156 task_inst.set_machine(machine)
157 scenario_tasks.append(task_inst)
159 try:
160 for t in scenario_tasks:
161 t.run()
162 except Exception as ex:
163 print(ex)
165 vmm.terminate(config.vterm_dump, config.last_screenshot)