1# Copyright 2015 The Chromium 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"""Small test to send a put request to buildbucket."""
6
7import re
8
9
10class BisectJob(object):
11  """A buildbot bisect job started and monitored through buildbucket."""
12
13  def __init__(self, try_job_id, bisect_director, good_revision, bad_revision,
14               test_command, metric, repeats, timeout_minutes, bug_id,
15               gs_bucket, recipe_tester_name, builder_host=None,
16               builder_port=None, test_type='perf',
17               required_initial_confidence=None):
18    if not all([good_revision, bad_revision, test_command, metric,
19                repeats, timeout_minutes, recipe_tester_name]):
20      raise ValueError('At least one of the values required for BisectJob '
21                       'construction was not given or was given with a None '
22                       'value.')
23    self.try_job_id = try_job_id
24    self.bisect_director = bisect_director
25    self.good_revision = good_revision
26    self.bad_revision = bad_revision
27    self.command = BisectJob.EnsureCommandPath(test_command)
28    self.metric = metric
29    self.repeat_count = repeats
30    self.max_time_minutes = timeout_minutes
31    self.bug_id = bug_id
32    self.gs_bucket = gs_bucket
33    self.builder_host = builder_host
34    self.builder_port = builder_port
35    self.test_type = test_type
36    self.recipe_tester_name = recipe_tester_name
37    self.required_initial_confidence = required_initial_confidence
38
39  @staticmethod
40  def EnsureCommandPath(command):
41    old_perf_path_regex = re.compile(r'(?<!src/)tools/perf')
42    if old_perf_path_regex.search(command):
43      return old_perf_path_regex.sub('src/tools/perf', command)
44    old_perf_path_regex_win = re.compile(r'(?<!src\\)tools\\perf')
45    if old_perf_path_regex_win.search(command):
46      return old_perf_path_regex_win.sub(r'src\\tools\\perf', command)
47    return command
48
49  def GetBuildParameters(self):
50    """Prepares a nested dict containing the bisect config."""
51    # TODO(robertocn): Some of these should be optional.
52    bisect_config = {
53        'try_job_id': self.try_job_id,
54        'test_type': self.test_type,
55        'command': self.command,
56        'good_revision': self.good_revision,
57        'bad_revision': self.bad_revision,
58        'metric': self.metric,
59        'repeat_count': self.repeat_count,
60        'max_time_minutes': self.max_time_minutes,
61        'bug_id': self.bug_id,
62        'gs_bucket': self.gs_bucket,
63        'builder_host': self.builder_host,
64        'builder_port': self.builder_port,
65        'recipe_tester_name': self.recipe_tester_name,
66    }
67    if self.required_initial_confidence:
68      bisect_config['required_initial_confidence'] = (
69          self.required_initial_confidence)
70    properties = {'bisect_config': bisect_config}
71    parameters = {
72        'builder_name': self.bisect_director,
73        'properties': properties,
74    }
75    return parameters
76
77  # TODO(robertocn): Add methods to query the status of a job form buildbucket.
78  # TODO(robertocn): Add static method to get a job by it's buildbucket id.
79  # TODO(robertocn): Add appropriate tests.
80