diff options
Diffstat (limited to 'llvm_tools/update_tryjob_status_unittest.py')
-rwxr-xr-x | llvm_tools/update_tryjob_status_unittest.py | 214 |
1 files changed, 37 insertions, 177 deletions
diff --git a/llvm_tools/update_tryjob_status_unittest.py b/llvm_tools/update_tryjob_status_unittest.py index b5e6556c..c42c6718 100755 --- a/llvm_tools/update_tryjob_status_unittest.py +++ b/llvm_tools/update_tryjob_status_unittest.py @@ -31,14 +31,13 @@ class UpdateTryjobStatusTest(unittest.TestCase): '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 - }] + }, { + 'rev': 1000, + 'url': 'https://some_url_to_CL.com', + 'cl': 'https://some_link_to_tryjob.com', + 'status': 'pending', + 'buildbucket_id': 10931 + }] expected_index = 0 @@ -55,87 +54,19 @@ class UpdateTryjobStatusTest(unittest.TestCase): '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 - }] + }, { + '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)) - # Simulate the behavior of `ChrootRunCommand()` when executing a command - # inside the chroot. - @mock.patch.object(update_tryjob_status, 'ChrootRunCommand') - def testGetStatusFromCrosBuildResult(self, mock_chroot_command): - tryjob_contents = { - '192': { - 'status': 'good', - 'CleanUpChroot': 'pass', - 'artifacts_url': None - } - } - - # Use the test function to simulate 'ChrootRunCommand()' behavior. - mock_chroot_command.return_value = json.dumps(tryjob_contents) - - buildbucket_id = 192 - - chroot_path = '/some/path/to/chroot' - - self.assertEqual( - update_tryjob_status.GetStatusFromCrosBuildResult( - chroot_path, buildbucket_id), 'good') - - expected_cmd = [ - 'cros', 'buildresult', '--buildbucket-id', - str(buildbucket_id), '--report', 'json' - ] - - mock_chroot_command.assert_called_once_with(chroot_path, expected_cmd) - - # Simulate the behavior of `GetStatusFromCrosBuildResult()` when `cros - # buildresult` returned a string that is not in the mapping. - @mock.patch.object( - update_tryjob_status, - 'GetStatusFromCrosBuildResult', - return_value='querying') - def testInvalidCrosBuildResultValue(self, mock_cros_buildresult): - chroot_path = '/some/path/to/chroot' - buildbucket_id = 50 - - # Verify the exception is raised when the return value of `cros buildresult` - # is not in the `builder_status_mapping`. - with self.assertRaises(ValueError) as err: - update_tryjob_status.GetAutoResult(chroot_path, buildbucket_id) - - self.assertEqual( - str(err.exception), - '"cros buildresult" return value is invalid: querying') - - mock_cros_buildresult.assert_called_once_with(chroot_path, buildbucket_id) - - # Simulate the behavior of `GetStatusFromCrosBuildResult()` when `cros - # buildresult` returned a string that is in the mapping. - @mock.patch.object( - update_tryjob_status, - 'GetStatusFromCrosBuildResult', - return_value=update_tryjob_status.BuilderStatus.PASS.value) - def testValidCrosBuildResultValue(self, mock_cros_buildresult): - chroot_path = '/some/path/to/chroot' - buildbucket_id = 100 - - self.assertEqual( - update_tryjob_status.GetAutoResult(chroot_path, buildbucket_id), - TryjobStatus.GOOD.value) - - mock_cros_buildresult.assert_called_once_with(chroot_path, buildbucket_id) - @mock.patch.object(subprocess, 'Popen') # Simulate the behavior of `os.rename()` when successfully renamed a file. @mock.patch.object(os, 'rename', return_value=None) @@ -186,8 +117,9 @@ class UpdateTryjobStatusTest(unittest.TestCase): # 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) + update_tryjob_status.GetCustomScriptResult(custom_script_path, + status_file_path, + tryjob_contents) self.assertEqual(str(err.exception), expected_error_message) @@ -212,8 +144,8 @@ class UpdateTryjobStatusTest(unittest.TestCase): # `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 + mock_exec_custom_script.return_value.returncode = ( + CustomScriptStatus.GOOD.value) tryjob_contents = { 'status': 'good', @@ -226,8 +158,9 @@ class UpdateTryjobStatusTest(unittest.TestCase): status_file_path = '/abs/path/to/status_file.json' self.assertEqual( - update_tryjob_status.GetCustomScriptResult( - custom_script_path, status_file_path, tryjob_contents), + update_tryjob_status.GetCustomScriptResult(custom_script_path, + status_file_path, + tryjob_contents), TryjobStatus.GOOD.value) mock_exec_custom_script.assert_called_once() @@ -247,16 +180,14 @@ class UpdateTryjobStatusTest(unittest.TestCase): revision_to_update = 369412 - chroot_path = '/abs/path/to/chroot' - 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, chroot_path, - custom_script) + 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) @@ -283,16 +214,14 @@ class UpdateTryjobStatusTest(unittest.TestCase): revision_to_update = 369416 - chroot_path = '/abs/path/to/chroot' - 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, chroot_path, - custom_script) + 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' % @@ -324,13 +253,11 @@ class UpdateTryjobStatusTest(unittest.TestCase): # Index of the tryjob that is going to have its 'status' value updated. tryjob_index = 0 - chroot_path = '/abs/path/to/chroot' - custom_script = None update_tryjob_status.UpdateTryjobStatus(revision_to_update, TryjobStatus.GOOD, temp_json_file, - chroot_path, custom_script) + custom_script) # Verify that the tryjob's 'status' has been updated in the status file. with open(temp_json_file) as status_file: @@ -365,13 +292,11 @@ class UpdateTryjobStatusTest(unittest.TestCase): # Index of the tryjob that is going to have its 'status' value updated. tryjob_index = 0 - chroot_path = '/abs/path/to/chroot' - custom_script = None update_tryjob_status.UpdateTryjobStatus(revision_to_update, TryjobStatus.BAD, temp_json_file, - chroot_path, custom_script) + custom_script) # Verify that the tryjob's 'status' has been updated in the status file. with open(temp_json_file) as status_file: @@ -407,13 +332,11 @@ class UpdateTryjobStatusTest(unittest.TestCase): # Index of the tryjob that is going to have its 'status' value updated. tryjob_index = 0 - chroot_path = '/abs/path/to/chroot' - custom_script = None update_tryjob_status.UpdateTryjobStatus( revision_to_update, update_tryjob_status.TryjobStatus.SKIP, - temp_json_file, chroot_path, custom_script) + 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: @@ -448,13 +371,11 @@ class UpdateTryjobStatusTest(unittest.TestCase): # Index of the tryjob that is going to have its 'status' value updated. tryjob_index = 0 - chroot_path = '/abs/path/to/chroot' - custom_script = None update_tryjob_status.UpdateTryjobStatus( revision_to_update, update_tryjob_status.TryjobStatus.PENDING, - temp_json_file, chroot_path, custom_script) + 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: @@ -465,69 +386,12 @@ class UpdateTryjobStatusTest(unittest.TestCase): 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) - # Simulate the behavior of `GetAutoResult()` when `cros buildresult` returns - # a value that is in the mapping. - @mock.patch.object( - update_tryjob_status, - 'GetAutoResult', - return_value=TryjobStatus.GOOD.value) - def testSuccessfullyUpdatedTryjobStatusToAuto(self, mock_get_auto_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 - - path_to_chroot = '/abs/path/to/chroot' - - custom_script = None - - update_tryjob_status.UpdateTryjobStatus( - revision_to_update, update_tryjob_status.TryjobStatus.AUTO, - temp_json_file, path_to_chroot, 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.GOOD.value) - - mock_get_auto_result.assert_called_once_with( - path_to_chroot, - bisect_test_contents['jobs'][tryjob_index]['buildbucket_id']) - - 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) - # Simulate the behavior of `GetCustomScriptResult()` when the custom script - # exit code is in the mapping. @mock.patch.object( update_tryjob_status, 'GetCustomScriptResult', return_value=TryjobStatus.SKIP.value) - def testSuccessfullyUpdatedTryjobStatusToAuto( + def testUpdatedTryjobStatusToAutoPassedWithCustomScript( self, mock_get_custom_script_result, mock_find_tryjob_index): bisect_test_contents = { 'start': 369410, @@ -550,13 +414,11 @@ class UpdateTryjobStatusTest(unittest.TestCase): # Index of the tryjob that is going to have its 'status' value updated. tryjob_index = 0 - path_to_chroot = '/abs/path/to/chroot' - 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, path_to_chroot, custom_script_path) + 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: @@ -593,8 +455,6 @@ class UpdateTryjobStatusTest(unittest.TestCase): revision_to_update = 369411 - path_to_chroot = '/abs/path/to/chroot' - nonexistent_update_status = 'revert_status' custom_script = None @@ -602,9 +462,9 @@ class UpdateTryjobStatusTest(unittest.TestCase): # 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, - path_to_chroot, custom_script) + update_tryjob_status.UpdateTryjobStatus(revision_to_update, + nonexistent_update_status, + temp_json_file, custom_script) self.assertEqual( str(err.exception), |