blob: 4033aea5778afd5829a9b2e3e7682906954847cb [file] [log] [blame]
Chris Masonefad911a2012-03-29 12:30:26 -07001# Copyright (c) 2012 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
Chris Masone67f06d62012-04-12 15:16:56 -07005
Chris Masone96f16632012-04-04 18:36:03 -07006import logging, re
Chris Masone67f06d62012-04-12 15:16:56 -07007import deduping_scheduler, forgiving_config_parser
8from distutils import version
Alex Miller511a9e32012-07-03 09:16:47 -07009from constants import Labels
Chris Masone96f16632012-04-04 18:36:03 -070010
11
12class MalformedConfigEntry(Exception):
13 """Raised to indicate a failure to parse a Task out of a config."""
14 pass
Chris Masonefad911a2012-03-29 12:30:26 -070015
16
Chris Masone67f06d62012-04-12 15:16:56 -070017BARE_BRANCHES = ['factory', 'firmware']
18
19
20def PickBranchName(type, milestone):
21 if type in BARE_BRANCHES:
22 return type
23 return milestone
24
25
Chris Masone013859b2012-04-01 13:45:26 -070026class Task(object):
Chris Masonefad911a2012-03-29 12:30:26 -070027 """Represents an entry from the scheduler config. Can schedule itself.
28
29 Each entry from the scheduler config file maps one-to-one to a
Chris Masone013859b2012-04-01 13:45:26 -070030 Task. Each instance has enough info to schedule itself
Chris Masonefad911a2012-03-29 12:30:26 -070031 on-demand with the AFE.
32
33 This class also overrides __hash__() and all comparitor methods to enable
34 correct use in dicts, sets, etc.
35 """
36
Chris Masone96f16632012-04-04 18:36:03 -070037
38 @staticmethod
39 def CreateFromConfigSection(config, section):
40 """Create a Task from a section of a config file.
41
42 The section to parse should look like this:
43 [TaskName]
44 suite: suite_to_run # Required
45 run_on: event_on which to run # Required
46 branch_specs: factory,firmware,>=R12 # Optional
47 pool: pool_of_devices # Optional
48
Chris Masone67f06d62012-04-12 15:16:56 -070049 By default, Tasks run on all release branches, not factory or firmware.
50
Chris Masone96f16632012-04-04 18:36:03 -070051 @param config: a ForgivingConfigParser.
52 @param section: the section to parse into a Task.
53 @return keyword, Task object pair. One or both will be None on error.
54 @raise MalformedConfigEntry if there's a problem parsing |section|.
55 """
Alex Millerbb535e22012-07-11 20:11:33 -070056 allowed = set(['suite', 'run_on', 'branch_specs', 'pool'])
57 # The parameter of union() is the keys under the section in the config
58 # The union merges this with the allowed set, so if any optional keys
59 # are omitted, then they're filled in. If any extra keys are present,
60 # then they will expand unioned set, causing it to fail the following
61 # comparison against the allowed set.
62 section_headers = allowed.union(dict(config.items(section)).keys())
63 if allowed != section_headers:
64 raise MalformedConfigEntry('unknown entries: %s' %
65 ", ".join(map(str, section_headers.difference(allowed))))
66
Chris Masone96f16632012-04-04 18:36:03 -070067 keyword = config.getstring(section, 'run_on')
68 suite = config.getstring(section, 'suite')
69 branches = config.getstring(section, 'branch_specs')
70 pool = config.getstring(section, 'pool')
71 if not keyword:
72 raise MalformedConfigEntry('No event to |run_on|.')
73 if not suite:
74 raise MalformedConfigEntry('No |suite|')
75 specs = []
76 if branches:
77 specs = re.split('\s*,\s*', branches)
78 Task.CheckBranchSpecs(specs)
Chris Masonecc4631d2012-04-20 12:06:39 -070079 return keyword, Task(section, suite, specs, pool)
Chris Masone96f16632012-04-04 18:36:03 -070080
81
82 @staticmethod
83 def CheckBranchSpecs(branch_specs):
84 """Make sure entries in the list branch_specs are correctly formed.
85
Chris Masone67f06d62012-04-12 15:16:56 -070086 We accept any of BARE_BRANCHES in |branch_specs|, as
Chris Masone96f16632012-04-04 18:36:03 -070087 well as _one_ string of the form '>=RXX', where 'RXX' is a
88 CrOS milestone number.
89
90 @param branch_specs: an iterable of branch specifiers.
91 @raise MalformedConfigEntry if there's a problem parsing |branch_specs|.
92 """
93 have_seen_numeric_constraint = False
94 for branch in branch_specs:
Chris Masone67f06d62012-04-12 15:16:56 -070095 if branch in BARE_BRANCHES:
Chris Masone96f16632012-04-04 18:36:03 -070096 continue
97 if branch.startswith('>=R') and not have_seen_numeric_constraint:
98 have_seen_numeric_constraint = True
99 continue
Chris Masone97cf0a72012-05-16 09:55:52 -0700100 raise MalformedConfigEntry("%s isn't a valid branch spec." % branch)
Chris Masone96f16632012-04-04 18:36:03 -0700101
102
Chris Masonecc4631d2012-04-20 12:06:39 -0700103 def __init__(self, name, suite, branch_specs, pool=None):
Chris Masonefad911a2012-03-29 12:30:26 -0700104 """Constructor
105
Chris Masone96f16632012-04-04 18:36:03 -0700106 Given an iterable in |branch_specs|, pre-vetted using CheckBranchSpecs,
107 we'll store them such that _FitsSpec() can be used to check whether a
108 given branch 'fits' with the specifications passed in here.
109 For example, given branch_specs = ['factory', '>=R18'], we'd set things
110 up so that _FitsSpec() would return True for 'factory', or 'RXX'
111 where XX is a number >= 18.
112
113 Given branch_specs = ['factory', 'firmware'], _FitsSpec()
114 would pass only those two specific strings.
115
116 Example usage:
Chris Masonecc4631d2012-04-20 12:06:39 -0700117 t = Task('Name', 'suite', ['factory', '>=R18'])
Chris Masone96f16632012-04-04 18:36:03 -0700118 t._FitsSpec('factory') # True
119 t._FitsSpec('R19') # True
120 t._FitsSpec('R17') # False
121 t._FitsSpec('firmware') # False
122 t._FitsSpec('goober') # False
123
Chris Masonefad911a2012-03-29 12:30:26 -0700124 @param suite: the name of the suite to run, e.g. 'bvt'
Chris Masone96f16632012-04-04 18:36:03 -0700125 @param branch_specs: a pre-vetted iterable of branch specifiers,
126 e.g. ['>=R18', 'factory']
Chris Masonefad911a2012-03-29 12:30:26 -0700127 @param pool: the pool of machines to use for scheduling purposes.
128 Default: None
129 """
Chris Masonecc4631d2012-04-20 12:06:39 -0700130 self._name = name
Chris Masonefad911a2012-03-29 12:30:26 -0700131 self._suite = suite
Chris Masone96f16632012-04-04 18:36:03 -0700132 self._branch_specs = branch_specs
Chris Masonefad911a2012-03-29 12:30:26 -0700133 self._pool = pool
Chris Masone96f16632012-04-04 18:36:03 -0700134
135 self._bare_branches = []
Chris Masone67f06d62012-04-12 15:16:56 -0700136 if not branch_specs:
137 # Any milestone is OK.
138 self._numeric_constraint = version.LooseVersion('0')
139 else:
140 self._numeric_constraint = None
141 for spec in branch_specs:
142 if spec.startswith('>='):
143 self._numeric_constraint = version.LooseVersion(
144 spec.lstrip('>=R'))
145 else:
146 self._bare_branches.append(spec)
Chris Masonefad911a2012-03-29 12:30:26 -0700147 # Since we expect __hash__() and other comparitor methods to be used
148 # frequently by set operations, and they use str() a lot, pre-compute
149 # the string representation of this object.
Chris Masone3fba86f2012-04-03 10:06:56 -0700150 self._str = '%s: %s on %s with pool %s' % (self.__class__.__name__,
Chris Masone96f16632012-04-04 18:36:03 -0700151 suite, branch_specs, pool)
152
153
154 def _FitsSpec(self, branch):
155 """Checks if a branch is deemed OK by this instance's branch specs.
156
157 When called on a branch name, will return whether that branch
158 'fits' the specifications stored in self._bare_branches and
159 self._numeric_constraint.
160
161 @param branch: the branch to check.
162 @return True if b 'fits' with stored specs, False otherwise.
163 """
Chris Masone657e1552012-05-30 17:06:20 -0700164 if branch in BARE_BRANCHES:
165 return branch in self._bare_branches
166 return (self._numeric_constraint and
167 version.LooseVersion(branch) >= self._numeric_constraint)
Chris Masonefad911a2012-03-29 12:30:26 -0700168
169
170 @property
171 def suite(self):
172 return self._suite
173
174
175 @property
Chris Masone96f16632012-04-04 18:36:03 -0700176 def branch_specs(self):
177 return self._branch_specs
Chris Masonefad911a2012-03-29 12:30:26 -0700178
179
180 @property
Chris Masone3fba86f2012-04-03 10:06:56 -0700181 def pool(self):
182 return self._pool
Chris Masonefad911a2012-03-29 12:30:26 -0700183
184
185 def __str__(self):
186 return self._str
187
188
189 def __lt__(self, other):
190 return str(self) < str(other)
191
192
193 def __le__(self, other):
194 return str(self) <= str(other)
195
196
197 def __eq__(self, other):
198 return str(self) == str(other)
199
200
201 def __ne__(self, other):
202 return str(self) != str(other)
203
204
205 def __gt__(self, other):
206 return str(self) > str(other)
207
208
209 def __ge__(self, other):
210 return str(self) >= str(other)
211
212
213 def __hash__(self):
214 """Allows instances to be correctly deduped when used in a set."""
215 return hash(str(self))
216
217
Alex Miller7ce1b362012-07-10 09:24:10 -0700218 def AvailableHosts(self, scheduler, board):
219 """Query what hosts are able to run a test on a board and pool
220 combination.
Alex Miller511a9e32012-07-03 09:16:47 -0700221
222 @param scheduler: an instance of DedupingScheduler, as defined in
223 deduping_scheduler.py
224 @param board: the board against which one wants to run the test.
Alex Miller7ce1b362012-07-10 09:24:10 -0700225 @return The list of hosts meeting the board and pool requirements,
226 or None if no hosts were found."""
Alex Miller511a9e32012-07-03 09:16:47 -0700227 labels = [Labels.BOARD_PREFIX + board]
228 if self._pool:
Chris Masonecd214e02012-07-10 16:22:10 -0700229 labels.append(Labels.POOL_PREFIX + self._pool)
Alex Miller511a9e32012-07-03 09:16:47 -0700230
Alex Miller7ce1b362012-07-10 09:24:10 -0700231 return scheduler.GetHosts(multiple_labels=labels)
Alex Miller511a9e32012-07-03 09:16:47 -0700232
233
Alex Millerd621cf22012-07-11 13:57:10 -0700234 def ShouldHaveAvailableHosts(self):
235 """As a sanity check, return true if we know for certain that
236 we should be able to schedule this test. If we claim this test
237 should be able to run, and it ends up not being scheduled, then
238 a warning will be reported.
239
240 @return True if this test should be able to run, False otherwise.
241 """
242 return self._pool == 'bvt'
243
244
Chris Masone96f16632012-04-04 18:36:03 -0700245 def Run(self, scheduler, branch_builds, board, force=False):
Chris Masone013859b2012-04-01 13:45:26 -0700246 """Run this task. Returns False if it should be destroyed.
Chris Masonefad911a2012-03-29 12:30:26 -0700247
Chris Masone013859b2012-04-01 13:45:26 -0700248 Execute this task. Attempt to schedule the associated suite.
249 Return True if this task should be kept around, False if it
250 should be destroyed. This allows for one-shot Tasks.
Chris Masonefad911a2012-03-29 12:30:26 -0700251
252 @param scheduler: an instance of DedupingScheduler, as defined in
253 deduping_scheduler.py
Chris Masone05b19442012-04-17 13:37:55 -0700254 @param branch_builds: a dict mapping branch name to the build(s) to
Chris Masone96f16632012-04-04 18:36:03 -0700255 install for that branch, e.g.
Chris Masone05b19442012-04-17 13:37:55 -0700256 {'R18': ['x86-alex-release/R18-1655.0.0'],
257 'R19': ['x86-alex-release/R19-2077.0.0']}
Chris Masone96f16632012-04-04 18:36:03 -0700258 @param board: the board against which to run self._suite.
Chris Masonefad911a2012-03-29 12:30:26 -0700259 @param force: Always schedule the suite.
Chris Masone013859b2012-04-01 13:45:26 -0700260 @return True if the task should be kept, False if not
Chris Masonefad911a2012-03-29 12:30:26 -0700261 """
Chris Masonea3a38172012-05-14 15:19:56 -0700262 logging.info('Running %s on %s', self._name, board)
Chris Masone96f16632012-04-04 18:36:03 -0700263 builds = []
Chris Masonefe5a5092012-04-11 18:29:07 -0700264 for branch, build in branch_builds.iteritems():
Chris Masonea3a38172012-05-14 15:19:56 -0700265 logging.info('Checking if %s fits spec %r',
266 branch, self.branch_specs)
Chris Masone96f16632012-04-04 18:36:03 -0700267 if self._FitsSpec(branch):
Chris Masone05b19442012-04-17 13:37:55 -0700268 builds.extend(build)
Chris Masone96f16632012-04-04 18:36:03 -0700269 for build in builds:
Chris Masone3fba86f2012-04-03 10:06:56 -0700270 try:
Chris Masone96f16632012-04-04 18:36:03 -0700271 if not scheduler.ScheduleSuite(self._suite, board, build,
Chris Masone3fba86f2012-04-03 10:06:56 -0700272 self._pool, force):
Chris Masone96f16632012-04-04 18:36:03 -0700273 logging.info('Skipping scheduling %s on %s for %s',
274 self._suite, build, board)
Chris Masone3fba86f2012-04-03 10:06:56 -0700275 except deduping_scheduler.DedupingSchedulerException as e:
276 logging.error(e)
Chris Masonefad911a2012-03-29 12:30:26 -0700277 return True
278
279
Chris Masone013859b2012-04-01 13:45:26 -0700280class OneShotTask(Task):
281 """A Task that can be run only once. Can schedule itself."""
Chris Masonefad911a2012-03-29 12:30:26 -0700282
283
Chris Masone96f16632012-04-04 18:36:03 -0700284 def Run(self, scheduler, branch_builds, board, force=False):
Chris Masone013859b2012-04-01 13:45:26 -0700285 """Run this task. Returns False, indicating it should be destroyed.
Chris Masonefad911a2012-03-29 12:30:26 -0700286
Chris Masone013859b2012-04-01 13:45:26 -0700287 Run this task. Attempt to schedule the associated suite.
288 Return False, indicating to the caller that it should discard this task.
Chris Masonefad911a2012-03-29 12:30:26 -0700289
290 @param scheduler: an instance of DedupingScheduler, as defined in
291 deduping_scheduler.py
Chris Masone05b19442012-04-17 13:37:55 -0700292 @param branch_builds: a dict mapping branch name to the build(s) to
Chris Masone96f16632012-04-04 18:36:03 -0700293 install for that branch, e.g.
Chris Masone05b19442012-04-17 13:37:55 -0700294 {'R18': ['x86-alex-release/R18-1655.0.0'],
295 'R19': ['x86-alex-release/R19-2077.0.0']}
Chris Masone96f16632012-04-04 18:36:03 -0700296 @param board: the board against which to run self._suite.
Chris Masonefad911a2012-03-29 12:30:26 -0700297 @param force: Always schedule the suite.
298 @return False
299 """
Chris Masone96f16632012-04-04 18:36:03 -0700300 super(OneShotTask, self).Run(scheduler, branch_builds, board, force)
Chris Masonefad911a2012-03-29 12:30:26 -0700301 return False