File: find_bad_builds_unittest.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (237 lines) | stat: -rwxr-xr-x 8,181 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env vpython3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for find_bad_builds."""

from __future__ import division
from __future__ import print_function

import contextlib
import datetime
import json

import argparse
import unittest

import git_utils
import find_bad_builds

import mock


class Pool:
  """Simple object used to mock out multiprocessing.Pool."""

  def map(self, fn, lst):
    return map(fn, lst)


@mock.patch('find_bad_builds._get_pool')
class FindBadBuildsIntegrationTest(unittest.TestCase):
  """Integration tests for find_bad_builds.

  This only tests that the script works from an end to end perspective. It mocks
  all the interactions with git and buildbucket in the script. That
  functionality is fairly simple, and doesn't need to be tested.
  """

  def _gen_build(self,
                 bid,
                 rev,
                 running_time,
                 builder='linux-rel',
                 bucket='try',
                 project='chromium'):
    """Generates a buildbucket dict representing a single build."""
    return {
        'id': str(bid),
        'revision': rev,
        'running_time': running_time,
        'builder': {
            'builder': builder,
            'bucket': bucket,
            'project': project
        },
    }

  @contextlib.contextmanager
  def _setup_git(self, commits, first_commit_date=None, time_gap=1):
    """Sets up git mocks.

    Args:
      commits: An ordered list of commit hashes. The first commit is assumed to
        be the most recent.
      first_commit_date: When the first commit starts. Datetime object.
      time_gap: How long between git commits (minutes).
    Returns:
      Nothing. Yields to the caller, to allow the patches to be used during the
        test.
    """
    if not first_commit_date:
      first_commit_date = datetime.datetime(2020, 1, 1, 0, 0, 0)
    with mock.patch('git_utils.get_revisions_between') as rev_btwn:

      def find_btwn(commit1, commit2):
        # technically inefficient, but probably fine.
        first = commits.index(commit1)
        last = commits.index(commit2)
        # Only want commits in between these two
        return commits[first + 1:last]

      rev_btwn.side_effect = find_btwn

      with mock.patch('git_utils.get_commit_date') as rev_date:

        def calc_date(rev):
          ind = commits.index(rev)
          return first_commit_date + datetime.timedelta(minutes=ind * time_gap)

        rev_date.side_effect = calc_date

        yield

  @contextlib.contextmanager
  def _setup_buildbucket(self, builds):
    """Sets up buildbucket mocks.

    Args:
      builds: A list of buildbucket builds (dict).

    Yields:
      The mock object for '_find_builds'. Callers can modify its side_effect to
      verify attributes of the predicate passed to buildbucket.
    """
    with mock.patch('find_bad_builds._find_builds') as find_builds:
      find_builds.return_value = [json.dumps(b) for b in builds]
      with mock.patch('find_bad_builds._fetch_build_revision') as fetch_rev:

        def find_rev(bid):
          for b in builds:
            if b['id'] == bid:
              return b['revision']
          self.fail('build %s is missing a revision' % bid)
          return None

        fetch_rev.side_effect = find_rev
        with mock.patch('find_bad_builds._get_build_running_time') as b_runtime:
          b_runtime.side_effect = lambda build: datetime.timedelta(minutes=int(
              build['running_time']))
          # Yield find_builds so callers can assert on the predicate being
          # passed to find_builds.
          yield find_builds

  def _run(self, args):
    """Runs the script. Returns the exit code and printed lines."""
    lines = []
    return find_bad_builds.main(args, lines.append), set(lines)

  def test_simple(self, get_pool):
    get_pool.return_value = Pool()
    with self._setup_git(['1,', '2', '3', '4', '5']):
      with self._setup_buildbucket([
          self._gen_build(11, '1', 5),
          self._gen_build(22, '2', 4),
          self._gen_build(33, '3', 3),
          self._gen_build(44, '4', 2),
          self._gen_build(55, '5', 1),
      ]) as find_builds:
        rval = find_builds.return_value

        def se(predicate):
          self.assertEqual(
              predicate['createTime']['startTime'],
              # commit '2' commit time.
              '2020-01-01T00:01:00+00:00')
          self.assertEqual(
              predicate['createTime']['endTime'],
              # commit '4' commit time, plus git replication lag.
              '2020-01-01T00:23:00+00:00')
          return rval

        find_builds.side_effect = se
        self.assertEqual(
            self._run(['4', '2', '100']),
            (
                # Only builds with revision '2' and '3' should be cancelled. '4'
                # has the good commit, so it's fine to run.
                0,
                set(['22', '33'])))

  def test_print_builds(self, get_pool):
    get_pool.return_value = Pool()
    with self._setup_git(['1,', '2', '3', '4', '5']):
      with self._setup_buildbucket([
          self._gen_build(11, '1', 29),
          self._gen_build(22, '2', 28),
          self._gen_build(33, '3', 27),
          self._gen_build(44, '4', 26),
          self._gen_build(55, '5', 25),
      ]):
        retcode, lines = self._run(['4', '2', '100', '-s'])
        self.assertEqual(retcode, 0)
        # Sort to get rid of header, then remove.
        lines = sorted(lines)
        self.assertTrue(lines[-1].startswith('Build '))
        self.assertTrue(lines[0].startswith('------'))
        self.assertEqual(
            set(lines[1:-1]),
            set([
                '11                   | False      | 29                    ',
                '22                   | True       | 28                    ',
                '33                   | True       | 27                    ',
                '44                   | False      | 26                    ',
                '55                   | False      | 25                    ',
            ]))

  def test_build_time_filter(self, get_pool):
    get_pool.return_value = Pool()
    with self._setup_git(['1,', '2', '3', '4', '5']):
      with self._setup_buildbucket([
          self._gen_build(11, '1', 31),
          self._gen_build(22, '2', 29),
          self._gen_build(33, '3', 27),
          self._gen_build(34, '3', 26),
          self._gen_build(44, '4', 25),
          self._gen_build(55, '5', 23),
      ]):
        self.assertEqual(self._run(['4', '2', '28']), (0, set(['34', '33'])))

  def test_builder_filter(self, get_pool):
    get_pool.return_value = Pool()
    with self._setup_git(['1,', '2', '3', '4', '5']):
      with self._setup_buildbucket([
          self._gen_build(11, '1', 4),
          self._gen_build(21, '2', 5, builder='linux-rel'),
          self._gen_build(22, '2', 5, builder='mac-rel'),
          self._gen_build(23, '2', 5, builder='win-rel'),
          self._gen_build(33, '3', 3),
          self._gen_build(44, '4', 2),
          self._gen_build(55, '5', 1),
      ]):
        self.assertEqual(
            self._run(['4', '2', '100', '-b', 'linux-rel', '-b', 'mac-rel']),
            (0, set(['21', '22', '33'])))

  def test_git_replication(self, get_pool):
    get_pool.return_value = Pool()
    with self._setup_git(['1,', '2', '3', '4', '5']):
      with self._setup_buildbucket([
          self._gen_build(11, '1', 6),
          self._gen_build(22, '2', 5),
          self._gen_build(33, '3', 4),
          self._gen_build(44, '4', 3),
          self._gen_build(55, '5', 2),
          # Sometimes git has replication lag, which means that builds can get
          # old git data. In this case, a build which ran after '5', which was
          # committed after '4', gets '4' as the HEAD revision. This can happen
          # in prod.
          self._gen_build(66, '3', 1),
      ]):
        self.assertEqual(self._run(['4', '2', '100']),
                         (0, set(['22', '33', '66'])))


if __name__ == '__main__':
  unittest.main()