[CIFuzz] Fix bug in tar_directory (#6083)

We need to split the archive_path
This commit is contained in:
jonathanmetzman 2021-07-21 10:19:32 -07:00 committed by GitHub
parent 67e3a77fd7
commit 6baa5e3e68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 16 deletions

View File

@ -135,13 +135,13 @@ class BuildFuzzersIntegrationTest(unittest.TestCase):
"""Integration tests for build_fuzzers."""
def setUp(self):
self.tmp_dir_obj = tempfile.TemporaryDirectory()
self.workspace = self.tmp_dir_obj.name
self.temp_dir_obj = tempfile.TemporaryDirectory()
self.workspace = self.temp_dir_obj.name
self.out_dir = os.path.join(self.workspace, 'build-out')
test_helpers.patch_environ(self)
def tearDown(self):
self.tmp_dir_obj.cleanup()
self.temp_dir_obj.cleanup()
def test_external_github_project(self):
"""Tests building fuzzers from an external project on Github."""
@ -171,7 +171,7 @@ class BuildFuzzersIntegrationTest(unittest.TestCase):
# github.com/jonathanmetzman/cifuzz-external-example.
manager = repo_manager.clone_repo_and_get_manager(
'https://github.com/jonathanmetzman/cifuzz-external-example',
self.tmp_dir_obj.name)
self.temp_dir_obj.name)
project_src_path = manager.repo_dir
config = test_helpers.create_build_config(
project_repo_name=project_repo_name,
@ -269,13 +269,13 @@ class CheckFuzzerBuildTest(unittest.TestCase):
LANGUAGE = 'c++'
def setUp(self):
self.tmp_dir_obj = tempfile.TemporaryDirectory()
workspace_path = os.path.join(self.tmp_dir_obj.name, 'workspace')
self.temp_dir_obj = tempfile.TemporaryDirectory()
workspace_path = os.path.join(self.temp_dir_obj.name, 'workspace')
self.workspace = test_helpers.create_workspace(workspace_path)
shutil.copytree(TEST_DATA_PATH, workspace_path)
def tearDown(self):
self.tmp_dir_obj.cleanup()
self.temp_dir_obj.cleanup()
def test_correct_fuzzer_build(self):
"""Checks check_fuzzer_build function returns True for valid fuzzers."""

View File

@ -113,7 +113,7 @@ class ClusterFuzzLite(BaseClusterFuzzDeployment):
self.workspace.clusterfuzz_build):
return self.workspace.clusterfuzz_build
except Exception as err: # pylint: disable=broad-except
logging.error('Could not download latest build because of: %s.', err)
logging.error('Could not download latest build because of: %s', err)
return None
@ -125,7 +125,7 @@ class ClusterFuzzLite(BaseClusterFuzzDeployment):
try:
self.filestore.download_corpus(corpus_name, corpus_dir)
except Exception as err: # pylint: disable=broad-except
logging.error('Failed to download corpus for target: %s. Error: %s.',
logging.error('Failed to download corpus for target: %s. Error: %s',
target_name, str(err))
return corpus_dir
@ -159,7 +159,7 @@ class ClusterFuzzLite(BaseClusterFuzzDeployment):
return self.filestore.upload_directory(build_name,
self.workspace.clusterfuzz_build)
except Exception as error: # pylint: disable=broad-except
logging.error('Failed to upload latest build: %s. Error: %s.',
logging.error('Failed to upload latest build: %s. Error: %s',
self.workspace.clusterfuzz_build, error)
def upload_crashes(self):
@ -174,7 +174,7 @@ class ClusterFuzzLite(BaseClusterFuzzDeployment):
self.filestore.upload_directory(crashes_artifact_name,
self.workspace.artifacts)
except Exception as error: # pylint: disable=broad-except
logging.error('Failed to upload crashes. Error: %s.', error)
logging.error('Failed to upload crashes. Error: %s', error)
def upload_coverage(self):
"""Uploads the coverage report to the filestore."""

View File

@ -29,14 +29,13 @@ def tar_directory(directory, archive_path):
must end in .tar"""
assert archive_path.endswith('.tar')
# Do this because make_archive will append the extension to archive_path.
archive_path = os.path.splitext('.tar')[0]
archive_path = os.path.splitext(archive_path)[0]
parent_directory = os.path.dirname(os.path.abspath(directory))
basename = os.path.basename(directory)
root_directory = os.path.abspath(directory)
shutil.make_archive(archive_path,
'tar',
root_dir=parent_directory,
base_dir=basename)
root_dir=root_directory,
base_dir='./')
class GithubActionsFilestore(filestore.BaseFilestore):

View File

@ -14,6 +14,8 @@
"""Tests for github_actions."""
import os
import sys
import tarfile
import tempfile
import unittest
from unittest import mock
@ -84,3 +86,29 @@ class GithubActionsFilestoreTest(unittest.TestCase):
dst_dir = 'corpus-dir'
self.assertFalse(filestore.download_corpus(name, dst_dir))
mocked_warning.assert_called_with('Could not download artifact: %s.', name)
class TarDirectoryTest(unittest.TestCase):
"""Tests for tar_directory."""
def test_tar_directory(self):
"""Tests that tar_directory writes the archive to the correct location and
archives properly."""
with tempfile.TemporaryDirectory() as temp_dir:
archive_path = os.path.join(temp_dir, 'myarchive.tar')
archived_dir = os.path.join(temp_dir, 'toarchive')
os.mkdir(archived_dir)
archived_filename = 'file1'
archived_file_path = os.path.join(archived_dir, archived_filename)
with open(archived_file_path, 'w') as file_handle:
file_handle.write('hi')
github_actions.tar_directory(archived_dir, archive_path)
self.assertTrue(os.path.exists(archive_path))
# Now check it archives correctly.
unpacked_directory = os.path.join(temp_dir, 'unpacked')
with tarfile.TarFile(archive_path) as artifact_tarfile:
artifact_tarfile.extractall(unpacked_directory)
unpacked_archived_file_path = os.path.join(unpacked_directory,
archived_filename)
self.assertTrue(os.path.exists(unpacked_archived_file_path))