OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
6 import re | 6 import re |
7 import shutil | 7 import shutil |
| 8 import sys |
8 import unittest | 9 import unittest |
9 | 10 |
| 11 SRC = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir) |
| 12 sys.path.append(os.path.join(SRC, 'third_party', 'pymock')) |
| 13 |
10 import bisect_perf_regression | 14 import bisect_perf_regression |
11 import bisect_results | 15 import bisect_results |
| 16 import mock |
12 import source_control as source_control_module | 17 import source_control as source_control_module |
13 | 18 |
| 19 |
14 def _GetBisectPerformanceMetricsInstance(): | 20 def _GetBisectPerformanceMetricsInstance(): |
15 """Returns an instance of the BisectPerformanceMetrics class.""" | 21 """Returns an instance of the BisectPerformanceMetrics class.""" |
16 options_dict = { | 22 options_dict = { |
17 'debug_ignore_build': True, | 23 'debug_ignore_build': True, |
18 'debug_ignore_sync': True, | 24 'debug_ignore_sync': True, |
19 'debug_ignore_perf_test': True, | 25 'debug_ignore_perf_test': True, |
20 'command': 'fake_command', | 26 'command': 'fake_command', |
21 'metric': 'fake/metric', | 27 'metric': 'fake/metric', |
22 'good_revision': 280000, | 28 'good_revision': 280000, |
23 'bad_revision': 280005, | 29 'bad_revision': 280005, |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 self.assertEqual(self.registry.GetDepotDir('cros'), '/mock/src/tools/cros') | 347 self.assertEqual(self.registry.GetDepotDir('cros'), '/mock/src/tools/cros') |
342 | 348 |
343 def testUsesDepotSpecToInitializeRegistry(self): | 349 def testUsesDepotSpecToInitializeRegistry(self): |
344 self.assertEqual(self.registry.GetDepotDir('mock_depot'), '/mock/src/foo') | 350 self.assertEqual(self.registry.GetDepotDir('mock_depot'), '/mock/src/foo') |
345 | 351 |
346 def testChangedTheDirectory(self): | 352 def testChangedTheDirectory(self): |
347 self.registry.ChangeToDepotDir('mock_depot') | 353 self.registry.ChangeToDepotDir('mock_depot') |
348 self.assertEqual(self.cur_dir, '/mock/src/foo') | 354 self.assertEqual(self.cur_dir, '/mock/src/foo') |
349 | 355 |
350 | 356 |
| 357 # The tests below test private functions (W0212). |
| 358 # pylint: disable=W0212 |
| 359 class GitTryJobTestCases(unittest.TestCase): |
| 360 """Test case for bisect try job.""" |
| 361 def setUp(self): |
| 362 bisect_utils_patcher = mock.patch('bisect_perf_regression.bisect_utils') |
| 363 self.mock_bisect_utils = bisect_utils_patcher.start() |
| 364 self.addCleanup(bisect_utils_patcher.stop) |
| 365 |
| 366 def _SetupRunGitMock(self, git_cmds): |
| 367 """Setup RunGit mock with expected output for given git command.""" |
| 368 def side_effect(git_cmd_args): |
| 369 for val in git_cmds: |
| 370 if set(val[0]) == set(git_cmd_args): |
| 371 return val[1] |
| 372 self.mock_bisect_utils.RunGit = mock.Mock(side_effect=side_effect) |
| 373 |
| 374 def _AssertRunGitExceptions(self, git_cmds, func, *args): |
| 375 """Setup RunGit mock and tests RunGitException. |
| 376 |
| 377 Args: |
| 378 git_cmds: List of tuples with git command and expected output. |
| 379 func: Callback function to be executed. |
| 380 args: List of arguments to be passed to the function. |
| 381 """ |
| 382 self._SetupRunGitMock(git_cmds) |
| 383 self.assertRaises(bisect_perf_regression.RunGitError, |
| 384 func, |
| 385 *args) |
| 386 |
| 387 def testNotGitRepo(self): |
| 388 new_branch = bisect_perf_regression.BISECT_TRYJOB_BRANCH |
| 389 parent_branch = bisect_perf_regression.BISECT_MASTER_BRANCH |
| 390 cmds = [(['rev-parse', '--abbrev-ref', 'HEAD'], (None, 128))] |
| 391 self._AssertRunGitExceptions(cmds, |
| 392 bisect_perf_regression._PrepareBisectBranch, |
| 393 parent_branch, new_branch) |
| 394 |
| 395 def testFailedCheckoutMaster(self): |
| 396 new_branch = bisect_perf_regression.BISECT_TRYJOB_BRANCH |
| 397 parent_branch = bisect_perf_regression.BISECT_MASTER_BRANCH |
| 398 cmds = [ |
| 399 (['rev-parse', '--abbrev-ref', 'HEAD'], (new_branch, 0)), |
| 400 (['checkout', '-f', parent_branch], ('Checkout Failed', 1))] |
| 401 self._AssertRunGitExceptions(cmds, |
| 402 bisect_perf_regression._PrepareBisectBranch, |
| 403 parent_branch, new_branch) |
| 404 |
| 405 def testDeleteBisectBranchIfExists(self): |
| 406 new_branch = bisect_perf_regression.BISECT_TRYJOB_BRANCH |
| 407 parent_branch = bisect_perf_regression.BISECT_MASTER_BRANCH |
| 408 cmds = [ |
| 409 (['rev-parse', '--abbrev-ref', 'HEAD'], (parent_branch, 0)), |
| 410 (['branch', '--list' ], ('bisect-tryjob\n*master\nsomebranch', 0)), |
| 411 (['branch', '-D', new_branch], ('Failed to delete branch', 128)) |
| 412 ] |
| 413 self._AssertRunGitExceptions(cmds, |
| 414 bisect_perf_regression._PrepareBisectBranch, |
| 415 parent_branch, new_branch) |
| 416 |
| 417 def testCreatNewBranchFails(self): |
| 418 new_branch = bisect_perf_regression.BISECT_TRYJOB_BRANCH |
| 419 parent_branch = bisect_perf_regression.BISECT_MASTER_BRANCH |
| 420 cmds = [ |
| 421 (['rev-parse', '--abbrev-ref', 'HEAD'], (parent_branch, 0)), |
| 422 (['branch', '--list' ], ('bisect-tryjob\n*master\nsomebranch', 0)), |
| 423 (['branch', '-D', new_branch], ('None', 0)), |
| 424 (['update-index', '--refresh', '-q'], (None, 0)), |
| 425 (['diff-index', 'HEAD'], (None, 0)), |
| 426 (['checkout', '-b', new_branch], ('Failed to create branch', 128)) |
| 427 ] |
| 428 |
| 429 self._AssertRunGitExceptions(cmds, |
| 430 bisect_perf_regression._PrepareBisectBranch, |
| 431 parent_branch, new_branch) |
| 432 |
| 433 def testSetUpstreamToFails(self): |
| 434 new_branch = bisect_perf_regression.BISECT_TRYJOB_BRANCH |
| 435 parent_branch = bisect_perf_regression.BISECT_MASTER_BRANCH |
| 436 cmds = [ |
| 437 (['rev-parse', '--abbrev-ref', 'HEAD'], (parent_branch, 0)), |
| 438 (['branch', '--list' ], ('bisect-tryjob\n*master\nsomebranch', 0)), |
| 439 (['branch', '-D', new_branch], ('None', 0)), |
| 440 (['update-index', '--refresh', '-q'], (None, 0)), |
| 441 (['diff-index', 'HEAD'], (None, 0)), |
| 442 (['checkout', '-b', new_branch], ('None', 0)), |
| 443 (['branch', '--set-upstream-to', parent_branch], |
| 444 ('Setuptream fails', 1)) |
| 445 ] |
| 446 self._AssertRunGitExceptions(cmds, |
| 447 bisect_perf_regression._PrepareBisectBranch, |
| 448 parent_branch, new_branch) |
| 449 def testBuilderTryJobForException(self): |
| 450 git_revision = 'ac4a9f31fe2610bd146857bbd55d7a260003a888' |
| 451 bot_name = 'linux_perf_bisect_builder' |
| 452 bisect_job_name = 'testBisectJobname' |
| 453 patch = None |
| 454 patch_content = '/dev/null' |
| 455 new_branch = bisect_perf_regression.BISECT_TRYJOB_BRANCH |
| 456 parent_branch = bisect_perf_regression.BISECT_MASTER_BRANCH |
| 457 try_cmd = [ |
| 458 (['rev-parse', '--abbrev-ref', 'HEAD'], (parent_branch, 0)), |
| 459 (['branch', '--list' ], ('bisect-tryjob\n*master\nsomebranch', 0)), |
| 460 (['branch', '-D', new_branch], ('None', 0)), |
| 461 (['update-index', '--refresh', '-q'], (None, 0)), |
| 462 (['diff-index', 'HEAD'], (None, 0)), |
| 463 (['checkout', '-b', new_branch], ('None', 0)), |
| 464 (['branch', '--set-upstream-to', parent_branch], |
| 465 ('Setuptream fails', 0)), |
| 466 (['try', |
| 467 '-b', bot_name, |
| 468 '-r', git_revision, |
| 469 '-n', bisect_job_name, |
| 470 '--svn_repo=%s' % bisect_perf_regression.SVN_REPO_URL, |
| 471 '--diff=%s' % patch_content |
| 472 ], (None, 1)) |
| 473 ] |
| 474 self._AssertRunGitExceptions(try_cmd, |
| 475 bisect_perf_regression._BuilderTryjob, |
| 476 git_revision, bot_name, bisect_job_name, patch) |
| 477 |
| 478 def testBuilderTryJob(self): |
| 479 git_revision = 'ac4a9f31fe2610bd146857bbd55d7a260003a888' |
| 480 bot_name = 'linux_perf_bisect_builder' |
| 481 bisect_job_name = 'testBisectJobname' |
| 482 patch = None |
| 483 patch_content = '/dev/null' |
| 484 new_branch = bisect_perf_regression.BISECT_TRYJOB_BRANCH |
| 485 parent_branch = bisect_perf_regression.BISECT_MASTER_BRANCH |
| 486 try_cmd = [ |
| 487 (['rev-parse', '--abbrev-ref', 'HEAD'], (parent_branch, 0)), |
| 488 (['branch', '--list' ], ('bisect-tryjob\n*master\nsomebranch', 0)), |
| 489 (['branch', '-D', new_branch], ('None', 0)), |
| 490 (['update-index', '--refresh', '-q'], (None, 0)), |
| 491 (['diff-index', 'HEAD'], (None, 0)), |
| 492 (['checkout', '-b', new_branch], ('None', 0)), |
| 493 (['branch', '--set-upstream-to', parent_branch], |
| 494 ('Setuptream fails', 0)), |
| 495 (['try', |
| 496 '-b', bot_name, |
| 497 '-r', git_revision, |
| 498 '-n', bisect_job_name, |
| 499 '--svn_repo=%s' % bisect_perf_regression.SVN_REPO_URL, |
| 500 '--diff=%s' % patch_content |
| 501 ], (None, 0)) |
| 502 ] |
| 503 self._SetupRunGitMock(try_cmd) |
| 504 bisect_perf_regression._BuilderTryjob( |
| 505 git_revision, bot_name, bisect_job_name, patch) |
| 506 |
| 507 |
351 if __name__ == '__main__': | 508 if __name__ == '__main__': |
352 unittest.main() | 509 unittest.main() |
OLD | NEW |