def test_download_artifact(self, mock_download_and_unpack_zip, mock_find_artifact): """Tests that _download_artifact works as intended.""" artifact_download_url = 'http://example.com/download' artifact_listing = { 'expired': False, 'name': 'corpus', 'archive_download_url': artifact_download_url } mock_find_artifact.return_value = artifact_listing self._create_local_dir() with tempfile.TemporaryDirectory() as temp_dir: # Create a tarball. archive_path = os.path.join(temp_dir, 'cifuzz-corpus.tar') github_actions.tar_directory(self.local_dir, archive_path) artifact_download_dst_dir = os.path.join(temp_dir, 'dst') os.mkdir(artifact_download_dst_dir) def mock_download_and_unpack_zip_impl(url, download_artifact_temp_dir, headers): self.assertEqual(url, artifact_download_url) self.assertEqual(headers, self._get_expected_http_headers()) shutil.copy( archive_path, os.path.join(download_artifact_temp_dir, os.path.basename(archive_path))) return True mock_download_and_unpack_zip.side_effect = ( mock_download_and_unpack_zip_impl) filestore = github_actions.GithubActionsFilestore(self.config) self.assertTrue( filestore._download_artifact('corpus', artifact_download_dst_dir)) mock_find_artifact.assert_called_with('cifuzz-corpus') self.assertTrue( os.path.exists( os.path.join(artifact_download_dst_dir, os.path.basename(self.testcase))))
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))