645
|
1 |
# -*- encoding: latin-1 -*-
|
|
2 |
|
|
3 |
#============================================================================
|
|
4 |
#Name : bootup_testing.py
|
|
5 |
#Part of : Helium
|
|
6 |
|
|
7 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
8 |
#All rights reserved.
|
|
9 |
#This component and the accompanying materials are made available
|
|
10 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
11 |
#which accompanies this distribution, and is available
|
|
12 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
13 |
#
|
|
14 |
#Initial Contributors:
|
|
15 |
#Nokia Corporation - initial contribution.
|
|
16 |
#
|
|
17 |
#Contributors:
|
|
18 |
#
|
|
19 |
#Description:
|
|
20 |
#===============================================================================
|
|
21 |
|
|
22 |
"""Bootup test drop generation."""
|
|
23 |
|
|
24 |
#W0142 => * and ** were used
|
|
25 |
#R* removed during refactoring
|
|
26 |
|
|
27 |
from optparse import OptionParser
|
|
28 |
from xml.etree import ElementTree as et
|
|
29 |
import logging
|
|
30 |
import os
|
|
31 |
import re
|
|
32 |
import tempfile
|
|
33 |
import zipfile
|
|
34 |
import pkg_resources # pylint: disable-msg=F0401
|
|
35 |
from path import path # pylint: disable-msg=F0401
|
|
36 |
import amara
|
|
37 |
import ntpath as atspath
|
|
38 |
import jinja2 # pylint: disable-msg=F0401
|
|
39 |
import ats3.parsers as parser
|
|
40 |
|
|
41 |
_logger = logging.getLogger('bootup-testing')
|
|
42 |
|
|
43 |
# Shortcuts
|
|
44 |
E = et.Element
|
|
45 |
SE = et.SubElement
|
|
46 |
|
|
47 |
class Configuration(object):
|
|
48 |
"""
|
|
49 |
Bootup test drop generation configuration.
|
|
50 |
"""
|
|
51 |
|
|
52 |
def __init__(self, opts):
|
|
53 |
"""
|
|
54 |
Initialize from optparse configuration options.
|
|
55 |
"""
|
|
56 |
|
|
57 |
self._opts = opts
|
|
58 |
# Customize some attributes from how optparse leaves them.
|
|
59 |
self.build_drive = path(self._opts.build_drive)
|
|
60 |
self.file_store = path(self._opts.file_store)
|
|
61 |
self.flash_images = self.split_paths(self._opts.flash_images)
|
|
62 |
self.template_loc = path(self._opts.template_loc)
|
|
63 |
|
|
64 |
|
|
65 |
def split_paths(self, arg, delim=","):
|
|
66 |
"""
|
|
67 |
Split the string by delim, removing extra whitespace.
|
|
68 |
"""
|
|
69 |
return [path(part.strip())
|
|
70 |
for part in arg.split(delim) if part.strip()]
|
|
71 |
|
|
72 |
def __getattr__(self, attr):
|
|
73 |
return getattr(self._opts, attr)
|
|
74 |
|
|
75 |
def __str__(self):
|
|
76 |
dump = "Configuration:\n"
|
|
77 |
seen = set()
|
|
78 |
for key, value in vars(self).items():
|
|
79 |
if not key.startswith("_"):
|
|
80 |
dump += "\t%s = %s\n" % (key, value)
|
|
81 |
seen.add(key)
|
|
82 |
for key, value in vars(self._opts).items():
|
|
83 |
if key not in seen:
|
|
84 |
dump += "\t%s = %s\n" % (key, value)
|
|
85 |
seen.add(key)
|
|
86 |
return dump
|
|
87 |
|
|
88 |
|
|
89 |
class BootupTestPlan(object):
|
|
90 |
"""
|
|
91 |
Tells ATS server that images have to be tested if they can start the device.
|
|
92 |
"""
|
|
93 |
|
|
94 |
def __init__(self, config):
|
|
95 |
self.pkg_parser = parser.PkgFileParser()
|
|
96 |
self.file_store = config.file_store
|
|
97 |
self.build_drive = config.build_drive
|
|
98 |
|
|
99 |
def insert_execution_block(self, block_count=1, image_files=None):
|
|
100 |
"""
|
|
101 |
Insert task and flash files into the execution block
|
|
102 |
"""
|
|
103 |
if image_files is None:
|
|
104 |
image_files = []
|
|
105 |
|
|
106 |
exe_dict = dict(name="exe%d" % block_count, image_files=image_files)
|
|
107 |
|
|
108 |
return exe_dict
|
|
109 |
|
|
110 |
|
|
111 |
def __getitem__(self, key):
|
|
112 |
return self.__dict__[key]
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
class BootupTestDropGenerator(object):
|
|
117 |
"""
|
|
118 |
Generate test drop zip file for Bootup testing.
|
|
119 |
|
|
120 |
The main responsibility of this class is to create testdrop and
|
|
121 |
test.xml file.
|
|
122 |
|
|
123 |
"""
|
|
124 |
|
|
125 |
def __init__(self):
|
|
126 |
self.drop_path_root = path("BootupDrop")
|
|
127 |
self.drop_path = None
|
|
128 |
self.defaults = {}
|
|
129 |
|
|
130 |
def generate(self, xml_dict, output_file, template_loc=None):
|
|
131 |
"""Generate a test drop file."""
|
|
132 |
xml = self.generate_xml(xml_dict, template_loc)
|
|
133 |
return self.generate_drop(xml_dict, xml, output_file)
|
|
134 |
|
|
135 |
def generate_drop(self, xml_dict, xml, output_file):
|
|
136 |
"""Generate test drop zip file."""
|
|
137 |
|
|
138 |
zfile = zipfile.ZipFile(output_file, "w", zipfile.ZIP_DEFLATED)
|
|
139 |
try:
|
|
140 |
for drop_file, src_file in self.drop_files(xml_dict):
|
|
141 |
|
|
142 |
_logger.info(" + Adding: %s" % src_file.strip())
|
|
143 |
try:
|
|
144 |
zfile.write(src_file.strip(), drop_file.encode('utf-8'))
|
|
145 |
except OSError, expr:
|
|
146 |
_logger.error(expr)
|
|
147 |
doc = amara.parse(et.tostring(xml.getroot()))
|
|
148 |
_logger.debug("XML output: %s" % doc.xml(indent=u"yes", encoding="ISO-8859-1"))
|
|
149 |
zfile.writestr("test.xml", doc.xml(indent="yes", encoding="ISO-8859-1"))
|
|
150 |
finally:
|
|
151 |
_logger.info("Testdrop for bootup testing created successfully!")
|
|
152 |
zfile.close()
|
|
153 |
|
|
154 |
def generate_xml(self, xml_dict, template_loc):
|
|
155 |
""" generate an XML file"""
|
|
156 |
template_loc = path(template_loc).normpath()
|
|
157 |
loader = jinja2.ChoiceLoader([jinja2.PackageLoader(__name__, 'templates')])
|
|
158 |
env = jinja2.Environment(loader=loader)
|
|
159 |
if template_loc is None or not ".xml" in template_loc.lower():
|
|
160 |
template = env.from_string(pkg_resources.resource_string(__name__, 'bootup_testing_template.xml'))# pylint: disable-msg=E1101
|
|
161 |
else:
|
|
162 |
template = env.from_string(open(template_loc).read())# pylint: disable-msg=E1101
|
|
163 |
|
|
164 |
xmltext = template.render(xml_dict=xml_dict, os=os, atspath=atspath, atsself=self).encode('ISO-8859-1')
|
|
165 |
#print xmltext
|
|
166 |
return et.ElementTree(et.XML(xmltext))
|
|
167 |
|
|
168 |
|
|
169 |
def drop_files(self, xml_dict):
|
|
170 |
"""Yield a list of drop files."""
|
|
171 |
|
|
172 |
drop_set = set()
|
|
173 |
drop_files = []
|
|
174 |
#Adding test asset, there's an execution block for every test asset
|
|
175 |
for execution_block in xml_dict["execution_blocks"]:
|
|
176 |
drop_dir = path(execution_block["name"])
|
|
177 |
drop_files = execution_block["image_files"]
|
|
178 |
for file_path in drop_files:
|
|
179 |
if file_path != None:
|
|
180 |
#Adding image files to the top level,
|
|
181 |
drop_file = drop_dir.joinpath("images", file_path.name)
|
|
182 |
|
|
183 |
drop_file = drop_file.normpath()
|
|
184 |
if drop_file not in drop_set:
|
|
185 |
drop_set.add(drop_file)
|
|
186 |
yield (drop_file, file_path.normpath())
|
|
187 |
|
|
188 |
|
|
189 |
class ComponentParser(object):
|
|
190 |
"""
|
|
191 |
Add information to the XML dictionary
|
|
192 |
"""
|
|
193 |
def __init__(self, config):
|
|
194 |
self.flash_images = [path(p) for p in config.flash_images]
|
|
195 |
self.build_drive = config.build_drive
|
|
196 |
self.diamonds_build_url = config.diamonds_build_url
|
|
197 |
self.testrun_name = config.testrun_name
|
|
198 |
self.alias_name = config.alias_name
|
|
199 |
self.device_type = config.device_type
|
|
200 |
self.report_email = config.report_email
|
|
201 |
self.email_format = config.email_format
|
|
202 |
self.email_subject = config.email_subject
|
|
203 |
|
|
204 |
self.xml_dict = {}
|
|
205 |
|
|
206 |
|
|
207 |
def insert_pre_data(self):
|
|
208 |
"""
|
|
209 |
Creates a dictionary for the data before
|
|
210 |
the <execution> block starts.
|
|
211 |
"""
|
|
212 |
self.xml_dict = dict(self.xml_dict, temp_directory=path(tempfile.mkdtemp()))
|
|
213 |
self.xml_dict = dict(self.xml_dict, diamonds_build_url=self.diamonds_build_url)
|
|
214 |
self.xml_dict = dict(self.xml_dict, testrun_name=self.testrun_name)
|
|
215 |
self.xml_dict = dict(self.xml_dict, alias_name=self.alias_name)
|
|
216 |
self.xml_dict = dict(self.xml_dict, device_type=self.device_type)
|
|
217 |
|
|
218 |
def create_execution_block(self, config):
|
|
219 |
"""Parse flash images """
|
|
220 |
execution_block_list = []
|
|
221 |
test_plan = BootupTestPlan(config)
|
|
222 |
block_count = 1
|
|
223 |
self.flash_images = self.get_sorted_images(self.flash_images)
|
|
224 |
execution_block_list.append(test_plan.insert_execution_block(block_count, self.flash_images))
|
|
225 |
|
|
226 |
|
|
227 |
self.xml_dict = dict(self.xml_dict, execution_blocks=execution_block_list)
|
|
228 |
|
|
229 |
def insert_post_data(self):
|
|
230 |
"""
|
|
231 |
Creates a dictionary for the data after
|
|
232 |
the <execution> block ends. Or, Postaction data
|
|
233 |
"""
|
|
234 |
self.xml_dict = dict(self.xml_dict, report_email=self.report_email)
|
|
235 |
self.xml_dict = dict(self.xml_dict, email_format=self.email_format)
|
|
236 |
self.xml_dict = dict(self.xml_dict, email_subject=self.email_subject)
|
|
237 |
|
|
238 |
return self.xml_dict
|
|
239 |
|
|
240 |
def get_sorted_images(self, image_files):
|
|
241 |
"""sort the images """
|
|
242 |
sorted_images = []
|
|
243 |
for image_file in image_files:
|
|
244 |
if 'core' in image_file.name:
|
|
245 |
sorted_images.append(image_file)
|
|
246 |
for image_file in image_files:
|
|
247 |
if 'rofs2' in image_file.name:
|
|
248 |
sorted_images.append(image_file)
|
|
249 |
for image_file in image_files:
|
|
250 |
if 'rofs3' in image_file.name:
|
|
251 |
sorted_images.append(image_file)
|
|
252 |
for image_file in image_files:
|
|
253 |
if 'udaerase' in image_file.name:
|
|
254 |
sorted_images.append(image_file)
|
|
255 |
for image_file in image_files:
|
|
256 |
if 'core' not in image_file.name and 'rofs2' not in image_file.name and 'rofs3' not in image_file.name and 'udaerase' not in image_file.name.lower():
|
|
257 |
sorted_images.append(image_file)
|
|
258 |
if len(sorted_images) > 0 and "rofs" in sorted_images[0]:
|
|
259 |
return image_files
|
|
260 |
return sorted_images
|
|
261 |
|
|
262 |
def create_drop(config):
|
|
263 |
"""Create a test drop."""
|
|
264 |
xml_dict = {}
|
|
265 |
|
|
266 |
_logger.debug("initialize configuration dictionary")
|
|
267 |
drop_parser = ComponentParser(config)
|
|
268 |
|
|
269 |
#Inserting data for test run and global through out the dictionary
|
|
270 |
drop_parser.insert_pre_data()
|
|
271 |
|
|
272 |
#for every asset path there should be a
|
|
273 |
#separate execution block
|
|
274 |
drop_parser.create_execution_block(config)
|
|
275 |
|
|
276 |
#Inserting reporting and email data (post actions)
|
|
277 |
xml_dict = drop_parser.insert_post_data()
|
|
278 |
|
|
279 |
|
|
280 |
# print "-------------------------------------------------"
|
|
281 |
# keys = xml_dict
|
|
282 |
# for key in xml_dict.keys():
|
|
283 |
# if key == "execution_blocks":
|
|
284 |
# for exe in xml_dict[key]:
|
|
285 |
# print key, "->"
|
|
286 |
# print exe['name']
|
|
287 |
# print exe['image_files']
|
|
288 |
# for file1 in exe['image_files']:
|
|
289 |
# print file1
|
|
290 |
#
|
|
291 |
# else:
|
|
292 |
# print key, "->", xml_dict[key]
|
|
293 |
#
|
|
294 |
# print xml_dict['diamonds_build_url']
|
|
295 |
# print xml_dict['testrun_name']
|
|
296 |
# print xml_dict['email_format']
|
|
297 |
# print xml_dict['email_subject']
|
|
298 |
#
|
|
299 |
# print "-------------------------------------------------"
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
generator = BootupTestDropGenerator()
|
|
304 |
_logger.info("generating drop file: %s" % config.drop_file)
|
|
305 |
generator.generate(xml_dict, output_file=config.drop_file, template_loc=config.template_loc)
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
def to_bool(param):
|
|
310 |
"""setting a true or false based on a param value"""
|
|
311 |
param = str(param).lower()
|
|
312 |
if "true" == param or "t" == param or "1" == param:
|
|
313 |
return "True"
|
|
314 |
else:
|
|
315 |
return "False"
|
|
316 |
|
|
317 |
def main():
|
|
318 |
"""Main entry point."""
|
|
319 |
|
|
320 |
|
|
321 |
cli = OptionParser(usage="%prog [options] PATH1 [PATH2 [PATH3 ...]]")
|
|
322 |
cli.add_option("--ats4-enabled", help="ATS4 enabled", default="True")
|
|
323 |
cli.add_option("--build-drive", help="Build area root drive")
|
|
324 |
cli.add_option("--drop-file", help="Name for the final drop zip file", default="ATSBootupDrop.zip")
|
|
325 |
|
|
326 |
cli.add_option("--minimum-flash-images", help="Minimum amount of flash images", default=2)
|
|
327 |
cli.add_option("--flash-images", help="Paths to the flash image files", default="")
|
|
328 |
|
|
329 |
cli.add_option("--template-loc", help="Custom template location", default="")
|
|
330 |
|
|
331 |
cli.add_option("--file-store", help="Destination path for reports.", default="")
|
|
332 |
cli.add_option("--report-email", help="Email notification receivers", default="")
|
|
333 |
cli.add_option("--testrun-name", help="Name of the test run", default="bootup_test")
|
|
334 |
cli.add_option("--alias-name", help="Name of the alias", default="alias")
|
|
335 |
cli.add_option("--device-type", help="Device type (e.g. 'PRODUCT')", default="unknown")
|
|
336 |
cli.add_option("--diamonds-build-url", help="Diamonds build url")
|
|
337 |
cli.add_option("--email-format", help="Format of an email", default="")
|
|
338 |
cli.add_option("--email-subject", help="Subject of an email", default="Bootup Testing")
|
|
339 |
cli.add_option("--verbose", help="Increase output verbosity", action="store_true", default=False)
|
|
340 |
|
|
341 |
opts, _ = cli.parse_args()
|
|
342 |
|
|
343 |
ats4_enabled = to_bool(opts.ats4_enabled)
|
|
344 |
|
|
345 |
if ats4_enabled == "False":
|
|
346 |
cli.error("Bootup test executes on ATS4. Set property 'ats4.enabled'")
|
|
347 |
|
|
348 |
if not opts.flash_images:
|
|
349 |
cli.error("no flash image files given")
|
|
350 |
if len(opts.flash_images.split(",")) < int(opts.minimum_flash_images):
|
|
351 |
cli.error("Not enough flash files: %i defined, %i needed" % (len(opts.flash_images.split(",")), int(opts.minimum_flash_images) ))
|
|
352 |
|
|
353 |
if opts.verbose:
|
|
354 |
_logger.setLevel(logging.DEBUG)
|
|
355 |
logging.basicConfig(level=logging.DEBUG)
|
|
356 |
_ = tempfile.mkdtemp()
|
|
357 |
config = Configuration(opts)
|
|
358 |
create_drop(config)
|
|
359 |
|
|
360 |
if __name__ == "__main__":
|
|
361 |
main()
|