aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/base-images/base-builder/detect_repo_test.py
diff options
context:
space:
mode:
authorGravatar Leo Neat <leosneat@gmail.com>2020-01-15 13:30:57 -0800
committerGravatar jonathanmetzman <31354670+jonathanmetzman@users.noreply.github.com>2020-01-15 13:30:57 -0800
commit14582175d07c8a66adf3f9087a464612f2a3f630 (patch)
tree831d57d5a406af6be2dcb3bd0ca7ba1970956cea /infra/base-images/base-builder/detect_repo_test.py
parent79860344beaf5b228c0f619451d860ec0f2adcfa (diff)
[infra] Scripts for building fuzzers with CIFuzz (#3207)
Diffstat (limited to 'infra/base-images/base-builder/detect_repo_test.py')
-rw-r--r--infra/base-images/base-builder/detect_repo_test.py64
1 files changed, 54 insertions, 10 deletions
diff --git a/infra/base-images/base-builder/detect_repo_test.py b/infra/base-images/base-builder/detect_repo_test.py
index cac0f882..e9029b7f 100644
--- a/infra/base-images/base-builder/detect_repo_test.py
+++ b/infra/base-images/base-builder/detect_repo_test.py
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test the functionality of the detect_repo module.
-The will consist of the following functional test
+This will consist of the following functional test:
1. Determine if a OSS-Fuzz projects main repo can be accurately deduce
from example commits.
"""
@@ -23,11 +23,14 @@ import tempfile
import unittest
import detect_repo
+
# Appending to path for access to repo_manager module.
+# pylint: disable=wrong-import-position
sys.path.append(
- os.path.dirname(
- os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
+ os.path.dirname(os.path.dirname(os.path.dirname(
+ os.path.abspath(__file__)))))
import repo_manager
+# pylint: enable=wrong-import-position
class DetectRepoTest(unittest.TestCase):
@@ -62,25 +65,66 @@ class DetectRepoTest(unittest.TestCase):
self.check_commit_with_repo(None, None,
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', tmp_dir)
- def check_commit_with_repo(self, repo_origin, repo_name, commit, tmp_dir):
+ def test_infer_main_repo_from_name(self):
+ """Tests that the main project repo can be inferred from a repo name."""
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ # Construct example repos to check for name.
+ repo_manager.RepoManager('https://github.com/curl/curl.git', tmp_dir)
+ repo_manager.RepoManager('https://github.com/ntop/nDPI.git', tmp_dir)
+ repo_manager.RepoManager('https://github.com/libarchive/libarchive.git',
+ tmp_dir)
+ self.check_ref_with_repo('https://github.com/curl/curl.git', 'curl',
+ tmp_dir)
+ self.check_ref_with_repo('https://github.com/ntop/nDPI.git', 'nDPI',
+ tmp_dir)
+ self.check_ref_with_repo('https://github.com/libarchive/libarchive.git',
+ 'libarchive', tmp_dir)
+
+ def check_ref_with_repo(self, repo_origin, repo_name, tmp_dir):
"""Checks the detect repo's main method for a specific set of inputs.
+ Args:
+ repo_origin: URL of the git repo.
+ repo_name: The name of the directory it is cloned to.
+ tmp_dir: The location of the directory of git repos to be searched.
+ """
+ command = [
+ 'python3', 'detect_repo.py', '--src_dir', tmp_dir, '--repo_name',
+ repo_name
+ ]
+ out, _ = detect_repo.execute(command,
+ location=os.path.dirname(
+ os.path.realpath(__file__)))
+ match = re.search(r'\bDetected repo: ([^ ]+) ([^ ]+)', out.rstrip())
+ if match and match.group(1) and match.group(2):
+ self.assertEqual(match.group(1), repo_origin)
+ else:
+ self.assertIsNone(repo_origin)
+
+ def check_commit_with_repo(self, repo_origin, repo_name, commit, tmp_dir):
+ """Checks the detect repos main method for a specific set of inputs.
+
Args:
- repo_origin: The location of where the git repo is stored
- repo_name: The name of the directory it is cloned to
- commit: The commit that should be used to look up the repo
- tmp_dir: The location of the directory of git repos to be searched
+ repo_origin: URL of the git repo.
+ repo_name: The name of the directory it is cloned to.
+ commit: The commit that should be used to look up the repo.
+ tmp_dir: The location of the directory of git repos to be searched.
"""
command = [
'python3', 'detect_repo.py', '--src_dir', tmp_dir, '--example_commit',
commit
]
- out, _ = detect_repo.execute(
- command, location=os.path.dirname(os.path.realpath(__file__)))
+ out, _ = detect_repo.execute(command,
+ location=os.path.dirname(
+ os.path.abspath(__file__)))
match = re.search(r'\bDetected repo: ([^ ]+) ([^ ]+)', out.rstrip())
if match and match.group(1) and match.group(2):
self.assertEqual(match.group(1), repo_origin)
self.assertEqual(match.group(2), repo_name)
+ else:
+ self.assertIsNone(repo_origin)
+ self.assertIsNone(repo_name)
if __name__ == '__main__':