def test_file_whitelist(self, mock_identify_revision, mock_get_vcs): repo = self.create_repo() revision = self.create_revision(repository=repo) project = self.create_project(repository=repo) self.create_plan(project) option = ProjectOption(project=project, name='build.file-whitelist', value='foo.txt') mock_vcs = self.get_fake_vcs() mock_vcs.export.side_effect = None mock_vcs.export.return_value = SAMPLE_DIFF mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files(mock_vcs, id) mock_vcs.update.side_effect = None mock_identify_revision.return_value = revision mock_get_vcs.return_value = mock_vcs db.session.add(option) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_vcs.export.assert_called_once_with(revision.sha) assert not Build.query.first() option.value = 'ci/*' db.session.add(option) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_identify_revision.assert_called_once_with(repo, revision.sha) assert Build.query.first()
def test_diff_all_failed(self): project = self.create_project() db.session.add( ProjectOption(project=project, name='mail.notify-author', value='1')) db.session.add( ProjectOption(project=project, name='mail.notify-addresses-revisions', value='[email protected], [email protected]')) author = self.create_author('*****@*****.**') author_recipient = '{0} <{1}>'.format(author.name, author.email) patch_build = self.create_build( project=project, source=self.create_source( project, patch=self.create_patch(repository=project.repository)), author=author, result=Result.failed, ) project2 = self.create_project() db.session.add( ProjectOption(project=project2, name='mail.notify-author', value='0')) db.session.add( ProjectOption(project=project2, name='mail.notify-addresses-revisions', value='[email protected], [email protected]')) author2 = self.create_author('*****@*****.**') author2_recipient = '{0} <{1}>'.format(author2.name, author2.email) patch_build2 = self.create_build( project=project2, source=self.create_source( project2, patch=self.create_patch(repository=project2.repository)), author=author2, result=Result.failed, ) db.session.commit() mock_context = mock.Mock(builds=[{ 'build': patch_build }, { 'build': patch_build2 }]) recipients = MailNotificationHandler().get_collection_recipients( mock_context) assert recipients == [author_recipient]
def test_with_revision_addressees(self): project = self.create_project() db.session.add( ProjectOption(project=project, name='mail.notify-author', value='1')) db.session.add( ProjectOption(project=project, name='mail.notify-addresses-revisions', value='[email protected], [email protected]')) author = self.create_author('*****@*****.**') author_recipient = '{0} <{1}>'.format(author.name, author.email) patch = self.create_patch(repository=project.repository) source = self.create_source(project, patch=patch) patch_build = self.create_build( project=project, source=source, author=author, result=Result.failed, ) db.session.commit() patch_recipients = MailNotificationHandler().get_build_recipients( patch_build) assert patch_recipients == [author_recipient] ss_build = self.create_build( project=project, result=Result.failed, author=author, tags=['test-snapshot'], ) ss_recipients = MailNotificationHandler().get_build_recipients( ss_build) assert ss_recipients == [author_recipient] commit_build = self.create_build( project=project, result=Result.failed, author=author, tags=['commit'], ) commit_recipients = MailNotificationHandler().get_build_recipients( commit_build) assert commit_recipients == [ author_recipient, '*****@*****.**', '*****@*****.**', ]
def test_with_expected_snapshot(self): project = self.create_project() build = self.create_build(project, cause=Cause.snapshot) plan = self.create_plan(project) job = self.create_job(build) jobphase = self.create_jobphase(job) jobstep = self.create_jobstep(jobphase) self.create_job_plan(job, plan) snapshot = self.create_snapshot(project, build=build) self.create_snapshot_image( plan=plan, snapshot=snapshot, ) db.session.add( ProjectOption( project_id=project.id, name='snapshot.current', value=snapshot.id.hex, )) new_snapshot = self.create_snapshot(project, build=build) new_image = self.create_snapshot_image( plan=plan, snapshot=new_snapshot, job=job, ) db.session.commit() path = '/api/0/jobsteps/{0}/'.format(jobstep.id.hex) resp = self.client.get(path) assert resp.status_code == 200 data = self.unserialize(resp) assert data['id'] == jobstep.id.hex assert data['snapshot'] is None assert data['expectedSnapshot']['id'] == new_image.id.hex
def test_multiple_jobsteps(self): project = self.create_project() build = self.create_build(project) plan_1 = self.create_plan(project) plan_2 = self.create_plan(project) plan_1.snapshot_plan_id = plan_2.id job = self.create_job(build) jobphase = self.create_jobphase(job) jobstep = self.create_jobstep(jobphase) snapshot = self.create_snapshot(project, build=build) image_1 = self.create_snapshot_image(snapshot, plan_1) image_2 = self.create_snapshot_image(snapshot, plan_2) db.session.add( ProjectOption( project_id=project.id, name='snapshot.current', value=snapshot.id.hex, )) db.session.commit() self.create_job_plan(job, plan_1, snapshot.id) db.session.commit() path = '/api/0/jobsteps/{0}/'.format(jobstep.id.hex) resp = self.client.get(path) assert resp.status_code == 200 data = self.unserialize(resp) assert data['snapshot']['id'] == image_2.id.hex
def test_when_in_whitelist(self, get_vcs): get_vcs.return_value = self.get_fake_vcs() po = ProjectOption( project=self.project, name='build.file-whitelist', value='ci/*', ) db.session.add(po) db.session.commit() build = self.create_build(project=self.project, source=self.source, status=Status.finished, result=Result.failed) job = self.create_job(build=build) path = '/api/0/phabricator_diffs/{0}/retry/'.format(self.diff.diff_id) resp = self.client.post(path, follow_redirects=True) assert resp.status_code == 200 data = self.unserialize(resp) assert len(data) == 1 new_build = Build.query.get(data[0]['id']) assert new_build.id != build.id assert new_build.collection_id != build.collection_id assert new_build.project_id == build.project_id assert new_build.source_id == build.source_id (new_job, ) = list(Job.query.filter(Job.build_id == new_build.id, )) assert new_job.id != job.id
def create_project_option(self, project, name, value, **kwargs): option = ProjectOption(project=project, name=name, value=value, **kwargs) db.session.add(option) db.session.commit() return option
def test_simple(self): project_1 = self.create_project() build_1 = self.create_build(project_1) snapshot_1 = self.create_snapshot(project=project_1, status=SnapshotStatus.active, build=build_1) plan_1 = self.create_plan(project_1) image_1 = self.create_snapshot_image(snapshot_1, plan_1) project_2 = self.create_project() build_2 = self.create_build(project_2) snapshot_2 = self.create_snapshot(project=project_2, status=SnapshotStatus.invalidated, build=build_2) plan_2 = self.create_plan(project_2) image_2 = self.create_snapshot_image(snapshot_2, plan_1) image_3 = self.create_snapshot_image(snapshot_2, plan_2) db.session.add( ProjectOption( project=project_2, name='snapshot.current', value=snapshot_2.id.hex, )) db.session.commit() path = '/api/0/snapshots/?state=' resp = self.client.get(path) assert resp.status_code == 200 data = self.unserialize(resp) assert len(data) == 2 assert data[0]['id'] == snapshot_2.id.hex assert data[0]['isActive'] assert len(data[0]['images']) == 2 assert data[0]['images'][0]['id'] == image_2.id.hex assert data[0]['images'][1]['id'] == image_3.id.hex assert data[1]['id'] == snapshot_1.id.hex assert not data[1]['isActive'] assert len(data[1]['images']) == 1 assert data[1]['images'][0]['id'] == image_1.id.hex path = '/api/0/snapshots/?state=valid' resp = self.client.get(path) assert resp.status_code == 200 data = self.unserialize(resp) assert len(data) == 1 assert data[0]['id'] == snapshot_1.id.hex path = '/api/0/snapshots/?state=invalid' resp = self.client.get(path) assert resp.status_code == 200 data = self.unserialize(resp) assert len(data) == 1 assert data[0]['id'] == snapshot_2.id.hex
def test_build_passed(self): project = self.create_project() db.session.add( ProjectOption(project=project, name='mail.notify-author', value='1')) db.session.add( ProjectOption(project=project, name='mail.notify-addresses', value='[email protected], [email protected]')) author = self.create_author('*****@*****.**') build = self.create_build(project, result=Result.passed, author=author) db.session.commit() handler = MailNotificationHandler() recipients = handler.get_build_recipients(build) assert recipients == []
def test_with_revision_addressees(self): project = self.create_project() db.session.add( ProjectOption(project=project, name='mail.notify-author', value='1')) db.session.add( ProjectOption(project=project, name='mail.notify-addresses-revisions', value='[email protected], [email protected]')) author = self.create_author('*****@*****.**') patch = self.create_patch(repository=project.repository) source = self.create_source(project, patch=patch) build = self.create_build( project=project, source=source, author=author, result=Result.failed, ) db.session.commit() handler = MailNotificationHandler() recipients = handler.get_build_recipients(build) assert recipients == ['{0} <*****@*****.**>'.format(author.name)] build = self.create_build( project=project, result=Result.failed, author=author, ) handler = MailNotificationHandler() recipients = handler.get_build_recipients(build) assert recipients == [ '{0} <*****@*****.**>'.format(author.name), '*****@*****.**', '*****@*****.**', ]
def test_get_snapshot_image_independent(self): project = self.create_project() plan = self.create_plan(project) snapshot = self.create_snapshot(project) db.session.add( ProjectOption( project_id=project.id, name='snapshot.current', value=snapshot.id.hex, )) snapshot_image = self.create_snapshot_image(snapshot, plan) assert snapshot_image == SnapshotImage.get(plan, snapshot.id)
def test_file_whitelist(self, mock_identify_revision, mock_get_vcs): repo = self.create_repo() revision = self.create_revision(repository=repo) project = self.create_project(repository=repo) self.create_plan(project) option = ProjectOption(project=project, name='build.file-whitelist', value='foo.txt') mock_vcs = self.get_fake_vcs() mock_vcs.export.side_effect = None mock_vcs.export.return_value = SAMPLE_DIFF mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files( mock_vcs, id) mock_vcs.update.side_effect = None mock_identify_revision.return_value = revision mock_get_vcs.return_value = mock_vcs db.session.add(option) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_vcs.export.assert_called_once_with(revision.sha) assert not Build.query.first() option.value = 'ci/*' db.session.add(option) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_identify_revision.assert_called_once_with(repo, revision.sha) assert Build.query.first()
def test_simple(self): project = self.create_project() plan = self.create_plan(project) build = self.create_build(project, result=Result.failed) job = self.create_job(build, result=Result.failed) db.session.add( ItemOption( item_id=plan.id, name='mail.notify-author', value='0', )) db.session.add( ProjectOption( project_id=project.id, name='mail.notify-author', value='1', )) db.session.add( ProjectOption( project_id=project.id, name='mail.notify-addresses', value='*****@*****.**', )) db.session.flush() self.create_job_plan(job, plan) db.session.commit() handler = MailNotificationHandler() assert handler.get_build_options(build) == { 'mail.notify-addresses': {'*****@*****.**'}, 'mail.notify-addresses-revisions': set(), 'mail.notify-author': False, }
def test_get_snapshot_image_dependent(self): project = self.create_project() plan_1 = self.create_plan(project) plan_2 = self.create_plan(project) plan_1.snapshot_plan_id = plan_2.id snapshot = self.create_snapshot(project) db.session.add( ProjectOption( project_id=project.id, name='snapshot.current', value=snapshot.id.hex, )) snapshot_image_1 = self.create_snapshot_image(snapshot, plan_1) snapshot_image_2 = self.create_snapshot_image(snapshot, plan_2) assert snapshot_image_2 == SnapshotImage.get(plan_1, snapshot.id) assert snapshot_image_2 == SnapshotImage.get(plan_2, snapshot.id)
def test_disabled(self, get_vcs): repo = self.create_repo() revision = self.create_revision(repository=repo) project = self.create_project(repository=repo) self.create_plan(project) get_vcs.return_value = self.get_fake_vcs() db.session.add( ProjectOption(project=project, name='build.commit-trigger', value='0')) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) assert not Build.query.first()
def test_associated_snapshot_image(self): project = self.create_project() build = self.create_build(project=project) plan = self.create_plan(project) job = self.create_job(build=build) snapshot = self.create_snapshot(project) image = self.create_snapshot_image(plan=plan, snapshot=snapshot) self.create_option(item_id=plan.id, name='snapshot.allow', value='1') db.session.add( ProjectOption(project_id=project.id, name='snapshot.current', value=snapshot.id.hex)) db.session.commit() self.create_job_plan(job=job, plan=plan, snapshot_id=snapshot.id) phase = self.create_jobphase(job) jobstep = self.create_jobstep(phase) result = serialize(jobstep) assert result['image']['id'] == image.id.hex
def test_when_not_in_whitelist(self, get_vcs): get_vcs.return_value = self.get_fake_vcs() po = ProjectOption( project=self.project, name='build.file-whitelist', value='nonexisting_directory', ) db.session.add(po) db.session.commit() build = self.create_build(project=self.project, source=self.source, status=Status.finished, result=Result.failed) self.create_job(build=build) path = '/api/0/phabricator_diffs/{0}/retry/'.format(self.diff.diff_id) resp = self.client.post(path, follow_redirects=True) assert resp.status_code == 200 data = self.unserialize(resp) assert len(data) == 0
def test_without_image(self): project = self.create_project() build = self.create_build(project) job = self.create_job(build) jobphase = self.create_jobphase(job) jobstep = self.create_jobstep(jobphase) snapshot = self.create_snapshot(project) db.session.add( ProjectOption( project_id=project.id, name='snapshot.current', value=snapshot.id.hex, )) db.session.commit() path = '/api/0/jobsteps/{0}/'.format(jobstep.id.hex) resp = self.client.get(path) assert resp.status_code == 200 data = self.unserialize(resp) assert data['id'] == jobstep.id.hex assert data['snapshot'] is None assert data['expectedSnapshot'] is None
def test_with_patch_without_diffs_enabled(self): repo = self.create_repo() project = self.create_project(repository=repo) self.create_plan(project) self.create_option( item_id=repo.id, name='phabricator.callsign', value='FOO', ) po = ProjectOption( project=project, name='phabricator.diff-trigger', value='0', ) db.session.add(po) db.session.commit() # Default to not creating a build (for tools) resp = self.post_sample_patch() assert resp.status_code == 200, resp.data data = self.unserialize(resp) assert len(data) == 0
def test_when_in_whitelist(self, get_vcs): get_vcs.return_value = self.get_fake_vcs() repo = self.create_repo() project = self.create_project(repository=repo) self.create_plan(project) self.create_option( item_id=repo.id, name='phabricator.callsign', value='FOO', ) po = ProjectOption( project=project, name='build.file-whitelist', value='ci/*', ) db.session.add(po) db.session.commit() resp = self.post_sample_patch() assert resp.status_code == 200, resp.data data = self.unserialize(resp) assert len(data) == 1
def test_multiple_jobs(self): project = self.create_project() build = self.create_build(project, result=Result.failed) job1 = self.create_job(build, result=Result.failed) job2 = self.create_job(build, result=Result.failed) plan1 = self.create_plan(project) plan2 = self.create_plan(project) # Plan1 options. db.session.add( ItemOption( item_id=plan1.id, name='mail.notify-addresses', value='*****@*****.**', )) db.session.add( ItemOption( item_id=plan1.id, name='mail.notify-author', value='0', )) # Plan2 options. db.session.add( ItemOption( item_id=plan2.id, name='mail.notify-addresses', value='*****@*****.**', )) db.session.add( ItemOption( item_id=plan2.id, name='mail.notify-author', value='1', )) # Project options (notify-author is set to test that plan options can # override it). db.session.add( ProjectOption( project_id=project.id, name='mail.notify-author', value='0', )) # Set notify addresses to verify that it is not used when all jobs # override it. db.session.add( ProjectOption( project_id=project.id, name='mail.notify-addresses', value='*****@*****.**', )) db.session.flush() for job, plan in [(job1, plan1), (job2, plan2)]: self.create_job_plan(job, plan) db.session.commit() handler = MailNotificationHandler() assert handler.get_build_options(build) == { 'mail.notify-addresses': {'*****@*****.**', '*****@*****.**'}, 'mail.notify-addresses-revisions': set(), 'mail.notify-author': True, }