1859fdecd96355279570007849d8bc46ff71874d7Chris Masone# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2859fdecd96355279570007849d8bc46ff71874d7Chris Masone# Use of this source code is governed by a BSD-style license that can be
3859fdecd96355279570007849d8bc46ff71874d7Chris Masone# found in the LICENSE file.
4859fdecd96355279570007849d8bc46ff71874d7Chris Masone
5859fdecd96355279570007849d8bc46ff71874d7Chris Masone"""
6859fdecd96355279570007849d8bc46ff71874d7Chris MasoneThe job module contains the objects and methods used to
7859fdecd96355279570007849d8bc46ff71874d7Chris Masonemanage jobs in Autotest.
8859fdecd96355279570007849d8bc46ff71874d7Chris Masone
9859fdecd96355279570007849d8bc46ff71874d7Chris MasoneThe valid actions are:
10859fdecd96355279570007849d8bc46ff71874d7Chris Masonelist:    lists job(s)
11859fdecd96355279570007849d8bc46ff71874d7Chris Masonecreate:  create a job
12859fdecd96355279570007849d8bc46ff71874d7Chris Masoneabort:   abort job(s)
13859fdecd96355279570007849d8bc46ff71874d7Chris Masonestat:    detailed listing of job(s)
14859fdecd96355279570007849d8bc46ff71874d7Chris Masone
15859fdecd96355279570007849d8bc46ff71874d7Chris MasoneThe common options are:
16859fdecd96355279570007849d8bc46ff71874d7Chris Masone
17859fdecd96355279570007849d8bc46ff71874d7Chris MasoneSee topic_common.py for a High Level Design and Algorithm.
18859fdecd96355279570007849d8bc46ff71874d7Chris Masone"""
19859fdecd96355279570007849d8bc46ff71874d7Chris Masone
2004afc8f3b08c565b8988383c02689d547e688823Allen Liimport warnings
2104afc8f3b08c565b8988383c02689d547e688823Allen Li
22859fdecd96355279570007849d8bc46ff71874d7Chris Masonefrom autotest_lib.cli import topic_common, action_common
23859fdecd96355279570007849d8bc46ff71874d7Chris Masone
24859fdecd96355279570007849d8bc46ff71874d7Chris Masone
25b774aa277a9e351708d9a8396ecee846dd0a8e91Allen Liclass suite(topic_common.atest):
26859fdecd96355279570007849d8bc46ff71874d7Chris Masone    """Suite class
27859fdecd96355279570007849d8bc46ff71874d7Chris Masone    atest suite [create] [options]"""
28859fdecd96355279570007849d8bc46ff71874d7Chris Masone    usage_action = '[create]'
29859fdecd96355279570007849d8bc46ff71874d7Chris Masone    topic = msg_topic = 'suite'
30859fdecd96355279570007849d8bc46ff71874d7Chris Masone    msg_items = ''
31859fdecd96355279570007849d8bc46ff71874d7Chris Masone
32859fdecd96355279570007849d8bc46ff71874d7Chris Masone
33b774aa277a9e351708d9a8396ecee846dd0a8e91Allen Liclass suite_help(suite):
34859fdecd96355279570007849d8bc46ff71874d7Chris Masone    """Just here to get the atest logic working.
35859fdecd96355279570007849d8bc46ff71874d7Chris Masone    Usage is set by its parent"""
36859fdecd96355279570007849d8bc46ff71874d7Chris Masone    pass
37859fdecd96355279570007849d8bc46ff71874d7Chris Masone
38859fdecd96355279570007849d8bc46ff71874d7Chris Masone
39b774aa277a9e351708d9a8396ecee846dd0a8e91Allen Liclass suite_create(action_common.atest_create, suite):
40859fdecd96355279570007849d8bc46ff71874d7Chris Masone    """Class containing the code for creating a suite."""
41859fdecd96355279570007849d8bc46ff71874d7Chris Masone    msg_items = 'suite_id'
42859fdecd96355279570007849d8bc46ff71874d7Chris Masone
43859fdecd96355279570007849d8bc46ff71874d7Chris Masone    def __init__(self):
44b774aa277a9e351708d9a8396ecee846dd0a8e91Allen Li        super(suite_create, self).__init__()
45859fdecd96355279570007849d8bc46ff71874d7Chris Masone
46859fdecd96355279570007849d8bc46ff71874d7Chris Masone        self.parser.add_option('-b', '--board', help='Board to test. Required.',
47859fdecd96355279570007849d8bc46ff71874d7Chris Masone                               metavar='BOARD')
48859fdecd96355279570007849d8bc46ff71874d7Chris Masone        self.parser.add_option('-i', '--build',
49859fdecd96355279570007849d8bc46ff71874d7Chris Masone                               help='OS image to install before running the '
50859fdecd96355279570007849d8bc46ff71874d7Chris Masone                                    'test, e.g. '
51859fdecd96355279570007849d8bc46ff71874d7Chris Masone                                    'x86-alex-release/R17-1412.144.0-a1-b115.'
52859fdecd96355279570007849d8bc46ff71874d7Chris Masone                                    ' Required.',
53859fdecd96355279570007849d8bc46ff71874d7Chris Masone                               metavar='BUILD')
5462579122694934d942a4844a615bc034bc2e8aa1Chris Masone        self.parser.add_option('-c', '--check_hosts',
5562579122694934d942a4844a615bc034bc2e8aa1Chris Masone                               default=False,
5662579122694934d942a4844a615bc034bc2e8aa1Chris Masone                               help='Check that enough live hosts exist to '\
5762579122694934d942a4844a615bc034bc2e8aa1Chris Masone                                    'run this suite. Default False.',
5862579122694934d942a4844a615bc034bc2e8aa1Chris Masone                               action='store_true',
5962579122694934d942a4844a615bc034bc2e8aa1Chris Masone                               metavar='CHECK_HOSTS')
60f43d0eb812e32df81accf388f3aa73b051b35d47Alex Miller        self.parser.add_option('-f', '--file_bugs', default=False,
61f43d0eb812e32df81accf388f3aa73b051b35d47Alex Miller                               help='File bugs on test failures.',
62f43d0eb812e32df81accf388f3aa73b051b35d47Alex Miller                               action='store_true', metavar='FILE_BUGS')
6318c70b3d929b759ea9ba4e876517c6b8fd4d6921Chris Sosa        self.parser.add_option('-n', '--num', type=int,
6446d0eb11086d97db0178aa3cf316c8eb6d23c88dChris Masone                               help='Number of machines to schedule across.',
6546d0eb11086d97db0178aa3cf316c8eb6d23c88dChris Masone                               metavar='NUM')
66fc96a684c0b0827671e1e8c5dacb519fedb628c5Chris Masone        self.parser.add_option('-p', '--pool', help='Pool of machines to use.',
67fc96a684c0b0827671e1e8c5dacb519fedb628c5Chris Masone                               metavar='POOL')
689512241978928631e38d59b5aca6467172fd404bDan Shi        self.parser.add_option('-w', '--wait_for_results',
699512241978928631e38d59b5aca6467172fd404bDan Shi                               default=True,
709512241978928631e38d59b5aca6467172fd404bDan Shi                               help=('Set to False for suite job to exit '
719512241978928631e38d59b5aca6467172fd404bDan Shi                                     'without waiting for test jobs to finish. '
729512241978928631e38d59b5aca6467172fd404bDan Shi                                     'Default is True.'),
739512241978928631e38d59b5aca6467172fd404bDan Shi                               metavar='WAIT_FOR_RESULTS')
74059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi        self.parser.add_option('-d', '--delay_minutes', type=int, default=0,
75059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                               help=('Delay the creation of test jobs for a '
76059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                                     'given number of minutes. This argument '
77059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                                     'can be used to force provision jobs '
78059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                                     'being delayed, which helps to distribute '
79059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                                     'loads across devservers.'),
80059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                               metavar='DELAY_MINUTES')
81859fdecd96355279570007849d8bc46ff71874d7Chris Masone
82859fdecd96355279570007849d8bc46ff71874d7Chris Masone
83859fdecd96355279570007849d8bc46ff71874d7Chris Masone    def parse(self):
84859fdecd96355279570007849d8bc46ff71874d7Chris Masone        board_info = topic_common.item_parse_info(attribute_name='board',
85859fdecd96355279570007849d8bc46ff71874d7Chris Masone                                                  inline_option='board')
86859fdecd96355279570007849d8bc46ff71874d7Chris Masone        build_info = topic_common.item_parse_info(attribute_name='build',
87859fdecd96355279570007849d8bc46ff71874d7Chris Masone                                                  inline_option='build')
8862579122694934d942a4844a615bc034bc2e8aa1Chris Masone        pool_info = topic_common.item_parse_info(attribute_name='pool',
8962579122694934d942a4844a615bc034bc2e8aa1Chris Masone                                                 inline_option='pool')
9046d0eb11086d97db0178aa3cf316c8eb6d23c88dChris Masone        num_info = topic_common.item_parse_info(attribute_name='num',
9146d0eb11086d97db0178aa3cf316c8eb6d23c88dChris Masone                                                inline_option='num')
9262579122694934d942a4844a615bc034bc2e8aa1Chris Masone        check_info = topic_common.item_parse_info(attribute_name='check_hosts',
9362579122694934d942a4844a615bc034bc2e8aa1Chris Masone                                                  inline_option='check_hosts')
94f43d0eb812e32df81accf388f3aa73b051b35d47Alex Miller        bugs_info = topic_common.item_parse_info(attribute_name='file_bugs',
95f43d0eb812e32df81accf388f3aa73b051b35d47Alex Miller                                                 inline_option='file_bugs')
96859fdecd96355279570007849d8bc46ff71874d7Chris Masone        suite_info = topic_common.item_parse_info(attribute_name='name',
97859fdecd96355279570007849d8bc46ff71874d7Chris Masone                                                  use_leftover=True)
989512241978928631e38d59b5aca6467172fd404bDan Shi        wait_for_results_info = topic_common.item_parse_info(
99059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                attribute_name='wait_for_results',
100059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                inline_option='wait_for_results')
101059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi        delay_minutes_info = topic_common.item_parse_info(
102059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                attribute_name='delay_minutes',
103059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi                inline_option='delay_minutes')
104859fdecd96355279570007849d8bc46ff71874d7Chris Masone
105b774aa277a9e351708d9a8396ecee846dd0a8e91Allen Li        options, leftover = suite.parse(
10662579122694934d942a4844a615bc034bc2e8aa1Chris Masone            self,
10746d0eb11086d97db0178aa3cf316c8eb6d23c88dChris Masone            [suite_info, board_info, build_info, pool_info, num_info,
108059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi             check_info, bugs_info, wait_for_results_info, delay_minutes_info],
10962579122694934d942a4844a615bc034bc2e8aa1Chris Masone            req_items='name')
110859fdecd96355279570007849d8bc46ff71874d7Chris Masone        self.data = {}
111859fdecd96355279570007849d8bc46ff71874d7Chris Masone        name = getattr(self, 'name')
112859fdecd96355279570007849d8bc46ff71874d7Chris Masone        if len(name) > 1:
113859fdecd96355279570007849d8bc46ff71874d7Chris Masone            self.invalid_syntax('Too many arguments specified, only expected '
114859fdecd96355279570007849d8bc46ff71874d7Chris Masone                                'to receive suite name: %s' % name)
115859fdecd96355279570007849d8bc46ff71874d7Chris Masone        self.data['suite_name'] = name[0]
116fc96a684c0b0827671e1e8c5dacb519fedb628c5Chris Masone        self.data['pool'] = options.pool  # None is OK.
11704afc8f3b08c565b8988383c02689d547e688823Allen Li        if options.num is not None:
11804afc8f3b08c565b8988383c02689d547e688823Allen Li            warnings.warn('num is deprecated')
11904afc8f3b08c565b8988383c02689d547e688823Allen Li        del options.num
12062579122694934d942a4844a615bc034bc2e8aa1Chris Masone        self.data['check_hosts'] = options.check_hosts
121f43d0eb812e32df81accf388f3aa73b051b35d47Alex Miller        self.data['file_bugs'] = options.file_bugs
1229512241978928631e38d59b5aca6467172fd404bDan Shi        self.data['wait_for_results'] = options.wait_for_results
123059261aaf4455fa6ab5e9d4369bb0650143ee467Dan Shi        self.data['delay_minutes'] = options.delay_minutes
124859fdecd96355279570007849d8bc46ff71874d7Chris Masone        if options.board:
125859fdecd96355279570007849d8bc46ff71874d7Chris Masone            self.data['board'] = options.board
126859fdecd96355279570007849d8bc46ff71874d7Chris Masone        else:
127859fdecd96355279570007849d8bc46ff71874d7Chris Masone            self.invalid_syntax('--board is required.')
128859fdecd96355279570007849d8bc46ff71874d7Chris Masone        if options.build:
129859fdecd96355279570007849d8bc46ff71874d7Chris Masone            self.data['build'] = options.build
130859fdecd96355279570007849d8bc46ff71874d7Chris Masone        else:
131859fdecd96355279570007849d8bc46ff71874d7Chris Masone            self.invalid_syntax('--build is required.')
132859fdecd96355279570007849d8bc46ff71874d7Chris Masone
133859fdecd96355279570007849d8bc46ff71874d7Chris Masone        return options, leftover
134859fdecd96355279570007849d8bc46ff71874d7Chris Masone
135859fdecd96355279570007849d8bc46ff71874d7Chris Masone
136859fdecd96355279570007849d8bc46ff71874d7Chris Masone    def execute(self):
137859fdecd96355279570007849d8bc46ff71874d7Chris Masone        return [self.execute_rpc(op='create_suite_job', **self.data)]
138