def test_find_artifact(self, mock_list_artifacts): """Tests that _find_artifact works as intended.""" artifact_listing_1 = { 'expired': False, 'name': 'other', 'archive_download_url': 'http://download1' } artifact_listing_2 = { 'expired': False, 'name': 'artifact', 'archive_download_url': 'http://download2' } artifact_listing_3 = { 'expired': True, 'name': 'artifact', 'archive_download_url': 'http://download3' } artifact_listing_4 = { 'expired': False, 'name': 'artifact', 'archive_download_url': 'http://download4' } artifacts = [ artifact_listing_1, artifact_listing_2, artifact_listing_3, artifact_listing_4 ] mock_list_artifacts.return_value = artifacts filestore = github_actions.GithubActionsFilestore(self.config) # Test that find_artifact will return the most recent unexpired artifact # with the correct name. self.assertEqual(filestore._find_artifact('artifact'), artifact_listing_2) mock_list_artifacts.assert_called_with( self.owner, self.repo, self._get_expected_http_headers())
def test_upload_crashes(self, mock_upload_artifact): """Test uploading crashes.""" self._create_local_dir() filestore = github_actions.GithubActionsFilestore(self.config) filestore.upload_crashes('current', self.local_dir) mock_upload_artifact.assert_has_calls( [mock.call('crashes-current', ['/local-dir/testcase'], '/local-dir')])
def test_download_corpus_no_artifact(self, _, __, mock_warning): """Tests that download_corpus_build returns None and doesn't exception when find_artifact can't find an artifact.""" filestore = github_actions.GithubActionsFilestore(self.config) name = 'name' dst_dir = 'local-dir' self.assertFalse(filestore.download_corpus(name, dst_dir)) mock_warning.assert_called_with('Could not download artifact: %s.', 'cifuzz-corpus-' + name)
def test_download_corpus_no_artifact(self, _, __, mocked_warning): """Tests that download_corpus_build returns None and doesn't exception when find_artifact can't find an artifact.""" config = test_helpers.create_run_config(github_token=self.github_token) filestore = github_actions.GithubActionsFilestore(config) name = 'corpus-name' dst_dir = 'corpus-dir' self.assertFalse(filestore.download_corpus(name, dst_dir)) mocked_warning.assert_called_with('Could not download artifact: %s.', name)
def test_list_artifacts(self, mocked_list_artifacts): """Tests that _list_artifacts works as intended.""" owner = 'exampleowner' repo = 'examplerepo' os.environ['GITHUB_REPOSITORY'] = '{owner}/{repo}'.format(owner=owner, repo=repo) config = test_helpers.create_run_config(github_token=self.github_token) filestore = github_actions.GithubActionsFilestore(config) filestore._list_artifacts() mocked_list_artifacts.assert_called_with( owner, repo, self._get_expected_http_headers())
def test_upload_corpus(self, mock_upload_artifact, mock_tar_directory): """Test uploading corpus.""" self._create_local_dir() def mock_tar_directory_impl(_, archive_path): self.fs.create_file(archive_path) mock_tar_directory.side_effect = mock_tar_directory_impl filestore = github_actions.GithubActionsFilestore(self.config) filestore.upload_corpus('target', self.local_dir) self.assert_upload(mock_upload_artifact, mock_tar_directory, 'corpus-target')
def test_upload_build(self, mock_upload_artifact, mock_tar_directory): """Test uploading build.""" self._create_local_dir() def mock_tar_directory_impl(_, archive_path): self.fs.create_file(archive_path) mock_tar_directory.side_effect = mock_tar_directory_impl filestore = github_actions.GithubActionsFilestore(self.config) filestore.upload_build('sanitizer', self.local_dir) self.assert_upload(mock_upload_artifact, mock_tar_directory, 'build-sanitizer')
def test_upload_coverage(self, mocked_upload_artifact, mocked_tar_directory): """Test uploading coverage.""" self._create_local_dir() def mock_tar_directory(_, archive_path): self.fs.create_file(archive_path) mocked_tar_directory.side_effect = mock_tar_directory filestore = github_actions.GithubActionsFilestore(self.config) filestore.upload_coverage('latest', self.local_dir) self.assert_upload(mocked_upload_artifact, mocked_tar_directory, 'coverage-latest')
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_list_artifacts(self, mock_list_artifacts): """Tests that _list_artifacts works as intended.""" filestore = github_actions.GithubActionsFilestore(self.config) filestore._list_artifacts() mock_list_artifacts.assert_called_with( self.owner, self.repo, self._get_expected_http_headers())