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 7Usage: ./abort_suite.py 8 9This code exists to allow buildbot to abort a HWTest run if another part of 10the build fails while HWTesting is going on. If we're going to fail the 11build anyway, there's no point in continuing to run tests. 12 13This script aborts suite job and its children jobs. 14""" 15 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20import logging 21import sys 22 23from lucifer import autotest 24from skylab_suite import suite_parser 25from skylab_suite import suite_tracking 26from skylab_suite import swarming_lib 27 28 29def _abort_suite_tasks(client, suite_tasks): 30 aborted_suite_num = 0 31 for pt in suite_tasks: 32 logging.info('Aborting suite task %s', pt['task_id']) 33 client.abort_task(pt['task_id']) 34 if 'children_task_ids' not in pt: 35 logging.info('No child tasks for task %s', pt['task_id']) 36 continue 37 38 for ct in pt['children_task_ids']: 39 logging.info('Aborting task %s', ct) 40 client.abort_task(ct) 41 42 43def _get_suite_tasks_by_suite_ids(client, suite_task_ids): 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: 47 suite_tasks.append(client.query_task_by_id(suite_task_id)) 48 49 return suite_tasks 50 51 52def _get_suite_tasks_by_specs(client, suite_spec): 53 """Return a list of tasks with given suite_spec.""" 54 tags = {'pool': swarming_lib.SKYLAB_SUITE_POOL, 55 'board': suite_spec.board, 56 'build': suite_spec.test_source_build, 57 'suite': suite_spec.suite_name} 58 return client.query_task_by_tags(tags) 59 60 61def _abort_suite(options): 62 """Abort the suite. 63 64 This method aborts the suite job and its children jobs, including 65 'RUNNING' jobs. 66 """ 67 client = swarming_lib.Client(options.swarming_auth_json) 68 suite_spec = suite_parser.parse_suite_spec(options) 69 if options.suite_task_ids: 70 parent_tasks = _get_suite_tasks_by_suite_ids(client, 71 options.suite_task_ids) 72 else: 73 parent_tasks = _get_suite_tasks_by_specs(client, suite_spec) 74 75 _abort_suite_tasks(client, parent_tasks[:min(options.abort_limit, 76 len(parent_tasks))]) 77 logging.info('Suite %s/%s has been aborted.', suite_spec.test_source_build, 78 suite_spec.suite_name) 79 80 81def parse_args(): 82 """Parse and validate skylab suite args.""" 83 parser = suite_parser.make_parser() 84 options = parser.parse_args() 85 if not suite_parser.verify_and_clean_options(options): 86 parser.print_help() 87 sys.exit(1) 88 89 return options 90 91 92def main(): 93 """Entry point.""" 94 autotest.monkeypatch() 95 96 options = parse_args() 97 print (options.suite_task_ids) 98 print (options.abort_limit) 99 suite_tracking.setup_logging() 100 _abort_suite(options) 101 return 0 102 103 104if __name__ == "__main__": 105 sys.exit(main()) 106