Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Wrapper for aborting suites of tests. |
| 6 | |
| 7 | Usage: ./abort_suite.py |
| 8 | |
| 9 | This code exists to allow buildbot to abort a HWTest run if another part of |
| 10 | the build fails while HWTesting is going on. If we're going to fail the |
| 11 | build anyway, there's no point in continuing to run tests. |
| 12 | |
| 13 | This script aborts suite job and its children jobs. |
| 14 | """ |
| 15 | |
| 16 | from __future__ import absolute_import |
| 17 | from __future__ import division |
| 18 | from __future__ import print_function |
| 19 | |
| 20 | import logging |
| 21 | import sys |
| 22 | |
| 23 | from lucifer import autotest |
| 24 | from skylab_suite import suite_parser |
| 25 | from skylab_suite import suite_tracking |
| 26 | from skylab_suite import swarming_lib |
| 27 | |
| 28 | |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 29 | def _abort_suite_tasks(client, suite_tasks): |
Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 30 | aborted_suite_num = 0 |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 31 | for pt in suite_tasks: |
Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 32 | logging.info('Aborting suite task %s', pt['task_id']) |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 33 | client.abort_task(pt['task_id']) |
Xixuan Wu | cc86e56 | 2018-07-13 16:08:07 -0700 | [diff] [blame] | 34 | if 'children_task_ids' not in pt: |
| 35 | logging.info('No child tasks for task %s', pt['task_id']) |
| 36 | continue |
| 37 | |
Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 38 | for ct in pt['children_task_ids']: |
| 39 | logging.info('Aborting task %s', ct) |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 40 | client.abort_task(ct) |
Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 41 | |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 42 | |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 43 | def _get_suite_tasks_by_suite_ids(client, suite_task_ids): |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 44 | """Return a list of tasks with the given list of suite_task_ids.""" |
| 45 | suite_tasks = [] |
| 46 | for suite_task_id in suite_task_ids: |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 47 | suite_tasks.append(client.query_task_by_id(suite_task_id)) |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 48 | |
| 49 | return suite_tasks |
| 50 | |
| 51 | |
Aviv Keshet | d56db9a | 2019-05-17 15:37:13 -0700 | [diff] [blame^] | 52 | def _get_suite_tasks_by_specs(client, board, build, suite): |
| 53 | """Return a list of tasks with given board/build/suite.""" |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 54 | tags = {'pool': swarming_lib.SKYLAB_SUITE_POOL, |
Aviv Keshet | d56db9a | 2019-05-17 15:37:13 -0700 | [diff] [blame^] | 55 | 'board': board, |
| 56 | 'build': build, |
| 57 | 'suite': suite} |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 58 | return client.query_task_by_tags(tags) |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | def _abort_suite(options): |
| 62 | """Abort the suite. |
| 63 | |
| 64 | This method aborts the suite job and its children jobs, including |
| 65 | 'RUNNING' jobs. |
| 66 | """ |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 67 | client = swarming_lib.Client(options.swarming_auth_json) |
Xixuan Wu | 9287fda | 2018-07-12 16:22:06 -0700 | [diff] [blame] | 68 | suite_spec = suite_parser.parse_suite_spec(options) |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 69 | if options.suite_task_ids: |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 70 | parent_tasks = _get_suite_tasks_by_suite_ids(client, |
| 71 | options.suite_task_ids) |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 72 | else: |
Aviv Keshet | d56db9a | 2019-05-17 15:37:13 -0700 | [diff] [blame^] | 73 | parent_tasks = _get_suite_tasks_by_specs( |
| 74 | client, suite_spec.board, suite_spec.build, suite_spec.suite) |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 75 | |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 76 | _abort_suite_tasks(client, parent_tasks[:min(options.abort_limit, |
| 77 | len(parent_tasks))]) |
Xixuan Wu | 9287fda | 2018-07-12 16:22:06 -0700 | [diff] [blame] | 78 | logging.info('Suite %s/%s has been aborted.', suite_spec.test_source_build, |
| 79 | suite_spec.suite_name) |
Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 80 | |
| 81 | |
| 82 | def parse_args(): |
| 83 | """Parse and validate skylab suite args.""" |
Aviv Keshet | d56db9a | 2019-05-17 15:37:13 -0700 | [diff] [blame^] | 84 | |
Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 85 | parser = suite_parser.make_parser() |
| 86 | options = parser.parse_args() |
| 87 | if not suite_parser.verify_and_clean_options(options): |
| 88 | parser.print_help() |
| 89 | sys.exit(1) |
| 90 | |
| 91 | return options |
| 92 | |
| 93 | |
| 94 | def main(): |
| 95 | """Entry point.""" |
| 96 | autotest.monkeypatch() |
| 97 | |
| 98 | options = parse_args() |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 99 | print (options.suite_task_ids) |
| 100 | print (options.abort_limit) |
Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 101 | suite_tracking.setup_logging() |
Xixuan Wu | ae8bfd2 | 2018-06-15 10:29:42 -0700 | [diff] [blame] | 102 | _abort_suite(options) |
Xixuan Wu | 53d1571 | 2018-06-12 10:52:55 -0700 | [diff] [blame] | 103 | return 0 |
| 104 | |
| 105 | |
| 106 | if __name__ == "__main__": |
| 107 | sys.exit(main()) |