From b96d2186aec635d2724f4a5b2d428732e37bede8 Mon Sep 17 00:00:00 2001 From: Leo Neat Date: Thu, 6 Feb 2020 15:35:42 -0800 Subject: [CIFuzz] Go support (#3352) * Add go support to detecting the main repo * Go support test update * Format * Format * Removing debug statment * Format --- infra/base-images/base-builder/detect_repo.py | 23 ++++++++++++++++++++++- infra/build_specified_commit_test.py | 5 +++-- infra/test_repos.py | 10 ++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) (limited to 'infra') diff --git a/infra/base-images/base-builder/detect_repo.py b/infra/base-images/base-builder/detect_repo.py index 272a915d3..8969e974f 100644 --- a/infra/base-images/base-builder/detect_repo.py +++ b/infra/base-images/base-builder/detect_repo.py @@ -30,6 +30,8 @@ import logging import os import subprocess +GO_PATH = '/root/go/src/' + def main(): """Function to get a git repo's url and name referenced by OSS-Fuzz @@ -55,7 +57,7 @@ def main(): else: src_dir = os.environ.get('SRC', '/src') - for single_dir in os.listdir(src_dir): + for single_dir in get_dirs_to_search(src_dir, args.repo_name): full_path = os.path.join(src_dir, single_dir) if not os.path.isdir(full_path): continue @@ -69,6 +71,25 @@ def main(): args.example_commit, src_dir) +def get_dirs_to_search(src_dir, repo_name): + """Gets a list of directories to search for the main git repo. + + Args: + src_dir: The location set for the projects SRC. + repo_name: The name of the repo you are searching for. + + Returns: + A list of directorys to search. + """ + dirs_to_search = os.listdir(src_dir) + if os.path.exists(GO_PATH) and repo_name: + for root, dirs, _ in os.walk(GO_PATH): + for test_dir in dirs: + if repo_name in test_dir: + dirs_to_search.append(os.path.join(root, test_dir)) + return dirs_to_search + + def get_repo(repo_path): """Gets a git repo link from a specific directory in a docker image. diff --git a/infra/build_specified_commit_test.py b/infra/build_specified_commit_test.py index 13ca7b6ee..9fdaae476 100644 --- a/infra/build_specified_commit_test.py +++ b/infra/build_specified_commit_test.py @@ -88,8 +88,9 @@ class BuildImageIntegrationTests(unittest.TestCase): repo_origin, repo_name = build_specified_commit.detect_main_repo( example_repo.project_name, repo_name=example_repo.git_repo_name) self.assertEqual(repo_origin, example_repo.git_url) - self.assertEqual(repo_name, - os.path.join('/src', example_repo.oss_repo_name)) + self.assertEqual( + repo_name, + os.path.join(example_repo.image_location, example_repo.oss_repo_name)) repo_origin, repo_name = build_specified_commit.detect_main_repo( test_repos.INVALID_REPO.project_name, diff --git a/infra/test_repos.py b/infra/test_repos.py index 708c95952..09e6937f4 100644 --- a/infra/test_repos.py +++ b/infra/test_repos.py @@ -25,8 +25,9 @@ import collections import os ExampleRepo = collections.namedtuple('ExampleRepo', [ - 'project_name', 'oss_repo_name', 'git_repo_name', 'git_url', 'new_commit', - 'old_commit', 'intro_commit', 'fuzz_target', 'test_case_path' + 'project_name', 'oss_repo_name', 'git_repo_name', 'image_location', + 'git_url', 'new_commit', 'old_commit', 'intro_commit', 'fuzz_target', + 'test_case_path' ]) TEST_DIR_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), @@ -38,6 +39,7 @@ TEST_REPOS = [ ExampleRepo(project_name='usrsctp', oss_repo_name='usrsctp', git_repo_name='usrsctp', + image_location='/src', git_url='https://github.com/weinrank/usrsctp', old_commit='4886aaa49fb90e479226fcfc3241d74208908232', new_commit='c710749b1053978179a027973a3ea3bccf20ee5c', @@ -48,6 +50,7 @@ TEST_REPOS = [ ExampleRepo(project_name='curl', oss_repo_name='curl', git_repo_name='curl', + image_location='/src', git_url='https://github.com/curl/curl.git', old_commit='df26f5f9c36e19cd503c0e462e9f72ad37b84c82', new_commit='dda418266c99ceab368d723facb52069cbb9c8d5', @@ -57,6 +60,7 @@ TEST_REPOS = [ ExampleRepo(project_name='libarchive', oss_repo_name='libarchive', git_repo_name='libarchive', + image_location='/src', git_url='https://github.com/libarchive/libarchive.git', old_commit='5bd2a9b6658a3a6efa20bb9ad75bd39a44d71da6', new_commit='458e49358f17ec58d65ab1c45cf299baaf3c98d1', @@ -67,6 +71,7 @@ TEST_REPOS = [ ExampleRepo(project_name='gonids', oss_repo_name='gonids', git_repo_name='gonids', + image_location='/root/go/src/github.com/google/', git_url='https://github.com/google/gonids', old_commit='', new_commit='', @@ -79,6 +84,7 @@ INVALID_REPO = ExampleRepo(project_name='notaproj', oss_repo_name='notarepo', git_repo_name='notarepo', git_url='invalid.git', + image_location='/src', old_commit='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', new_commit='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', intro_commit='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', -- cgit v1.2.3