aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/update_tryjob_status_unittest.py
blob: c42c6718d4b5d5c3d7ee6be325306a8f3f9c443c (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests when updating a tryjob's status."""

from __future__ import print_function

import json
import os
import subprocess
import unittest
import unittest.mock as mock

from test_helpers import CreateTemporaryJsonFile
from test_helpers import WritePrettyJsonFile
from update_tryjob_status import TryjobStatus
from update_tryjob_status import CustomScriptStatus
import update_tryjob_status


class UpdateTryjobStatusTest(unittest.TestCase):
  """Unittests for updating a tryjob's 'status'."""

  def testFoundTryjobIndex(self):
    test_tryjobs = [{
        'rev': 123,
        'url': 'https://some_url_to_CL.com',
        'cl': 'https://some_link_to_tryjob.com',
        'status': 'good',
        'buildbucket_id': 91835
    }, {
        'rev': 1000,
        'url': 'https://some_url_to_CL.com',
        'cl': 'https://some_link_to_tryjob.com',
        'status': 'pending',
        'buildbucket_id': 10931
    }]

    expected_index = 0

    revision_to_find = 123

    self.assertEqual(
        update_tryjob_status.FindTryjobIndex(revision_to_find, test_tryjobs),
        expected_index)

  def testNotFindTryjobIndex(self):
    test_tryjobs = [{
        'rev': 500,
        'url': 'https://some_url_to_CL.com',
        'cl': 'https://some_link_to_tryjob.com',
        'status': 'bad',
        'buildbucket_id': 390
    }, {
        'rev': 10,
        'url': 'https://some_url_to_CL.com',
        'cl': 'https://some_link_to_tryjob.com',
        'status': 'skip',
        'buildbucket_id': 10
    }]

    revision_to_find = 250

    self.assertIsNone(
        update_tryjob_status.FindTryjobIndex(revision_to_find, test_tryjobs))

  @mock.patch.object(subprocess, 'Popen')
  # Simulate the behavior of `os.rename()` when successfully renamed a file.
  @mock.patch.object(os, 'rename', return_value=None)
  # Simulate the behavior of `os.path.basename()` when successfully retrieved
  # the basename of the temp .JSON file.
  @mock.patch.object(os.path, 'basename', return_value='tmpFile.json')
  def testInvalidExitCodeByCustomScript(self, mock_basename, mock_rename_file,
                                        mock_exec_custom_script):

    error_message_by_custom_script = 'Failed to parse .JSON file'

    # Simulate the behavior of 'subprocess.Popen()' when executing the custom
    # script.
    #
    # `Popen.communicate()` returns a tuple of `stdout` and `stderr`.
    mock_exec_custom_script.return_value.communicate.return_value = (
        None, error_message_by_custom_script)

    # Exit code of 1 is not in the mapping, so an exception will be raised.
    custom_script_exit_code = 1

    mock_exec_custom_script.return_value.returncode = custom_script_exit_code

    tryjob_contents = {
        'status': 'good',
        'rev': 1234,
        'url': 'https://some_url_to_CL.com',
        'link': 'https://some_url_to_tryjob.com'
    }

    custom_script_path = '/abs/path/to/script.py'
    status_file_path = '/abs/path/to/status_file.json'

    name_json_file = os.path.join(
        os.path.dirname(status_file_path), 'tmpFile.json')

    expected_error_message = (
        'Custom script %s exit code %d did not match '
        'any of the expected exit codes: %s for "good", '
        '%d for "bad", or %d for "skip".\nPlease check '
        '%s for information about the tryjob: %s' %
        (custom_script_path, custom_script_exit_code,
         CustomScriptStatus.GOOD.value, CustomScriptStatus.BAD.value,
         CustomScriptStatus.SKIP.value, name_json_file,
         error_message_by_custom_script))

    # Verify the exception is raised when the exit code by the custom script
    # does not match any of the exit codes in the mapping of
    # `custom_script_exit_value_mapping`.
    with self.assertRaises(ValueError) as err:
      update_tryjob_status.GetCustomScriptResult(custom_script_path,
                                                 status_file_path,
                                                 tryjob_contents)

    self.assertEqual(str(err.exception), expected_error_message)

    mock_exec_custom_script.assert_called_once()

    mock_rename_file.assert_called_once()

    mock_basename.assert_called_once()

  @mock.patch.object(subprocess, 'Popen')
  # Simulate the behavior of `os.rename()` when successfully renamed a file.
  @mock.patch.object(os, 'rename', return_value=None)
  # Simulate the behavior of `os.path.basename()` when successfully retrieved
  # the basename of the temp .JSON file.
  @mock.patch.object(os.path, 'basename', return_value='tmpFile.json')
  def testValidExitCodeByCustomScript(self, mock_basename, mock_rename_file,
                                      mock_exec_custom_script):

    # Simulate the behavior of 'subprocess.Popen()' when executing the custom
    # script.
    #
    # `Popen.communicate()` returns a tuple of `stdout` and `stderr`.
    mock_exec_custom_script.return_value.communicate.return_value = (None, None)

    mock_exec_custom_script.return_value.returncode = (
        CustomScriptStatus.GOOD.value)

    tryjob_contents = {
        'status': 'good',
        'rev': 1234,
        'url': 'https://some_url_to_CL.com',
        'link': 'https://some_url_to_tryjob.com'
    }

    custom_script_path = '/abs/path/to/script.py'
    status_file_path = '/abs/path/to/status_file.json'

    self.assertEqual(
        update_tryjob_status.GetCustomScriptResult(custom_script_path,
                                                   status_file_path,
                                                   tryjob_contents),
        TryjobStatus.GOOD.value)

    mock_exec_custom_script.assert_called_once()

    mock_rename_file.assert_not_called()

    mock_basename.assert_not_called()

  def testNoTryjobsInStatusFileWhenUpdatingTryjobStatus(self):
    bisect_test_contents = {'start': 369410, 'end': 369420, 'jobs': []}

    # Create a temporary .JSON file to simulate a .JSON file that has bisection
    # contents.
    with CreateTemporaryJsonFile() as temp_json_file:
      with open(temp_json_file, 'w') as f:
        WritePrettyJsonFile(bisect_test_contents, f)

      revision_to_update = 369412

      custom_script = None

      # Verify the exception is raised when the `status_file` does not have any
      # `jobs` (empty).
      with self.assertRaises(SystemExit) as err:
        update_tryjob_status.UpdateTryjobStatus(revision_to_update,
                                                TryjobStatus.GOOD,
                                                temp_json_file, custom_script)

      self.assertEqual(str(err.exception), 'No tryjobs in %s' % temp_json_file)

  # Simulate the behavior of `FindTryjobIndex()` when the tryjob does not exist
  # in the status file.
  @mock.patch.object(update_tryjob_status, 'FindTryjobIndex', return_value=None)
  def testNotFindTryjobIndexWhenUpdatingTryjobStatus(self,
                                                     mock_find_tryjob_index):

    bisect_test_contents = {
        'start': 369410,
        'end': 369420,
        'jobs': [{
            'rev': 369411,
            'status': 'pending'
        }]
    }

    # Create a temporary .JSON file to simulate a .JSON file that has bisection
    # contents.
    with CreateTemporaryJsonFile() as temp_json_file:
      with open(temp_json_file, 'w') as f:
        WritePrettyJsonFile(bisect_test_contents, f)

      revision_to_update = 369416

      custom_script = None

      # Verify the exception is raised when the `status_file` does not have any
      # `jobs` (empty).
      with self.assertRaises(ValueError) as err:
        update_tryjob_status.UpdateTryjobStatus(revision_to_update,
                                                TryjobStatus.SKIP,
                                                temp_json_file, custom_script)

      self.assertEqual(
          str(err.exception), 'Unable to find tryjob for %d in %s' %
          (revision_to_update, temp_json_file))

    mock_find_tryjob_index.assert_called_once()

  # Simulate the behavior of `FindTryjobIndex()` when the tryjob exists in the
  # status file.
  @mock.patch.object(update_tryjob_status, 'FindTryjobIndex', return_value=0)
  def testSuccessfullyUpdatedTryjobStatusToGood(self, mock_find_tryjob_index):
    bisect_test_contents = {
        'start': 369410,
        'end': 369420,
        'jobs': [{
            'rev': 369411,
            'status': 'pending'
        }]
    }

    # Create a temporary .JSON file to simulate a .JSON file that has bisection
    # contents.
    with CreateTemporaryJsonFile() as temp_json_file:
      with open(temp_json_file, 'w') as f:
        WritePrettyJsonFile(bisect_test_contents, f)

      revision_to_update = 369411

      # Index of the tryjob that is going to have its 'status' value updated.
      tryjob_index = 0

      custom_script = None

      update_tryjob_status.UpdateTryjobStatus(revision_to_update,
                                              TryjobStatus.GOOD, temp_json_file,
                                              custom_script)

      # Verify that the tryjob's 'status' has been updated in the status file.
      with open(temp_json_file) as status_file:
        bisect_contents = json.load(status_file)

        self.assertEqual(bisect_contents['jobs'][tryjob_index]['status'],
                         TryjobStatus.GOOD.value)

    mock_find_tryjob_index.assert_called_once()

  # Simulate the behavior of `FindTryjobIndex()` when the tryjob exists in the
  # status file.
  @mock.patch.object(update_tryjob_status, 'FindTryjobIndex', return_value=0)
  def testSuccessfullyUpdatedTryjobStatusToBad(self, mock_find_tryjob_index):
    bisect_test_contents = {
        'start': 369410,
        'end': 369420,
        'jobs': [{
            'rev': 369411,
            'status': 'pending'
        }]
    }

    # Create a temporary .JSON file to simulate a .JSON file that has bisection
    # contents.
    with CreateTemporaryJsonFile() as temp_json_file:
      with open(temp_json_file, 'w') as f:
        WritePrettyJsonFile(bisect_test_contents, f)

      revision_to_update = 369411

      # Index of the tryjob that is going to have its 'status' value updated.
      tryjob_index = 0

      custom_script = None

      update_tryjob_status.UpdateTryjobStatus(revision_to_update,
                                              TryjobStatus.BAD, temp_json_file,
                                              custom_script)

      # Verify that the tryjob's 'status' has been updated in the status file.
      with open(temp_json_file) as status_file:
        bisect_contents = json.load(status_file)

        self.assertEqual(bisect_contents['jobs'][tryjob_index]['status'],
                         TryjobStatus.BAD.value)

    mock_find_tryjob_index.assert_called_once()

  # Simulate the behavior of `FindTryjobIndex()` when the tryjob exists in the
  # status file.
  @mock.patch.object(update_tryjob_status, 'FindTryjobIndex', return_value=0)
  def testSuccessfullyUpdatedTryjobStatusToPending(self,
                                                   mock_find_tryjob_index):
    bisect_test_contents = {
        'start': 369410,
        'end': 369420,
        'jobs': [{
            'rev': 369411,
            'status': 'skip'
        }]
    }

    # Create a temporary .JSON file to simulate a .JSON file that has bisection
    # contents.
    with CreateTemporaryJsonFile() as temp_json_file:
      with open(temp_json_file, 'w') as f:
        WritePrettyJsonFile(bisect_test_contents, f)

      revision_to_update = 369411

      # Index of the tryjob that is going to have its 'status' value updated.
      tryjob_index = 0

      custom_script = None

      update_tryjob_status.UpdateTryjobStatus(
          revision_to_update, update_tryjob_status.TryjobStatus.SKIP,
          temp_json_file, custom_script)

      # Verify that the tryjob's 'status' has been updated in the status file.
      with open(temp_json_file) as status_file:
        bisect_contents = json.load(status_file)

        self.assertEqual(bisect_contents['jobs'][tryjob_index]['status'],
                         update_tryjob_status.TryjobStatus.SKIP.value)

    mock_find_tryjob_index.assert_called_once()

  # Simulate the behavior of `FindTryjobIndex()` when the tryjob exists in the
  # status file.
  @mock.patch.object(update_tryjob_status, 'FindTryjobIndex', return_value=0)
  def testSuccessfullyUpdatedTryjobStatusToSkip(self, mock_find_tryjob_index):
    bisect_test_contents = {
        'start': 369410,
        'end': 369420,
        'jobs': [{
            'rev': 369411,
            'status': 'pending',
        }]
    }

    # Create a temporary .JSON file to simulate a .JSON file that has bisection
    # contents.
    with CreateTemporaryJsonFile() as temp_json_file:
      with open(temp_json_file, 'w') as f:
        WritePrettyJsonFile(bisect_test_contents, f)

      revision_to_update = 369411

      # Index of the tryjob that is going to have its 'status' value updated.
      tryjob_index = 0

      custom_script = None

      update_tryjob_status.UpdateTryjobStatus(
          revision_to_update, update_tryjob_status.TryjobStatus.PENDING,
          temp_json_file, custom_script)

      # Verify that the tryjob's 'status' has been updated in the status file.
      with open(temp_json_file) as status_file:
        bisect_contents = json.load(status_file)

        self.assertEqual(bisect_contents['jobs'][tryjob_index]['status'],
                         update_tryjob_status.TryjobStatus.PENDING.value)

    mock_find_tryjob_index.assert_called_once()

  @mock.patch.object(update_tryjob_status, 'FindTryjobIndex', return_value=0)
  @mock.patch.object(
      update_tryjob_status,
      'GetCustomScriptResult',
      return_value=TryjobStatus.SKIP.value)
  def testUpdatedTryjobStatusToAutoPassedWithCustomScript(
      self, mock_get_custom_script_result, mock_find_tryjob_index):
    bisect_test_contents = {
        'start': 369410,
        'end': 369420,
        'jobs': [{
            'rev': 369411,
            'status': 'pending',
            'buildbucket_id': 1200
        }]
    }

    # Create a temporary .JSON file to simulate a .JSON file that has bisection
    # contents.
    with CreateTemporaryJsonFile() as temp_json_file:
      with open(temp_json_file, 'w') as f:
        WritePrettyJsonFile(bisect_test_contents, f)

      revision_to_update = 369411

      # Index of the tryjob that is going to have its 'status' value updated.
      tryjob_index = 0

      custom_script_path = '/abs/path/to/custom_script.py'

      update_tryjob_status.UpdateTryjobStatus(
          revision_to_update, update_tryjob_status.TryjobStatus.CUSTOM_SCRIPT,
          temp_json_file, custom_script_path)

      # Verify that the tryjob's 'status' has been updated in the status file.
      with open(temp_json_file) as status_file:
        bisect_contents = json.load(status_file)

        self.assertEqual(bisect_contents['jobs'][tryjob_index]['status'],
                         update_tryjob_status.TryjobStatus.SKIP.value)

    mock_get_custom_script_result.assert_called_once()

    mock_find_tryjob_index.assert_called_once()

  # Simulate the behavior of `FindTryjobIndex()` when the tryjob exists in the
  # status file.
  @mock.patch.object(update_tryjob_status, 'FindTryjobIndex', return_value=0)
  def testSetStatusDoesNotExistWhenUpdatingTryjobStatus(self,
                                                        mock_find_tryjob_index):

    bisect_test_contents = {
        'start': 369410,
        'end': 369420,
        'jobs': [{
            'rev': 369411,
            'status': 'pending',
            'buildbucket_id': 1200
        }]
    }

    # Create a temporary .JSON file to simulate a .JSON file that has bisection
    # contents.
    with CreateTemporaryJsonFile() as temp_json_file:
      with open(temp_json_file, 'w') as f:
        WritePrettyJsonFile(bisect_test_contents, f)

      revision_to_update = 369411

      nonexistent_update_status = 'revert_status'

      custom_script = None

      # Verify the exception is raised when the `set_status` command line
      # argument does not exist in the mapping.
      with self.assertRaises(ValueError) as err:
        update_tryjob_status.UpdateTryjobStatus(revision_to_update,
                                                nonexistent_update_status,
                                                temp_json_file, custom_script)

      self.assertEqual(
          str(err.exception),
          'Invalid "set_status" option provided: revert_status')

    mock_find_tryjob_index.assert_called_once()


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