ros_prerelease_tests.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import os
4 import subprocess
5 import sys
6 
7 import urllib.request
8 import yaml
9 
10 import argparse
11 
12 from termcolor import cprint
13 
14 
15 def print_blue(x): return cprint(x, 'blue', attrs=['bold'])
16 def print_cyan(x): return cprint(x, 'cyan', attrs=['bold'])
17 def print_green(x): return cprint(x, 'green', attrs=['bold'])
18 def print_magenta(x): return cprint(x, 'magenta', attrs=['bold'])
19 def print_red(x): return cprint(x, 'red', attrs=['bold'])
20 def print_yellow(x): return cprint(x, 'yellow', attrs=['bold'])
21 
22 
23 REPO_URL = 'https://raw.githubusercontent.com/ros-infrastructure/ros_buildfarm_config'
24 BRANCH = 'production'
25 INDEX_FILE = 'index.yaml'
26 
27 
28 def run_test(target, branch):
29  test_name = "%(ros_distro)s_%(os_distro)s_%(os_release)s_%(arch)s" % target
30 
31  config = target
32  config['branch'] = branch
33  config['test_name'] = test_name
34 
35  # move working directory into test folder
36  os.mkdir(test_name)
37  os.chdir(test_name)
38 
39  # generate prerelease scripts
40  print("Generating prerelease scripts...")
41  generate_prerelease_command = """
42  generate_prerelease_script.py \\
43  https://raw.githubusercontent.com/ros-infrastructure/ros_buildfarm_config/production/index.yaml \\
44  %(ros_distro)s default %(os_distro)s %(os_release)s %(arch)s \\
45  --custom-repo \\
46  async_com__custom-1:git:https://github.com/dpkoch/async_comm.git:%(branch)s \\
47  --level 0 \\
48  --output-dir ./
49  """ % config
50 
51  with open("../%s-generate.log" % (test_name), 'w') as log_file:
52  process = subprocess.run(
53  generate_prerelease_command, shell=True, stdout=log_file, stderr=subprocess.STDOUT)
54  if process.returncode != 0:
55  print_yellow("Generating prerelease scripts failed!")
56  os.chdir('..')
57  print_red("[Failed]")
58  return False
59 
60  # run prerelease test
61  print("Running prerelease tests...")
62  with open("../%s-test.log" % (test_name), 'w') as log_file:
63  process = subprocess.run(
64  "./prerelease.sh", stdout=log_file, stderr=subprocess.STDOUT)
65  os.chdir('..')
66  if process.returncode == 0:
67  print_green("[Passed]")
68  return True
69  else:
70  print_red("[Failed]")
71  return False
72 
73 
74 def get_repo_file(repo_path):
75  url = '/'.join([REPO_URL, BRANCH, repo_path])
76  with urllib.request.urlopen(url) as response:
77  return response.read()
78 
79 
81  targets = []
82  index_yaml = yaml.safe_load(get_repo_file(INDEX_FILE))
83  for ros_distro in distros:
84  config_files = index_yaml['distributions'][ros_distro]['release_builds']
85  for file_path in config_files.values():
86  config_yaml = yaml.safe_load(get_repo_file(file_path))
87  for os_distro in config_yaml['targets']:
88  for os_release in config_yaml['targets'][os_distro]:
89  for arch in config_yaml['targets'][os_distro][os_release]:
90  targets.append({'ros_distro': ros_distro,
91  'os_distro': os_distro,
92  'os_release': os_release,
93  'arch': arch})
94  return targets
95 
96 
97 def filter_prerelease_targets(targets, os_distro, os_release, arch):
98  def check_attribute(value, keys):
99  if len(keys) > 0:
100  return value in keys
101  else:
102  return True
103 
104  filtered_targets = []
105  for target in targets:
106  if check_attribute(target['os_distro'], os_distro) \
107  and check_attribute(target['os_release'], os_release) \
108  and check_attribute(target['arch'], arch):
109  filtered_targets.append(target)
110 
111  return filtered_targets
112 
113 
115 
116  print_magenta("Running all tests for the \"%s\" branch" % (args.branch))
117 
118  print()
119  print_magenta("Running tests for the following ROS distros:")
120  for distro in args.distro:
121  print(distro)
122 
123  print()
124  print("Retrieving release targets...")
126  args.distro), args.os, args.release, args.arch)
127 
128  print()
129  if len(targets) > 0:
130  print_magenta("The following release targets will be tested:")
131  for target in targets:
132  print("%(ros_distro)s %(os_distro)s %(os_release)s %(arch)s" % target)
133  else:
134  print_yellow("No valid prerelease targets!")
135  return False
136 
137  # run all tests and aggregate overall result into exit code
138  failed_tests = 0
139  for target in targets:
140  print()
141  print_blue(
142  "Testing %(ros_distro)s %(os_distro)s %(os_release)s %(arch)s" % target)
143  if not run_test(target, args.branch):
144  failed_tests += 1
145 
146  # print and return overall result
147  print()
148  if failed_tests > 0:
149  print_red("Failed %d of %d tests." % (failed_tests, len(targets)))
150  return False
151  else:
152  print_green("Passed %d tests." % (len(targets)))
153  return True
154 
155 
156 if __name__ == '__main__':
157 
158  # process command line arguments
159  parser = argparse.ArgumentParser(description='Run ROS prerelease tests')
160  parser.add_argument(
161  'branch', help='The branch for which to run the prerelease tests')
162  parser.add_argument('--distro',
163  nargs='*',
164  type=str,
165  default=['kinetic', 'melodic', 'noetic'],
166  help='A list of one or more ROS distros for which to run the prerelease tests (default: %(default)s)')
167  parser.add_argument('--os',
168  nargs='*',
169  type=str,
170  default=[],
171  help='A list of one or more OS distros (e.g. ubuntu, debian) for which to run the prerelease tests')
172  parser.add_argument('--release',
173  nargs='*',
174  type=str,
175  default=[],
176  help='A list of one or more OS releases (e.g. bionic, stretch) for which to run the prerelease tests')
177  parser.add_argument('--arch',
178  nargs='*',
179  type=str,
180  default=[],
181  help='A list of one or more architectures (e.g. amd64, armhf) for which to run the prerelease tests')
182 
183  args = parser.parse_args()
184 
185  # check that working directory is empty
186  if os.listdir('.'):
187  print("Non-empty directory; please run in an empty directory. Aborting.")
188  exit(3)
189 
190  exit_code = 0 if run_prerelease_tests(args) else 1
191  print_cyan("Exit code: %d" % (exit_code))
192  exit(exit_code)
def run_test(target, branch)
def get_prerelease_targets(distros)
def filter_prerelease_targets(targets, os_distro, os_release, arch)
def get_repo_file(repo_path)


async_comm
Author(s):
autogenerated on Fri May 14 2021 02:35:38