Ejemplo n.º 1
0
    def test_failed_logs(self, workflow, source_dir, error_type):
        # just test a random combination of the method inputs and hope it's ok for other
        #   combinations
        session = MockedClientSession('', has_kerberos=True)
        pathinfo = MockedPathInfo('https://koji')

        flexmock(koji, ClientSession=lambda hub, opts: session, PathInfo=pathinfo)
        kwargs = {
            'url': 'https://something.com',
            'smtp_host': 'smtp.bar.com',
            'from_address': '*****@*****.**',
            'to_koji_submitter': True,
            'to_koji_pkgowner': False,
        }

        mock_store_metadata_results(workflow)
        workflow.data.plugins_results[KojiImportPlugin.key] = MOCK_KOJI_BUILD_ID

        mock_dockerfile(workflow)

        smtp_map = {
            'from_address': '*****@*****.**',
            'host': 'smtp.bar.com',
            'send_to_submitter': True,
            'send_to_pkg_owner': False,
        }
        rcm = {'version': 1, 'smtp': smtp_map, 'openshift': {'url': 'https://something.com'}}
        workflow.conf = Configuration(raw_config=rcm)
        add_koji_map_in_workflow(workflow, hub_url='/', root_url='',
                                 ssl_certs_dir='/certs')

        p = SendMailPlugin(workflow, **kwargs)
        _, _, fail_logs = p._render_mail(False, False)
        assert not fail_logs
Ejemplo n.º 2
0
    def test_render_mail(self, workflow, source_dir, caplog,
                         manual_cancel, to_koji_submitter,
                         koji_integration, success, has_repositories,
                         has_store_metadata_results, annotations, empty_repositories,
                         expect_error):
        git_source_url = 'git_source_url'
        git_source_ref = '123423431234123'
        VcsInfo = namedtuple('VcsInfo', ['vcs_type', 'vcs_url', 'vcs_ref'])
        session = MockedClientSession('', has_kerberos=True)
        pathinfo = MockedPathInfo('https://koji')
        (flexmock(pathinfo)
            .should_receive('work')
            .and_raise(RuntimeError, "xyz"))

        flexmock(koji, ClientSession=lambda hub, opts: session, PathInfo=pathinfo)
        kwargs = {
            'url': 'https://something.com',
            'smtp_host': 'smtp.bar.com',
            'from_address': '*****@*****.**',
            'to_koji_submitter': to_koji_submitter,
            'to_koji_pkgowner': False
        }

        workflow.data.plugins_results[KojiImportPlugin.key] = MOCK_KOJI_BUILD_ID
        workflow.user_params['koji_task_id'] = MOCK_KOJI_TASK_ID

        mock_dockerfile(workflow)

        flexmock(workflow.source, get_vcs_info=VcsInfo(vcs_type='git',
                                                       vcs_url=git_source_url,
                                                       vcs_ref=git_source_ref))

        MockEnv(workflow).mock_build_outcome(failed=True, cancelled=manual_cancel)

        if has_store_metadata_results:
            if annotations:
                if empty_repositories:
                    mock_store_metadata_results(workflow, {'repositories': {}})
                else:
                    mock_store_metadata_results(workflow)
                if not has_repositories:
                    result = workflow.data.plugins_results[StoreMetadataPlugin.key]
                    del result['annotations']['repositories']
            else:
                mock_store_metadata_results(workflow, {})

        smtp_map = {
            'from_address': '*****@*****.**',
            'host': 'smtp.bar.com',
            'send_to_submitter': to_koji_submitter,
            'send_to_pkg_owner': False,
        }
        rcm = {'version': 1, 'smtp': smtp_map, 'openshift': {'url': 'https://something.com'}}
        workflow.conf = Configuration(raw_config=rcm)
        add_koji_map_in_workflow(workflow,
                                 hub_url='/' if koji_integration else None,
                                 root_url='https://koji/',
                                 ssl_certs_dir='/certs')

        p = SendMailPlugin(workflow, **kwargs)

        # Submitter is updated in _get_receivers_list
        try:
            p._get_receivers_list()
        except RuntimeError as ex:
            # Only valid exception is a RuntimeError when there are no
            # recipients available
            assert str(ex) == 'No recipients found'

        if expect_error:
            with pytest.raises(ValueError):
                p._render_mail(success, manual_cancel)
            return

        subject, body, logs = p._render_mail(success, manual_cancel)

        if manual_cancel:
            status = 'Canceled'
            assert not logs
        elif success:
            status = 'Succeeded'
            assert not logs
        else:
            status = 'Failed'

        if not has_repositories or empty_repositories:
            exp_subject = '%s building image %s' % (status, MOCK_NAME_LABEL)
            exp_body = [
                'Image Name: ' + MOCK_NAME_LABEL,
                'Repositories: ',
                ]
        else:
            exp_subject = '%s building image foo/bar' % status
            exp_body = [
                'Image Name: foo/bar',
                'Repositories: ',
                '    foo/bar:baz',
                '    foo/bar:spam',
            ]

        result = workflow.data.plugins_results[StoreMetadataPlugin.key]
        if 'repositories' not in result['annotations']:
            assert "repositories is not included in annotations" in caplog.text

        common_body = [
            'Status: ' + status,
            'Submitted by: ',
            'Task id: ' + str(MOCK_KOJI_TASK_ID),
            'Source url: ' + git_source_url,
            'Source ref: ' + git_source_ref,
        ]
        exp_body.extend(common_body)

        if koji_integration and to_koji_submitter:
            exp_body[-4] += MOCK_KOJI_SUBMITTER_EMAIL
        else:
            exp_body[-4] += SendMailPlugin.DEFAULT_SUBMITTER

        if koji_integration:
            exp_body.insert(-2, "Logs: <not available>")

        assert subject == exp_subject
        assert body == '\n'.join(exp_body)