def test_run_ok_and_send(self, workflow): MockEnv(workflow).mock_build_outcome(failed=True) class SMTP(object): def sendmail(self, from_addr, to, msg): pass def quit(self): pass smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.spam.com', } 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') receivers = ['*****@*****.**', '*****@*****.**'] p = SendMailPlugin(workflow, from_address='*****@*****.**', smtp_host='smtp.spam.com', send_on=[]) (flexmock(p).should_receive('_should_send') .with_args(False, False).and_return(True)) flexmock(p).should_receive('_get_receivers_list').and_return(receivers) flexmock(p).should_receive('_get_image_name_and_repos').and_return(('foobar', ['foo/bar:baz', 'foo/bar:spam'])) smtp_inst = SMTP() flexmock(smtplib).should_receive('SMTP').and_return(smtp_inst) p.run()
def test_run_invalid_receivers(self, caplog, workflow): MockEnv(workflow).mock_build_outcome(failed=True) error_addresses = ['*****@*****.**'] mock_store_metadata_results(workflow) smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.bar.com', 'error_addresses': ['*****@*****.**'], } 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, from_address='*****@*****.**', smtp_host='smtp.spam.com', send_on=[], error_addresses=error_addresses) (flexmock(p).should_receive('_should_send') .with_args(False, False).and_return(True)) flexmock(p).should_receive('_get_receivers_list').and_return([]) flexmock(p).should_receive('_get_image_name_and_repos').and_return(('foobar', ['foo/bar:baz', 'foo/bar:spam'])) p.run() assert 'no valid addresses in requested addresses. Doing nothing' in caplog.text
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
def test_run_ok(self, tmpdir, workflow, source_dir): MockEnv(workflow).mock_build_outcome(failed=True) receivers = ['*****@*****.**', '*****@*****.**'] mock_dockerfile(workflow) mock_store_metadata_results(workflow) smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.bar.com', } 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, from_address='*****@*****.**', smtp_host='smtp.spam.com', send_on=[]) (flexmock(p).should_receive('_should_send') .with_args(False, False).and_return(True)) flexmock(p).should_receive('_get_receivers_list').and_return(receivers) flexmock(p).should_receive('_send_mail').with_args(receivers, str, str, None) p.run()
def test_get_receiver_list(self, additional_addresses, expected_receivers, workflow): 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': '*****@*****.**', 'additional_addresses': additional_addresses } workflow.data.plugins_results[KojiImportPlugin.key] = MOCK_KOJI_BUILD_ID smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.bar.com', 'send_to_submitter': False, 'send_to_pkg_owner': False, 'additional_addresses': additional_addresses } 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=None, root_url='https://koji/', ssl_certs_dir='/certs') p = SendMailPlugin(workflow, **kwargs) if expected_receivers is not None: assert sorted(expected_receivers) == sorted(p._get_receivers_list()) else: with pytest.raises(RuntimeError) as ex: p._get_receivers_list() assert str(ex.value) == 'No recipients found'
def test_get_email_from_koji_obj(self, has_kerberos, email_domain, expected_email, workflow): session = MockedClientSession('', has_kerberos=has_kerberos) flexmock(koji, ClientSession=lambda hub, opts: session) workflow.data.plugins_results[KojiImportPlugin.key] = MOCK_KOJI_BUILD_ID smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.bar.com', 'domain': email_domain, } 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, email_domain=email_domain) koji_task_owner = get_koji_task_owner(p.session, p.koji_task_id) try: found_email = p._get_email_from_koji_obj(koji_task_owner) assert expected_email == found_email except RuntimeError as exc: if not email_domain: assert str(exc) == "Empty email_domain specified" else: assert str(exc) == "Koji task owner name is missing"
def test_koji_recepients_exception(self, exception_location, expected_receivers, workflow): if exception_location == 'empty_owner': koji_build_id = None else: koji_build_id = MOCK_KOJI_BUILD_ID has_kerberos = exception_location != 'empty_email_domain' session = MockedClientSession('', has_kerberos=has_kerberos) if exception_location == 'koji_connection': (flexmock(session) .should_receive('ssl_login') .and_raise(RuntimeError, "xyz")) elif exception_location == 'submitter': (flexmock(session) .should_receive('getTaskInfo') .and_raise(RuntimeError, "xyz")) elif exception_location == 'owner': (flexmock(session) .should_receive('getPackageConfig') .and_raise(RuntimeError, "xyz")) flexmock(koji, ClientSession=lambda hub, opts: session, PathInfo=MockedPathInfo) kwargs = { 'url': 'https://something.com', 'smtp_host': 'smtp.bar.com', 'from_address': '*****@*****.**', 'to_koji_submitter': True, 'to_koji_pkgowner': True } smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.bar.com', 'send_to_submitter': True, 'send_to_pkg_owner': True, } if exception_location != 'empty_email_domain': kwargs['email_domain'] = MOCK_EMAIL_DOMAIN smtp_map['domain'] = MOCK_EMAIL_DOMAIN workflow.data.plugins_results[KojiImportPlugin.key] = koji_build_id if exception_location == 'empty_submitter': workflow.user_params['koji_task_id'] = None else: workflow.user_params['koji_task_id'] = MOCK_KOJI_TASK_ID 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) if not expected_receivers: with pytest.raises(RuntimeError): p._get_receivers_list() else: receivers = p._get_receivers_list() assert sorted(receivers) == sorted(expected_receivers)
def test_skip_plugin(self, caplog, workflow): rcm = {'version': 1, '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) p.run() log_msg = 'no smtp configuration, skipping plugin' assert log_msg in caplog.text
def test_fails_with_unknown_states(self, workflow): smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.spam.com', } 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, smtp_host='smtp.bar.com', from_address='*****@*****.**', send_on=['unknown_state', MS]) with pytest.raises(PluginFailedException) as e: p.run() assert str(e.value) == 'Unknown state(s) "unknown_state" for sendmail plugin'
def test_should_send(self, success, manual_canceled, send_on, expected, workflow): kwargs = { 'smtp_host': 'smtp.bar.com', 'from_address': '*****@*****.**', 'send_on': send_on, } workflow.data.plugins_results[KojiImportPlugin.key] = MOCK_KOJI_BUILD_ID smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.spam.com', } 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) assert p._should_send(success, manual_canceled) == expected
def test_run_does_nothing_if_conditions_not_met(self, workflow): MockEnv(workflow).mock_build_outcome(failed=True) smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.spam.com', } 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, from_address='*****@*****.**', smtp_host='smtp.spam.com', send_on=[MS]) (flexmock(p).should_receive('_should_send') .with_args(False, False).and_return(False)) flexmock(p).should_receive('_get_receivers_list').times(0) flexmock(p).should_receive('_send_mail').times(0) p.run()
def test_send_mail(self, throws_exception, workflow): smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.bar.com', } 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, from_address='*****@*****.**', smtp_host='smtp.spam.com') class SMTP(object): def sendmail(self, from_addr, to, msg): pass def quit(self): pass smtp_inst = SMTP() flexmock(smtplib).should_receive('SMTP').and_return(smtp_inst) sendmail_chain = (flexmock(smtp_inst).should_receive('sendmail'). with_args('*****@*****.**', ['*****@*****.**'], str)) if throws_exception: sendmail_chain.and_raise(smtplib.SMTPException, "foo") flexmock(smtp_inst).should_receive('quit') if throws_exception: with pytest.raises(SMTPException) as e: p._send_mail(['*****@*****.**'], 'subject', 'body') assert str(e.value) == 'foo' else: p._send_mail(['*****@*****.**'], 'subject', 'body')
def test_recepients_from_koji(self, has_addit_address, to_koji_submitter, to_koji_pkgowner, expected_receivers, workflow): session = MockedClientSession('', has_kerberos=True) flexmock(koji, ClientSession=lambda hub, opts: session, PathInfo=MockedPathInfo) kwargs = { 'url': 'https://something.com', 'smtp_host': 'smtp.bar.com', 'from_address': '*****@*****.**', 'to_koji_submitter': to_koji_submitter, 'to_koji_pkgowner': to_koji_pkgowner, 'email_domain': MOCK_EMAIL_DOMAIN } smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.bar.com', 'send_to_submitter': to_koji_submitter, 'send_to_pkg_owner': to_koji_pkgowner, 'domain': MOCK_EMAIL_DOMAIN, } if has_addit_address: kwargs['additional_addresses'] = [MOCK_ADDITIONAL_EMAIL] smtp_map['additional_addresses'] = [MOCK_ADDITIONAL_EMAIL] workflow.data.plugins_results[KojiImportPlugin.key] = MOCK_KOJI_BUILD_ID workflow.user_params['koji_task_id'] = MOCK_KOJI_TASK_ID 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) if not expected_receivers: with pytest.raises(RuntimeError): p._get_receivers_list() else: receivers = p._get_receivers_list() assert sorted(receivers) == sorted(expected_receivers)
def test_generated_email(self, has_kerberos, expected_receivers, workflow): session = MockedClientSession('', has_kerberos=has_kerberos) flexmock(koji, ClientSession=lambda hub, opts: session, PathInfo=MockedPathInfo) kwargs = { 'url': 'https://something.com', 'smtp_host': 'smtp.bar.com', 'from_address': '*****@*****.**', 'to_koji_submitter': True, 'to_koji_pkgowner': True, 'email_domain': MOCK_EMAIL_DOMAIN } smtp_map = { 'from_address': '*****@*****.**', 'host': 'smtp.bar.com', 'send_to_submitter': True, 'send_to_pkg_owner': True, 'domain': MOCK_EMAIL_DOMAIN, } workflow.data.plugins_results[KojiImportPlugin.key] = MOCK_KOJI_BUILD_ID workflow.user_params['koji_task_id'] = MOCK_KOJI_TASK_ID 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) receivers = p._get_receivers_list() assert sorted(receivers) == sorted(expected_receivers) if has_kerberos: assert p.submitter == MOCK_KOJI_SUBMITTER_EMAIL else: assert p.submitter == MOCK_KOJI_SUBMITTER_GENERATED
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)