aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/auto_llvm_bisection_unittest.py
blob: 30e7dfb3bd702a90dd89d6a9ec0f5f3e537dff7c (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
#!/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 auto bisection of LLVM."""


import json
import os
import subprocess
import time
import traceback
import unittest
from unittest import mock

import auto_llvm_bisection
import chroot
import llvm_bisection
import test_helpers
import update_tryjob_status


class AutoLLVMBisectionTest(unittest.TestCase):
    """Unittests for auto bisection of LLVM."""

    @mock.patch.object(chroot, "VerifyChromeOSRoot")
    @mock.patch.object(chroot, "VerifyOutsideChroot", return_value=True)
    @mock.patch.object(
        llvm_bisection,
        "GetCommandLineArgs",
        return_value=test_helpers.ArgsOutputTest(),
    )
    @mock.patch.object(time, "sleep")
    @mock.patch.object(traceback, "print_exc")
    @mock.patch.object(llvm_bisection, "main")
    @mock.patch.object(os.path, "isfile")
    @mock.patch.object(auto_llvm_bisection, "open")
    @mock.patch.object(json, "load")
    @mock.patch.object(auto_llvm_bisection, "GetBuildResult")
    @mock.patch.object(os, "rename")
    def testAutoLLVMBisectionPassed(
        self,
        # pylint: disable=unused-argument
        mock_rename,
        mock_get_build_result,
        mock_json_load,
        # pylint: disable=unused-argument
        mock_open,
        mock_isfile,
        mock_llvm_bisection,
        mock_traceback,
        mock_sleep,
        mock_get_args,
        mock_outside_chroot,
        mock_chromeos_root,
    ):

        mock_isfile.side_effect = [False, False, True, True]
        mock_llvm_bisection.side_effect = [
            0,
            ValueError("Failed to launch more tryjobs."),
            llvm_bisection.BisectionExitStatus.BISECTION_COMPLETE.value,
        ]
        mock_json_load.return_value = {
            "start": 369410,
            "end": 369420,
            "jobs": [
                {
                    "buildbucket_id": 12345,
                    "rev": 369411,
                    "status": update_tryjob_status.TryjobStatus.PENDING.value,
                }
            ],
        }
        mock_get_build_result.return_value = (
            update_tryjob_status.TryjobStatus.GOOD.value
        )

        # Verify the excpetion is raised when successfully found the bad
        # revision. Uses `sys.exit(0)` to indicate success.
        with self.assertRaises(SystemExit) as err:
            auto_llvm_bisection.main()

        self.assertEqual(err.exception.code, 0)

        mock_outside_chroot.assert_called_once()
        mock_get_args.assert_called_once()
        self.assertEqual(mock_isfile.call_count, 3)
        self.assertEqual(mock_llvm_bisection.call_count, 3)
        mock_traceback.assert_called_once()
        mock_sleep.assert_called_once()

    @mock.patch.object(chroot, "VerifyChromeOSRoot")
    @mock.patch.object(chroot, "VerifyOutsideChroot", return_value=True)
    @mock.patch.object(time, "sleep")
    @mock.patch.object(traceback, "print_exc")
    @mock.patch.object(llvm_bisection, "main")
    @mock.patch.object(os.path, "isfile")
    @mock.patch.object(
        llvm_bisection,
        "GetCommandLineArgs",
        return_value=test_helpers.ArgsOutputTest(),
    )
    def testFailedToStartBisection(
        self,
        mock_get_args,
        mock_isfile,
        mock_llvm_bisection,
        mock_traceback,
        mock_sleep,
        mock_outside_chroot,
        mock_chromeos_root,
    ):

        mock_isfile.return_value = False
        mock_llvm_bisection.side_effect = ValueError(
            "Failed to launch more tryjobs."
        )

        # Verify the exception is raised when the number of attempts to launched
        # more tryjobs is exceeded, so unable to continue
        # bisection.
        with self.assertRaises(SystemExit) as err:
            auto_llvm_bisection.main()

        self.assertEqual(err.exception.code, "Unable to continue bisection.")

        mock_chromeos_root.assert_called_once()
        mock_outside_chroot.assert_called_once()
        mock_get_args.assert_called_once()
        self.assertEqual(mock_isfile.call_count, 2)
        self.assertEqual(mock_llvm_bisection.call_count, 3)
        self.assertEqual(mock_traceback.call_count, 3)
        self.assertEqual(mock_sleep.call_count, 2)

    @mock.patch.object(chroot, "VerifyChromeOSRoot")
    @mock.patch.object(chroot, "VerifyOutsideChroot", return_value=True)
    @mock.patch.object(
        llvm_bisection,
        "GetCommandLineArgs",
        return_value=test_helpers.ArgsOutputTest(),
    )
    @mock.patch.object(time, "time")
    @mock.patch.object(time, "sleep")
    @mock.patch.object(os.path, "isfile")
    @mock.patch.object(auto_llvm_bisection, "open")
    @mock.patch.object(json, "load")
    @mock.patch.object(auto_llvm_bisection, "GetBuildResult")
    def testFailedToUpdatePendingTryJobs(
        self,
        mock_get_build_result,
        mock_json_load,
        # pylint: disable=unused-argument
        mock_open,
        mock_isfile,
        mock_sleep,
        mock_time,
        mock_get_args,
        mock_outside_chroot,
        mock_chromeos_root,
    ):

        # Simulate behavior of `time.time()` for time passed.
        @test_helpers.CallCountsToMockFunctions
        def MockTimePassed(call_count):
            if call_count < 3:
                return call_count

            assert False, "Called `time.time()` more than expected."

        mock_isfile.return_value = True
        mock_json_load.return_value = {
            "start": 369410,
            "end": 369420,
            "jobs": [
                {
                    "buildbucket_id": 12345,
                    "rev": 369411,
                    "status": update_tryjob_status.TryjobStatus.PENDING.value,
                }
            ],
        }
        mock_get_build_result.return_value = None
        mock_time.side_effect = MockTimePassed
        # Reduce the polling limit for the test case to terminate faster.
        auto_llvm_bisection.POLLING_LIMIT_SECS = 1

        # Verify the exception is raised when unable to update tryjobs whose
        # 'status' value is 'pending'.
        with self.assertRaises(SystemExit) as err:
            auto_llvm_bisection.main()

        self.assertEqual(
            err.exception.code, "Failed to update pending tryjobs."
        )

        mock_outside_chroot.assert_called_once()
        mock_get_args.assert_called_once()
        self.assertEqual(mock_isfile.call_count, 2)
        mock_sleep.assert_called_once()
        self.assertEqual(mock_time.call_count, 3)

    @mock.patch.object(subprocess, "check_output")
    def testGetBuildResult(self, mock_chroot_command):
        buildbucket_id = 192
        status = auto_llvm_bisection.BuilderStatus.PASS.value
        tryjob_contents = {buildbucket_id: {"status": status}}
        mock_chroot_command.return_value = json.dumps(tryjob_contents)
        chroot_path = "/some/path/to/chroot"

        self.assertEqual(
            auto_llvm_bisection.GetBuildResult(chroot_path, buildbucket_id),
            update_tryjob_status.TryjobStatus.GOOD.value,
        )

        mock_chroot_command.assert_called_once_with(
            [
                "cros",
                "buildresult",
                "--buildbucket-id",
                str(buildbucket_id),
                "--report",
                "json",
            ],
            cwd="/some/path/to/chroot",
            stderr=subprocess.STDOUT,
            encoding="UTF-8",
        )

    @mock.patch.object(subprocess, "check_output")
    def testGetBuildResultPassedWithUnstartedTryjob(self, mock_chroot_command):
        buildbucket_id = 192
        chroot_path = "/some/path/to/chroot"
        mock_chroot_command.side_effect = subprocess.CalledProcessError(
            returncode=1, cmd=[], output="No build found. Perhaps not started"
        )
        auto_llvm_bisection.GetBuildResult(chroot_path, buildbucket_id)
        mock_chroot_command.assert_called_once_with(
            [
                "cros",
                "buildresult",
                "--buildbucket-id",
                "192",
                "--report",
                "json",
            ],
            cwd=chroot_path,
            stderr=subprocess.STDOUT,
            encoding="UTF-8",
        )

    @mock.patch.object(subprocess, "check_output")
    def testGetBuildReusultFailedWithInvalidBuildStatus(
        self, mock_chroot_command
    ):
        chroot_path = "/some/path/to/chroot"
        buildbucket_id = 50
        invalid_build_status = "querying"
        tryjob_contents = {buildbucket_id: {"status": invalid_build_status}}
        mock_chroot_command.return_value = json.dumps(tryjob_contents)

        # Verify an exception is raised when the return value of `cros
        # buildresult` is not in the `builder_status_mapping`.
        with self.assertRaises(ValueError) as err:
            auto_llvm_bisection.GetBuildResult(chroot_path, buildbucket_id)

        self.assertEqual(
            str(err.exception),
            '"cros buildresult" return value is invalid: %s'
            % invalid_build_status,
        )

        mock_chroot_command.assert_called_once_with(
            [
                "cros",
                "buildresult",
                "--buildbucket-id",
                str(buildbucket_id),
                "--report",
                "json",
            ],
            cwd=chroot_path,
            stderr=subprocess.STDOUT,
            encoding="UTF-8",
        )


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