From b0b99d5ccdf5e2e49cfe3138fbcf64e6fef6ea7f Mon Sep 17 00:00:00 2001 From: jonathanmetzman <31354670+jonathanmetzman@users.noreply.github.com> Date: Mon, 7 Dec 2020 10:50:11 -0800 Subject: Cifuzz external build (#4656) * Support building fuzzers for projects outside of OSS-Fuzz * Use retry wrapper * Fix some tests. --- infra/repo_manager_test.py | 105 +++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 51 deletions(-) (limited to 'infra/repo_manager_test.py') diff --git a/infra/repo_manager_test.py b/infra/repo_manager_test.py index f3294bc2a..653a21718 100644 --- a/infra/repo_manager_test.py +++ b/infra/repo_manager_test.py @@ -13,6 +13,7 @@ # limitations under the License. """Test the functionality of the RepoManager class.""" +import contextlib import os import tempfile import unittest @@ -21,31 +22,36 @@ from unittest import mock import repo_manager import utils -OSS_FUZZ_REPO = 'https://github.com/google/oss-fuzz' +# pylint: disable=protected-access +OSS_FUZZ_REPO_URL = 'https://github.com/google/oss-fuzz' -class RepoManagerCloneTest(unittest.TestCase): - """Tests the cloning functionality of RepoManager.""" + +@contextlib.contextmanager +def get_oss_fuzz_repo(): + """Clones a temporary copy of the OSS-Fuzz repo. Returns the path to the + repo.""" + repo_name = 'oss-fuzz' + with tempfile.TemporaryDirectory() as tmp_dir: + repo_manager._clone(OSS_FUZZ_REPO_URL, tmp_dir, repo_name) + yield os.path.join(tmp_dir, repo_name) + + +class CloneIntegrationTest(unittest.TestCase): + """Tests the _clone function.""" def test_clone_valid_repo(self): """Tests the correct location of the git repo.""" - with tempfile.TemporaryDirectory() as tmp_dir: - test_repo_manager = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) - git_path = os.path.join(test_repo_manager.base_dir, - test_repo_manager.repo_name, '.git') + with get_oss_fuzz_repo() as oss_fuzz_repo: + git_path = os.path.join(oss_fuzz_repo, '.git') self.assertTrue(os.path.isdir(git_path)) - test_repo_manager.remove_repo() def test_clone_invalid_repo(self): - """Tests that constructing RepoManager with an invalid repo will fail.""" + """Tests that cloning an invalid repo will fail.""" with tempfile.TemporaryDirectory() as tmp_dir: - with self.assertRaises(ValueError): - repo_manager.RepoManager(' ', tmp_dir) - with self.assertRaises(ValueError): - repo_manager.RepoManager('not_a_valid_repo', tmp_dir) - with self.assertRaises(ValueError): - repo_manager.RepoManager('https://github.com/oss-fuzz-not-real.git', - tmp_dir) + with self.assertRaises(RuntimeError): + repo_manager._clone('https://github.com/oss-fuzz-not-real.git', tmp_dir, + 'oss-fuzz') class RepoManagerCheckoutTest(unittest.TestCase): @@ -53,23 +59,22 @@ class RepoManagerCheckoutTest(unittest.TestCase): def test_checkout_valid_commit(self): """Tests that the git checkout command works.""" - with tempfile.TemporaryDirectory() as tmp_dir: - test_repo_manager = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) commit_to_test = '04ea24ee15bbe46a19e5da6c5f022a2ffdfbdb3b' - test_repo_manager.checkout_commit(commit_to_test) - self.assertEqual(commit_to_test, test_repo_manager.get_current_commit()) + repo_man.checkout_commit(commit_to_test) + self.assertEqual(commit_to_test, repo_man.get_current_commit()) def test_checkout_invalid_commit(self): """Tests that the git checkout invalid commit fails.""" - with tempfile.TemporaryDirectory() as tmp_dir: - test_repo_manager = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) with self.assertRaises(ValueError): - test_repo_manager.checkout_commit(' ') + repo_man.checkout_commit(' ') with self.assertRaises(ValueError): - test_repo_manager.checkout_commit( - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') + repo_man.checkout_commit('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') with self.assertRaises(ValueError): - test_repo_manager.checkout_commit('not-a-valid-commit') + repo_man.checkout_commit('not-a-valid-commit') class RepoManagerGetCommitListTest(unittest.TestCase): @@ -77,8 +82,8 @@ class RepoManagerGetCommitListTest(unittest.TestCase): def test_get_valid_commit_list(self): """Tests an accurate commit list can be retrieved from the repo manager.""" - with tempfile.TemporaryDirectory() as tmp_dir: - test_repo_manager = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) old_commit = '04ea24ee15bbe46a19e5da6c5f022a2ffdfbdb3b' new_commit = 'fa662173bfeb3ba08d2e84cefc363be11e6c8463' commit_list = [ @@ -87,22 +92,22 @@ class RepoManagerGetCommitListTest(unittest.TestCase): '97dee00a3c4ce95071c3e061592f5fd577dea886', '04ea24ee15bbe46a19e5da6c5f022a2ffdfbdb3b' ] - result_list = test_repo_manager.get_commit_list(new_commit, old_commit) + result_list = repo_man.get_commit_list(new_commit, old_commit) self.assertListEqual(commit_list, result_list) def test_get_invalid_commit_list(self): """Tests that the proper errors are thrown when invalid commits are passed to get_commit_list.""" - with tempfile.TemporaryDirectory() as tmp_dir: + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) old_commit = '04ea24ee15bbe46a19e5da6c5f022a2ffdfbdb3b' new_commit = 'fa662173bfeb3ba08d2e84cefc363be11e6c8463' - test_repo_manager = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) with self.assertRaises(ValueError): - test_repo_manager.get_commit_list('fakecommit', new_commit) + repo_man.get_commit_list('fakecommit', new_commit) with self.assertRaises(ValueError): - test_repo_manager.get_commit_list(new_commit, 'fakecommit') + repo_man.get_commit_list(new_commit, 'fakecommit') with self.assertRaises(RuntimeError): - test_repo_manager.get_commit_list(old_commit, new_commit) # pylint: disable=arguments-out-of-order + repo_man.get_commit_list(old_commit, new_commit) # pylint: disable=arguments-out-of-order class GitDiffTest(unittest.TestCase): @@ -110,8 +115,8 @@ class GitDiffTest(unittest.TestCase): def test_diff_exists(self): """Tests that a real diff is returned when a valid repo manager exists.""" - with tempfile.TemporaryDirectory() as tmp_dir: - repo_man = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) with mock.patch.object(utils, 'execute', return_value=('test.py\ndiff.py', None, 0)): @@ -120,16 +125,16 @@ class GitDiffTest(unittest.TestCase): def test_diff_empty(self): """Tests that None is returned when there is no difference between repos.""" - with tempfile.TemporaryDirectory() as tmp_dir: - repo_man = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) with mock.patch.object(utils, 'execute', return_value=('', None, 0)): diff = repo_man.get_git_diff() self.assertIsNone(diff) def test_error_on_command(self): """Tests that None is returned when the command errors out.""" - with tempfile.TemporaryDirectory() as tmp_dir: - repo_man = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) with mock.patch.object(utils, 'execute', return_value=('', 'Test error.', 1)): @@ -138,8 +143,8 @@ class GitDiffTest(unittest.TestCase): def test_diff_no_change(self): """Tests that None is returned when there is no difference between repos.""" - with tempfile.TemporaryDirectory() as tmp_dir: - repo_man = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) diff = repo_man.get_git_diff() self.assertIsNone(diff) @@ -149,24 +154,22 @@ class CheckoutPrIntegrationTest(unittest.TestCase): def test_pull_request_exists(self): """Tests that a diff is returned when a valid PR is checked out.""" - with tempfile.TemporaryDirectory() as tmp_dir: - repo_man = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) repo_man.checkout_pr('refs/pull/3415/merge') diff = repo_man.get_git_diff() - print(diff) self.assertCountEqual(diff, ['README.md']) def test_checkout_invalid_pull_request(self): """Tests that the git checkout invalid pull request fails.""" - with tempfile.TemporaryDirectory() as tmp_dir: - test_repo_manager = repo_manager.RepoManager(OSS_FUZZ_REPO, tmp_dir) + with get_oss_fuzz_repo() as oss_fuzz_repo: + repo_man = repo_manager.RepoManager(oss_fuzz_repo) with self.assertRaises(RuntimeError): - test_repo_manager.checkout_pr(' ') + repo_man.checkout_pr(' ') with self.assertRaises(RuntimeError): - test_repo_manager.checkout_pr( - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') + repo_man.checkout_pr('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') with self.assertRaises(RuntimeError): - test_repo_manager.checkout_pr('not/a/valid/pr') + repo_man.checkout_pr('not/a/valid/pr') if __name__ == '__main__': -- cgit v1.2.3