2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
import mock
|
2018-10-01 19:16:19 +00:00
|
|
|
import textwrap
|
2017-09-15 06:24:41 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2017-11-12 12:42:27 +00:00
|
|
|
import unittest2
|
2017-11-11 22:59:38 +00:00
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
import mitogen.master
|
|
|
|
import testlib
|
|
|
|
|
|
|
|
import plain_old_module
|
|
|
|
import simple_pkg.a
|
|
|
|
|
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class NeutralizeMainTest(testlib.RouterMixin, testlib.TestCase):
|
2018-10-01 19:16:19 +00:00
|
|
|
klass = mitogen.master.ModuleResponder
|
|
|
|
|
|
|
|
def call(self, *args, **kwargs):
|
2019-01-19 11:32:35 +00:00
|
|
|
router = mock.Mock()
|
|
|
|
return self.klass(router).neutralize_main(*args, **kwargs)
|
2018-10-01 19:16:19 +00:00
|
|
|
|
|
|
|
def test_missing_exec_guard(self):
|
|
|
|
path = testlib.data_path('main_with_no_exec_guard.py')
|
|
|
|
args = [sys.executable, path]
|
|
|
|
proc = subprocess.Popen(args, stderr=subprocess.PIPE)
|
|
|
|
_, stderr = proc.communicate()
|
|
|
|
self.assertEquals(1, proc.returncode)
|
|
|
|
expect = self.klass.main_guard_msg % (path,)
|
|
|
|
self.assertTrue(expect in stderr.decode())
|
|
|
|
|
|
|
|
HAS_MITOGEN_MAIN = mitogen.core.b(
|
|
|
|
textwrap.dedent("""
|
|
|
|
herp derp
|
|
|
|
|
|
|
|
def myprog():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@mitogen.main(maybe_some_option=True)
|
|
|
|
def main(router):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_mitogen_main(self):
|
|
|
|
untouched = self.call("derp.py", self.HAS_MITOGEN_MAIN)
|
|
|
|
self.assertEquals(untouched, self.HAS_MITOGEN_MAIN)
|
|
|
|
|
|
|
|
HAS_EXEC_GUARD = mitogen.core.b(
|
|
|
|
textwrap.dedent("""
|
|
|
|
herp derp
|
|
|
|
|
|
|
|
def myprog():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def main():
|
|
|
|
pass
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
""")
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_exec_guard(self):
|
|
|
|
touched = self.call("derp.py", self.HAS_EXEC_GUARD)
|
|
|
|
bits = touched.decode().split()
|
|
|
|
self.assertEquals(bits[-3:], ['def', 'main():', 'pass'])
|
|
|
|
|
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class GoodModulesTest(testlib.RouterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
def test_plain_old_module(self):
|
|
|
|
# The simplest case: a top-level module with no interesting imports or
|
|
|
|
# package machinery damage.
|
2017-09-17 15:03:43 +00:00
|
|
|
context = self.router.local()
|
2019-01-20 08:11:54 +00:00
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
self.assertEquals(256, context.call(plain_old_module.pow, 2, 8))
|
2019-01-19 15:55:52 +00:00
|
|
|
self.assertEquals(1, self.router.responder.get_module_count)
|
|
|
|
self.assertEquals(1, self.router.responder.good_load_module_count)
|
2019-01-20 06:18:03 +00:00
|
|
|
self.assertLess(300, self.router.responder.good_load_module_size)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_simple_pkg(self):
|
|
|
|
# Ensure success of a simple package containing two submodules, one of
|
|
|
|
# which imports the other.
|
2017-09-17 15:03:43 +00:00
|
|
|
context = self.router.local()
|
2017-09-15 06:24:41 +00:00
|
|
|
self.assertEquals(3,
|
|
|
|
context.call(simple_pkg.a.subtract_one_add_two, 2))
|
2019-01-19 15:55:52 +00:00
|
|
|
self.assertEquals(2, self.router.responder.get_module_count)
|
|
|
|
self.assertEquals(3, self.router.responder.good_load_module_count)
|
|
|
|
self.assertEquals(0, self.router.responder.bad_load_module_count)
|
2019-01-20 06:18:03 +00:00
|
|
|
self.assertLess(450, self.router.responder.good_load_module_size)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_self_contained_program(self):
|
|
|
|
# Ensure a program composed of a single script can be imported
|
|
|
|
# successfully.
|
|
|
|
args = [sys.executable, testlib.data_path('self_contained_program.py')]
|
2018-06-26 07:25:40 +00:00
|
|
|
output = testlib.subprocess__check_output(args).decode()
|
2017-09-15 06:24:41 +00:00
|
|
|
self.assertEquals(output, "['__main__', 50]\n")
|
|
|
|
|
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class BrokenModulesTest(testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
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.
|
|
|
|
|
2018-04-16 15:58:44 +00:00
|
|
|
stream = mock.Mock()
|
|
|
|
stream.sent_modules = set()
|
2017-09-17 15:03:43 +00:00
|
|
|
router = mock.Mock()
|
2018-04-16 15:58:44 +00:00
|
|
|
router.stream_by_id = lambda n: stream
|
|
|
|
|
|
|
|
msg = mitogen.core.Message(
|
2018-06-26 07:25:40 +00:00
|
|
|
data=mitogen.core.b('non_existent_module'),
|
2018-04-16 15:58:44 +00:00
|
|
|
reply_to=50,
|
2017-09-17 15:03:43 +00:00
|
|
|
)
|
2018-04-16 15:58:44 +00:00
|
|
|
msg.router = router
|
|
|
|
|
|
|
|
responder = mitogen.master.ModuleResponder(router)
|
|
|
|
responder._on_get_module(msg)
|
2018-05-29 12:44:04 +00:00
|
|
|
self.assertEquals(1, len(router._async_route.mock_calls))
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2019-01-19 15:55:52 +00:00
|
|
|
self.assertEquals(1, responder.get_module_count)
|
|
|
|
self.assertEquals(0, responder.good_load_module_count)
|
|
|
|
self.assertEquals(0, responder.good_load_module_size)
|
|
|
|
self.assertEquals(1, responder.bad_load_module_count)
|
|
|
|
|
2018-05-29 12:44:04 +00:00
|
|
|
call = router._async_route.mock_calls[0]
|
2017-09-17 15:03:43 +00:00
|
|
|
msg, = call[1]
|
2018-04-16 15:58:44 +00:00
|
|
|
self.assertEquals(mitogen.core.LOAD_MODULE, msg.handle)
|
|
|
|
self.assertEquals(('non_existent_module', None, None, None, ()),
|
|
|
|
msg.unpickle())
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_ansible_six_messed_up_path(self):
|
|
|
|
# The copy of six.py shipped with Ansible appears in a package whose
|
|
|
|
# __path__ subsequently ends up empty, which prevents pkgutil from
|
|
|
|
# finding its submodules. After ansible.compat.six is initialized in
|
|
|
|
# the parent, attempts to execute six/__init__.py on the slave will
|
|
|
|
# cause an attempt to request ansible.compat.six._six from the master.
|
|
|
|
import six_brokenpkg
|
|
|
|
|
2018-04-16 15:58:44 +00:00
|
|
|
stream = mock.Mock()
|
|
|
|
stream.sent_modules = set()
|
2017-09-17 15:03:43 +00:00
|
|
|
router = mock.Mock()
|
2018-04-16 15:58:44 +00:00
|
|
|
router.stream_by_id = lambda n: stream
|
|
|
|
|
|
|
|
msg = mitogen.core.Message(
|
2018-06-26 07:25:40 +00:00
|
|
|
data=mitogen.core.b('six_brokenpkg._six'),
|
2018-04-16 15:58:44 +00:00
|
|
|
reply_to=50,
|
2017-09-17 15:03:43 +00:00
|
|
|
)
|
2018-04-16 15:58:44 +00:00
|
|
|
msg.router = router
|
|
|
|
|
|
|
|
responder = mitogen.master.ModuleResponder(router)
|
|
|
|
responder._on_get_module(msg)
|
2018-05-29 12:44:04 +00:00
|
|
|
self.assertEquals(1, len(router._async_route.mock_calls))
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2019-01-19 15:55:52 +00:00
|
|
|
self.assertEquals(1, responder.get_module_count)
|
|
|
|
self.assertEquals(0, responder.good_load_module_count)
|
|
|
|
self.assertEquals(0, responder.good_load_module_size)
|
|
|
|
self.assertEquals(1, responder.bad_load_module_count)
|
|
|
|
|
2018-05-29 12:44:04 +00:00
|
|
|
call = router._async_route.mock_calls[0]
|
2017-09-17 15:03:43 +00:00
|
|
|
msg, = call[1]
|
2018-04-16 15:58:44 +00:00
|
|
|
self.assertEquals(mitogen.core.LOAD_MODULE, msg.handle)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIsInstance(msg.unpickle(), tuple)
|
2017-09-28 09:07:27 +00:00
|
|
|
|
|
|
|
|
2019-01-19 15:55:52 +00:00
|
|
|
class ForwardTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
def test_stats(self):
|
|
|
|
# Forwarding stats broken because forwarding is broken. See #469.
|
|
|
|
c1 = self.router.local()
|
|
|
|
c2 = self.router.fork(via=c1)
|
|
|
|
|
|
|
|
self.assertEquals(256, c2.call(plain_old_module.pow, 2, 8))
|
|
|
|
self.assertEquals(3, self.router.responder.get_module_count)
|
2019-01-20 08:11:54 +00:00
|
|
|
self.assertEquals(3, self.router.responder.good_load_module_count)
|
|
|
|
self.assertLess(23000, self.router.responder.good_load_module_size)
|
2019-01-19 15:55:52 +00:00
|
|
|
|
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class BlacklistTest(testlib.TestCase):
|
2018-03-18 14:59:47 +00:00
|
|
|
@unittest2.skip('implement me')
|
2018-02-16 02:23:51 +00:00
|
|
|
def test_whitelist_no_blacklist(self):
|
|
|
|
assert 0
|
|
|
|
|
2018-03-18 14:59:47 +00:00
|
|
|
@unittest2.skip('implement me')
|
2018-02-16 02:23:51 +00:00
|
|
|
def test_whitelist_has_blacklist(self):
|
|
|
|
assert 0
|
|
|
|
|
2018-03-18 14:59:47 +00:00
|
|
|
@unittest2.skip('implement me')
|
2018-02-16 02:23:51 +00:00
|
|
|
def test_blacklist_no_whitelist(self):
|
|
|
|
assert 0
|
|
|
|
|
2018-03-18 14:59:47 +00:00
|
|
|
@unittest2.skip('implement me')
|
2018-02-16 02:23:51 +00:00
|
|
|
def test_blacklist_has_whitelist(self):
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
|
2017-09-28 09:07:27 +00:00
|
|
|
if __name__ == '__main__':
|
2017-11-12 12:42:27 +00:00
|
|
|
unittest2.main()
|