diff options
author | Salud Lemus <saludlemus@google.com> | 2019-09-10 09:47:33 -0700 |
---|---|---|
committer | Salud Lemus <saludlemus@google.com> | 2019-09-12 00:18:46 +0000 |
commit | e398ca2b3c99ceb4264c7a317f5f9cbc54db6787 (patch) | |
tree | c4b493353aab08d2415ba2336f1f9b592aba83f8 /llvm_tools | |
parent | 723eb0ff87880d5ce7ebc3697866b933e0aa7961 (diff) | |
download | toolchain-utils-e398ca2b3c99ceb4264c7a317f5f9cbc54db6787.tar.gz |
LLVM tools: Updated unittests for update_chromeos_llvm_next_hash.py
BUG=None
TEST='./update_chromeos_llvm_next_hash_unittest.py' passes
Change-Id: I9793dde698fe21dc4d9462c552bc1c321ad06ff4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1796383
Tested-by: Salud Lemus <saludlemus@google.com>
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Diffstat (limited to 'llvm_tools')
-rwxr-xr-x | llvm_tools/update_chromeos_llvm_next_hash_unittest.py | 881 |
1 files changed, 345 insertions, 536 deletions
diff --git a/llvm_tools/update_chromeos_llvm_next_hash_unittest.py b/llvm_tools/update_chromeos_llvm_next_hash_unittest.py index 62615def..756ee9c9 100755 --- a/llvm_tools/update_chromeos_llvm_next_hash_unittest.py +++ b/llvm_tools/update_chromeos_llvm_next_hash_unittest.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/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 @@ -9,16 +9,14 @@ from __future__ import print_function from collections import namedtuple -from pipes import quote -from tempfile import mkstemp import os +import subprocess import unittest +import unittest.mock as mock -from cros_utils import command_executer from failure_modes import FailureModes -from test_helpers import CallCountsToMockFunctions +from test_helpers import CreateTemporaryJsonFile import llvm_patch_management -import mock import update_chromeos_llvm_next_hash # These are unittests; protected access is OK to a point. @@ -28,82 +26,61 @@ import update_chromeos_llvm_next_hash class UpdateLLVMNextHashTest(unittest.TestCase): """Test class for updating 'LLVM_NEXT_HASH' of packages.""" - @mock.patch.object(command_executer.CommandExecuter, - 'ChrootRunCommandWOutput') - def testFailedToGetChrootPathForInvalidPackage(self, mock_chroot_command): - - # Emulate ChrootRunCommandWOutput behavior when an invalid package is - # passed in. - # - # Returns shell error code, stdout, stderr. - mock_chroot_command.return_value = (1, None, 'Invalid package provided.') - - # Verify the exception is raised when an invalid package is passed in. - with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.GetChrootBuildPaths( - '/test/chroot/path', ['test-pckg/test']) - - self.assertEqual( - err.exception.message, - 'Failed to get chroot path for the package (test-pckg/test): ' - 'Invalid package provided.') - - mock_chroot_command.assert_called_once_with( - chromeos_root='/test/chroot/path', - command='equery w test-pckg/test', - print_to_console=False) - - @mock.patch.object(command_executer.CommandExecuter, - 'ChrootRunCommandWOutput') + @mock.patch.object(update_chromeos_llvm_next_hash, 'ChrootRunCommand') def testSucceedsToGetChrootPathForPackage(self, mock_chroot_command): + package_chroot_path = '/chroot/path/to/package.ebuild' + # Emulate ChrootRunCommandWOutput behavior when a chroot path is found for # a valid package. - # - # Returns shell error code, stdout, stderr. - mock_chroot_command.return_value = (0, '/chroot/path/to/package.ebuild\n', - 0) + mock_chroot_command.return_value = package_chroot_path + + chroot_path = '/test/chroot/path' + package_list = ['new-test/package'] self.assertEqual( update_chromeos_llvm_next_hash.GetChrootBuildPaths( - '/test/chroot/path', ['new-test/package']), - ['/chroot/path/to/package.ebuild']) + chroot_path, package_list), [package_chroot_path]) - mock_chroot_command.assert_called_once_with( - chromeos_root='/test/chroot/path', - command='equery w new-test/package', - print_to_console=False) + mock_chroot_command.assert_called_once() def testFailedToConvertChrootPathWithInvalidPrefixToSymlinkPath(self): + chroot_path = '/path/to/chroot' + chroot_file_path = '/src/package.ebuild' + # Verify the exception is raised when a symlink does not have the prefix # '/mnt/host/source/'. with self.assertRaises(ValueError) as err: update_chromeos_llvm_next_hash._ConvertChrootPathsToSymLinkPaths( - '/path/to/chroot', ['/src/package.ebuild']) + chroot_path, [chroot_file_path]) self.assertEqual( - err.exception.message, 'Invalid prefix for the chroot path: ' - '/src/package.ebuild') + str(err.exception), 'Invalid prefix for the chroot path: ' + '%s' % chroot_file_path) def testSucceedsToConvertChrootPathToSymlinkPath(self): + chroot_path = '/path/to/chroot' + chroot_file_paths = ['/mnt/host/source/src/package.ebuild'] + + expected_symlink_path = '/path/to/chroot/src/package.ebuild' + self.assertEqual( update_chromeos_llvm_next_hash._ConvertChrootPathsToSymLinkPaths( - '/path/to/chroot', ['/mnt/host/source/src/package.ebuild']), - ['/path/to/chroot/src/package.ebuild']) + chroot_path, chroot_file_paths), [expected_symlink_path]) # Simulate 'os.path.islink' when a path is not a symbolic link. @mock.patch.object(os.path, 'islink', return_value=False) def testFailedToGetEbuildPathFromInvalidSymlink(self, mock_islink): + symlink_path = '/symlink/path/src/to/package-r1.ebuild' + # Verify the exception is raised when the argument is not a symbolic link. with self.assertRaises(ValueError) as err: update_chromeos_llvm_next_hash.GetEbuildPathsFromSymLinkPaths( - ['/symlink/path/src/to/package-r1.ebuild']) + [symlink_path]) self.assertEqual( - err.exception.message, - 'Invalid symlink provided: /symlink/path/src/to/package-r1.ebuild') + str(err.exception), 'Invalid symlink provided: %s' % symlink_path) - mock_islink.assert_called_once_with( - '/symlink/path/src/to/package-r1.ebuild') + mock_islink.assert_called_once_with(symlink_path) # Simulate 'os.path.islink' when a path is a symbolic link. @mock.patch.object(os.path, 'islink', return_value=True) @@ -111,33 +88,39 @@ class UpdateLLVMNextHashTest(unittest.TestCase): def testSucceedsToGetEbuildPathFromValidSymlink(self, mock_realpath, mock_islink): + symlink_path = '/path/to/chroot/src/package-r1.ebuild' + + abs_path_to_package = '/abs/path/to/src/package.ebuild' + # Simulate 'os.path.realpath' when a valid path is passed in. - mock_realpath.return_value = '/abs/path/to/src/package.ebuild' + mock_realpath.return_value = abs_path_to_package + + expected_resolved_paths = {symlink_path: abs_path_to_package} self.assertEqual( update_chromeos_llvm_next_hash.GetEbuildPathsFromSymLinkPaths( - ['/path/to/chroot/src/package-r1.ebuild']), { - '/path/to/chroot/src/package-r1.ebuild': - '/abs/path/to/src/package.ebuild' - }) + [symlink_path]), expected_resolved_paths) - mock_realpath.assert_called_once_with( - '/path/to/chroot/src/package-r1.ebuild') + mock_realpath.assert_called_once_with(symlink_path) - mock_islink.assert_called_once_with('/path/to/chroot/src/package-r1.ebuild') + mock_islink.assert_called_once_with(symlink_path) # Simulate behavior of 'os.path.isfile()' when the ebuild path to a package # does not exist. @mock.patch.object(os.path, 'isfile', return_value=False) def testFailedToUpdateLLVMNextHashForInvalidEbuildPath(self, mock_isfile): + ebuild_path = '/some/path/to/package.ebuild' + + llvm_hash = 'a123testhash1' + llvm_revision = 1000 + # Verify the exception is raised when the ebuild path does not exist. with self.assertRaises(ValueError) as err: update_chromeos_llvm_next_hash.UpdateBuildLLVMNextHash( - '/some/path/to/package.ebuild', 'a123testhash1', 1000) + ebuild_path, llvm_hash, llvm_revision) self.assertEqual( - err.exception.message, - 'Invalid ebuild path provided: /some/path/to/package.ebuild') + str(err.exception), 'Invalid ebuild path provided: %s' % ebuild_path) mock_isfile.assert_called_once() @@ -145,108 +128,53 @@ class UpdateLLVMNextHashTest(unittest.TestCase): @mock.patch.object(os.path, 'isfile', return_value=True) def testFailedToUpdateLLVMNextHash(self, mock_isfile): # Create a temporary file to simulate an ebuild file of a package. - ebuild_file, file_path = mkstemp() - - os.write( - ebuild_file, '\n'.join([ + with CreateTemporaryJsonFile() as ebuild_file: + with open(ebuild_file, 'w') as f: + f.write('\n'.join([ 'First line in the ebuild', 'Second line in the ebuild', 'Last line in the ebuild' ])) - os.close(ebuild_file) + llvm_hash = 'a123testhash1' + llvm_revision = 1000 - try: # Verify the exception is raised when the ebuild file does not have # 'LLVM_NEXT_HASH'. with self.assertRaises(ValueError) as err: update_chromeos_llvm_next_hash.UpdateBuildLLVMNextHash( - file_path, 'a123testhash1', 1000) - - self.assertEqual(err.exception.message, 'Failed to update the LLVM hash.') - finally: - os.remove(file_path) - - mock_isfile.assert_called_once() - - # Simulate 'os.path.isfile' behavior on a valid ebuild path. - @mock.patch.object(os.path, 'isfile', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') - def testFailedToStageTheEbuildForCommitForLLVMNextHashUpdate( - self, mock_stage_commit_command, mock_isfile): - - # Simulate 'RunCommandWOutput' when failed to stage the ebuild file for - # commit. - # - # Returns shell error code, stdout, stderr. - mock_stage_commit_command.return_value = (1, None, 'Failed to add file.') - - # Create a temporary file to simulate an ebuild file of a package. - ebuild_file, file_path = mkstemp() - - os.write( - ebuild_file, '\n'.join([ - 'First line in the ebuild', 'Second line in the ebuild', - 'LLVM_NEXT_HASH=\"a12b34c56d78e90\" # r500', - 'Last line in the ebuild' - ])) - - os.close(ebuild_file) - - try: - # Verify the exception is raised when staging the ebuild file. - with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.UpdateBuildLLVMNextHash( - file_path, 'a123testhash1', 1000) - - self.assertEqual( - err.exception.message, 'Failed to stage the ebuild for commit: ' - 'Failed to add file.') + ebuild_file, llvm_hash, llvm_revision) - expected_file_contents = [ - 'First line in the ebuild\n', 'Second line in the ebuild\n', - 'LLVM_NEXT_HASH=\"a123testhash1\" # r1000\n', - 'Last line in the ebuild' - ] + self.assertEqual(str(err.exception), 'Failed to update the LLVM hash.') - # Verify the new file contents of the ebuild file match - # the expected file contents. - with open(file_path) as new_file: - file_contents_as_a_list = [cur_line for cur_line in new_file] - self.assertListEqual(file_contents_as_a_list, expected_file_contents) - - finally: - os.remove(file_path) - - mock_isfile.assert_called_once() - mock_stage_commit_command.assert_called_once() + self.assertEqual(mock_isfile.call_count, 2) # Simulate 'os.path.isfile' behavior on a valid ebuild path. @mock.patch.object(os.path, 'isfile', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') + # Simulate 'ExecCommandAndCaptureOutput()' when successfully staged the + # ebuild file for commit. + @mock.patch.object( + update_chromeos_llvm_next_hash, + 'ExecCommandAndCaptureOutput', + return_value=None) def testSuccessfullyStageTheEbuildForCommitForLLVMNextHashUpdate( self, mock_stage_commit_command, mock_isfile): - # Simulate 'RunCommandWOutput' when successfully staged the ebuild file for - # commit. - # - # Returns shell error code, stdout, stderr. - mock_stage_commit_command.return_value = (0, None, 0) - # Create a temporary file to simulate an ebuild file of a package. - ebuild_file, file_path = mkstemp() - - os.write( - ebuild_file, '\n'.join([ + with CreateTemporaryJsonFile() as ebuild_file: + with open(ebuild_file, 'w') as f: + f.write('\n'.join([ 'First line in the ebuild', 'Second line in the ebuild', 'LLVM_NEXT_HASH=\"a12b34c56d78e90\" # r500', 'Last line in the ebuild' ])) - os.close(ebuild_file) + # Updates the ebuild's git hash to 'llvm_hash' and revision to + # 'llvm_revision'. + llvm_hash = 'a123testhash1' + llvm_revision = 1000 - try: update_chromeos_llvm_next_hash.UpdateBuildLLVMNextHash( - file_path, 'a123testhash1', 1000) + ebuild_file, llvm_hash, llvm_revision) expected_file_contents = [ 'First line in the ebuild\n', 'Second line in the ebuild\n', @@ -256,184 +184,144 @@ class UpdateLLVMNextHashTest(unittest.TestCase): # Verify the new file contents of the ebuild file match the expected file # contents. - with open(file_path) as new_file: + with open(ebuild_file) as new_file: file_contents_as_a_list = [cur_line for cur_line in new_file] self.assertListEqual(file_contents_as_a_list, expected_file_contents) - finally: - os.remove(file_path) + self.assertEqual(mock_isfile.call_count, 2) - mock_isfile.assert_called_once() mock_stage_commit_command.assert_called_once() # Simulate behavior of 'os.path.islink()' when the argument passed in is not a # symbolic link. @mock.patch.object(os.path, 'islink', return_value=False) def testFailedToUprevEbuildForInvalidSymlink(self, mock_islink): + symlink_to_uprev = '/symlink/to/package.ebuild' + # Verify the exception is raised when a symbolic link is not passed in. with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.UprevEbuild('/symlink/to/package.ebuild') + update_chromeos_llvm_next_hash.UprevEbuild(symlink_to_uprev) - self.assertEqual(err.exception.message, - 'Invalid symlink provided: /symlink/to/package.ebuild') + self.assertEqual( + str(err.exception), 'Invalid symlink provided: %s' % symlink_to_uprev) mock_islink.assert_called_once() # Simulate 'os.path.islink' when a symbolic link is passed in. @mock.patch.object(os.path, 'islink', return_value=True) def testFailedToUprevEbuild(self, mock_islink): + symlink_to_uprev = '/symlink/to/package.ebuild' + # Verify the exception is raised when the symlink does not have a revision # number. with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.UprevEbuild('/symlink/to/package.ebuild') + update_chromeos_llvm_next_hash.UprevEbuild(symlink_to_uprev) - self.assertEqual(err.exception.message, 'Failed to uprev the ebuild.') + self.assertEqual(str(err.exception), 'Failed to uprev the ebuild.') - mock_islink.assert_called_once_with('/symlink/to/package.ebuild') + mock_islink.assert_called_once_with(symlink_to_uprev) # Simulate 'os.path.islink' when a valid symbolic link is passed in. @mock.patch.object(os.path, 'islink', return_value=True) # Simulate 'os.path.dirname' when returning a path to the directory of a # valid symbolic link. @mock.patch.object(os.path, 'dirname', return_value='/symlink/to') - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') + # Simulate 'RunCommandWOutput' when successfully added the upreved symlink + # for commit. + @mock.patch.object( + update_chromeos_llvm_next_hash, + 'ExecCommandAndCaptureOutput', + return_value=None) def testSuccessfullyUprevEbuild(self, mock_command_output, mock_dirname, mock_islink): - # Simulate 'RunCommandWOutput' when the symbolic link was incremented by 1 - # and staged for commit. - # - # Returns shell error code, stdout, stderr. - mock_command_output.return_value = (0, None, 0) + symlink_to_uprev = '/symlink/to/package-r1.ebuild' - update_chromeos_llvm_next_hash.UprevEbuild('/symlink/to/package-r1.ebuild') + update_chromeos_llvm_next_hash.UprevEbuild(symlink_to_uprev) - mock_islink.assert_called_once_with('/symlink/to/package-r1.ebuild') + mock_islink.assert_called_once_with(symlink_to_uprev) - mock_dirname.assert_called_once_with('/symlink/to/package-r1.ebuild') + mock_dirname.assert_called_once_with(symlink_to_uprev) - mock_command_output.assert_called_once_with( - 'git -C /symlink/to mv ' - '/symlink/to/package-r1.ebuild /symlink/to/package-r2.ebuild', - print_to_console=False) + mock_command_output.assert_called_once() # Simulate behavior of 'os.path.isdir()' when the path to the repo is not a # directory. @mock.patch.object(os.path, 'isdir', return_value=False) def testFailedToCreateRepoForInvalidDirectoryPath(self, mock_isdir): - # Verify the exception is raised when provided an invalid directory path. - with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash._CreateRepo('/path/to/repo', - 'a123testhash1') - - self.assertEqual(err.exception.message, - 'Invalid directory path provided: /path/to/repo') - - mock_isdir.assert_called_once() + path_to_repo = '/path/to/repo' - # Simulate 'os.path.isdir' when the path to the repo is valid. - @mock.patch.object(os.path, 'isdir', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') - def testFailedToCreateRepo(self, mock_command_output, mock_isdir): - # Simulate 'RunCommandWOutput' when 'repo start' fails. - # - # Returns shell error code, stdout, stderr. - mock_command_output.return_value = (1, None, 'Invalid branch name.') + # The name to use for the repo name. + llvm_hash = 'a123testhash1' - # Verify exception is raised when failed to create a repo for the changes. + # Verify the exception is raised when provided an invalid directory path. with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash._CreateRepo('/path/to/repo', - 'a123testhash1') + update_chromeos_llvm_next_hash._CreateRepo(path_to_repo, llvm_hash) self.assertEqual( - err.exception.message, - 'Failed to create the repo (llvm-next-update-a123testhash1): ' - 'Invalid branch name.') - - mock_isdir.assert_called_once_with('/path/to/repo') + str(err.exception), + 'Invalid directory path provided: %s' % path_to_repo) - mock_command_output.assert_called_once() + mock_isdir.assert_called_once() # Simulate 'os.path.isdir' when a valid repo path is provided. @mock.patch.object(os.path, 'isdir', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') + # Simulate behavior of 'ExecCommandAndCaptureOutput()' when successfully reset + # changes and created a repo. + @mock.patch.object( + update_chromeos_llvm_next_hash, + 'ExecCommandAndCaptureOutput', + return_value=None) def testSuccessfullyCreatedRepo(self, mock_command_output, mock_isdir): - # Test function to simulate 'RunCommandWOutput' when 'repo start' succeeds. - def GoodRepoStart(create_repo_cmd, _print_to_console): - self.assertEqual(create_repo_cmd.split()[-1], - 'llvm-next-update-a123testhash1') - - # Returns shell error code, stdout, stderr. - return 0, None, 0 + path_to_repo = '/path/to/repo' - # Use test function to simulate 'RunCommandWOutput' behavior. - mock_command_output.side_effect = GoodRepoStart + # The name to use for the repo name. + llvm_hash = 'a123testhash1' - update_chromeos_llvm_next_hash._CreateRepo('/path/to/repo', 'a123testhash1') + update_chromeos_llvm_next_hash._CreateRepo(path_to_repo, llvm_hash) - mock_isdir.assert_called_once_with('/path/to/repo') + mock_isdir.assert_called_once_with(path_to_repo) - mock_command_output.assert_called_once() + self.assertEqual(mock_command_output.call_count, 2) # Simulate behavior of 'os.path.isdir()' when the path to the repo is not a # directory. @mock.patch.object(os.path, 'isdir', return_value=False) def testFailedToDeleteRepoForInvalidDirectoryPath(self, mock_isdir): - # Verify the exception is raised on an invalid repo path. - with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash._DeleteRepo('/some/path/to/repo', - 'a123testhash2') - - self.assertEqual(err.exception.message, - 'Invalid directory path provided: /some/path/to/repo') + path_to_repo = '/some/path/to/repo' - mock_isdir.assert_called_once() + # The name to use for the repo name. + llvm_hash = 'a123testhash2' - # Simulate 'os.path.isdir' on a valid directory. - @mock.patch.object(os.path, 'isdir', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') - def testFailedToDeleteRepo(self, mock_command_output, mock_isdir): - # Simulate 'RunCommandWOutput' when failed to delete a branch. - # - # Returns shell error code, stdout, stderr. - mock_command_output.return_value = (1, None, 'Invalid branch name.') - - # Verify exception is raised when failed to delete the repo. + # Verify the exception is raised on an invalid repo path. with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash._DeleteRepo('/some/path/to/repo', - 'a123testhash2') + update_chromeos_llvm_next_hash._DeleteRepo(path_to_repo, llvm_hash) self.assertEqual( - err.exception.message, - 'Failed to delete the repo (llvm-next-update-a123testhash2): ' - 'Invalid branch name.') + str(err.exception), + 'Invalid directory path provided: %s' % path_to_repo) - mock_isdir.assert_called_once_with('/some/path/to/repo') - - mock_command_output.assert_called_once() + mock_isdir.assert_called_once() # Simulate 'os.path.isdir' on valid directory path. @mock.patch.object(os.path, 'isdir', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') + # Simulate 'ExecCommandAndCaptureOutput()' when successfully checkout to + # cros/master, reset changes, and deleted the repo. + @mock.patch.object( + update_chromeos_llvm_next_hash, + 'ExecCommandAndCaptureOutput', + return_value=None) def testSuccessfullyDeletedRepo(self, mock_command_output, mock_isdir): - # Test function to simulate 'RunCommandWOutput' when successfully deleted a - # repo. - def GoodRepoDelete(create_repo_cmd, _print_to_console): - self.assertEqual(create_repo_cmd.split()[-1], - 'llvm-next-update-a123testhash2') + path_to_repo = '/some/path/to/repo' - # Returns shell error code, stdout, stderr. - return 0, None, 0 + # The name of the repo to be deleted. + llvm_hash = 'a123testhash2' - # Use test function to simulate 'RunCommandWOutput' behavior. - mock_command_output.side_effect = GoodRepoDelete + update_chromeos_llvm_next_hash._DeleteRepo(path_to_repo, llvm_hash) - update_chromeos_llvm_next_hash._DeleteRepo('/some/path/to/repo', - 'a123testhash2') + mock_isdir.assert_called_once_with(path_to_repo) - mock_isdir.assert_called_once_with('/some/path/to/repo') - - mock_command_output.assert_called_once() + self.assertEqual(mock_command_output.call_count, 3) def testFailedToFindChangeListURL(self): repo_upload_contents = 'remote: https://some_url' @@ -444,7 +332,7 @@ class UpdateLLVMNextHashTest(unittest.TestCase): update_chromeos_llvm_next_hash.GetGerritRepoUploadContents( repo_upload_contents) - self.assertEqual(err.exception.message, 'Failed to find change list URL.') + self.assertEqual(str(err.exception), 'Failed to find change list URL.') def testSuccessfullyGetGerritRepoUploadContents(self): repo_upload_contents = ('remote: https://chromium-review.googlesource.com' @@ -465,123 +353,105 @@ class UpdateLLVMNextHashTest(unittest.TestCase): # directory. @mock.patch.object(os.path, 'isdir', return_value=False) def testFailedToUploadChangesForInvalidPathDirectory(self, mock_isdir): + path_to_repo = '/some/path/to/repo' + + # The name of repo to upload for review. + llvm_hash = 'a123testhash3' + + # Commit messages to add to the CL. + commit_messages = ['-m Test message'] + # Verify exception is raised when on an invalid repo path. with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.UploadChanges( - '/some/path/to/repo', 'a123testhash3', '-m \"Test message\"') + update_chromeos_llvm_next_hash.UploadChanges(path_to_repo, llvm_hash, + commit_messages) - self.assertEqual(err.exception.message, - 'Invalid directory path provided: /some/path/to/repo') + self.assertEqual( + str(err.exception), + 'Invalid directory path provided: %s' % path_to_repo) mock_isdir.assert_called_once() - # Simulate 'os.path.isdir' on a valid repo directory. + # Simulate 'os.path.isdir' on a valid repo path. @mock.patch.object(os.path, 'isdir', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') - def testFailedToCreateACommitForTheChanges(self, mock_command_output, - mock_isdir): - - # Simulate 'RunCommandWOutput' when failed to create a commit for the - # changes. + # Simulate behavior of 'ExecCommandAndCaptureOutput()' when successfully + # committed the changes. + @mock.patch.object( + update_chromeos_llvm_next_hash, + 'ExecCommandAndCaptureOutput', + return_value=None) + @mock.patch.object(subprocess, 'Popen') + def testFailedToUploadChangesForReview(self, mock_repo_upload, + mock_command_output, mock_isdir): + + # Simulate the behavior of 'subprocess.Popen()' when uploading the changes + # for review # - # Returns shell error code, stdout, stderr. - mock_command_output.return_value = (1, None, 'Nothing to commit.') + # `Popen.communicate()` returns a tuple of `stdout` and `stderr`. + mock_repo_upload.return_value.communicate.return_value = ( + None, 'Branch does not exist.') - # Verify exception is raised when failed to create a commit. - with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.UploadChanges( - '/some/path/to/repo', 'a123testhash3', '-m \"Test message\"') - - self.assertEqual( - err.exception.message, - 'Failed to create a commit for the changes: Nothing to commit.') + # Exit code of 1 means failed to upload changes for review. + mock_repo_upload.return_value.returncode = 1 - mock_isdir.assert_called_once_with('/some/path/to/repo') + path_to_repo = '/some/path/to/repo' - mock_command_output.assert_called_once_with( - 'cd /some/path/to/repo && git commit -m \"Test message\"', - print_to_console=False) + # The name of repo to upload for review. + llvm_hash = 'a123testhash3' - # Simulate 'os.path.isdir' on a valid repo path. - @mock.patch.object(os.path, 'isdir', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') - def testFailedToUploadChangesForReview(self, mock_command_output, mock_isdir): - # Test function to simulate 'RunCommandWOutput' when attempting to create - # a commit and upload the changes for review. - @CallCountsToMockFunctions - def MultipleCallsToUploadACommit(call_count, cmd, _print_to_console): - # Creating a commit for the changes. - if call_count == 0: # first call to 'RunCommandWOutput' - self.assertEqual( - cmd, 'cd /some/path/to/repo && git commit -m \"Test message\"') - - # Returns shell error code, stdout, stderr. - return 0, None, 0 - - # Trying to upload the commit for review. - if call_count == 1: # second call to 'RunCommandWOutput' - # Make sure the branch name matches expected. - self.assertEqual(cmd.split()[-2], '--br=llvm-next-update-a123testhash3') - - # Returns shell error code, stdout, stderr. - return 1, None, 'Branch does not exist.' - - # Testing function was called more times than expected (2 times). - assert False, 'RunCommandWOutput was called more than 2 times.' - - # Use test function to simulate 'RunCommandWOutput' behavior. - mock_command_output.side_effect = MultipleCallsToUploadACommit + # Commit messages to add to the CL. + commit_messages = ['-m Test message'] # Verify exception is raised when failed to upload the changes for review. with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.UploadChanges( - '/some/path/to/repo', 'a123testhash3', '-m \"Test message\"') + update_chromeos_llvm_next_hash.UploadChanges(path_to_repo, llvm_hash, + commit_messages) - self.assertEqual( - err.exception.message, - 'Failed to upload changes for review: Branch does not exist.') + self.assertEqual(str(err.exception), 'Failed to upload changes for review') - mock_isdir.assert_called_once_with('/some/path/to/repo') + mock_isdir.assert_called_once_with(path_to_repo) - self.assertEqual(mock_command_output.call_count, 2) + mock_command_output.assert_called_once() + + mock_repo_upload.assert_called_once() # Simulate 'os.path.isdir' when a valid repo path is passed in. @mock.patch.object(os.path, 'isdir', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') - def testSuccessfullyUploadedChangesForReview(self, mock_command_output, - mock_isdir): - - # Test function to simulate 'RunCommandWOutput' when creating a commit for - # the changes and uploading the changes for review. - @CallCountsToMockFunctions - def MultipleCallsToUploadACommit(call_count, cmd, _print_to_console): - # Creating a commit in the repo path. - if call_count == 0: # first call to 'RunCommandWOutput' - self.assertEqual( - cmd, 'cd /some/path/to/repo && git commit -m \"Test message\"') - - # Returns shell error code, stdout, stderr. - return 0, None, 0 - # Uploading the changes for review. - if call_count == 1: # second call to 'RunCommandWOutput' - # Make sure the branch name matches expected. - self.assertEqual(cmd.split()[-2], '--br=llvm-next-update-a123testhash3') - - repo_upload_contents = ('remote: https://chromium-review.googlesource.' - 'com/c/chromiumos/overlays/chromiumos-overlay/' - '+/193147 Fix stdout') - - # Returns shell error code, stdout, stderr. - return 0, None, repo_upload_contents - - # Testing function was called more times than expected (2 times). - assert False, 'RunCommandWOutput was called more than 2 times.' - - # Use test function to simulate 'RunCommandWOutput' behavior. - mock_command_output.side_effect = MultipleCallsToUploadACommit + # Simulate behavior of 'ExecCommandAndCaptureOutput()' when successfully + # committed the changes. + @mock.patch.object( + update_chromeos_llvm_next_hash, + 'ExecCommandAndCaptureOutput', + return_value=None) + @mock.patch.object(subprocess, 'Popen') + def testSuccessfullyUploadedChangesForReview(self, mock_repo_upload, + mock_command_output, mock_isdir): + + # A test CL generated by `repo upload`. + repo_upload_contents = ('remote: https://chromium-review.googlesource.' + 'com/c/chromiumos/overlays/chromiumos-overlay/' + '+/193147 Fix stdout') + + # Simulate the behavior of 'subprocess.Popen()' when uploading the changes + # for review + # + # `Popen.communicate()` returns a tuple of `stdout` and `stderr`. + mock_repo_upload.return_value.communicate.return_value = ( + repo_upload_contents, None) + + # Exit code of 0 means successfully uploaded changes for review. + mock_repo_upload.return_value.returncode = 0 + + path_to_repo = '/some/path/to/repo' + + # The name of the hash to upload for review. + llvm_hash = 'a123testhash3' + + # Commit messages to add to the CL. + commit_messages = ['-m Test message'] change_list = update_chromeos_llvm_next_hash.UploadChanges( - '/some/path/to/repo', 'a123testhash3', '-m \"Test message\"') + path_to_repo, llvm_hash, commit_messages) self.assertEqual( change_list.url, @@ -590,9 +460,11 @@ class UpdateLLVMNextHashTest(unittest.TestCase): self.assertEqual(change_list.cl_number, 193147) - mock_isdir.assert_called_once_with('/some/path/to/repo') + mock_isdir.assert_called_once_with(path_to_repo) - self.assertEqual(mock_command_output.call_count, 2) + mock_command_output.assert_called_once() + + mock_repo_upload.assert_called_once() @mock.patch.object(update_chromeos_llvm_next_hash, 'GetChrootBuildPaths') @mock.patch.object(update_chromeos_llvm_next_hash, @@ -600,18 +472,21 @@ class UpdateLLVMNextHashTest(unittest.TestCase): def testExceptionRaisedWhenCreatingPathDictionaryFromPackages( self, mock_chroot_paths_to_symlinks, mock_get_chroot_paths): + chroot_path = '/some/path/to/chroot' + + package_name = 'test-pckg/package' + package_chroot_path = '/some/chroot/path/to/package-r1.ebuild' + # Test function to simulate '_ConvertChrootPathsToSymLinkPaths' when a # symlink does not start with the prefix '/mnt/host/source'. def BadPrefixChrootPath(_chroot_path, _chroot_file_paths): raise ValueError('Invalid prefix for the chroot path: ' - '/some/chroot/path/to/package-r1.ebuild') + '%s' % package_chroot_path) # Simulate 'GetChrootBuildPaths' when valid packages are passed in. # # Returns a list of chroot paths. - mock_get_chroot_paths.return_value = [ - '/some/chroot/path/to/package-r1.ebuild' - ] + mock_get_chroot_paths.return_value = [package_chroot_path] # Use test function to simulate '_ConvertChrootPathsToSymLinkPaths' # behavior. @@ -620,17 +495,16 @@ class UpdateLLVMNextHashTest(unittest.TestCase): # Verify exception is raised when for an invalid prefix in the symlink. with self.assertRaises(ValueError) as err: update_chromeos_llvm_next_hash.CreatePathDictionaryFromPackages( - '/some/path/to/chroot', ['test-pckg/package']) + chroot_path, [package_name]) self.assertEqual( - err.exception.message, 'Invalid prefix for the chroot path: ' - '/some/chroot/path/to/package-r1.ebuild') + str(err.exception), 'Invalid prefix for the chroot path: ' + '%s' % package_chroot_path) - mock_get_chroot_paths.assert_called_once_with('/some/path/to/chroot', - ['test-pckg/package']) + mock_get_chroot_paths.assert_called_once_with(chroot_path, [package_name]) mock_chroot_paths_to_symlinks.assert_called_once_with( - '/some/path/to/chroot', ['/some/chroot/path/to/package-r1.ebuild']) + chroot_path, [package_chroot_path]) @mock.patch.object(update_chromeos_llvm_next_hash, 'GetChrootBuildPaths') @mock.patch.object(update_chromeos_llvm_next_hash, @@ -641,21 +515,23 @@ class UpdateLLVMNextHashTest(unittest.TestCase): self, mock_ebuild_paths_from_symlink_paths, mock_chroot_paths_to_symlinks, mock_get_chroot_paths): + package_chroot_path = '/mnt/host/source/src/path/to/package-r1.ebuild' + # Simulate 'GetChrootBuildPaths' when returning a chroot path for a valid # package. # # Returns a list of chroot paths. - mock_get_chroot_paths.return_value = [ - '/mnt/host/source/src/path/to/package-r1.ebuild' - ] + mock_get_chroot_paths.return_value = [package_chroot_path] + + package_symlink_path = '/some/path/to/chroot/src/path/to/package-r1.ebuild' # Simulate '_ConvertChrootPathsToSymLinkPaths' when returning a symlink to # a chroot path that points to a package. # # Returns a list of symlink file paths. - mock_chroot_paths_to_symlinks.return_value = [ - '/some/path/to/chroot/src/path/to/package-r1.ebuild' - ] + mock_chroot_paths_to_symlinks.return_value = [package_symlink_path] + + chroot_package_path = '/some/path/to/chroot/src/path/to/package.ebuild' # Simulate 'GetEbuildPathsFromSymlinkPaths' when returning a dictionary of # a symlink that points to an ebuild. @@ -664,90 +540,39 @@ class UpdateLLVMNextHashTest(unittest.TestCase): # where the key is the absolute path to the symlink of the ebuild file # and the value is the absolute path to the ebuild file of the package. mock_ebuild_paths_from_symlink_paths.return_value = { - '/some/path/to/chroot/src/path/to/package-r1.ebuild': - '/some/path/to/chroot/src/path/to/package.ebuild' + package_symlink_path: chroot_package_path } + chroot_path = '/some/path/to/chroot' + package_name = 'test-pckg/package' + self.assertEqual( update_chromeos_llvm_next_hash.CreatePathDictionaryFromPackages( - '/some/path/to/chroot', ['test-pckg/package']), { - '/some/path/to/chroot/src/path/to/package-r1.ebuild': - '/some/path/to/chroot/src/path/to/package.ebuild' - }) + chroot_path, [package_name]), + {package_symlink_path: chroot_package_path}) - mock_get_chroot_paths.assert_called_once_with('/some/path/to/chroot', - ['test-pckg/package']) + mock_get_chroot_paths.assert_called_once_with(chroot_path, [package_name]) mock_chroot_paths_to_symlinks.assert_called_once_with( - '/some/path/to/chroot', - ['/mnt/host/source/src/path/to/package-r1.ebuild']) + chroot_path, [package_chroot_path]) mock_ebuild_paths_from_symlink_paths.assert_called_once_with( - ['/some/path/to/chroot/src/path/to/package-r1.ebuild']) - - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') - def testFailedToRemovePatchFromFilesDir(self, mock_run_cmd): - # Simulate the behavior of 'RunCommandWOutput()' when failed to remove a - # patch whose absolute path does not exist. - # - # Returns shell error code, stdout, stderr. - mock_run_cmd.return_value = (1, None, 'Patch does not exist.') - - # Verify the exception is raised when the patch does not exist and the - # command executer attempts to remove the patch. - with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.RemovePatchesFromFilesDir([ - '/abs/path/to/filesdir/display_results.patch', - '/abs/path/to/filesdir/cherry/fix_output.patch' - ]) - - self.assertEqual( - err.exception.message, 'Failed to remove patch display_results.patch: ' - 'Patch does not exist.') - - self.assertEqual(mock_run_cmd.call_count, 1) - - mock_run_cmd.assert_called_once_with( - 'git -C /abs/path/to/filesdir rm -f ' - '/abs/path/to/filesdir/display_results.patch', - print_to_console=False) - - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') + [package_symlink_path]) + + # Simulate behavior of 'ExecCommandAndCaptureOutput()' when successfully + # removed patches. + @mock.patch.object( + update_chromeos_llvm_next_hash, + 'ExecCommandAndCaptureOutput', + return_value=None) def testSuccessfullyRemovedPatchesFromFilesDir(self, mock_run_cmd): - # Simulate the behavior of 'RunCommandWOutput()' when all patches exist and - # were removed from $FILESDIR. - @CallCountsToMockFunctions - def MultipleCallsToRemovePatches(call_count, rm_cmd, print_to_console): - # First patch to remove from $FILESDIR. - if call_count == 0: - self.assertEqual( - rm_cmd, 'git -C /abs/path/to/filesdir/cherry rm -f ' - '/abs/path/to/filesdir/cherry/fix_output.patch') - self.assertFalse(print_to_console) - - # Returns shell error code, stdout, stderr. - return 0, None, 0 - - # Second (and last patch) to remove from $FILESDIR. - if call_count == 1: - self.assertEqual( - rm_cmd, 'git -C /abs/path/to/filesdir rm -f ' - '/abs/path/to/filesdir/display_results.patch') - self.assertFalse(print_to_console) - - # Returns shell error code, stdout, stderr. - return 0, None, 0 - - # 'RunCommandWOutput()' called more than times than expected (2 times). - assert False, 'Unexpectedly called more than 2 times.' - - # Use test function to simulate 'RunCommandWOutput()' behavior. - mock_run_cmd.side_effect = MultipleCallsToRemovePatches - - update_chromeos_llvm_next_hash.RemovePatchesFromFilesDir([ + patches_to_remove_list = [ '/abs/path/to/filesdir/cherry/fix_output.patch', '/abs/path/to/filesdir/display_results.patch' - ]) + ] + + update_chromeos_llvm_next_hash.RemovePatchesFromFilesDir( + patches_to_remove_list) self.assertEqual(mock_run_cmd.call_count, 2) @@ -755,65 +580,38 @@ class UpdateLLVMNextHashTest(unittest.TestCase): # metadata file does not exist. @mock.patch.object(os.path, 'isfile', return_value=False) def testInvalidPatchMetadataFileStagedForCommit(self, mock_isfile): + patch_metadata_path = '/abs/path/to/filesdir/PATCHES' + # Verify the exception is raised when the absolute path to the patch # metadata file does not exist or is not a file. with self.assertRaises(ValueError) as err: update_chromeos_llvm_next_hash.StagePatchMetadataFileForCommit( - '/abs/path/to/filesdir/PATCHES') + patch_metadata_path) self.assertEqual( - err.exception.message, 'Invalid patch metadata file provided: ' - '/abs/path/to/filesdir/PATCHES') + str(err.exception), 'Invalid patch metadata file provided: ' + '%s' % patch_metadata_path) mock_isfile.assert_called_once() # Simulate the behavior of 'os.path.isfile()' when the absolute path to the # patch metadata file exists. @mock.patch.object(os.path, 'isfile', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') - def testFailedToStagePatchMetadataFileForCommit(self, mock_run_cmd, - _mock_isfile): - - # Simulate the behavior of 'RunCommandWOutput()' when failed to stage the - # patch metadata file for commit. - # - # Returns shell error code, stdout, stderr. - mock_run_cmd.return_value = (1, None, 'No changes made to the file.') - - # Verify the exception is raised when no changes were made to the patch - # metadata file when attempting to stage the file for commit. - with self.assertRaises(ValueError) as err: - update_chromeos_llvm_next_hash.StagePatchMetadataFileForCommit( - '/abs/path/to/filesdir/PATCHES.json') - - self.assertEqual( - err.exception.message, - 'Failed to stage patch metadata file PATCHES.json for ' - 'commit: No changes made to the file.') - - mock_run_cmd.assert_called_once_with( - 'git -C /abs/path/to/filesdir add /abs/path/to/filesdir/PATCHES.json', - print_to_console=False) - - # Simulate the behavior of 'os.path.isfile()' when the absolute path to the - # patch metadata file exists. - @mock.patch.object(os.path, 'isfile', return_value=True) - @mock.patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') + # Simulate the behavior of 'ExecCommandAndCaptureOutput()' when successfully + # staged the patch metadata file for commit. + @mock.patch.object( + update_chromeos_llvm_next_hash, + 'ExecCommandAndCaptureOutput', + return_value=None) def testSuccessfullyStagedPatchMetadataFileForCommit(self, mock_run_cmd, _mock_isfile): - # Simulate the behavior of 'RunCommandWOutput()' when successfully staged - # the patch metadata file for commit. - # - # Returns shell error code, stdout, stderr. - mock_run_cmd.return_value = (0, None, 0) + patch_metadata_path = '/abs/path/to/filesdir/PATCHES.json' update_chromeos_llvm_next_hash.StagePatchMetadataFileForCommit( - '/abs/path/to/filesdir/PATCHES.json') + patch_metadata_path) - mock_run_cmd.assert_called_once_with( - 'git -C /abs/path/to/filesdir add /abs/path/to/filesdir/PATCHES.json', - print_to_console=False) + mock_run_cmd.assert_called_once() def testNoPatchResultsForCommit(self): package_1_patch_info_dict = { @@ -839,7 +637,7 @@ class UpdateLLVMNextHashTest(unittest.TestCase): 'test-packages/package2': package_2_patch_info_dict } - test_commit_message = ['-m %s' % quote('Updated packages')] + test_commit_message = ['-m %s' % 'Updated packages'] self.assertListEqual( update_chromeos_llvm_next_hash.StagePackagesPatchResultsForCommit( @@ -875,18 +673,18 @@ class UpdateLLVMNextHashTest(unittest.TestCase): 'test-packages/package2': package_2_patch_info_dict } - test_commit_message = ['-m %s' % quote('Updated packages')] + test_commit_message = ['-m %s' % 'Updated packages'] expected_commit_messages = [ - '-m %s' % quote('Updated packages'), - '-m %s' % quote('For the package test-packages/package2:'), - '-m %s' % quote('The patch metadata file PATCHES.json was modified'), - '-m %s' % quote('The following patches were removed:'), - '-m %s' % quote('redirect_stdout.patch'), - '-m %s' % quote('For the package test-packages/package1:'), - '-m %s' % quote('The patch metadata file PATCHES.json was modified'), - '-m %s' % quote('The following patches were disabled:'), - '-m %s' % quote('fixes_output.patch') + '-m %s' % 'Updated packages', + '-m %s' % 'For the package test-packages/package1:', + '-m %s' % 'The patch metadata file PATCHES.json was modified', + '-m %s' % 'The following patches were disabled:', + '-m %s' % 'fixes_output.patch', + '-m %s' % 'For the package test-packages/package2:', + '-m %s' % 'The patch metadata file PATCHES.json was modified', + '-m %s' % 'The following patches were removed:', + '-m %s' % 'redirect_stdout.patch' ] self.assertListEqual( @@ -894,8 +692,9 @@ class UpdateLLVMNextHashTest(unittest.TestCase): test_package_info_dict, test_commit_message), expected_commit_messages) - mock_remove_patches.assert_called_once_with( - ['/abs/path/to/filesdir/redirect_stdout.patch']) + path_to_removed_patch = '/abs/path/to/filesdir/redirect_stdout.patch' + + mock_remove_patches.assert_called_once_with([path_to_removed_patch]) self.assertEqual(mock_stage_patches_for_commit.call_count, 2) @@ -910,6 +709,13 @@ class UpdateLLVMNextHashTest(unittest.TestCase): self, mock_delete_repo, mock_upload_changes, mock_uprev_ebuild, mock_update_llvm_next, mock_create_repo, mock_create_path_dict): + abs_path_to_package = '/some/path/to/chroot/src/path/to/package.ebuild' + + symlink_path_to_package = \ + '/some/path/to/chroot/src/path/to/package-r1.ebuild' + + path_to_package_dir = '/some/path/to/chroot/src/path/to' + # Test function to simulate '_CreateRepo' when successfully created the # repo on a valid repo path. def SuccessfullyCreateRepoForChanges(_repo_path, llvm_hash): @@ -919,8 +725,7 @@ class UpdateLLVMNextHashTest(unittest.TestCase): # Test function to simulate 'UpdateBuildLLVMNextHash' when successfully # updated the ebuild's 'LLVM_NEXT_HASH'. def SuccessfullyUpdatedLLVMNextHash(ebuild_path, llvm_hash, llvm_version): - self.assertEqual(ebuild_path, - '/some/path/to/chroot/src/path/to/package.ebuild') + self.assertEqual(ebuild_path, abs_path_to_package) self.assertEqual(llvm_hash, 'a123testhash4') self.assertEqual(llvm_version, 1000) return @@ -934,18 +739,14 @@ class UpdateLLVMNextHashTest(unittest.TestCase): # Test function to fail on 'UploadChanges' if the function gets called # when an exception is raised. - def ShouldNotExecuteUploadChanges(_repo_path, _llvm_hash, - _commit_messages): + def ShouldNotExecuteUploadChanges(_repo_path, _llvm_hash, _commit_messages): # Test function should not be called (i.e. execution should resume in the # 'finally' block) because 'UprevEbuild()' raised an # exception. - assert False, "Failed to go to 'finally' block " \ + assert False, 'Failed to go to "finally" block ' \ 'after the exception was raised.' - test_package_path_dict = { - '/some/path/to/chroot/src/path/to/package-r1.ebuild': - '/some/path/to/chroot/src/path/to/package.ebuild' - } + test_package_path_dict = {symlink_path_to_package: abs_path_to_package} # Simulate behavior of 'CreatePathDictionaryFromPackages()' when # successfully created a dictionary where the key is the absolute path to @@ -959,32 +760,35 @@ class UpdateLLVMNextHashTest(unittest.TestCase): mock_uprev_ebuild.side_effect = FailedToUprevEbuild mock_upload_changes.side_effect = ShouldNotExecuteUploadChanges + packages_to_update = ['test-packages/package1'] + patch_metadata_file = 'PATCHES.json' + llvm_hash = 'a123testhash4' + llvm_version = 1000 + chroot_path = '/some/path/to/chroot' + svn_option = 'google3' + # Verify exception is raised when an exception is thrown within # the 'try' block by UprevEbuild function. with self.assertRaises(ValueError) as err: update_chromeos_llvm_next_hash.UpdatePackages( - ['test-packages/package1'], 'a123testhash4', 1000, - '/some/path/to/chroot', 'PATCHES.json', FailureModes.FAIL, 'google3') + packages_to_update, llvm_hash, llvm_version, chroot_path, + patch_metadata_file, FailureModes.FAIL, svn_option) - self.assertEqual(err.exception.message, 'Failed to uprev the ebuild.') + self.assertEqual(str(err.exception), 'Failed to uprev the ebuild.') - mock_create_path_dict.assert_called_once_with('/some/path/to/chroot', - ['test-packages/package1']) + mock_create_path_dict.assert_called_once_with(chroot_path, + packages_to_update) - mock_create_repo.assert_called_once_with('/some/path/to/chroot/src/path/to', - 'a123testhash4') + mock_create_repo.assert_called_once_with(path_to_package_dir, llvm_hash) - mock_update_llvm_next.assert_called_once_with( - '/some/path/to/chroot/src/path/to/package.ebuild', 'a123testhash4', - 1000) + mock_update_llvm_next.assert_called_once_with(abs_path_to_package, + llvm_hash, llvm_version) - mock_uprev_ebuild.assert_called_once_with( - '/some/path/to/chroot/src/path/to/package-r1.ebuild') + mock_uprev_ebuild.assert_called_once_with(symlink_path_to_package) mock_upload_changes.assert_not_called() - mock_delete_repo.assert_called_once_with('/some/path/to/chroot/src/path/to', - 'a123testhash4') + mock_delete_repo.assert_called_once_with(path_to_package_dir, llvm_hash) @mock.patch.object(update_chromeos_llvm_next_hash, 'CreatePathDictionaryFromPackages') @@ -1001,6 +805,13 @@ class UpdateLLVMNextHashTest(unittest.TestCase): mock_delete_repo, mock_upload_changes, mock_uprev_ebuild, mock_update_llvm_next, mock_create_repo, mock_create_path_dict): + abs_path_to_package = '/some/path/to/chroot/src/path/to/package.ebuild' + + symlink_path_to_package = \ + '/some/path/to/chroot/src/path/to/package-r1.ebuild' + + path_to_package_dir = '/some/path/to/chroot/src/path/to' + # Test function to simulate '_CreateRepo' when successfully created the repo # for the changes to be made to the ebuild files. def SuccessfullyCreateRepoForChanges(_repo_path, llvm_hash): @@ -1064,10 +875,7 @@ class UpdateLLVMNextHashTest(unittest.TestCase): return update_chromeos_llvm_next_hash.CommitContents( url=commit_url, cl_number=12345) - test_package_path_dict = { - '/some/path/to/chroot/src/path/to/package-r1.ebuild': - '/some/path/to/chroot/src/path/to/package.ebuild' - } + test_package_path_dict = {symlink_path_to_package: abs_path_to_package} # Simulate behavior of 'CreatePathDictionaryFromPackages()' when # successfully created a dictionary where the key is the absolute path to @@ -1082,50 +890,51 @@ class UpdateLLVMNextHashTest(unittest.TestCase): mock_update_package_metadata_file.side_effect = RetrievedPatchResults mock_upload_changes.side_effect = SuccessfullyUploadedChanges + packages_to_update = ['test-packages/package1'] + patch_metadata_file = 'PATCHES.json' + llvm_hash = 'a123testhash5' + llvm_version = 1000 + chroot_path = '/some/path/to/chroot' + svn_option = 'tot' + change_list = update_chromeos_llvm_next_hash.UpdatePackages( - ['test-packages/package1'], 'a123testhash5', 1000, - '/some/path/to/chroot', 'PATCHES.json', FailureModes.DISABLE_PATCHES, - 'tot') + packages_to_update, llvm_hash, llvm_version, chroot_path, + patch_metadata_file, FailureModes.DISABLE_PATCHES, svn_option) self.assertEqual(change_list.url, 'https://some_name/path/to/commit/+/12345') self.assertEqual(change_list.cl_number, 12345) - mock_create_path_dict.assert_called_once_with('/some/path/to/chroot', - ['test-packages/package1']) + mock_create_path_dict.assert_called_once_with(chroot_path, + packages_to_update) - mock_create_repo.assert_called_once_with('/some/path/to/chroot/src/path/to', - 'a123testhash5') + mock_create_repo.assert_called_once_with(path_to_package_dir, llvm_hash) - mock_update_llvm_next.assert_called_once_with( - '/some/path/to/chroot/src/path/to/package.ebuild', 'a123testhash5', - 1000) + mock_update_llvm_next.assert_called_once_with(abs_path_to_package, + llvm_hash, llvm_version) - mock_uprev_ebuild.assert_called_once_with( - '/some/path/to/chroot/src/path/to/package-r1.ebuild') + mock_uprev_ebuild.assert_called_once_with(symlink_path_to_package) - expected_commit_messages = ' '.join([ - '-m %s' % quote('llvm-next/tot: Update packages to r1000'), - '-m %s' % quote('Following packages have been updated:'), - '-m %s' % quote('path/to'), - '-m %s' % quote('For the package path/to:'), - '-m %s' % quote('The patch metadata file PATCHES.json was modified'), - '-m %s' % quote('The following patches were disabled:'), - '-m %s' % quote('fix_stdout.patch') - ]) + expected_commit_messages = [ + '-m %s' % 'llvm-next/tot: Update packages to r1000', + '-m %s' % 'Following packages have been updated:', + '-m %s' % 'path/to', + '-m %s' % 'For the package path/to:', + '-m %s' % 'The patch metadata file PATCHES.json was modified', + '-m %s' % 'The following patches were disabled:', + '-m %s' % 'fix_stdout.patch' + ] mock_update_package_metadata_file.assert_called_once() mock_stage_patch_file.assert_called_once_with( '/abs/path/to/filesdir/PATCHES.json') - mock_upload_changes.assert_called_once_with( - '/some/path/to/chroot/src/path/to', 'a123testhash5', - expected_commit_messages) + mock_upload_changes.assert_called_once_with(path_to_package_dir, llvm_hash, + expected_commit_messages) - mock_delete_repo.assert_called_once_with('/some/path/to/chroot/src/path/to', - 'a123testhash5') + mock_delete_repo.assert_called_once_with(path_to_package_dir, llvm_hash) if __name__ == '__main__': |