2019-12-12 17:26:37 +00:00
|
|
|
# Copyright 2019 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
2020-11-17 21:39:57 +00:00
|
|
|
# See the License for the specific language governing permissions and
|
2019-12-12 17:26:37 +00:00
|
|
|
# limitations under the License.
|
2020-01-31 23:31:18 +00:00
|
|
|
"""Test the functionality of the RepoManager class."""
|
2019-12-12 17:26:37 +00:00
|
|
|
|
2020-12-07 18:50:11 +00:00
|
|
|
import contextlib
|
2019-12-12 17:26:37 +00:00
|
|
|
import os
|
2020-11-17 21:39:57 +00:00
|
|
|
import tempfile
|
2019-12-12 17:26:37 +00:00
|
|
|
import unittest
|
2020-02-28 16:30:42 +00:00
|
|
|
from unittest import mock
|
2019-12-12 17:26:37 +00:00
|
|
|
|
2019-12-17 00:48:49 +00:00
|
|
|
import repo_manager
|
2020-02-28 16:30:42 +00:00
|
|
|
import utils
|
2019-12-12 17:26:37 +00:00
|
|
|
|
2020-12-07 18:50:11 +00:00
|
|
|
# pylint: disable=protected-access
|
2020-01-30 18:27:56 +00:00
|
|
|
|
2020-12-07 18:50:11 +00:00
|
|
|
OSS_FUZZ_REPO_URL = 'https://github.com/google/oss-fuzz'
|
2019-12-12 17:26:37 +00:00
|
|
|
|
2020-12-07 18:50:11 +00:00
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2020-12-18 18:44:12 +00:00
|
|
|
class CloneTest(unittest.TestCase):
|
2020-12-07 18:50:11 +00:00
|
|
|
"""Tests the _clone function."""
|
2020-01-30 18:27:56 +00:00
|
|
|
|
2020-12-18 18:44:12 +00:00
|
|
|
@unittest.skipIf(not os.getenv('INTEGRATION_TESTS'),
|
|
|
|
'INTEGRATION_TESTS=1 not set')
|
|
|
|
def test_clone_valid_repo_integration(self):
|
|
|
|
"""Integration test that tests the correct location of the git repo."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
git_path = os.path.join(oss_fuzz_repo, '.git')
|
2020-01-06 20:17:26 +00:00
|
|
|
self.assertTrue(os.path.isdir(git_path))
|
2020-01-30 18:27:56 +00:00
|
|
|
|
|
|
|
def test_clone_invalid_repo(self):
|
2020-12-07 18:50:11 +00:00
|
|
|
"""Tests that cloning an invalid repo will fail."""
|
2020-01-30 18:27:56 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
2020-12-07 18:50:11 +00:00
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
repo_manager._clone('https://github.com/oss-fuzz-not-real.git', tmp_dir,
|
|
|
|
'oss-fuzz')
|
2020-01-30 18:27:56 +00:00
|
|
|
|
2021-10-20 02:51:57 +00:00
|
|
|
@mock.patch('utils.execute')
|
|
|
|
def test_clone_with_username(self, mock_execute): # pylint: disable=no-self-use
|
|
|
|
"""Test clone with username."""
|
|
|
|
repo_manager._clone('https://github.com/fake/repo.git',
|
|
|
|
'/',
|
|
|
|
'name',
|
|
|
|
username='user',
|
|
|
|
password='password')
|
|
|
|
mock_execute.assert_called_once_with([
|
|
|
|
'git', 'clone', 'https://user:password@github.com/fake/repo.git', 'name'
|
|
|
|
],
|
|
|
|
location='/',
|
|
|
|
check_result=True,
|
|
|
|
log_command=False)
|
|
|
|
|
2020-01-30 18:27:56 +00:00
|
|
|
|
2020-12-18 18:44:12 +00:00
|
|
|
@unittest.skipIf(not os.getenv('INTEGRATION_TESTS'),
|
|
|
|
'INTEGRATION_TESTS=1 not set')
|
2020-11-17 21:39:57 +00:00
|
|
|
class RepoManagerCheckoutTest(unittest.TestCase):
|
|
|
|
"""Tests the checkout functionality of RepoManager."""
|
2019-12-12 17:26:37 +00:00
|
|
|
|
2020-01-30 18:27:56 +00:00
|
|
|
def test_checkout_valid_commit(self):
|
2019-12-12 17:26:37 +00:00
|
|
|
"""Tests that the git checkout command works."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-01-30 18:27:56 +00:00
|
|
|
commit_to_test = '04ea24ee15bbe46a19e5da6c5f022a2ffdfbdb3b'
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.checkout_commit(commit_to_test)
|
|
|
|
self.assertEqual(commit_to_test, repo_man.get_current_commit())
|
2020-01-30 18:27:56 +00:00
|
|
|
|
|
|
|
def test_checkout_invalid_commit(self):
|
|
|
|
"""Tests that the git checkout invalid commit fails."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.checkout_commit(' ')
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.checkout_commit('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.checkout_commit('not-a-valid-commit')
|
2019-12-12 17:26:37 +00:00
|
|
|
|
2020-01-30 18:27:56 +00:00
|
|
|
|
2020-12-18 18:44:12 +00:00
|
|
|
@unittest.skipIf(not os.getenv('INTEGRATION_TESTS'),
|
|
|
|
'INTEGRATION_TESTS=1 not set')
|
2020-11-17 21:39:57 +00:00
|
|
|
class RepoManagerGetCommitListTest(unittest.TestCase):
|
|
|
|
"""Tests the get_commit_list method of RepoManager."""
|
2020-01-30 18:27:56 +00:00
|
|
|
|
|
|
|
def test_get_valid_commit_list(self):
|
|
|
|
"""Tests an accurate commit list can be retrieved from the repo manager."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-01-30 18:27:56 +00:00
|
|
|
old_commit = '04ea24ee15bbe46a19e5da6c5f022a2ffdfbdb3b'
|
|
|
|
new_commit = 'fa662173bfeb3ba08d2e84cefc363be11e6c8463'
|
2020-01-06 20:17:26 +00:00
|
|
|
commit_list = [
|
2020-01-30 18:27:56 +00:00
|
|
|
'fa662173bfeb3ba08d2e84cefc363be11e6c8463',
|
|
|
|
'17035317a44fa89d22fe6846d868d4bf57def78b',
|
|
|
|
'97dee00a3c4ce95071c3e061592f5fd577dea886',
|
|
|
|
'04ea24ee15bbe46a19e5da6c5f022a2ffdfbdb3b'
|
2020-01-06 20:17:26 +00:00
|
|
|
]
|
2020-12-07 18:50:11 +00:00
|
|
|
result_list = repo_man.get_commit_list(new_commit, old_commit)
|
2020-01-06 20:17:26 +00:00
|
|
|
self.assertListEqual(commit_list, result_list)
|
2020-01-30 18:27:56 +00:00
|
|
|
|
2020-11-17 21:39:57 +00:00
|
|
|
def test_get_invalid_commit_list(self):
|
|
|
|
"""Tests that the proper errors are thrown when invalid commits are
|
|
|
|
passed to get_commit_list."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-01-30 18:27:56 +00:00
|
|
|
old_commit = '04ea24ee15bbe46a19e5da6c5f022a2ffdfbdb3b'
|
|
|
|
new_commit = 'fa662173bfeb3ba08d2e84cefc363be11e6c8463'
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.get_commit_list('fakecommit', new_commit)
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.get_commit_list(new_commit, 'fakecommit')
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(RuntimeError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.get_commit_list(old_commit, new_commit) # pylint: disable=arguments-out-of-order
|
2019-12-12 17:26:37 +00:00
|
|
|
|
|
|
|
|
2020-12-18 18:44:12 +00:00
|
|
|
@unittest.skipIf(not os.getenv('INTEGRATION_TESTS'),
|
|
|
|
'INTEGRATION_TESTS=1 not set')
|
2020-11-17 21:39:57 +00:00
|
|
|
class GitDiffTest(unittest.TestCase):
|
|
|
|
"""Tests get_git_diff."""
|
2020-01-31 23:31:18 +00:00
|
|
|
|
2020-02-28 16:30:42 +00:00
|
|
|
def test_diff_exists(self):
|
|
|
|
"""Tests that a real diff is returned when a valid repo manager exists."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-02-28 16:30:42 +00:00
|
|
|
with mock.patch.object(utils,
|
|
|
|
'execute',
|
|
|
|
return_value=('test.py\ndiff.py', None, 0)):
|
|
|
|
diff = repo_man.get_git_diff()
|
|
|
|
self.assertCountEqual(diff, ['test.py', 'diff.py'])
|
|
|
|
|
|
|
|
def test_diff_empty(self):
|
|
|
|
"""Tests that None is returned when there is no difference between repos."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-02-28 16:30:42 +00:00
|
|
|
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."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-02-28 16:30:42 +00:00
|
|
|
with mock.patch.object(utils,
|
|
|
|
'execute',
|
|
|
|
return_value=('', 'Test error.', 1)):
|
|
|
|
diff = repo_man.get_git_diff()
|
|
|
|
self.assertIsNone(diff)
|
|
|
|
|
|
|
|
def test_diff_no_change(self):
|
|
|
|
"""Tests that None is returned when there is no difference between repos."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-02-28 16:30:42 +00:00
|
|
|
diff = repo_man.get_git_diff()
|
|
|
|
self.assertIsNone(diff)
|
|
|
|
|
|
|
|
|
2020-12-18 18:44:12 +00:00
|
|
|
@unittest.skipIf(not os.getenv('INTEGRATION_TESTS'),
|
|
|
|
'INTEGRATION_TESTS=1 not set')
|
2020-11-17 21:39:57 +00:00
|
|
|
class CheckoutPrIntegrationTest(unittest.TestCase):
|
|
|
|
"""Does Integration tests on the checkout_pr method of RepoManager."""
|
2020-02-28 16:30:42 +00:00
|
|
|
|
|
|
|
def test_pull_request_exists(self):
|
|
|
|
"""Tests that a diff is returned when a valid PR is checked out."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-02-28 16:30:42 +00:00
|
|
|
repo_man.checkout_pr('refs/pull/3415/merge')
|
|
|
|
diff = repo_man.get_git_diff()
|
|
|
|
self.assertCountEqual(diff, ['README.md'])
|
2020-01-31 23:31:18 +00:00
|
|
|
|
|
|
|
def test_checkout_invalid_pull_request(self):
|
|
|
|
"""Tests that the git checkout invalid pull request fails."""
|
2020-12-07 18:50:11 +00:00
|
|
|
with get_oss_fuzz_repo() as oss_fuzz_repo:
|
|
|
|
repo_man = repo_manager.RepoManager(oss_fuzz_repo)
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(RuntimeError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.checkout_pr(' ')
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(RuntimeError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.checkout_pr('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
|
2020-01-31 23:31:18 +00:00
|
|
|
with self.assertRaises(RuntimeError):
|
2020-12-07 18:50:11 +00:00
|
|
|
repo_man.checkout_pr('not/a/valid/pr')
|
2020-01-31 23:31:18 +00:00
|
|
|
|
|
|
|
|
2019-12-12 17:26:37 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|