aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/modify_a_tryjob_unittest.py
blob: 0f693dc3a6f492c28c96ecfb5898dcbab33d407c (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
#!/usr/bin/env python3
# Copyright 2019 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for modifying a tryjob."""

import json
import unittest
from unittest import mock

import get_llvm_hash
import modify_a_tryjob
import test_helpers
import update_packages_and_run_tests
import update_tryjob_status


class ModifyATryjobTest(unittest.TestCase):
    """Unittests for modifying a tryjob."""

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

        # Create a temporary .JSON file to simulate a .JSON file that has
        # bisection contents.
        with test_helpers.CreateTemporaryJsonFile() as temp_json_file:
            with open(temp_json_file, "w", encoding="utf-8") as f:
                test_helpers.WritePrettyJsonFile(bisect_test_contents, f)

            revision_to_modify = 369411

            args_output = test_helpers.ArgsOutputTest()
            args_output.builders = None
            args_output.options = None

            # Verify the exception is raised there are no tryjobs in the status
            # file and the mode is not to 'add' a tryjob.
            with self.assertRaises(SystemExit) as err:
                modify_a_tryjob.PerformTryjobModification(
                    revision_to_modify,
                    modify_a_tryjob.ModifyTryjob.REMOVE,
                    temp_json_file,
                    args_output.extra_change_lists,
                    args_output.options,
                    args_output.builders,
                    args_output.chromeos_path,
                )

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

    # Simulate the behavior of `FindTryjobIndex()` when the index of the tryjob
    # was not found.
    @mock.patch.object(
        update_tryjob_status, "FindTryjobIndex", return_value=None
    )
    def testNoTryjobIndexFound(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 test_helpers.CreateTemporaryJsonFile() as temp_json_file:
            with open(temp_json_file, "w", encoding="utf-8") as f:
                test_helpers.WritePrettyJsonFile(bisect_test_contents, f)

            revision_to_modify = 369412

            args_output = test_helpers.ArgsOutputTest()
            args_output.builders = None
            args_output.options = None

            # Verify the exception is raised when the index of the tryjob was
            # not found in the status file and the mode is not to 'add' a
            # tryjob.
            with self.assertRaises(ValueError) as err:
                modify_a_tryjob.PerformTryjobModification(
                    revision_to_modify,
                    modify_a_tryjob.ModifyTryjob.REMOVE,
                    temp_json_file,
                    args_output.extra_change_lists,
                    args_output.options,
                    args_output.builders,
                    args_output.chromeos_path,
                )

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

        mock_find_tryjob_index.assert_called_once()

    # Simulate the behavior of `FindTryjobIndex()` when the index of the tryjob
    # was found.
    @mock.patch.object(update_tryjob_status, "FindTryjobIndex", return_value=0)
    def testSuccessfullyRemovedTryjobInStatusFile(self, mock_find_tryjob_index):
        bisect_test_contents = {
            "start": 369410,
            "end": 369420,
            "jobs": [
                {"rev": 369414, "status": "pending", "buildbucket_id": 1200}
            ],
        }

        # Create a temporary .JSON file to simulate a .JSON file that has
        # bisection contents.
        with test_helpers.CreateTemporaryJsonFile() as temp_json_file:
            with open(temp_json_file, "w", encoding="utf-8") as f:
                test_helpers.WritePrettyJsonFile(bisect_test_contents, f)

            revision_to_modify = 369414

            args_output = test_helpers.ArgsOutputTest()
            args_output.builders = None
            args_output.options = None

            modify_a_tryjob.PerformTryjobModification(
                revision_to_modify,
                modify_a_tryjob.ModifyTryjob.REMOVE,
                temp_json_file,
                args_output.extra_change_lists,
                args_output.options,
                args_output.builders,
                args_output.chromeos_path,
            )

            # Verify that the tryjob was removed from the status file.
            with open(temp_json_file, encoding="utf-8") as status_file:
                bisect_contents = json.load(status_file)

                expected_file_contents = {
                    "start": 369410,
                    "end": 369420,
                    "jobs": [],
                }

                self.assertDictEqual(bisect_contents, expected_file_contents)

        mock_find_tryjob_index.assert_called_once()

    # Simulate the behavior of `RunTryJobs()` when successfully submitted a
    # tryjob.
    @mock.patch.object(update_packages_and_run_tests, "RunTryJobs")
    # Simulate the behavior of `FindTryjobIndex()` when the index of the tryjob
    # was found.
    @mock.patch.object(update_tryjob_status, "FindTryjobIndex", return_value=0)
    def testSuccessfullyRelaunchedTryjob(
        self, mock_find_tryjob_index, mock_run_tryjob
    ):
        bisect_test_contents = {
            "start": 369410,
            "end": 369420,
            "jobs": [
                {
                    "rev": 369411,
                    "status": "bad",
                    "link": "https://some_tryjob_link.com",
                    "buildbucket_id": 1200,
                    "cl": 123,
                    "extra_cls": None,
                    "options": None,
                    "builder": ["some-builder-tryjob"],
                }
            ],
        }

        tryjob_result = [
            {"link": "https://some_new_tryjob_link.com", "buildbucket_id": 20}
        ]

        mock_run_tryjob.return_value = tryjob_result

        # Create a temporary .JSON file to simulate a .JSON file that has
        # bisection contents.
        with test_helpers.CreateTemporaryJsonFile() as temp_json_file:
            with open(temp_json_file, "w", encoding="utf-8") as f:
                test_helpers.WritePrettyJsonFile(bisect_test_contents, f)

            revision_to_modify = 369411

            args_output = test_helpers.ArgsOutputTest()
            args_output.builders = None
            args_output.options = None

            modify_a_tryjob.PerformTryjobModification(
                revision_to_modify,
                modify_a_tryjob.ModifyTryjob.RELAUNCH,
                temp_json_file,
                args_output.extra_change_lists,
                args_output.options,
                args_output.builders,
                args_output.chromeos_path,
            )

            # Verify that the tryjob's information was updated after submtting
            # the tryjob.
            with open(temp_json_file, encoding="utf-8") as status_file:
                bisect_contents = json.load(status_file)

                expected_file_contents = {
                    "start": 369410,
                    "end": 369420,
                    "jobs": [
                        {
                            "rev": 369411,
                            "status": "pending",
                            "link": "https://some_new_tryjob_link.com",
                            "buildbucket_id": 20,
                            "cl": 123,
                            "extra_cls": None,
                            "options": None,
                            "builder": ["some-builder-tryjob"],
                        }
                    ],
                }

                self.assertDictEqual(bisect_contents, expected_file_contents)

        mock_find_tryjob_index.assert_called_once()

        mock_run_tryjob.assert_called_once()

    # Simulate the behavior of `FindTryjobIndex()` when the index of the tryjob
    # was found.
    @mock.patch.object(update_tryjob_status, "FindTryjobIndex", return_value=0)
    def testAddingTryjobThatAlreadyExists(self, mock_find_tryjob_index):
        bisect_test_contents = {
            "start": 369410,
            "end": 369420,
            "jobs": [
                {"rev": 369411, "status": "bad", "builder": ["some-builder"]}
            ],
        }

        # Create a temporary .JSON file to simulate a .JSON file that has
        # bisection contents.
        with test_helpers.CreateTemporaryJsonFile() as temp_json_file:
            with open(temp_json_file, "w", encoding="utf-8") as f:
                test_helpers.WritePrettyJsonFile(bisect_test_contents, f)

            revision_to_add = 369411

            # Index of the tryjob in 'jobs' list.
            tryjob_index = 0

            args_output = test_helpers.ArgsOutputTest()
            args_output.options = None

            # Verify the exception is raised when the tryjob that is going to
            # added already exists in the status file (found its index).
            with self.assertRaises(ValueError) as err:
                modify_a_tryjob.PerformTryjobModification(
                    revision_to_add,
                    modify_a_tryjob.ModifyTryjob.ADD,
                    temp_json_file,
                    args_output.extra_change_lists,
                    args_output.options,
                    args_output.builders,
                    args_output.chromeos_path,
                )

            self.assertEqual(
                str(err.exception),
                "Tryjob already exists (index is %d) in %s."
                % (tryjob_index, temp_json_file),
            )

        mock_find_tryjob_index.assert_called_once()

    # Simulate the behavior of `FindTryjobIndex()` when the tryjob was not
    # found.
    @mock.patch.object(
        update_tryjob_status, "FindTryjobIndex", return_value=None
    )
    def testSuccessfullyDidNotAddTryjobOutsideOfBisectionBounds(
        self, mock_find_tryjob_index
    ):
        bisect_test_contents = {
            "start": 369410,
            "end": 369420,
            "jobs": [{"rev": 369411, "status": "bad"}],
        }

        # Create a temporary .JSON file to simulate a .JSON file that has
        # bisection contents.
        with test_helpers.CreateTemporaryJsonFile() as temp_json_file:
            with open(temp_json_file, "w", encoding="utf-8") as f:
                test_helpers.WritePrettyJsonFile(bisect_test_contents, f)

            # Add a revision that is outside of 'start' and 'end'.
            revision_to_add = 369450

            args_output = test_helpers.ArgsOutputTest()
            args_output.options = None

            # Verify the exception is raised when adding a tryjob that does not
            # exist and is not within 'start' and 'end'.
            with self.assertRaises(ValueError) as err:
                modify_a_tryjob.PerformTryjobModification(
                    revision_to_add,
                    modify_a_tryjob.ModifyTryjob.ADD,
                    temp_json_file,
                    args_output.extra_change_lists,
                    args_output.options,
                    args_output.builders,
                    args_output.chromeos_path,
                )

            self.assertEqual(
                str(err.exception),
                "Failed to add tryjob to %s" % temp_json_file,
            )

        mock_find_tryjob_index.assert_called_once()

    # Simulate the behavior of `AddTryjob()` when successfully submitted the
    # tryjob and constructed the tryjob information (a dictionary).
    @mock.patch.object(modify_a_tryjob, "AddTryjob")
    # Simulate the behavior of `GetLLVMHashAndVersionFromSVNOption()` when
    # successfully retrieved the git hash of the revision to launch a tryjob
    # for.
    @mock.patch.object(
        get_llvm_hash,
        "GetLLVMHashAndVersionFromSVNOption",
        return_value=("a123testhash1", 369418),
    )
    # Simulate the behavior of `FindTryjobIndex()` when the tryjob was not
    # found.
    @mock.patch.object(
        update_tryjob_status, "FindTryjobIndex", return_value=None
    )
    def testSuccessfullyAddedTryjob(
        self, mock_find_tryjob_index, mock_get_llvm_hash, mock_add_tryjob
    ):
        bisect_test_contents = {
            "start": 369410,
            "end": 369420,
            "jobs": [{"rev": 369411, "status": "bad"}],
        }

        # Create a temporary .JSON file to simulate a .JSON file that has
        # bisection contents.
        with test_helpers.CreateTemporaryJsonFile() as temp_json_file:
            with open(temp_json_file, "w", encoding="utf-8") as f:
                test_helpers.WritePrettyJsonFile(bisect_test_contents, f)

            # Add a revision that is outside of 'start' and 'end'.
            revision_to_add = 369418

            args_output = test_helpers.ArgsOutputTest()
            args_output.options = None

            new_tryjob_info = {
                "rev": revision_to_add,
                "status": "pending",
                "options": args_output.options,
                "extra_cls": args_output.extra_change_lists,
                "builder": args_output.builders,
            }

            mock_add_tryjob.return_value = new_tryjob_info

            modify_a_tryjob.PerformTryjobModification(
                revision_to_add,
                modify_a_tryjob.ModifyTryjob.ADD,
                temp_json_file,
                args_output.extra_change_lists,
                args_output.options,
                args_output.builders,
                args_output.chromeos_path,
            )

            # Verify that the tryjob was added to the status file.
            with open(temp_json_file, encoding="utf-8") as status_file:
                bisect_contents = json.load(status_file)

                expected_file_contents = {
                    "start": 369410,
                    "end": 369420,
                    "jobs": [{"rev": 369411, "status": "bad"}, new_tryjob_info],
                }

                self.assertDictEqual(bisect_contents, expected_file_contents)

        mock_find_tryjob_index.assert_called_once()

        mock_get_llvm_hash.assert_called_once_with(revision_to_add)

        mock_add_tryjob.assert_called_once()

    # Simulate the behavior of `FindTryjobIndex()` when the tryjob was found.
    @mock.patch.object(update_tryjob_status, "FindTryjobIndex", return_value=0)
    def testModifyATryjobOptionDoesNotExist(self, mock_find_tryjob_index):
        bisect_test_contents = {
            "start": 369410,
            "end": 369420,
            "jobs": [{"rev": 369414, "status": "bad"}],
        }

        # Create a temporary .JSON file to simulate a .JSON file that has
        # bisection contents.
        with test_helpers.CreateTemporaryJsonFile() as temp_json_file:
            with open(temp_json_file, "w", encoding="utf-8") as f:
                test_helpers.WritePrettyJsonFile(bisect_test_contents, f)

            # Add a revision that is outside of 'start' and 'end'.
            revision_to_modify = 369414

            args_output = test_helpers.ArgsOutputTest()
            args_output.builders = None
            args_output.options = None

            # Verify the exception is raised when the modify a tryjob option
            # does not exist.
            with self.assertRaises(ValueError) as err:
                modify_a_tryjob.PerformTryjobModification(
                    revision_to_modify,
                    "remove_link",
                    temp_json_file,
                    args_output.extra_change_lists,
                    args_output.options,
                    args_output.builders,
                    args_output.chromeos_path,
                )

            self.assertEqual(
                str(err.exception),
                'Invalid "modify_tryjob" option provided: remove_link',
            )

            mock_find_tryjob_index.assert_called_once()


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