OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import hashlib | 5 import hashlib |
6 import os | |
7 import struct | 6 import struct |
8 import sys | |
9 from recipe_engine import recipe_test_api | 7 from recipe_engine import recipe_test_api |
10 | 8 |
11 # TODO(phajdan.jr): Clean up this somewhat ugly import. | |
12 sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'resources')) | |
13 import bot_update | |
14 | |
15 | 9 |
16 class BotUpdateTestApi(recipe_test_api.RecipeTestApi): | 10 class BotUpdateTestApi(recipe_test_api.RecipeTestApi): |
17 def output_json(self, master, builder, slave, root, first_sln, | 11 def output_json(self, root, first_sln, revision_mapping, fail_patch=False, |
18 revision_mapping, force=False, fail_patch=False, | |
19 output_manifest=False, fixed_revisions=None): | 12 output_manifest=False, fixed_revisions=None): |
20 """Deterministically synthesize json.output test data for gclient's | 13 """Deterministically synthesize json.output test data for gclient's |
21 --output-json option. | 14 --output-json option. |
22 """ | 15 """ |
23 active = bot_update.check_valid_host(master, builder, slave) or force | |
24 | |
25 output = { | 16 output = { |
26 'did_run': active, | 17 'did_run': True, |
27 'patch_failure': False | 18 'patch_failure': False |
28 } | 19 } |
29 | 20 |
30 # Add in extra json output if active. | 21 properties = { |
31 if active: | 22 property_name: self.gen_revision(project_name) |
32 properties = { | 23 for project_name, property_name in revision_mapping.iteritems() |
33 property_name: self.gen_revision(project_name) | 24 } |
34 for project_name, property_name in revision_mapping.iteritems() | 25 properties.update({ |
35 } | 26 '%s_cp' % property_name: ('refs/heads/master@{#%s}' % |
36 properties.update({ | 27 self.gen_commit_position(project_name)) |
37 '%s_cp' % property_name: ('refs/heads/master@{#%s}' % | 28 for project_name, property_name in revision_mapping.iteritems() |
38 self.gen_commit_position(project_name)) | 29 }) |
39 for project_name, property_name in revision_mapping.iteritems() | 30 |
| 31 output.update({ |
| 32 'patch_root': root or first_sln, |
| 33 'root': first_sln, |
| 34 'properties': properties, |
| 35 'step_text': 'Some step text' |
| 36 }) |
| 37 |
| 38 if output_manifest: |
| 39 output.update({ |
| 40 'manifest': { |
| 41 project_name: { |
| 42 'repository': 'https://fake.org/%s.git' % project_name, |
| 43 'revision': self.gen_revision(project_name), |
| 44 } |
| 45 for project_name in revision_mapping |
| 46 } |
40 }) | 47 }) |
41 | 48 |
42 output.update({ | 49 if fixed_revisions: |
43 'patch_root': root or first_sln, | 50 output['fixed_revisions'] = fixed_revisions |
44 'root': first_sln, | |
45 'properties': properties, | |
46 'step_text': 'Some step text' | |
47 }) | |
48 | 51 |
49 if output_manifest: | 52 if fail_patch: |
50 output.update({ | 53 output['log_lines'] = [('patch error', 'Patch failed to apply'),] |
51 'manifest': { | 54 output['patch_failure'] = True |
52 project_name: { | 55 output['patch_apply_return_code'] = 1 |
53 'repository': 'https://fake.org/%s.git' % project_name, | 56 if fail_patch == 'download': |
54 'revision': self.gen_revision(project_name), | 57 output['patch_apply_return_code'] = 3 |
55 } | |
56 for project_name in revision_mapping | |
57 } | |
58 }) | |
59 | |
60 if fixed_revisions: | |
61 output['fixed_revisions'] = fixed_revisions | |
62 | |
63 if fail_patch: | |
64 output['log_lines'] = [('patch error', 'Patch failed to apply'),] | |
65 output['patch_failure'] = True | |
66 output['patch_apply_return_code'] = 1 | |
67 if fail_patch == 'download': | |
68 output['patch_apply_return_code'] = 3 | |
69 return self.m.json.output(output) | 58 return self.m.json.output(output) |
70 | 59 |
71 @staticmethod | 60 @staticmethod |
72 def gen_revision(project): | 61 def gen_revision(project): |
73 """Hash project to bogus deterministic git hash values.""" | 62 """Hash project to bogus deterministic git hash values.""" |
74 h = hashlib.sha1(project) | 63 h = hashlib.sha1(project) |
75 return h.hexdigest() | 64 return h.hexdigest() |
76 | 65 |
77 @staticmethod | 66 @staticmethod |
78 def gen_commit_position(project): | 67 def gen_commit_position(project): |
79 """Hash project to bogus deterministic Cr-Commit-Position values.""" | 68 """Hash project to bogus deterministic Cr-Commit-Position values.""" |
80 h = hashlib.sha1(project) | 69 h = hashlib.sha1(project) |
81 return struct.unpack('!I', h.digest()[:4])[0] % 300000 | 70 return struct.unpack('!I', h.digest()[:4])[0] % 300000 |
OLD | NEW |