| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 from slave import recipe_api | 5 from slave import recipe_api |
| 6 | 6 |
| 7 class TestUtilsApi(recipe_api.RecipeApi): | 7 class TestUtilsApi(recipe_api.RecipeApi): |
| 8 @staticmethod | 8 @staticmethod |
| 9 def format_step_text(data): | 9 def format_step_text(data): |
| 10 """ | 10 """ |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 # Only displaying the section (even the header) when it's non-empty | 28 # Only displaying the section (even the header) when it's non-empty |
| 29 # simplifies caller code. | 29 # simplifies caller code. |
| 30 if section[1]: | 30 if section[1]: |
| 31 step_text.append('<br/>%s<br/>' % section[0]) | 31 step_text.append('<br/>%s<br/>' % section[0]) |
| 32 step_text.extend(('%s<br/>' % line for line in section[1])) | 32 step_text.extend(('%s<br/>' % line for line in section[1])) |
| 33 else: # pragma: no cover | 33 else: # pragma: no cover |
| 34 raise ValueError( | 34 raise ValueError( |
| 35 'Expected a one or two-element list, got %r instead.' % section) | 35 'Expected a one or two-element list, got %r instead.' % section) |
| 36 return ''.join(step_text) | 36 return ''.join(step_text) |
| 37 | 37 |
| 38 class Test(object): | 38 def determine_new_failures(self, caller_api, tests, deapply_patch_fn): |
| 39 """ | |
| 40 Base class for tests that can be retried after deapplying a previously | |
| 41 applied patch. | |
| 42 """ | |
| 43 | |
| 44 @property | |
| 45 def name(self): # pragma: no cover | |
| 46 """Name of the test.""" | |
| 47 raise NotImplementedError() | |
| 48 | |
| 49 def pre_run(self, suffix): # pragma: no cover | |
| 50 """Steps to execute before running the test.""" | |
| 51 return [] | |
| 52 | |
| 53 def run(self, suffix): # pragma: no cover | |
| 54 """Run the test. suffix is 'with patch' or 'without patch'.""" | |
| 55 raise NotImplementedError() | |
| 56 | |
| 57 def post_run(self, suffix): # pragma: no cover | |
| 58 """Steps to execute after running the test.""" | |
| 59 return [] | |
| 60 | |
| 61 def has_valid_results(self, suffix): # pragma: no cover | |
| 62 """ | |
| 63 Returns True if results (failures) are valid. | |
| 64 | |
| 65 This makes it possible to distinguish between the case of no failures | |
| 66 and the test failing to even report its results in machine-readable | |
| 67 format. | |
| 68 """ | |
| 69 raise NotImplementedError() | |
| 70 | |
| 71 def failures(self, suffix): # pragma: no cover | |
| 72 """Return list of failures (list of strings).""" | |
| 73 raise NotImplementedError() | |
| 74 | |
| 75 def _step_name(self, suffix): | |
| 76 """Helper to uniformly combine tests's name with a suffix.""" | |
| 77 return '%s (%s)' % (self.name, suffix) | |
| 78 | |
| 79 def determine_new_failures(self, tests, deapply_patch_fn): | |
| 80 """ | 39 """ |
| 81 Utility function for running steps with a patch applied, and retrying | 40 Utility function for running steps with a patch applied, and retrying |
| 82 failing steps without the patch. Failures from the run without the patch are | 41 failing steps without the patch. Failures from the run without the patch are |
| 83 ignored. | 42 ignored. |
| 84 | 43 |
| 85 Args: | 44 Args: |
| 45 caller_api - caller's recipe API; this is needed because self.m here |
| 46 is different than in the caller (different recipe modules |
| 47 get injected depending on caller's DEPS vs. this module's |
| 48 DEPS) |
| 86 tests - iterable of objects implementing the Test interface above | 49 tests - iterable of objects implementing the Test interface above |
| 87 deapply_patch_fn - function that takes a list of failing tests | 50 deapply_patch_fn - function that takes a list of failing tests |
| 88 and undoes any effect of the previously applied patch | 51 and undoes any effect of the previously applied patch |
| 89 """ | 52 """ |
| 90 # Convert iterable to list, since it is enumerated multiple times. | 53 # Convert iterable to list, since it is enumerated multiple times. |
| 91 tests = list(tests) | 54 tests = list(tests) |
| 92 | 55 |
| 93 if self.m.step_history.failed: | 56 if self.m.step_history.failed: |
| 94 yield self.m.python.inline( | 57 yield self.m.python.inline( |
| 95 'Aborting due to failed build state', | 58 'Aborting due to failed build state', |
| 96 "import sys; sys.exit(1)", | 59 "import sys; sys.exit(1)", |
| 97 always_run=True, abort_on_failure=True) | 60 always_run=True, abort_on_failure=True) |
| 98 return # won't actually hit this, but be explicit | 61 return # won't actually hit this, but be explicit |
| 99 | 62 |
| 100 def run(prefix, tests): | 63 def run(prefix, tests): |
| 101 yield (t.pre_run(prefix) for t in tests) | 64 yield (t.pre_run(caller_api, prefix) for t in tests) |
| 102 yield (t.run(prefix) for t in tests) | 65 yield (t.run(caller_api, prefix) for t in tests) |
| 103 yield (t.post_run(prefix) for t in tests) | 66 yield (t.post_run(caller_api, prefix) for t in tests) |
| 104 | 67 |
| 105 yield run('with patch', tests) | 68 yield run('with patch', tests) |
| 106 | 69 |
| 107 failing_tests = [] | 70 failing_tests = [] |
| 108 for t in tests: | 71 for t in tests: |
| 109 if not t.has_valid_results('with patch'): | 72 if not t.has_valid_results(caller_api, 'with patch'): |
| 110 yield self.m.python.inline( | 73 yield self.m.python.inline( |
| 111 t.name, | 74 t.name, |
| 112 r""" | 75 r""" |
| 113 import sys | 76 import sys |
| 114 print 'TEST RESULTS WERE INVALID' | 77 print 'TEST RESULTS WERE INVALID' |
| 115 sys.exit(1) | 78 sys.exit(1) |
| 116 """, | 79 """, |
| 117 always_run=True) | 80 always_run=True) |
| 118 elif t.failures('with patch'): | 81 elif t.failures(caller_api, 'with patch'): |
| 119 failing_tests.append(t) | 82 failing_tests.append(t) |
| 120 if not failing_tests: | 83 if not failing_tests: |
| 121 return | 84 return |
| 122 | 85 |
| 123 yield deapply_patch_fn(failing_tests) | 86 yield deapply_patch_fn(failing_tests) |
| 124 | 87 |
| 125 yield run('without patch', failing_tests) | 88 yield run('without patch', failing_tests) |
| 126 yield (self._summarize_retried_test(t) for t in failing_tests) | 89 yield (self._summarize_retried_test(caller_api, t) for t in failing_tests) |
| 127 | 90 |
| 128 def _summarize_retried_test(self, test): | 91 def _summarize_retried_test(self, caller_api, test): |
| 129 if not test.has_valid_results('without patch'): | 92 if not test.has_valid_results(caller_api, 'without patch'): |
| 130 return self.m.python.inline( | 93 return self.m.python.inline( |
| 131 test.name, | 94 test.name, |
| 132 r""" | 95 r""" |
| 133 import sys | 96 import sys |
| 134 print 'TEST RESULTS WERE INVALID' | 97 print 'TEST RESULTS WERE INVALID' |
| 135 sys.exit(1) | 98 sys.exit(1) |
| 136 """, | 99 """, |
| 137 always_run=True) | 100 always_run=True) |
| 138 | 101 |
| 139 ignored_failures = set(test.failures('without patch')) | 102 ignored_failures = set(test.failures(caller_api, 'without patch')) |
| 140 new_failures = set(test.failures('with patch')) - ignored_failures | 103 new_failures = set(test.failures(caller_api, 'with patch')) - ignored_failur
es |
| 141 | 104 |
| 142 def followup_fn(step_result): | 105 def followup_fn(step_result): |
| 143 p = step_result.presentation | 106 p = step_result.presentation |
| 144 | 107 |
| 145 p.step_text += self.format_step_text([ | 108 p.step_text += self.format_step_text([ |
| 146 ['failures:', new_failures], | 109 ['failures:', new_failures], |
| 147 ['ignored:', ignored_failures] | 110 ['ignored:', ignored_failures] |
| 148 ]) | 111 ]) |
| 149 | 112 |
| 150 if new_failures: | 113 if new_failures: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 175 """, | 138 """, |
| 176 args=[ | 139 args=[ |
| 177 self.m.json.input({ | 140 self.m.json.input({ |
| 178 'new': list(new_failures), | 141 'new': list(new_failures), |
| 179 'ignored': list(ignored_failures), | 142 'ignored': list(ignored_failures), |
| 180 }) | 143 }) |
| 181 ], | 144 ], |
| 182 followup_fn=followup_fn, | 145 followup_fn=followup_fn, |
| 183 always_run=True, | 146 always_run=True, |
| 184 ) | 147 ) |
| OLD | NEW |