blob: 1642ade9a99a3b26d48f7d037cd1dc6dd09ae63c [file] [log] [blame]
Matthew Barthccb7f852016-11-23 17:43:02 -06001#!/usr/bin/env python
2
3"""
4This script determines the given package's openbmc dependencies from its
5configure.ac file where it downloads, configures, builds, and installs each of
6these dependencies. Then the given package is configured, built, and installed
7prior to executing its unit tests.
8"""
9
Matthew Barthd1810372016-12-19 16:57:21 -060010from git import Repo
Matthew Barthccb7f852016-11-23 17:43:02 -060011from urlparse import urljoin
Andrew Jefferya4e31c62018-03-08 13:45:28 +103012from subprocess import check_call, call, CalledProcessError
Matthew Barthccb7f852016-11-23 17:43:02 -060013import os
14import sys
Matthew Barth33df8792016-12-19 14:30:17 -060015import argparse
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -050016import re
17
18
19class DepTree():
20 """
21 Represents package dependency tree, where each node is a DepTree with a
22 name and DepTree children.
23 """
24
25 def __init__(self, name):
26 """
27 Create new DepTree.
28
29 Parameter descriptions:
30 name Name of new tree node.
31 """
32 self.name = name
33 self.children = list()
34
35 def AddChild(self, name):
36 """
37 Add new child node to current node.
38
39 Parameter descriptions:
40 name Name of new child
41 """
42 new_child = DepTree(name)
43 self.children.append(new_child)
44 return new_child
45
46 def AddChildNode(self, node):
47 """
48 Add existing child node to current node.
49
50 Parameter descriptions:
51 node Tree node to add
52 """
53 self.children.append(node)
54
55 def RemoveChild(self, name):
56 """
57 Remove child node.
58
59 Parameter descriptions:
60 name Name of child to remove
61 """
62 for child in self.children:
63 if child.name == name:
64 self.children.remove(child)
65 return
66
67 def GetNode(self, name):
68 """
69 Return node with matching name. Return None if not found.
70
71 Parameter descriptions:
72 name Name of node to return
73 """
74 if self.name == name:
75 return self
76 for child in self.children:
77 node = child.GetNode(name)
78 if node:
79 return node
80 return None
81
82 def GetParentNode(self, name, parent_node=None):
83 """
84 Return parent of node with matching name. Return none if not found.
85
86 Parameter descriptions:
87 name Name of node to get parent of
88 parent_node Parent of current node
89 """
90 if self.name == name:
91 return parent_node
92 for child in self.children:
93 found_node = child.GetParentNode(name, self)
94 if found_node:
95 return found_node
96 return None
97
98 def GetPath(self, name, path=None):
99 """
100 Return list of node names from head to matching name.
101 Return None if not found.
102
103 Parameter descriptions:
104 name Name of node
105 path List of node names from head to current node
106 """
107 if not path:
108 path = []
109 if self.name == name:
110 path.append(self.name)
111 return path
112 for child in self.children:
113 match = child.GetPath(name, path + [self.name])
114 if match:
115 return match
116 return None
117
118 def GetPathRegex(self, name, regex_str, path=None):
119 """
120 Return list of node paths that end in name, or match regex_str.
121 Return empty list if not found.
122
123 Parameter descriptions:
124 name Name of node to search for
125 regex_str Regex string to match node names
126 path Path of node names from head to current node
127 """
128 new_paths = []
129 if not path:
130 path = []
131 match = re.match(regex_str, self.name)
132 if (self.name == name) or (match):
133 new_paths.append(path + [self.name])
134 for child in self.children:
135 return_paths = None
136 full_path = path + [self.name]
137 return_paths = child.GetPathRegex(name, regex_str, full_path)
138 for i in return_paths:
139 new_paths.append(i)
140 return new_paths
141
142 def MoveNode(self, from_name, to_name):
143 """
144 Mode existing from_name node to become child of to_name node.
145
146 Parameter descriptions:
147 from_name Name of node to make a child of to_name
148 to_name Name of node to make parent of from_name
149 """
150 parent_from_node = self.GetParentNode(from_name)
151 from_node = self.GetNode(from_name)
152 parent_from_node.RemoveChild(from_name)
153 to_node = self.GetNode(to_name)
154 to_node.AddChildNode(from_node)
155
156 def ReorderDeps(self, name, regex_str):
157 """
158 Reorder dependency tree. If tree contains nodes with names that
159 match 'name' and 'regex_str', move 'regex_str' nodes that are
160 to the right of 'name' node, so that they become children of the
161 'name' node.
162
163 Parameter descriptions:
164 name Name of node to look for
165 regex_str Regex string to match names to
166 """
167 name_path = self.GetPath(name)
168 if not name_path:
169 return
170 paths = self.GetPathRegex(name, regex_str)
171 is_name_in_paths = False
172 name_index = 0
173 for i in range(len(paths)):
174 path = paths[i]
175 if path[-1] == name:
176 is_name_in_paths = True
177 name_index = i
178 break
179 if not is_name_in_paths:
180 return
181 for i in range(name_index + 1, len(paths)):
182 path = paths[i]
183 if name in path:
184 continue
185 from_name = path[-1]
186 self.MoveNode(from_name, name)
187
188 def GetInstallList(self):
189 """
190 Return post-order list of node names.
191
192 Parameter descriptions:
193 """
194 install_list = []
195 for child in self.children:
196 child_install_list = child.GetInstallList()
197 install_list.extend(child_install_list)
198 install_list.append(self.name)
199 return install_list
200
201 def PrintTree(self, level=0):
202 """
203 Print pre-order node names with indentation denoting node depth level.
204
205 Parameter descriptions:
206 level Current depth level
207 """
208 INDENT_PER_LEVEL = 4
209 print ' ' * (level * INDENT_PER_LEVEL) + self.name
210 for child in self.children:
211 child.PrintTree(level + 1)
Matthew Barth33df8792016-12-19 14:30:17 -0600212
213
214def check_call_cmd(dir, *cmd):
215 """
216 Verbose prints the directory location the given command is called from and
217 the command, then executes the command using check_call.
218
219 Parameter descriptions:
220 dir Directory location command is to be called from
221 cmd List of parameters constructing the complete command
222 """
223 printline(dir, ">", " ".join(cmd))
224 check_call(cmd)
Matthew Barthccb7f852016-11-23 17:43:02 -0600225
226
227def clone_pkg(pkg):
Matthew Barth33df8792016-12-19 14:30:17 -0600228 """
229 Clone the given openbmc package's git repository from gerrit into
230 the WORKSPACE location
231
232 Parameter descriptions:
233 pkg Name of the package to clone
234 """
Andrew Jeffery7be94ca2018-03-08 13:15:33 +1030235 pkg_dir = os.path.join(WORKSPACE, pkg)
236 if os.path.exists(os.path.join(pkg_dir, '.git')):
237 return pkg_dir
Matthew Barthccb7f852016-11-23 17:43:02 -0600238 pkg_repo = urljoin('https://gerrit.openbmc-project.xyz/openbmc/', pkg)
Andrew Jeffery7be94ca2018-03-08 13:15:33 +1030239 os.mkdir(pkg_dir)
240 printline(pkg_dir, "> git clone", pkg_repo, "./")
241 return Repo.clone_from(pkg_repo, pkg_dir).working_dir
Matthew Barth33df8792016-12-19 14:30:17 -0600242
243
244def get_deps(configure_ac):
245 """
246 Parse the given 'configure.ac' file for package dependencies and return
247 a list of the dependencies found.
248
249 Parameter descriptions:
250 configure_ac Opened 'configure.ac' file object
251 """
252 line = ""
Brad Bishopebb49112017-02-13 11:07:26 -0500253 dep_pkgs = set()
Matthew Barth33df8792016-12-19 14:30:17 -0600254 for cfg_line in configure_ac:
255 # Remove whitespace & newline
256 cfg_line = cfg_line.rstrip()
257 # Check for line breaks
258 if cfg_line.endswith('\\'):
259 line += str(cfg_line[:-1])
260 continue
261 line = line+cfg_line
262
263 # Find any defined dependency
Brad Bishopebb49112017-02-13 11:07:26 -0500264 line_has = lambda x: x if x in line else None
265 macros = set(filter(line_has, DEPENDENCIES.iterkeys()))
266 if len(macros) == 1:
267 macro = ''.join(macros)
268 deps = filter(line_has, DEPENDENCIES[macro].iterkeys())
269 dep_pkgs.update(map(lambda x: DEPENDENCIES[macro][x], deps))
270
Matthew Barth33df8792016-12-19 14:30:17 -0600271 line = ""
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500272 deps = list(dep_pkgs)
Matthew Barth33df8792016-12-19 14:30:17 -0600273
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500274 return deps
Matthew Barthccb7f852016-11-23 17:43:02 -0600275
276
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500277def install_deps(dep_list):
Matthew Barthccb7f852016-11-23 17:43:02 -0600278 """
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500279 Install each package in the ordered dep_list.
Matthew Barthccb7f852016-11-23 17:43:02 -0600280
281 Parameter descriptions:
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500282 dep_list Ordered list of dependencies
Matthew Barthccb7f852016-11-23 17:43:02 -0600283 """
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500284 for pkg in dep_list:
285 pkgdir = os.path.join(WORKSPACE, pkg)
286 # Build & install this package
William A. Kennington III7005cff2018-06-20 00:14:26 -0700287 conf_flags = [
288 '--disable-silent-rules',
William A. Kennington III32383352018-06-20 00:18:05 -0700289 '--enable-code-coverage',
290 '--enable-valgrind',
William A. Kennington III7005cff2018-06-20 00:14:26 -0700291 ]
Matthew Barthccb7f852016-11-23 17:43:02 -0600292 os.chdir(pkgdir)
293 # Add any necessary configure flags for package
294 if CONFIGURE_FLAGS.get(pkg) is not None:
Matthew Barth1d1c6732017-03-24 10:00:28 -0500295 conf_flags.extend(CONFIGURE_FLAGS.get(pkg))
Matthew Barth33df8792016-12-19 14:30:17 -0600296 check_call_cmd(pkgdir, './bootstrap.sh')
Matthew Barth1d1c6732017-03-24 10:00:28 -0500297 check_call_cmd(pkgdir, './configure', *conf_flags)
Matthew Barth33df8792016-12-19 14:30:17 -0600298 check_call_cmd(pkgdir, 'make')
299 check_call_cmd(pkgdir, 'make', 'install')
Matthew Barthccb7f852016-11-23 17:43:02 -0600300
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500301
302def build_dep_tree(pkg, pkgdir, dep_added, head, dep_tree=None):
303 """
304 For each package(pkg), starting with the package to be unit tested,
305 parse its 'configure.ac' file from within the package's directory(pkgdir)
306 for each package dependency defined recursively doing the same thing
307 on each package found as a dependency.
308
309 Parameter descriptions:
310 pkg Name of the package
311 pkgdir Directory where package source is located
312 dep_added Current list of dependencies and added status
313 head Head node of the dependency tree
314 dep_tree Current dependency tree node
315 """
316 if not dep_tree:
317 dep_tree = head
318 os.chdir(pkgdir)
319 # Open package's configure.ac
Andrew Jeffery2cb0c7a2018-03-08 13:19:08 +1030320 with open("/root/.depcache", "r") as depcache:
321 cached = depcache.readline()
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500322 with open("configure.ac", "rt") as configure_ac:
323 # Retrieve dependency list from package's configure.ac
324 configure_ac_deps = get_deps(configure_ac)
325 for dep_pkg in configure_ac_deps:
Andrew Jeffery2cb0c7a2018-03-08 13:19:08 +1030326 if dep_pkg in cached:
327 continue
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500328 # Dependency package not already known
329 if dep_added.get(dep_pkg) is None:
330 # Dependency package not added
331 new_child = dep_tree.AddChild(dep_pkg)
332 dep_added[dep_pkg] = False
Andrew Jeffery7be94ca2018-03-08 13:15:33 +1030333 dep_pkgdir = clone_pkg(dep_pkg)
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500334 # Determine this dependency package's
335 # dependencies and add them before
336 # returning to add this package
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500337 dep_added = build_dep_tree(dep_pkg,
Andrew Jeffery7be94ca2018-03-08 13:15:33 +1030338 dep_pkgdir,
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500339 dep_added,
340 head,
341 new_child)
342 else:
343 # Dependency package known and added
344 if dep_added[dep_pkg]:
345 continue
346 else:
347 # Cyclic dependency failure
348 raise Exception("Cyclic dependencies found in "+pkg)
349
350 if not dep_added[pkg]:
351 dep_added[pkg] = True
352
353 return dep_added
Matthew Barthccb7f852016-11-23 17:43:02 -0600354
355
356if __name__ == '__main__':
357 # CONFIGURE_FLAGS = [GIT REPO]:[CONFIGURE FLAGS]
358 CONFIGURE_FLAGS = {
Adriana Kobylak43c31e82017-02-13 09:28:35 -0600359 'phosphor-objmgr': ['--enable-unpatched-systemd'],
Matthew Barth1d1c6732017-03-24 10:00:28 -0500360 'sdbusplus': ['--enable-transaction'],
361 'phosphor-logging':
362 ['--enable-metadata-processing',
Deepak Kodihalli3a4e1b42017-06-08 09:52:35 -0500363 'YAML_DIR=/usr/local/share/phosphor-dbus-yaml/yaml']
Matthew Barthccb7f852016-11-23 17:43:02 -0600364 }
365
366 # DEPENDENCIES = [MACRO]:[library/header]:[GIT REPO]
367 DEPENDENCIES = {
368 'AC_CHECK_LIB': {'mapper': 'phosphor-objmgr'},
Matthew Barth710f3f02017-01-18 15:20:19 -0600369 'AC_CHECK_HEADER': {
370 'host-ipmid': 'phosphor-host-ipmid',
371 'sdbusplus': 'sdbusplus',
Saqib Khan66145052017-02-14 12:02:07 -0600372 'phosphor-logging/log.hpp': 'phosphor-logging',
Patrick Williamseab8a372017-01-30 11:21:32 -0600373 },
Brad Bishopebb49112017-02-13 11:07:26 -0500374 'AC_PATH_PROG': {'sdbus++': 'sdbusplus'},
Patrick Williamseab8a372017-01-30 11:21:32 -0600375 'PKG_CHECK_MODULES': {
Matthew Barth19e261e2017-02-01 12:55:22 -0600376 'phosphor-dbus-interfaces': 'phosphor-dbus-interfaces',
Patrick Williamsf128b402017-03-29 06:45:59 -0500377 'openpower-dbus-interfaces': 'openpower-dbus-interfaces',
Matt Spinler7be19032018-04-13 09:43:14 -0500378 'ibm-dbus-interfaces': 'ibm-dbus-interfaces',
Brad Bishopebb49112017-02-13 11:07:26 -0500379 'sdbusplus': 'sdbusplus',
380 'phosphor-logging': 'phosphor-logging',
381 },
Matthew Barthccb7f852016-11-23 17:43:02 -0600382 }
383
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500384 # DEPENDENCIES_REGEX = [GIT REPO]:[REGEX STRING]
385 DEPENDENCIES_REGEX = {
386 'phosphor-logging': '\S+-dbus-interfaces$'
387 }
388
Matthew Barth33df8792016-12-19 14:30:17 -0600389 # Set command line arguments
390 parser = argparse.ArgumentParser()
391 parser.add_argument("-w", "--workspace", dest="WORKSPACE", required=True,
392 help="Workspace directory location(i.e. /home)")
393 parser.add_argument("-p", "--package", dest="PACKAGE", required=True,
394 help="OpenBMC package to be unit tested")
395 parser.add_argument("-v", "--verbose", action="store_true",
396 help="Print additional package status messages")
Andrew Jeffery468309d2018-03-08 13:46:33 +1030397 parser.add_argument("-r", "--repeat", help="Repeat tests N times",
398 type=int, default=1)
Matthew Barth33df8792016-12-19 14:30:17 -0600399 args = parser.parse_args(sys.argv[1:])
400 WORKSPACE = args.WORKSPACE
401 UNIT_TEST_PKG = args.PACKAGE
402 if args.verbose:
403 def printline(*line):
404 for arg in line:
405 print arg,
406 print
407 else:
408 printline = lambda *l: None
Matthew Barthccb7f852016-11-23 17:43:02 -0600409
Adriana Kobylakbcee22b2018-01-10 16:58:27 -0600410 # First validate code formattting if repo has style formatting files.
411 # The format-code.sh checks for these files.
Andrew Geisslera28286d2018-01-10 11:00:00 -0800412 CODE_SCAN_DIR = WORKSPACE + "/" + UNIT_TEST_PKG
Adriana Kobylakbcee22b2018-01-10 16:58:27 -0600413 check_call_cmd(WORKSPACE, "./format-code.sh", CODE_SCAN_DIR)
Andrew Geisslera28286d2018-01-10 11:00:00 -0800414
Andrew Geissler71a7cc12018-01-31 14:18:37 -0800415 # The rest of this script is CI testing, which currently only supports
416 # Automake based repos. Check if this repo is Automake, if not exit
417 if not os.path.isfile(CODE_SCAN_DIR + "/configure.ac"):
418 print "Not a supported repo for CI Tests, exit"
419 quit()
420
Matthew Barthccb7f852016-11-23 17:43:02 -0600421 prev_umask = os.umask(000)
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500422 # Determine dependencies and add them
423 dep_added = dict()
424 dep_added[UNIT_TEST_PKG] = False
425 # Create dependency tree
426 dep_tree = DepTree(UNIT_TEST_PKG)
427 build_dep_tree(UNIT_TEST_PKG,
428 os.path.join(WORKSPACE, UNIT_TEST_PKG),
429 dep_added,
430 dep_tree)
431
432 # Reorder Dependency Tree
433 for pkg_name, regex_str in DEPENDENCIES_REGEX.iteritems():
434 dep_tree.ReorderDeps(pkg_name, regex_str)
435 if args.verbose:
436 dep_tree.PrintTree()
437 install_list = dep_tree.GetInstallList()
Gunnar Mills5f811802017-10-25 16:10:27 -0500438 # install reordered dependencies
Leonel Gonzaleza62a1a12017-03-24 11:03:47 -0500439 install_deps(install_list)
Matthew Barthccb7f852016-11-23 17:43:02 -0600440 os.chdir(os.path.join(WORKSPACE, UNIT_TEST_PKG))
Matthew Barth948b7cc2017-02-21 09:13:54 -0600441 # Refresh dynamic linker run time bindings for dependencies
442 check_call_cmd(os.path.join(WORKSPACE, UNIT_TEST_PKG), 'ldconfig')
Matthew Barthccb7f852016-11-23 17:43:02 -0600443 # Run package unit tests
Andrew Jefferya4e31c62018-03-08 13:45:28 +1030444 try:
Andrew Jeffery468309d2018-03-08 13:46:33 +1030445 cmd = [ 'make', 'check' ]
446 for i in range(0, args.repeat):
447 check_call_cmd(os.path.join(WORKSPACE, UNIT_TEST_PKG), *cmd)
Andrew Jefferya4e31c62018-03-08 13:45:28 +1030448 except CalledProcessError:
William A. Kennington III386e05c2018-06-20 00:13:11 -0700449 for root, _, files in os.walk(os.path.join(WORKSPACE, UNIT_TEST_PKG)):
450 if 'test-suite.log' not in files:
Andrew Jeffery88721792018-04-30 14:30:32 +0930451 continue
William A. Kennington III386e05c2018-06-20 00:13:11 -0700452 check_call_cmd(root, 'cat', os.path.join(root, 'test-suite.log'))
453 raise Exception('Unit tests failed')
William A. Kennington III32383352018-06-20 00:18:05 -0700454
455 with open(os.devnull, 'w') as devnull:
456 # Run unit tests through valgrind if it exists
457 top_dir = os.path.join(WORKSPACE, UNIT_TEST_PKG)
458 try:
459 cmd = [ 'make', '-n', 'check-valgrind' ]
460 check_call(cmd, stdout=devnull, stderr=devnull)
461 try:
462 cmd = [ 'make', 'check-valgrind' ]
463 check_call_cmd(top_dir, *cmd)
464 except CalledProcessError:
465 for root, _, files in os.walk(top_dir):
466 for f in files:
467 if re.search('test-suite-[a-z]+.log', f) is None:
468 continue
469 check_call_cmd(root, 'cat', os.path.join(root, f))
470 raise Exception('Valgrind tests failed')
471 except CalledProcessError:
472 pass
473
474 # Run code coverage if possible
475 try:
476 cmd = [ 'make', '-n', 'check-code-coverage' ]
477 check_call(cmd, stdout=devnull, stderr=devnull)
478 try:
479 cmd = [ 'make', 'check-code-coverage' ]
480 check_call_cmd(top_dir, *cmd)
481 except CalledProcessError:
482 raise Exception('Code coverage failed')
483 except CalledProcessError:
484 pass
485
Matthew Barthccb7f852016-11-23 17:43:02 -0600486 os.umask(prev_umask)