OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import contextlib |
| 4 import copy |
| 5 import datetime |
| 6 import json |
| 7 import os |
| 8 import re |
| 9 import shutil |
| 10 import subprocess |
| 11 import sys |
| 12 import tempfile |
| 13 import time |
| 14 import unittest |
| 15 |
| 16 RECIPE_ENGINE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 17 THIRD_PARTY = os.path.dirname(RECIPE_ENGINE) |
| 18 sys.path.insert(0, THIRD_PARTY) |
| 19 |
| 20 from recipe_engine import package |
| 21 |
| 22 @contextlib.contextmanager |
| 23 def _in_directory(target_dir): |
| 24 old_dir = os.getcwd() |
| 25 os.chdir(target_dir) |
| 26 try: |
| 27 yield |
| 28 finally: |
| 29 os.chdir(old_dir) |
| 30 |
| 31 |
| 32 def _with_updates(inp, updates): |
| 33 if inp is None: |
| 34 return updates |
| 35 |
| 36 outp = copy.copy(inp) |
| 37 for k, v in updates.iteritems(): |
| 38 if isinstance(v, dict): |
| 39 outp[k] = _with_updates(inp[k], v) |
| 40 else: |
| 41 outp[k] = v |
| 42 return outp |
| 43 |
| 44 |
| 45 def _recstrify(thing): |
| 46 if isinstance(thing, basestring): |
| 47 return str(thing) |
| 48 elif isinstance(thing, dict): |
| 49 out = {} |
| 50 for k,v in thing.iteritems(): |
| 51 out[str(k)] = _recstrify(v) |
| 52 return out |
| 53 elif isinstance(thing, list): |
| 54 return map(_recstrify, thing) |
| 55 else: |
| 56 return thing |
| 57 |
| 58 |
| 59 class MultiRepoTest(unittest.TestCase): |
| 60 def _run_cmd(self, cmd, env=None): |
| 61 subprocess.call(cmd, env=env) |
| 62 |
| 63 def _create_repo(self, name, spec): |
| 64 repo_dir = os.path.join(self._root_dir, name) |
| 65 os.mkdir(repo_dir) |
| 66 with _in_directory(repo_dir): |
| 67 self._run_cmd(['git', 'init']) |
| 68 with open('recipe_package.pyl', 'w') as fh: |
| 69 json.dump(spec, fh, indent=2, separators=(',', ': ')) |
| 70 self._run_cmd(['git', 'add', 'recipe_package.pyl']) |
| 71 self._run_cmd(['git', 'commit', '-m', 'New recipe package']) |
| 72 rev = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip() |
| 73 return { |
| 74 'root': repo_dir, |
| 75 'revision': rev, |
| 76 'spec': spec, |
| 77 } |
| 78 |
| 79 def _commit_in_repo(self, repo, message='Empty commit', commit_date=None): |
| 80 with _in_directory(repo['root']): |
| 81 env = dict(os.environ) |
| 82 if commit_date: |
| 83 env.update({ 'GIT_COMMITTER_DATE': commit_date.isoformat() }) |
| 84 env.update({ 'GIT_AUTHOR_DATE': commit_date.isoformat() }) |
| 85 self._run_cmd(['git', 'commit', '-a', '--allow-empty', '-m', message], |
| 86 env=env) |
| 87 rev = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip() |
| 88 return { |
| 89 'root': repo['root'], |
| 90 'revision': rev, |
| 91 'spec': repo['spec'], |
| 92 } |
| 93 |
| 94 def setUp(self): |
| 95 self.maxDiff = None |
| 96 |
| 97 self._root_dir = tempfile.mkdtemp() |
| 98 self._recipe_tool = os.path.join( |
| 99 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
| 100 'recipes.py') |
| 101 |
| 102 def tearDown(self): |
| 103 shutil.rmtree(self._root_dir) |
| 104 |
| 105 def _repo_setup(self, repo_deps): |
| 106 # In order to avoid a topsort, we require that repo names are in |
| 107 # alphebetical dependency order -- i.e. later names depend on earlier |
| 108 # ones. |
| 109 repos = {} |
| 110 for k in sorted(repo_deps): |
| 111 repos[k] = self._create_repo(k, { |
| 112 'api_version': 0, |
| 113 'id': k, |
| 114 'deps': { |
| 115 d: { |
| 116 'repo': repos[d]['root'], |
| 117 'branch': 'master', |
| 118 'revision': repos[d]['revision'], |
| 119 'path': '', |
| 120 } for d in repo_deps[k] |
| 121 }, |
| 122 }) |
| 123 return repos |
| 124 |
| 125 def _run_roll(self, repo, expect_updates, commit=False): |
| 126 with _in_directory(repo['root']): |
| 127 stdout, _ = ( |
| 128 subprocess.Popen(['python', self._recipe_tool, 'roll'], |
| 129 stdout=subprocess.PIPE) |
| 130 .communicate()) |
| 131 if expect_updates: |
| 132 self.assertRegexpMatches(stdout, r'Wrote \S*recipe_package\.pyl') |
| 133 else: |
| 134 self.assertRegexpMatches(stdout, r'No consistent rolls found') |
| 135 |
| 136 if commit: |
| 137 assert expect_updates, 'Cannot commit when not expecting updates' |
| 138 git_match = re.search(r'^git commit .*', stdout, re.MULTILINE) |
| 139 self.assertTrue(git_match) |
| 140 git_command = git_match.group(0) |
| 141 subprocess.call(git_command, shell=True) |
| 142 rev = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip() |
| 143 return { |
| 144 'root': repo['root'], |
| 145 'revision': rev, |
| 146 'spec': repo['spec'], |
| 147 } |
| 148 |
| 149 |
| 150 def _get_spec(self, repo): |
| 151 with open(os.path.join(repo['root'], 'recipe_package.pyl')) as fp: |
| 152 return _recstrify(json.load(fp)) |
| 153 |
| 154 def _get_commit_date(self, repo): |
| 155 with _in_directory(repo['root']): |
| 156 commit_date = subprocess.check_output( |
| 157 ['git', 'log', '--format=%cI', |
| 158 '%s~..%s' % (repo['revision'], repo['revision'])]).strip() |
| 159 return package._parse_date(commit_date) |
| 160 |
| 161 def test_empty_roll(self): |
| 162 repos = self._repo_setup({ |
| 163 'a': [], |
| 164 'b': [ 'a' ], |
| 165 }) |
| 166 self._run_roll(repos['b'], expect_updates=False) |
| 167 |
| 168 def test_simple_roll(self): |
| 169 repos = self._repo_setup({ |
| 170 'a': [], |
| 171 'b': ['a'], |
| 172 }) |
| 173 new_a = self._commit_in_repo(repos['a']) |
| 174 self._run_roll(repos['b'], expect_updates=True) |
| 175 self.assertEqual( |
| 176 self._get_spec(repos['b']), |
| 177 _with_updates(repos['b']['spec'], { |
| 178 'deps': { |
| 179 'a': { |
| 180 'revision': new_a['revision'], |
| 181 }, |
| 182 }, |
| 183 })) |
| 184 self._run_roll(repos['b'], expect_updates=False) |
| 185 |
| 186 def test_indepdendent_roll(self): |
| 187 repos = self._repo_setup({ |
| 188 'b': [], |
| 189 'c': [], |
| 190 'd': ['b', 'c'], |
| 191 }) |
| 192 new_b = self._commit_in_repo(repos['b']) |
| 193 new_c = self._commit_in_repo(repos['c']) |
| 194 self._run_roll(repos['d'], expect_updates=True) |
| 195 # There is no guarantee on the order the two updates come in. |
| 196 # (Usually we sort by date but these commits are within 1 second) |
| 197 # However after one roll we expect only one of the two updates to |
| 198 # have come in. |
| 199 d_spec = self._get_spec(repos['d']) |
| 200 self.assertTrue( |
| 201 (d_spec['deps']['b']['revision'] == new_b['revision']) |
| 202 != (d_spec['deps']['c']['revision'] == new_c['revision'])) |
| 203 self._run_roll(repos['d'], expect_updates=True) |
| 204 self.assertEqual( |
| 205 self._get_spec(repos['d']), |
| 206 _with_updates(repos['d']['spec'], { |
| 207 'deps': { |
| 208 'b': { |
| 209 'revision': new_b['revision'], |
| 210 }, |
| 211 'c': { |
| 212 'revision': new_c['revision'], |
| 213 }, |
| 214 } |
| 215 })) |
| 216 self._run_roll(repos['d'], expect_updates=False) |
| 217 |
| 218 def test_dependent_roll(self): |
| 219 repos = self._repo_setup({ |
| 220 'a': [], |
| 221 'b': ['a'], |
| 222 'c': ['a'], |
| 223 'd': ['b', 'c'], |
| 224 }) |
| 225 new_a = self._commit_in_repo(repos['a']) |
| 226 new_b = self._run_roll(repos['b'], expect_updates=True, commit=True) |
| 227 new_c = self._run_roll(repos['c'], expect_updates=True, commit=True) |
| 228 |
| 229 # We only expect one roll here because to roll b without c would |
| 230 # result in an inconsistent revision of a, so we should skip it. |
| 231 self._run_roll(repos['d'], expect_updates=True) |
| 232 d_spec = self._get_spec(repos['d']) |
| 233 self.assertEqual( |
| 234 self._get_spec(repos['d']), |
| 235 _with_updates(repos['d']['spec'], { |
| 236 'deps': { |
| 237 'b': { |
| 238 'revision': new_b['revision'], |
| 239 }, |
| 240 'c': { |
| 241 'revision': new_c['revision'], |
| 242 }, |
| 243 } |
| 244 })) |
| 245 self._run_roll(repos['d'], expect_updates=False) |
| 246 |
| 247 def test_roll_coherence(self): |
| 248 repos = self._repo_setup({ |
| 249 'a': [], |
| 250 'b': [], |
| 251 'c': ['a','b'], |
| 252 'd': ['a','b'], |
| 253 'e': ['c','d'], |
| 254 }) |
| 255 # Add new commits in b and then a at a later time. Dependent repos |
| 256 # should always roll a first, so we get as many consistent rolls of |
| 257 # e as possible (this is "coherence"). |
| 258 base_date = datetime.datetime(2015, 10, 21, 4, 29, 00) |
| 259 |
| 260 new_b = self._commit_in_repo(repos['b'], commit_date=base_date) |
| 261 new_a = self._commit_in_repo( |
| 262 repos['a'], commit_date=base_date+datetime.timedelta(seconds=2)) |
| 263 |
| 264 new_c_with_b_rolled = self._run_roll( |
| 265 repos['c'], expect_updates=True, commit=True) |
| 266 self.assertEqual( |
| 267 self._get_spec(repos['c']), |
| 268 _with_updates(repos['c']['spec'], { |
| 269 'deps': { |
| 270 'b': { |
| 271 'revision': new_b['revision'], |
| 272 }, |
| 273 }, |
| 274 })) |
| 275 new_c_with_both_rolled = self._run_roll( |
| 276 repos['c'], expect_updates=True, commit=True) |
| 277 self._run_roll(repos['c'], expect_updates=False) |
| 278 |
| 279 new_d_with_b_rolled = self._run_roll( |
| 280 repos['d'], expect_updates=True, commit=True) |
| 281 self.assertEqual( |
| 282 self._get_spec(repos['d']), |
| 283 _with_updates(repos['d']['spec'], { |
| 284 'deps': { |
| 285 'b': { |
| 286 'revision': new_b['revision'], |
| 287 }, |
| 288 }, |
| 289 })) |
| 290 new_d_with_both_rolled = self._run_roll( |
| 291 repos['d'], expect_updates=True, commit=True) |
| 292 self._run_roll(repos['d'], expect_updates=False) |
| 293 |
| 294 self._run_roll(repos['e'], expect_updates=True) |
| 295 self.assertEqual( |
| 296 self._get_spec(repos['e']), |
| 297 _with_updates(repos['e']['spec'], { |
| 298 'deps': { |
| 299 'c': { |
| 300 'revision': new_c_with_b_rolled['revision'], |
| 301 }, |
| 302 'd': { |
| 303 'revision': new_d_with_b_rolled['revision'], |
| 304 }, |
| 305 }, |
| 306 })) |
| 307 self._run_roll(repos['e'], expect_updates=True) |
| 308 self.assertEqual( |
| 309 self._get_spec(repos['e']), |
| 310 _with_updates(repos['e']['spec'], { |
| 311 'deps': { |
| 312 'c': { |
| 313 'revision': new_c_with_both_rolled['revision'], |
| 314 }, |
| 315 'd': { |
| 316 'revision': new_d_with_both_rolled['revision'], |
| 317 }, |
| 318 }, |
| 319 })) |
| 320 self._run_roll(repos['e'], expect_updates=False) |
| 321 |
| 322 if __name__ == '__main__': |
| 323 unittest.main() |
OLD | NEW |