blob: 85d54e87419c05748d949d61a94f37930e5f456c [file] [log] [blame]
Xixuan Wu53d15712018-06-12 10:52:55 -07001# 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
Aviv Keshetd3adbfa2019-03-19 11:43:24 -070029def _abort_suite_tasks(client, suite_tasks):
Xixuan Wu53d15712018-06-12 10:52:55 -070030 aborted_suite_num = 0
Xixuan Wuae8bfd22018-06-15 10:29:42 -070031 for pt in suite_tasks:
Xixuan Wu53d15712018-06-12 10:52:55 -070032 logging.info('Aborting suite task %s', pt['task_id'])
Aviv Keshetd3adbfa2019-03-19 11:43:24 -070033 client.abort_task(pt['task_id'])
Xixuan Wucc86e562018-07-13 16:08:07 -070034 if 'children_task_ids' not in pt:
35 logging.info('No child tasks for task %s', pt['task_id'])
36 continue
37
Xixuan Wu53d15712018-06-12 10:52:55 -070038 for ct in pt['children_task_ids']:
39 logging.info('Aborting task %s', ct)
Aviv Keshetd3adbfa2019-03-19 11:43:24 -070040 client.abort_task(ct)
Xixuan Wu53d15712018-06-12 10:52:55 -070041
Xixuan Wuae8bfd22018-06-15 10:29:42 -070042
Aviv Keshetd3adbfa2019-03-19 11:43:24 -070043def _get_suite_tasks_by_suite_ids(client, suite_task_ids):
Xixuan Wuae8bfd22018-06-15 10:29:42 -070044 """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 Keshetd3adbfa2019-03-19 11:43:24 -070047 suite_tasks.append(client.query_task_by_id(suite_task_id))
Xixuan Wuae8bfd22018-06-15 10:29:42 -070048
49 return suite_tasks
50
51
Aviv Keshetd56db9a2019-05-17 15:37:13 -070052def _get_suite_tasks_by_specs(client, board, build, suite):
53 """Return a list of tasks with given board/build/suite."""
Xixuan Wuae8bfd22018-06-15 10:29:42 -070054 tags = {'pool': swarming_lib.SKYLAB_SUITE_POOL,
Aviv Keshetd56db9a2019-05-17 15:37:13 -070055 'board': board,
56 'build': build,
57 'suite': suite}
Aviv Keshetd3adbfa2019-03-19 11:43:24 -070058 return client.query_task_by_tags(tags)
Xixuan Wuae8bfd22018-06-15 10:29:42 -070059
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 """
Aviv Keshetd3adbfa2019-03-19 11:43:24 -070067 client = swarming_lib.Client(options.swarming_auth_json)
Xixuan Wu9287fda2018-07-12 16:22:06 -070068 suite_spec = suite_parser.parse_suite_spec(options)
Xixuan Wuae8bfd22018-06-15 10:29:42 -070069 if options.suite_task_ids:
Aviv Keshetd3adbfa2019-03-19 11:43:24 -070070 parent_tasks = _get_suite_tasks_by_suite_ids(client,
71 options.suite_task_ids)
Xixuan Wuae8bfd22018-06-15 10:29:42 -070072 else:
Aviv Keshetd56db9a2019-05-17 15:37:13 -070073 parent_tasks = _get_suite_tasks_by_specs(
74 client, suite_spec.board, suite_spec.build, suite_spec.suite)
Xixuan Wuae8bfd22018-06-15 10:29:42 -070075
Aviv Keshetd3adbfa2019-03-19 11:43:24 -070076 _abort_suite_tasks(client, parent_tasks[:min(options.abort_limit,
77 len(parent_tasks))])
Xixuan Wu9287fda2018-07-12 16:22:06 -070078 logging.info('Suite %s/%s has been aborted.', suite_spec.test_source_build,
79 suite_spec.suite_name)
Xixuan Wu53d15712018-06-12 10:52:55 -070080
81
82def parse_args():
83 """Parse and validate skylab suite args."""
Aviv Keshetd56db9a2019-05-17 15:37:13 -070084
Xixuan Wu53d15712018-06-12 10:52:55 -070085 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
94def main():
95 """Entry point."""
96 autotest.monkeypatch()
97
98 options = parse_args()
Xixuan Wuae8bfd22018-06-15 10:29:42 -070099 print (options.suite_task_ids)
100 print (options.abort_limit)
Xixuan Wu53d15712018-06-12 10:52:55 -0700101 suite_tracking.setup_logging()
Xixuan Wuae8bfd22018-06-15 10:29:42 -0700102 _abort_suite(options)
Xixuan Wu53d15712018-06-12 10:52:55 -0700103 return 0
104
105
106if __name__ == "__main__":
107 sys.exit(main())