Beispiel #1
0
    def test_simple(self):
        project = self.project
        plan = self.create_plan()
        plan.projects.append(project)
        build = self.create_build(project)
        job = self.create_job(build)
        self.create_job_plan(job, plan)

        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.commit()

        handler = MailNotificationHandler()
        assert handler.get_job_options(job) == {
            'mail.notify-addresses': '*****@*****.**',
            'mail.notify-author': '0',
        }
Beispiel #2
0
    def test_with_addressees(self):
        db.session.add(
            ProjectOption(project=self.project,
                          name='mail.notify-author',
                          value='1'))
        db.session.add(
            ProjectOption(project=self.project,
                          name='mail.notify-addresses',
                          value='[email protected], [email protected]'))

        author = self.create_author('*****@*****.**')
        build = self.create_build(self.project,
                                  result=Result.failed,
                                  author=author)
        job = self.create_job(build)
        db.session.commit()

        handler = MailNotificationHandler()
        recipients = handler.get_recipients(job)

        assert recipients == [
            '{0} <*****@*****.**>'.format(author.name),
            '*****@*****.**',
            '*****@*****.**',
        ]
Beispiel #3
0
    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 = Mock()
        mock_vcs.export.return_value = SAMPLE_DIFF
        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()
Beispiel #4
0
    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 = Mock()
        mock_vcs.export.return_value = SAMPLE_DIFF
        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()
Beispiel #5
0
    def test_with_revision_addressees(self):
        db.session.add(
            ProjectOption(project=self.project,
                          name='mail.notify-author',
                          value='1'))
        db.session.add(
            ProjectOption(project=self.project,
                          name='mail.notify-addresses-revisions',
                          value='[email protected], [email protected]'))

        author = self.create_author('*****@*****.**')
        patch = Patch(
            repository=self.repo,
            project=self.project,
            label='foo',
            diff='',
        )
        source = self.create_source(self.project, patch=patch)
        build = self.create_build(
            project=self.project,
            source=source,
            author=author,
            result=Result.failed,
        )
        job = self.create_job(build=build)
        db.session.commit()

        handler = MailNotificationHandler()
        recipients = handler.get_recipients(job)

        assert recipients == ['{0} <*****@*****.**>'.format(author.name)]

        build = self.create_build(
            project=self.project,
            result=Result.failed,
            author=author,
        )
        job = self.create_job(build=build)

        job_finished_handler(job)

        handler = MailNotificationHandler()
        recipients = handler.get_recipients(job)

        assert recipients == [
            '{0} <*****@*****.**>'.format(author.name),
            '*****@*****.**',
            '*****@*****.**',
        ]
Beispiel #6
0
    def test_dependent_snapshot(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)
        self.create_job_plan(job, plan_1)
        snapshot = self.create_snapshot(project)
        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()

        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
Beispiel #7
0
    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)
        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)
        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_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
Beispiel #9
0
    def test_when_in_whitelist_commit_build_false(self, get_vcs):
        po = ProjectOption(
            project=self.project,
            name='build.file-whitelist',
            value='nonexisting_directory',
        )
        db.session.add(po)
        db.session.commit()
        get_vcs.return_value = self.get_fake_vcs()
        revision = self.create_revision(repository=self.project.repository,
                                        sha='a' * 40)
        resp = self.client.post(self.path,
                                data={
                                    'sha': 'a' * 40,
                                    'repository': self.project.repository.url,
                                    'apply_project_files_trigger': '',
                                    'ensure_only': '1',
                                })
        assert resp.status_code == 200
        data = self.unserialize(resp)
        assert len(data) == 1
        assert data[0]['id']

        job = Job.query.filter(Job.build_id == data[0]['id']).first()
        build = job.build
        source = build.source

        assert job.project == self.project
        assert source.revision_sha == revision.sha
    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
Beispiel #11
0
    def test_when_in_whitelist(self):
        po = ProjectOption(
            project=self.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
Beispiel #12
0
    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)
Beispiel #13
0
    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()
Beispiel #14
0
    def test_when_in_whitelist_diff_build_default_true_negative(self, get_vcs):
        po = ProjectOption(
            project=self.project,
            name='build.file-whitelist',
            value='nonexisting_directory',
        )
        db.session.add(po)
        db.session.commit()
        get_vcs.return_value = self.get_fake_vcs()

        resp = self.post_sample_patch()
        assert resp.status_code == 200, resp.data
        data = self.unserialize(resp)
        assert len(data) == 0
Beispiel #15
0
    def test_without_author_option(self):
        project = self.create_project()
        db.session.add(
            ProjectOption(project=project,
                          name='mail.notify-author',
                          value='0'))
        author = self.create_author('*****@*****.**')
        build = self.create_build(project, result=Result.failed, author=author)
        job = self.create_job(build)
        db.session.commit()

        handler = MailNotificationHandler()
        recipients = handler.get_recipients(job)

        assert recipients == []
Beispiel #16
0
    def test_when_in_whitelist_diff_build(self, get_vcs):
        po = ProjectOption(
            project=self.project,
            name='build.file-whitelist',
            value='ci/*',
        )
        db.session.add(po)
        db.session.commit()
        get_vcs.return_value = self.get_fake_vcs()

        resp = self.post_sample_patch({
            'apply_project_files_trigger': '1',
        })
        assert resp.status_code == 200, resp.data
        data = self.unserialize(resp)
        assert len(data) == 1
Beispiel #17
0
    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)
Beispiel #18
0
    def test_disabled(self):
        repo = self.create_repo()
        revision = self.create_revision(repository=repo)
        project = self.create_project(repository=repo)
        plan = self.create_plan()
        plan.projects.append(project)

        db.session.add(
            ProjectOption(project=project,
                          name='build.commit-trigger',
                          value='0'))
        db.session.commit()

        revision_created_handler(revision_sha=revision.sha,
                                 repository_id=repo.id)

        assert not Build.query.first()
Beispiel #19
0
    def test_missing_test_results_and_expected(self, get_implementation,
                                               queue_delay):
        implementation = mock.Mock()
        get_implementation.return_value = implementation

        def mark_finished(step):
            step.status = Status.finished
            step.result = Result.passed

        implementation.update_step.side_effect = mark_finished

        build = self.create_build(project=self.project)
        job = self.create_job(build=build)

        plan = self.create_plan()
        self.create_step(plan, implementation='test', order=0)
        self.create_job_plan(job, plan)

        phase = self.create_jobphase(job)
        step = self.create_jobstep(phase)

        db.session.add(
            ProjectOption(project_id=self.project.id,
                          name='build.expect-tests',
                          value='1'))
        db.session.commit()

        sync_job_step(
            step_id=step.id.hex,
            task_id=step.id.hex,
            parent_task_id=job.id.hex,
        )

        db.session.expire(step)

        step = JobStep.query.get(step.id)

        assert step.status == Status.finished
        assert step.result == Result.failed

        stat = ItemStat.query.filter(
            ItemStat.item_id == step.id,
            ItemStat.name == 'tests_missing',
        ).first()
        assert stat.value == 1
Beispiel #20
0
    def test_with_patch_without_diffs_enabled(self):
        po = ProjectOption(
            project=self.project,
            name='build.allow-patches',
            value='0',
        )
        db.session.add(po)
        db.session.commit()

        resp = self.client.post(self.path, data={
            'sha': 'a' * 40,
            'project': self.project.slug,
            'patch': (StringIO(SAMPLE_DIFF), 'foo.diff'),
            'patch[label]': 'D1234',
        })
        assert resp.status_code == 200, resp.data
        data = self.unserialize(resp)
        assert len(data) == 0
Beispiel #21
0
 def test_when_not_in_whitelist_commit_build(self, get_vcs):
     po = ProjectOption(
         project=self.project,
         name='build.file-whitelist',
         value='nonexisting_directory',
     )
     db.session.add(po)
     db.session.commit()
     get_vcs.return_value = self.get_fake_vcs()
     self.create_revision(repository=self.project.repository, sha='a' * 40)
     resp = self.client.post(self.path,
                             data={
                                 'apply_project_files_trigger': '1',
                                 'sha': 'a' * 40,
                                 'repository': self.project.repository.url
                             })
     assert resp.status_code == 200
     data = self.unserialize(resp)
     assert len(data) == 0
Beispiel #22
0
    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
Beispiel #23
0
    def test_when_in_whitelist(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='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_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_when_not_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='nonexisting_directory',
        )
        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) == 0
    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
Beispiel #27
0
 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