Merge pull request #65 from moreati/unittest2-this-time-its-personal

Use unittest2 and specific assertX methods
pull/79/head
dw 7 years ago committed by GitHub
commit 5e552e597c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,3 +5,4 @@ docker[tls]==2.5.1
mock==2.0.0
pytest-catchlog==1.2.2
pytest==3.1.2
unittest2=1.1.0

@ -1,6 +1,7 @@
import logging
import time
import unittest
import unittest2
import mitogen.core
import mitogen.master
@ -38,7 +39,7 @@ class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
self.local = self.router.local()
def test_succeeds(self):
assert 3 == self.local.call(function_that_adds_numbers, 1, 2)
self.assertEqual(3, self.local.call(function_that_adds_numbers, 1, 2))
def test_crashes(self):
exc = self.assertRaises(mitogen.core.CallError,
@ -46,13 +47,13 @@ class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
s = str(exc)
etype, _, s = s.partition(': ')
assert etype == 'exceptions.ValueError'
self.assertEqual(etype, 'exceptions.ValueError')
msg, _, s = s.partition('\n')
assert msg == 'exception text'
self.assertEqual(msg, 'exception text')
# Traceback
assert len(s) > 0
self.assertGreater(len(s), 0)
def test_bad_return_value(self):
exc = self.assertRaises(mitogen.core.StreamError,
@ -60,7 +61,7 @@ class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
self.assertEquals(exc[0], "cannot unpickle '__main__'/'CrazyType'")
def test_returns_dead(self):
assert mitogen.core._DEAD == self.local.call(func_returns_dead)
self.assertEqual(mitogen.core._DEAD, self.local.call(func_returns_dead))
def test_aborted_on_local_context_disconnect(self):
stream = self.router._stream_by_id[self.local.context_id]
@ -82,10 +83,10 @@ class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
def test_accepts_returns_context(self):
context = self.local.call(func_accepts_returns_context, self.local)
assert context is not self.local
assert context.context_id == self.local.context_id
assert context.name == self.local.name
self.assertIsNot(context, self.local)
self.assertEqual(context.context_id, self.local.context_id)
self.assertEqual(context.name, self.local.name)
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,4 +1,4 @@
import unittest
import unittest2
import mitogen.core
import testlib
@ -9,12 +9,12 @@ class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
# issue 32
l1 = self.router.local()
chan = mitogen.core.Channel(self.router, l1, 123)
assert chan.router == self.router
assert chan.context == l1
assert chan.dst_handle == 123
assert chan.handle is not None
assert chan.handle > 0
self.assertEqual(chan.router, self.router)
self.assertEqual(chan.context, l1)
self.assertEqual(chan.dst_handle, 123)
self.assertIsNotNone(chan.handle)
self.assertGreater(chan.handle, 0)
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,14 +1,15 @@
import os
import shutil
import unittest
import unittest2
import mitogen.fakessh
import testlib
class RsyncTest(testlib.DockerMixin, unittest.TestCase):
class RsyncTest(testlib.DockerMixin, unittest2.TestCase):
def test_rsync_from_master(self):
context = self.docker_ssh_any()
@ -20,9 +21,9 @@ class RsyncTest(testlib.DockerMixin, unittest.TestCase):
testlib.data_path('.'), 'target:/tmp/data'
])
assert return_code == 0
assert context.call(os.path.exists, '/tmp/data')
assert context.call(os.path.exists, '/tmp/data/simple_pkg/a.py')
self.assertEqual(return_code, 0)
self.assertTrue(context.call(os.path.exists, '/tmp/data'))
self.assertTrue(context.call(os.path.exists, '/tmp/data/simple_pkg/a.py'))
def test_rsync_between_direct_children(self):
# master -> SSH -> has-sudo-pubkey -> rsync(.ssh) -> master ->
@ -51,10 +52,12 @@ class RsyncTest(testlib.DockerMixin, unittest.TestCase):
'rsync', '--progress', '-vvva', '.ssh/', 'target:' + dest_path
])
assert return_code == 0
assert pubkey_acct.call(os.path.getsize, '.ssh/authorized_keys') == \
webapp_acct.call(os.path.getsize, dest_path + '/authorized_keys')
self.assertEqual(return_code, 0)
self.assertEqual(
pubkey_acct.call(os.path.getsize, '.ssh/authorized_keys'),
webapp_acct.call(os.path.getsize, dest_path + '/authorized_keys'),
)
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,6 +1,7 @@
import subprocess
import unittest
import unittest2
import mitogen.master
import testlib
@ -35,8 +36,8 @@ class CommandLineTest(testlib.RouterMixin, testlib.TestCase):
stdout, stderr = proc.communicate()
self.assertEquals(0, proc.returncode)
self.assertEquals("EC0\n", stdout)
self.assertContains("EOFError", stderr)
self.assertIn("EOFError", stderr)
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,5 +1,5 @@
import unittest
import unittest2
import testlib
import id_allocation
@ -9,8 +9,8 @@ class SlaveTest(testlib.RouterMixin, testlib.TestCase):
def test_slave_allocates_id(self):
context = self.router.local()
id_ = context.call(id_allocation.allocate_an_id)
assert id_ == (self.router.id_allocator.next_id - 1)
self.assertEqual(id_, self.router.id_allocator.next_id - 1)
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -2,11 +2,11 @@
import email.utils
import sys
import types
import unittest
import zlib
import mock
import pytest
import unittest2
import mitogen.core
import testlib
@ -39,8 +39,8 @@ class LoadModuleTest(ImporterMixin, testlib.TestCase):
def test_module_added_to_sys_modules(self):
self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname)
self.assertTrue(sys.modules[self.modname] is mod)
self.assertTrue(isinstance(mod, types.ModuleType))
self.assertIs(sys.modules[self.modname], mod)
self.assertIsInstance(mod, types.ModuleType)
def test_module_file_set(self):
self.context.send_await.return_value = self.response
@ -50,12 +50,12 @@ class LoadModuleTest(ImporterMixin, testlib.TestCase):
def test_module_loader_set(self):
self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname)
self.assertTrue(mod.__loader__ is self.importer)
self.assertIs(mod.__loader__, self.importer)
def test_module_package_unset(self):
self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname)
self.assertTrue(mod.__package__ is None)
self.assertIsNone(mod.__package__)
class LoadSubmoduleTest(ImporterMixin, testlib.TestCase):
@ -96,7 +96,7 @@ class LoadModulePackageTest(ImporterMixin, testlib.TestCase):
def test_module_loader_set(self):
self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname)
self.assertTrue(mod.__loader__ is self.importer)
self.assertIs(mod.__loader__, self.importer)
def test_module_path_present(self):
self.context.send_await.return_value = self.response
@ -111,7 +111,7 @@ class LoadModulePackageTest(ImporterMixin, testlib.TestCase):
def test_module_data(self):
self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname)
self.assertTrue(isinstance(mod.func, types.FunctionType))
self.assertIsInstance(mod.func, types.FunctionType)
self.assertEquals(mod.func.__module__, self.modname)
@ -128,4 +128,4 @@ class EmailParseAddrSysTest(testlib.RouterMixin, testlib.TestCase):
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,6 +1,7 @@
import os
import unittest
import unittest2
import mitogen
import mitogen.ssh
@ -10,7 +11,7 @@ import testlib
import plain_old_module
class LocalTest(testlib.RouterMixin, unittest.TestCase):
class LocalTest(testlib.RouterMixin, unittest2.TestCase):
stream_class = mitogen.ssh.Stream
def test_stream_name(self):
@ -20,4 +21,4 @@ class LocalTest(testlib.RouterMixin, unittest.TestCase):
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,13 +1,14 @@
import subprocess
import time
import unittest
import unittest2
import testlib
import mitogen.master
class ScanCodeImportsTest(unittest.TestCase):
class ScanCodeImportsTest(unittest2.TestCase):
func = staticmethod(mitogen.master.scan_code_imports)
def test_simple(self):
@ -15,13 +16,13 @@ class ScanCodeImportsTest(unittest.TestCase):
self.assertEquals(list(self.func(co)), [
(-1, 'subprocess', ()),
(-1, 'time', ()),
(-1, 'unittest', ()),
(-1, 'unittest2', ()),
(-1, 'testlib', ()),
(-1, 'mitogen.master', ()),
])
class IterReadTest(unittest.TestCase):
class IterReadTest(unittest2.TestCase):
func = staticmethod(mitogen.master.iter_read)
def make_proc(self):
@ -35,7 +36,7 @@ class IterReadTest(unittest.TestCase):
try:
reader = self.func(proc.stdout.fileno())
for i, chunk in enumerate(reader, 1):
assert i == int(chunk)
self.assertEqual(i, int(chunk))
if i > 3:
break
finally:
@ -51,7 +52,7 @@ class IterReadTest(unittest.TestCase):
got.append(chunk)
assert 0, 'TimeoutError not raised'
except mitogen.core.TimeoutError:
assert len(got) == 0
self.assertEqual(len(got), 0)
finally:
proc.terminate()
@ -67,12 +68,13 @@ class IterReadTest(unittest.TestCase):
except mitogen.core.TimeoutError:
# Give a little wiggle room in case of imperfect scheduling.
# Ideal number should be 9.
assert 3 < len(got) < 5
self.assertLess(3, len(got))
self.assertLess(len(got), 5)
finally:
proc.terminate()
class WriteAllTest(unittest.TestCase):
class WriteAllTest(unittest2.TestCase):
func = staticmethod(mitogen.master.write_all)
def make_proc(self):
@ -113,4 +115,4 @@ class WriteAllTest(unittest.TestCase):
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,5 +1,6 @@
import inspect
import unittest
import unittest2
import mitogen.master
@ -96,12 +97,12 @@ class GetModuleViaSysModulesTest(testlib.TestCase):
# _socket comes from a .so
import _socket
tup = self.call('_socket')
self.assertEquals(None, tup)
self.assertIsNone(tup)
def test_builtin_fails(self):
# sys is built-in
tup = self.call('sys')
self.assertEquals(None, tup)
self.assertIsNone(tup)
class ResolveRelPathTest(testlib.TestCase):
@ -145,4 +146,4 @@ class FindRelatedImportsTest(testlib.TestCase):
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,5 +1,6 @@
import os
import unittest
import unittest2
import testlib
@ -12,8 +13,8 @@ class NestedTest(testlib.RouterMixin, testlib.TestCase):
context = self.router.local(via=context, name='local%d' % x)
pid = context.call(os.getpid)
self.assertTrue(isinstance(pid, int))
self.assertIsInstance(pid, int)
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,9 +1,10 @@
import mock
import subprocess
import unittest
import sys
import unittest2
import mitogen.master
import testlib
@ -11,7 +12,7 @@ import plain_old_module
import simple_pkg.a
class GoodModulesTest(testlib.RouterMixin, unittest.TestCase):
class GoodModulesTest(testlib.RouterMixin, unittest2.TestCase):
def test_plain_old_module(self):
# The simplest case: a top-level module with no interesting imports or
# package machinery damage.
@ -33,7 +34,7 @@ class GoodModulesTest(testlib.RouterMixin, unittest.TestCase):
self.assertEquals(output, "['__main__', 50]\n")
class BrokenModulesTest(unittest.TestCase):
class BrokenModulesTest(unittest2.TestCase):
def test_obviously_missing(self):
# Ensure we don't crash in the case of a module legitimately being
# unavailable. Should never happen in the real world.
@ -51,7 +52,7 @@ class BrokenModulesTest(unittest.TestCase):
call = router.route.mock_calls[0]
msg, = call[1]
self.assertEquals(50, msg.handle)
self.assertTrue(msg.unpickle() is None)
self.assertIsNone(msg.unpickle())
def test_ansible_six_messed_up_path(self):
# The copy of six.py shipped with Ansible appears in a package whose
@ -74,8 +75,8 @@ class BrokenModulesTest(unittest.TestCase):
call = router.route.mock_calls[0]
msg, = call[1]
self.assertEquals(50, msg.handle)
self.assertTrue(isinstance(msg.unpickle(), tuple))
self.assertIsInstance(msg.unpickle(), tuple)
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,5 +1,6 @@
import unittest
import unittest2
import mitogen.master
import testlib
@ -261,4 +262,4 @@ class GetTest(testlib.RouterMixin, testlib.TestCase):
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -1,15 +1,14 @@
import unittest
import mitogen
import mitogen.ssh
import mitogen.utils
import unittest2
import testlib
import plain_old_module
class FakeSshTest(testlib.RouterMixin, unittest.TestCase):
class FakeSshTest(testlib.RouterMixin, unittest2.TestCase):
def test_okay(self):
context = self.router.ssh(
hostname='hostname',
@ -21,7 +20,7 @@ class FakeSshTest(testlib.RouterMixin, unittest.TestCase):
self.assertEquals(3, context.call(plain_old_module.add, 1, 2))
class SshTest(testlib.DockerMixin, unittest.TestCase):
class SshTest(testlib.DockerMixin, unittest2.TestCase):
stream_class = mitogen.ssh.Stream
def test_stream_name(self):
@ -54,7 +53,7 @@ class SshTest(testlib.DockerMixin, unittest.TestCase):
except mitogen.ssh.PasswordError, e:
pass
assert e[0] == self.stream_class.password_required_msg
self.assertEqual(e[0], self.stream_class.password_required_msg)
def test_password_incorrect(self):
try:
@ -66,7 +65,7 @@ class SshTest(testlib.DockerMixin, unittest.TestCase):
except mitogen.ssh.PasswordError, e:
pass
assert e[0] == self.stream_class.password_incorrect_msg
self.assertEqual(e[0], self.stream_class.password_incorrect_msg)
def test_password_specified(self):
context = self.docker_ssh(
@ -74,8 +73,10 @@ class SshTest(testlib.DockerMixin, unittest.TestCase):
password='y',
)
sentinel = 'i-am-mitogen-test-docker-image\n'
assert sentinel == context.call(plain_old_module.get_sentinel_value)
self.assertEqual(
'i-am-mitogen-test-docker-image\n',
context.call(plain_old_module.get_sentinel_value),
)
def test_pubkey_required(self):
try:
@ -86,16 +87,18 @@ class SshTest(testlib.DockerMixin, unittest.TestCase):
except mitogen.ssh.PasswordError, e:
pass
assert e[0] == self.stream_class.password_required_msg
self.assertEqual(e[0], self.stream_class.password_required_msg)
def test_pubkey_specified(self):
context = self.docker_ssh(
username='has-sudo-pubkey',
identity_file=testlib.data_path('docker/has-sudo-pubkey.key'),
)
sentinel = 'i-am-mitogen-test-docker-image\n'
assert sentinel == context.call(plain_old_module.get_sentinel_value)
self.assertEqual(
'i-am-mitogen-test-docker-image\n',
context.call(plain_old_module.get_sentinel_value),
)
if __name__ == '__main__':
unittest.main()
unittest2.main()

@ -5,9 +5,10 @@ import re
import socket
import sys
import time
import unittest
import urlparse
import unittest2
import mitogen.master
if mitogen.is_master: # TODO: shouldn't be necessary.
import docker
@ -113,7 +114,7 @@ def wait_for_port(
% (host, port))
class TestCase(unittest.TestCase):
class TestCase(unittest2.TestCase):
def assertRaises(self, exc, func, *args, **kwargs):
"""Like regular assertRaises, except return the exception that was
raised. Can't use context manager because tests must run on Python2.4"""
@ -125,9 +126,6 @@ class TestCase(unittest.TestCase):
assert 0, '%r raised %r, not %r' % (func, e, exc)
assert 0, '%r did not raise %r' % (func, exc)
def assertContains(self, needle, hay):
assert needle in hay, "%r not found in %r" % (needle, hay)
class DockerizedSshDaemon(object):
def __init__(self):

@ -1,6 +1,6 @@
#!/usr/bin/env python
import unittest
import unittest2
import mitogen.master
import mitogen.utils
@ -15,22 +15,22 @@ def func(router):
return router
class RunWithRouterTest(unittest.TestCase):
class RunWithRouterTest(unittest2.TestCase):
# test_shutdown_on_exception
# test_shutdown_on_success
def test_run_with_broker(self):
router = mitogen.utils.run_with_router(func0)
self.assertTrue(isinstance(router, mitogen.master.Router))
self.assertIsInstance(router, mitogen.master.Router)
self.assertFalse(router.broker._thread.isAlive())
class WithRouterTest(unittest.TestCase):
class WithRouterTest(unittest2.TestCase):
def test_with_broker(self):
router = func()
self.assertTrue(isinstance(router, mitogen.master.Router))
self.assertIsInstance(router, mitogen.master.Router)
self.assertFalse(router.broker._thread.isAlive())
if __name__ == '__main__':
unittest.main()
unittest2.main()

Loading…
Cancel
Save