| OLD | NEW |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Utilities for testing with real repos (e.g. git).""" | 5 """Utilities for testing with real repos (e.g. git).""" |
| 6 | 6 |
| 7 | 7 |
| 8 import contextlib | 8 import contextlib |
| 9 import os | 9 import os |
| 10 import shutil | 10 import shutil |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 subprocess.check_call(['git', 'branch', '-f', 'origin/master', rev]) | 170 subprocess.check_call(['git', 'branch', '-f', 'origin/master', rev]) |
| 171 return { | 171 return { |
| 172 'root': repo['root'], | 172 'root': repo['root'], |
| 173 'revision': rev, | 173 'revision': rev, |
| 174 'spec': repo['spec'], | 174 'spec': repo['spec'], |
| 175 'author_name': author_name, | 175 'author_name': author_name, |
| 176 'author_email': author_email, | 176 'author_email': author_email, |
| 177 'message': message, | 177 'message': message, |
| 178 } | 178 } |
| 179 | 179 |
| 180 def train_recipes(self, repo, overrides=None): |
| 181 """Trains recipe simulation tests in given repo. |
| 182 |
| 183 Arguments: |
| 184 repo(dict): one of the repos returned by |repo_setup| |
| 185 overrides: iterable((str, str)): optional list of overrides |
| 186 first element of the tuple is the module name, and second |
| 187 is the overriding path |
| 188 """ |
| 189 if not overrides: |
| 190 overrides = [] |
| 191 with in_directory(repo['root']): |
| 192 args = [ |
| 193 sys.executable, self._recipe_tool, |
| 194 '--package', os.path.join( |
| 195 repo['root'], 'infra', 'config', 'recipes.cfg'), |
| 196 ] |
| 197 for repo, path in overrides: |
| 198 args.extend(['-O', '%s=%s' % (repo, path)]) |
| 199 args.extend([ |
| 200 'simulation_test', |
| 201 'train', |
| 202 ]) |
| 203 subprocess.check_output(args, stderr=subprocess.STDOUT) |
| 204 |
| 180 def update_recipe(self, repo, name, deps, calls): | 205 def update_recipe(self, repo, name, deps, calls): |
| 181 """Updates or creates a recipe in given repo. | 206 """Updates or creates a recipe in given repo. |
| 182 Commits the change. | 207 Commits the change. |
| 183 | 208 |
| 184 Arguments: | 209 Arguments: |
| 185 repo(dict): one of the repos returned by |repo_setup| | 210 repo(dict): one of the repos returned by |repo_setup| |
| 186 name(str): name of the recipe (without .py) | 211 name(str): name of the recipe (without .py) |
| 187 deps(iterable(str)): list of recipe dependencies (DEPS) | 212 deps(iterable(str)): list of recipe dependencies (DEPS) |
| 188 calls(iterable((str, str))): list of calls to recipe module | 213 calls(iterable((str, str))): list of calls to recipe module |
| 189 methods to make in the recipe; first element of the tuple | 214 methods to make in the recipe; first element of the tuple |
| 190 is the module name, and second is the method name | 215 is the module name, and second is the method name |
| 191 | 216 |
| 192 """ | 217 """ |
| 193 with in_directory(repo['root']): | 218 with in_directory(repo['root']): |
| 194 recipes_dir = 'recipes' | 219 recipes_dir = 'recipes' |
| 195 if not os.path.exists(recipes_dir): | 220 if not os.path.exists(recipes_dir): |
| 196 os.makedirs(recipes_dir) | 221 os.makedirs(recipes_dir) |
| 197 with open(os.path.join(recipes_dir, '%s.py' % name), 'w') as f: | 222 with open(os.path.join(recipes_dir, '%s.py' % name), 'w') as f: |
| 198 f.write('\n'.join([ | 223 f.write('\n'.join([ |
| 199 'DEPS = %r' % deps, | 224 'DEPS = %r' % deps, |
| 200 '', | 225 '', |
| 201 'def RunSteps(api):', | 226 'def RunSteps(api):', |
| 202 ] + [' api.%s.%s()' % c for c in calls] + [ | 227 ] + [' api.%s.%s()' % c for c in calls] + [ |
| 203 '', | 228 '', |
| 204 'def GenTests(api):', | 229 'def GenTests(api):', |
| 205 ' yield api.test("basic")', | 230 ' yield api.test("basic")', |
| 206 ])) | 231 ])) |
| 207 | 232 |
| 208 subprocess.check_output([ | 233 self.train_recipes(repo) |
| 209 sys.executable, self._recipe_tool, | |
| 210 '--package', os.path.join( | |
| 211 repo['root'], 'infra', 'config', 'recipes.cfg'), | |
| 212 'simulation_test', | |
| 213 'train', | |
| 214 ]) | |
| 215 | 234 |
| 216 subprocess.check_call( | 235 subprocess.check_call( |
| 217 ['git', 'add', os.path.join(recipes_dir, '%s.py' % name)]) | 236 ['git', 'add', os.path.join(recipes_dir, '%s.py' % name)]) |
| 218 subprocess.check_call( | 237 subprocess.check_call( |
| 219 ['git', 'add', os.path.join(recipes_dir, '%s.expected' % name)]) | 238 ['git', 'add', os.path.join(recipes_dir, '%s.expected' % name)]) |
| 220 return self.commit_in_repo(repo, message='recipe update') | 239 return self.commit_in_repo(repo, message='recipe update') |
| 221 | 240 |
| 222 def update_recipe_module(self, repo, name, methods): | 241 def update_recipe_module(self, repo, name, methods): |
| 223 """Updates or creates a recipe module in given repo. | 242 """Updates or creates a recipe module in given repo. |
| 224 Commits the change. | 243 Commits the change. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 message = ' '.join( | 288 message = ' '.join( |
| 270 ['update %r recipe_module: ' % name] + | 289 ['update %r recipe_module: ' % name] + |
| 271 ['%s(%s)' % t for t in methods.iteritems()] | 290 ['%s(%s)' % t for t in methods.iteritems()] |
| 272 ) | 291 ) |
| 273 return self.commit_in_repo(repo, message) | 292 return self.commit_in_repo(repo, message) |
| 274 | 293 |
| 275 def reset_repo(self, repo, revision): | 294 def reset_repo(self, repo, revision): |
| 276 """Resets repo contents to given revision.""" | 295 """Resets repo contents to given revision.""" |
| 277 with in_directory(repo['root']): | 296 with in_directory(repo['root']): |
| 278 subprocess.check_output(['git', 'reset', '--hard', revision]) | 297 subprocess.check_output(['git', 'reset', '--hard', revision]) |
| OLD | NEW |