def test_attempt(self): env = Envelope() m = PipeRelay(["relaytest"]) self.mox.StubOutWithMock(m, "_exec_process") m._exec_process(env).AndReturn((0, "", "")) self.mox.ReplayAll() m.attempt(env, 0)
def test_attempt(self): self.mox.StubOutWithMock(subprocess, 'Popen') env = Envelope('*****@*****.**', ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\r\n') self._mock_popen('*****@*****.**', 0, '') self._mock_popen('*****@*****.**', 1337, 'transient') self._mock_popen('*****@*****.**', 1337, '5.0.0 permanent') subprocess.Popen(['relaytest', '-f', '*****@*****.**', '*****@*****.**'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).AndRaise(Timeout) self.mox.ReplayAll() m = PipeRelay(['relaytest', '-f', '{sender}', '{recipient}']) results = m.attempt(env, 0) self.assertEqual(4, len(results)) self.assertEqual(None, results['*****@*****.**']) self.assertIsInstance(results['*****@*****.**'], TransientRelayError) self.assertEqual('transient', str(results['*****@*****.**'])) self.assertEqual('450', results['*****@*****.**'].reply.code) self.assertIsInstance(results['*****@*****.**'], PermanentRelayError) self.assertEqual('5.0.0 permanent', str(results['*****@*****.**'])) self.assertEqual('550', results['*****@*****.**'].reply.code) self.assertIsInstance(results['*****@*****.**'], TransientRelayError) self.assertEqual('Delivery timed out', str(results['*****@*****.**'])) self.assertEqual('450', results['*****@*****.**'].reply.code)
def test_attempt(self): env = Envelope() m = PipeRelay(['relaytest']) self.mox.StubOutWithMock(m, '_exec_process') m._exec_process(env).AndReturn((0, '', '')) self.mox.ReplayAll() m.attempt(env, 0)
def test_attempt_permanentfail(self): env = Envelope() m = PipeRelay(['relaytest']) self.mox.StubOutWithMock(m, '_exec_process') m._exec_process(env).AndReturn((13, '5.0.0 permanent failure', '')) self.mox.ReplayAll() with assert_raises(PermanentRelayError): m.attempt(env, 0)
def test_attempt_timeout(self): env = Envelope() m = PipeRelay(['relaytest']) self.mox.StubOutWithMock(m, '_exec_process') m._exec_process(env).AndRaise(Timeout) self.mox.ReplayAll() with assert_raises(TransientRelayError): m.attempt(env, 0)
def test_attempt_transientfail(self): env = Envelope() m = PipeRelay(['relaytest']) self.mox.StubOutWithMock(m, '_exec_process') m._exec_process(env).AndReturn((1337, 'transient failure', '')) self.mox.ReplayAll() with self.assertRaises(TransientRelayError): m.attempt(env, 0)
def test_attempt_permanentfail(self): env = Envelope() m = PipeRelay(["relaytest"]) self.mox.StubOutWithMock(m, "_exec_process") m._exec_process(env).AndReturn((13, "5.0.0 permanent failure", "")) self.mox.ReplayAll() with self.assertRaises(PermanentRelayError): m.attempt(env, 0)
def test_exec_process_error(self): pmock = self.mox.CreateMock(subprocess.Popen) self.mox.StubOutWithMock(subprocess, 'Popen') env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test\r\n') subprocess.Popen(['relaytest', '-f', '*****@*****.**'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).AndReturn(pmock) pmock.communicate('From: [email protected]\r\n\r\ntest test\r\n').AndReturn(('', '')) pmock.pid = -1 pmock.returncode = 1337 self.mox.ReplayAll() m = PipeRelay(['relaytest', '-f', '{sender}']) status, stdout, stderr = m._exec_process(env) assert_equal(1337, status) assert_equal('', stdout) assert_equal('', stderr)
def test_exec_process_error(self): pmock = self.mox.CreateMock(subprocess.Popen) self.mox.StubOutWithMock(subprocess, "Popen") env = Envelope("*****@*****.**", ["*****@*****.**"]) env.parse("From: [email protected]\r\n\r\ntest test\r\n") subprocess.Popen( ["relaytest", "-f", "*****@*****.**"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ).AndReturn(pmock) pmock.communicate("From: [email protected]\r\n\r\ntest test\r\n").AndReturn(("", "")) pmock.pid = -1 pmock.returncode = 1337 self.mox.ReplayAll() m = PipeRelay(["relaytest", "-f", "{sender}"]) status, stdout, stderr = m._exec_process(env) self.assertEqual(1337, status) self.assertEqual("", stdout) self.assertEqual("", stderr)
def test_attempt_permanentfail(self): env = Envelope() m = PipeRelay(['relaytest']) self.mox.StubOutWithMock(m, '_exec_process') m._exec_process(env).AndReturn((13, '5.0.0 permanent failure', '')) self.mox.ReplayAll() with self.assertRaises(PermanentRelayError): m.attempt(env, 0)
def test_attempt_timeout(self): env = Envelope() m = PipeRelay(['relaytest']) self.mox.StubOutWithMock(m, '_exec_process') m._exec_process(env).AndRaise(Timeout) self.mox.ReplayAll() with self.assertRaises(TransientRelayError): m.attempt(env, 0)
def test_attempt_transientfail(self): env = Envelope() m = PipeRelay(['relaytest']) self.mox.StubOutWithMock(m, '_exec_process') m._exec_process(env).AndReturn((1337, 'transient failure', '')) self.mox.ReplayAll() with assert_raises(TransientRelayError): m.attempt(env, 0)
def _start_inbound_relay(args): from slimta.relay.pipe import PipeRelay relay = PipeRelay(['tee', '{message_id}.eml']) return relay
def _start_relay(self, name, options=None): if name in self.relays: return self.relays[name] if not options: options = getattr(self.cfg.relay, name) new_relay = None if options.type == 'mx': from slimta.relay.smtp.mx import MxSmtpRelay from .helpers import fill_hostname_template kwargs = {} kwargs['connect_timeout'] = options.get('connect_timeout', 30) kwargs['command_timeout'] = options.get('command_timeout', 30) kwargs['data_timeout'] = options.get('data_timeout', 60) kwargs['idle_timeout'] = options.get('idle_timeout', 10) kwargs['pool_size'] = options.get('concurrent_connections', 5) kwargs['ehlo_as'] = fill_hostname_template(options.ehlo_as) kwargs['context'] = self._get_client_ssl_context(options.tls) if options.ipv4_only: kwargs['socket_creator'] = build_ipv4_socket_creator([25]) new_relay = MxSmtpRelay(**kwargs) if 'force_mx' in options: for domain, dest in options.force_mx: new_relay.force_mx(domain, dest) elif options.type == 'static': from slimta.relay.smtp.static import StaticSmtpRelay from .helpers import fill_hostname_template, get_relay_credentials kwargs = {} kwargs['host'] = options.host kwargs['port'] = options.get('port', 25) kwargs['connect_timeout'] = options.get('connect_timeout', 30) kwargs['command_timeout'] = options.get('command_timeout', 30) kwargs['data_timeout'] = options.get('data_timeout', 60) kwargs['idle_timeout'] = options.get('idle_timeout', 10) kwargs['pool_size'] = options.get('concurrent_connections', 5) kwargs['ehlo_as'] = fill_hostname_template(options.ehlo_as) kwargs['context'] = self._get_client_ssl_context(options.tls) if 'credentials' in options: credentials = get_relay_credentials(options.credentials) kwargs['credentials'] = credentials if options.ipv4_only: kwargs['socket_creator'] = \ build_ipv4_socket_creator([kwargs['port']]) new_relay = StaticSmtpRelay(**kwargs) elif options.type == 'lmtp': from slimta.relay.smtp.static import StaticLmtpRelay from .helpers import fill_hostname_template, get_relay_credentials kwargs = {} kwargs['host'] = options.get('host', 'localhost') kwargs['port'] = options.get('port', 24) kwargs['connect_timeout'] = options.get('connect_timeout', 30) kwargs['command_timeout'] = options.get('command_timeout', 30) kwargs['data_timeout'] = options.get('data_timeout', 60) kwargs['idle_timeout'] = options.get('idle_timeout', 10) kwargs['pool_size'] = options.get('concurrent_connections', 5) kwargs['ehlo_as'] = fill_hostname_template(options.ehlo_as) kwargs['context'] = self._get_client_ssl_context(options.tls) if 'credentials' in options: credentials = get_relay_credentials(options.credentials) kwargs['credentials'] = credentials if options.ipv4_only: kwargs['socket_creator'] = \ build_ipv4_socket_creator([kwargs['port']]) new_relay = StaticLmtpRelay(**kwargs) elif options.type == 'http': from slimta.relay.http import HttpRelay from .helpers import fill_hostname_template kwargs = {} kwargs['ehlo_as'] = fill_hostname_template(options.ehlo_as) kwargs['timeout'] = options.get('timeout', 60) kwargs['idle_timeout'] = options.get('idle_timeout', 10) kwargs['context'] = self._get_client_ssl_context(options.tls) new_relay = HttpRelay(options.url, **kwargs) elif options.type == 'blackhole': from slimta.relay.blackhole import BlackholeRelay new_relay = BlackholeRelay() elif options.type == 'pipe': from slimta.relay.pipe import PipeRelay new_relay = PipeRelay(options.args) elif options.type == 'maildrop': from slimta.relay.pipe import MaildropRelay path = options.path new_relay = MaildropRelay(path) elif options.type == 'dovecot': from slimta.relay.pipe import DovecotLdaRelay path = options.path new_relay = DovecotLdaRelay(path) elif options.type == 'custom': new_relay = custom_factory(options) else: msg = 'relay type does not exist: '+options.type raise ConfigValidationError(msg) self.relays[name] = new_relay return new_relay