tests: another smattering of 3.x type/API fixes.

pull/295/head
David Wilson 7 years ago
parent 0422a8c263
commit 6cbf34d5fe

@ -99,6 +99,9 @@ def run_with_router(func, *args, **kwargs):
def with_router(func): def with_router(func):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
return run_with_router(func, *args, **kwargs) return run_with_router(func, *args, **kwargs)
if mitogen.core.PY3:
wrapper.func_name = func.__name__
else:
wrapper.func_name = func.func_name wrapper.func_name = func.func_name
return wrapper return wrapper

@ -12,7 +12,7 @@ class MyError(Exception):
def get_sentinel_value(): def get_sentinel_value():
# Some proof we're even talking to the mitogen-test Docker image # Some proof we're even talking to the mitogen-test Docker image
return open('/etc/sentinel').read() return open('/etc/sentinel').read().decode()
def add(x, y): def add(x, y):

@ -30,7 +30,7 @@ class GoodModulesTest(testlib.RouterMixin, unittest2.TestCase):
# Ensure a program composed of a single script can be imported # Ensure a program composed of a single script can be imported
# successfully. # successfully.
args = [sys.executable, testlib.data_path('self_contained_program.py')] args = [sys.executable, testlib.data_path('self_contained_program.py')]
output = testlib.subprocess__check_output(args) output = testlib.subprocess__check_output(args).decode()
self.assertEquals(output, "['__main__', 50]\n") self.assertEquals(output, "['__main__', 50]\n")
@ -45,7 +45,7 @@ class BrokenModulesTest(unittest2.TestCase):
router.stream_by_id = lambda n: stream router.stream_by_id = lambda n: stream
msg = mitogen.core.Message( msg = mitogen.core.Message(
data='non_existent_module', data=mitogen.core.b('non_existent_module'),
reply_to=50, reply_to=50,
) )
msg.router = router msg.router = router
@ -74,7 +74,7 @@ class BrokenModulesTest(unittest2.TestCase):
router.stream_by_id = lambda n: stream router.stream_by_id = lambda n: stream
msg = mitogen.core.Message( msg = mitogen.core.Message(
data='six_brokenpkg._six', data=mitogen.core.b('six_brokenpkg._six'),
reply_to=50, reply_to=50,
) )
msg.router = router msg.router = router

@ -1,4 +1,3 @@
import StringIO
import logging import logging
import subprocess import subprocess
import time import time
@ -146,7 +145,7 @@ class PolicyTest(testlib.RouterMixin, testlib.TestCase):
# Verify CallError received by reply_to target. # Verify CallError received by reply_to target.
e = self.assertRaises(mitogen.core.CallError, e = self.assertRaises(mitogen.core.CallError,
lambda: reply_target.get().unpickle()) lambda: reply_target.get().unpickle())
self.assertEquals(e[0], self.router.refused_msg) self.assertEquals(e.args[0], self.router.refused_msg)
class CrashTest(testlib.BrokerMixin, unittest2.TestCase): class CrashTest(testlib.BrokerMixin, unittest2.TestCase):

@ -83,7 +83,7 @@ class PermissionTest(testlib.RouterMixin, testlib.TestCase):
exc = self.assertRaises(mitogen.core.CallError, lambda: exc = self.assertRaises(mitogen.core.CallError, lambda:
l2.call(call_service_in, l1, MyService.name(), 'privileged_op')) l2.call(call_service_in, l1, MyService.name(), 'privileged_op'))
msg = mitogen.service.Invoker.unauthorized_msg % ( msg = mitogen.service.Invoker.unauthorized_msg % (
'privileged_op', u'privileged_op',
MyService.name(), MyService.name(),
) )
self.assertTrue(msg in exc.args[0]) self.assertTrue(msg in exc.args[0])

@ -58,7 +58,7 @@ class SshTest(testlib.DockerMixin, unittest2.TestCase):
except mitogen.ssh.PasswordError: except mitogen.ssh.PasswordError:
e = sys.exc_info()[1] e = sys.exc_info()[1]
self.assertEqual(e[0], self.stream_class.password_required_msg) self.assertEqual(e.args[0], self.stream_class.password_required_msg)
def test_password_incorrect(self): def test_password_incorrect(self):
try: try:
@ -70,7 +70,7 @@ class SshTest(testlib.DockerMixin, unittest2.TestCase):
except mitogen.ssh.PasswordError: except mitogen.ssh.PasswordError:
e = sys.exc_info()[1] e = sys.exc_info()[1]
self.assertEqual(e[0], self.stream_class.password_incorrect_msg) self.assertEqual(e.args[0], self.stream_class.password_incorrect_msg)
def test_password_specified(self): def test_password_specified(self):
context = self.docker_ssh( context = self.docker_ssh(
@ -92,7 +92,7 @@ class SshTest(testlib.DockerMixin, unittest2.TestCase):
except mitogen.ssh.PasswordError: except mitogen.ssh.PasswordError:
e = sys.exc_info()[1] e = sys.exc_info()[1]
self.assertEqual(e[0], self.stream_class.password_required_msg) self.assertEqual(e.args[0], self.stream_class.password_required_msg)
def test_pubkey_specified(self): def test_pubkey_specified(self):
context = self.docker_ssh( context = self.docker_ssh(

@ -214,7 +214,7 @@ class DockerizedSshDaemon(object):
def _get_container_port(self): def _get_container_port(self):
s = subprocess__check_output(['docker', 'port', self.container_name]) s = subprocess__check_output(['docker', 'port', self.container_name])
for line in s.splitlines(): for line in s.decode().splitlines():
dport, proto, baddr, bport = self.PORT_RE.match(line).groups() dport, proto, baddr, bport = self.PORT_RE.match(line).groups()
if dport == '22' and proto == 'tcp': if dport == '22' and proto == 'tcp':
self.port = int(bport) self.port = int(bport)
@ -291,7 +291,7 @@ class DockerMixin(RouterMixin):
kwargs.setdefault('hostname', self.dockerized_ssh.host) kwargs.setdefault('hostname', self.dockerized_ssh.host)
kwargs.setdefault('port', self.dockerized_ssh.port) kwargs.setdefault('port', self.dockerized_ssh.port)
kwargs.setdefault('check_host_keys', 'ignore') kwargs.setdefault('check_host_keys', 'ignore')
kwargs.setdefault('ssh_debug_level', '3') kwargs.setdefault('ssh_debug_level', 3)
return self.router.ssh(**kwargs) return self.router.ssh(**kwargs)
def docker_ssh_any(self, **kwargs): def docker_ssh_any(self, **kwargs):

@ -1,16 +1,22 @@
import cStringIO try:
from io import StringIO
from io import BytesIO
except ImportError:
from StringIO import StringIO as StringIO
from StringIO import StringIO as BytesIO
import unittest2 import unittest2
import mitogen.core import mitogen.core
from mitogen.core import b
class BlobTest(unittest2.TestCase): class BlobTest(unittest2.TestCase):
klass = mitogen.core.Blob klass = mitogen.core.Blob
def make(self): def make(self):
return self.klass('x' * 128) return self.klass(b('x') * 128)
def test_repr(self): def test_repr(self):
blob = self.make() blob = self.make()
@ -18,14 +24,14 @@ class BlobTest(unittest2.TestCase):
def test_decays_on_constructor(self): def test_decays_on_constructor(self):
blob = self.make() blob = self.make()
self.assertEquals('x'*128, mitogen.core.BytesType(blob)) self.assertEquals(b('x')*128, mitogen.core.BytesType(blob))
def test_decays_on_write(self): def test_decays_on_write(self):
blob = self.make() blob = self.make()
io = cStringIO.StringIO() io = BytesIO()
io.write(blob) io.write(blob)
self.assertEquals(128, io.tell()) self.assertEquals(128, io.tell())
self.assertEquals('x'*128, io.getvalue()) self.assertEquals(b('x')*128, io.getvalue())
def test_message_roundtrip(self): def test_message_roundtrip(self):
blob = self.make() blob = self.make()
@ -53,7 +59,7 @@ class SecretTest(unittest2.TestCase):
def test_decays_on_write(self): def test_decays_on_write(self):
secret = self.make() secret = self.make()
io = cStringIO.StringIO() io = StringIO()
io.write(secret) io.write(secret)
self.assertEquals(8, io.tell()) self.assertEquals(8, io.tell())
self.assertEquals('password', io.getvalue()) self.assertEquals('password', io.getvalue())
@ -64,8 +70,8 @@ class SecretTest(unittest2.TestCase):
secret2 = msg.unpickle() secret2 = msg.unpickle()
self.assertEquals(type(secret), type(secret2)) self.assertEquals(type(secret), type(secret2))
self.assertEquals(repr(secret), repr(secret2)) self.assertEquals(repr(secret), repr(secret2))
self.assertEquals(mitogen.core.BytesType(secret), self.assertEquals(mitogen.core.b(secret),
mitogen.core.BytesType(secret2)) mitogen.core.b(secret2))
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save