def test_plumbum_colors_like_pkg_succeeds(self):
        # plumbum has been eating too many rainbow-colored pills
        import pkg_like_plumbum.colors
        path, src, is_pkg = self.call('pkg_like_plumbum.colors')
        self.assertEquals(path,
                          testlib.data_path('pkg_like_plumbum/colors.py'))

        s = open(testlib.data_path('pkg_like_plumbum/colors.py'), 'rb').read()
        self.assertEquals(src, s)
        self.assertFalse(is_pkg)
    class DjangoMixin(object):
        WEBPROJECT_PATH = testlib.data_path('webproject')

        # TODO: rip out Django and replace with a static tree of weird imports
        # that don't depend on .. Django! The hack below is because the version
        # of Django we need to test against 2.6 doesn't actually run on 3.6.
        # But we don't care, we just need to be able to import it.
        #
        #   File "django/utils/html_parser.py", line 12, in <module>
        #   AttributeError: module 'html.parser' has no attribute
        #   'HTMLParseError'
        #
        import pkg_resources._vendor.six
        from django.utils.six.moves import html_parser as _html_parser
        _html_parser.HTMLParseError = Exception

        @classmethod
        def setUpClass(cls):
            super(DjangoMixin, cls).setUpClass()
            sys.path.append(cls.WEBPROJECT_PATH)
            os.environ['DJANGO_SETTINGS_MODULE'] = 'webproject.settings'

        @classmethod
        def tearDownClass(cls):
            sys.path.remove(cls.WEBPROJECT_PATH)
            del os.environ['DJANGO_SETTINGS_MODULE']
            super(DjangoMixin, cls).tearDownClass()
示例#3
0
 def test_pubkey_specified(self):
     context = self.docker_ssh(
         username='******',
         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)
示例#4
0
    def test_rsync_between_direct_children(self):
        # master -> SSH -> mitogen__has_sudo_pubkey -> rsync(.ssh) -> master ->
        # mitogen__has_sudo -> rsync

        pubkey_acct = self.docker_ssh(
            username='******',
            identity_file=testlib.data_path('docker/mitogen__has_sudo_pubkey.key'),
        )

        nopw_acct = self.docker_ssh(
            username='******',
            password='******',
        )

        webapp_acct = self.router.sudo(
            via=nopw_acct,
            username='******',
        )

        dest_path = webapp_acct.call(os.path.expanduser, '~/.ssh')
        if webapp_acct.call(os.path.exists, dest_path):
            webapp_acct.call(shutil.rmtree, dest_path)

        return_code = pubkey_acct.call(mitogen.fakessh.run, webapp_acct, args=[
            'rsync', '--progress', '-vvva', '.ssh/', 'target:' + dest_path
        ])

        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'),
        )
示例#5
0
    def test_rsync_between_direct_children(self):
        # master -> SSH -> has-sudo-pubkey -> rsync(.ssh) -> master ->
        # has-sudo -> rsync

        pubkey_acct = self.docker_ssh(
            username='******',
            identity_file=testlib.data_path('docker/has-sudo-pubkey.key'),
        )

        nopw_acct = self.docker_ssh(
            username='******',
            password='******',
        )

        webapp_acct = self.router.sudo(
            via=nopw_acct,
            username='******',
        )

        dest_path = webapp_acct.call(os.path.expanduser, '~/.ssh')
        if webapp_acct.call(os.path.exists, dest_path):
            webapp_acct.call(shutil.rmtree, dest_path)

        return_code = pubkey_acct.call(mitogen.fakessh.run,
                                       webapp_acct,
                                       args=[
                                           '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')
示例#6
0
    def test_okay(self):
        stub_path = testlib.data_path('stubs/stub-podman.py')

        context = self.router.podman(
            container='container_name',
            podman_path=stub_path,
        )
        stream = self.router.stream_by_id(context.context_id)

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        expected_call = [
            stub_path, 'exec', '--interactive', '--', 'container_name',
            stream.conn.options.python_path
        ]
        self.assertEquals(argv[:len(expected_call)], expected_call)

        context = self.router.podman(
            container='container_name',
            podman_path=stub_path,
            username='******',
        )
        stream = self.router.stream_by_id(context.context_id)

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        expected_call = [
            stub_path, 'exec', '--user=some_user', '--interactive', '--',
            'container_name', stream.conn.options.python_path
        ]
        self.assertEquals(argv[:len(expected_call)], expected_call)
示例#7
0
 def test_regular_mod(self):
     from module_finder_testmod import regular_mod
     path, src, is_pkg = self.call('module_finder_testmod.regular_mod')
     self.assertEquals(
         path, testlib.data_path('module_finder_testmod/regular_mod.py'))
     self.assertEquals(src, inspect.getsource(regular_mod))
     self.assertFalse(is_pkg)
示例#8
0
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
    sudo_path = testlib.data_path('stubs/stub-sudo.py')

    def run_sudo(self, **kwargs):
        context = self.router.sudo(sudo_path=self.sudo_path, **kwargs)
        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        return context, argv

    def test_basic(self):
        context, argv = self.run_sudo()
        self.assertEquals(argv[:4], [self.sudo_path, '-u', 'root', '--'])

    def test_selinux_type_role(self):
        context, argv = self.run_sudo(
            selinux_type='setype',
            selinux_role='serole',
        )
        self.assertEquals(argv[:8], [
            self.sudo_path, '-u', 'root', '-r', 'serole', '-t', 'setype', '--'
        ])

    def test_reparse_args(self):
        context, argv = self.run_sudo(sudo_args=[
            '--type', 'setype', '--role', 'serole', '--user', 'user'
        ])
        self.assertEquals(argv[:8], [
            self.sudo_path, '-u', 'user', '-r', 'serole', '-t', 'setype', '--'
        ])
示例#9
0
class ConstructionTest(testlib.RouterMixin, testlib.TestCase):
    stub_python_path = testlib.data_path('stubs/stub-python.py')

    def test_stream_name(self):
        context = self.router.local()
        pid = context.call(os.getpid)
        self.assertEqual('local.%d' % (pid,), context.name)

    def test_python_path_inherited(self):
        context = self.router.local()
        self.assertEqual(sys.executable, context.call(get_sys_executable))

    def test_python_path_string(self):
        context = self.router.local(
            python_path=self.stub_python_path,
        )
        env = context.call(get_os_environ)
        self.assertEqual('1', env['THIS_IS_STUB_PYTHON'])

    def test_python_path_list(self):
        context = self.router.local(
            python_path=[
                self.stub_python_path,
                "magic_first_arg",
                sys.executable
            ]
        )
        self.assertEqual(sys.executable, context.call(get_sys_executable))
        env = context.call(get_os_environ)
        self.assertEqual('magic_first_arg', env['STUB_PYTHON_FIRST_ARG'])
        self.assertEqual('1', env['THIS_IS_STUB_PYTHON'])
示例#10
0
 def test_regular_mod(self):
     from module_finder_testmod import regular_mod
     path, src, is_pkg = self.call('module_finder_testmod.regular_mod')
     self.assertEquals(path,
         testlib.data_path('module_finder_testmod/regular_mod.py'))
     self.assertEquals(mitogen.core.to_text(src),
                       inspect.getsource(regular_mod))
     self.assertFalse(is_pkg)
示例#11
0
 def test_string(self):
     os.environ['PYTHON'] = sys.executable
     context = self.router.local(
         python_path=testlib.data_path('env_wrapper.sh'),
     )
     self.assertEquals(sys.executable, context.call(get_sys_executable))
     env = context.call(get_os_environ)
     self.assertEquals('1', env['EXECUTED_VIA_ENV_WRAPPER'])
示例#12
0
 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())
示例#13
0
 def test_okay(self):
     context = self.router.ssh(
         hostname='hostname',
         username='******',
         ssh_path=testlib.data_path('fakessh.py'),
     )
     #context.call(mitogen.utils.log_to_file, '/tmp/log')
     #context.call(mitogen.utils.disable_site_packages)
     self.assertEquals(3, context.call(plain_old_module.add, 1, 2))
示例#14
0
 def test_pubkey_specified(self):
     context = self.docker_ssh(
         username='******',
         identity_file=testlib.data_path('docker/mitogen__has_sudo_pubkey.key'),
     )
     self.assertEqual(
         'i-am-mitogen-test-docker-image\n',
         context.call(plain_old_module.get_sentinel_value),
     )
示例#15
0
 def test_pubkey_specified(self):
     context = self.docker_ssh(
         username='******',
         identity_file=testlib.data_path('docker/mitogen__has_sudo_pubkey.key'),
     )
     self.assertEqual(
         'i-am-mitogen-test-docker-image\n',
         context.call(plain_old_module.get_sentinel_value),
     )
示例#16
0
 def test_okay(self):
     context = self.router.ssh(
         hostname='hostname',
         username='******',
         ssh_path=testlib.data_path('stubs/stub-ssh.py'),
     )
     #context.call(mitogen.utils.log_to_file, '/tmp/log')
     #context.call(mitogen.utils.disable_site_packages)
     self.assertEqual(3, context.call(plain_old_module.add, 1, 2))
示例#17
0
文件: ssh_test.py 项目: gridl/mitogen
 def fake_ssh(self, FAKESSH_MODE=None, **kwargs):
     os.environ['FAKESSH_MODE'] = str(FAKESSH_MODE)
     try:
         return self.router.ssh(hostname='hostname',
                                username='******',
                                ssh_path=testlib.data_path('fakessh.py'),
                                **kwargs)
     finally:
         del os.environ['FAKESSH_MODE']
示例#18
0
 def stub_ssh(self, STUBSSH_MODE=None, **kwargs):
     os.environ['STUBSSH_MODE'] = str(STUBSSH_MODE)
     try:
         return self.router.ssh(
             hostname='hostname',
             username='******',
             ssh_path=testlib.data_path('stubs/stub-ssh.py'),
             **kwargs)
     finally:
         del os.environ['STUBSSH_MODE']
示例#19
0
    def test_okay(self):
        lxc_attach_path = testlib.data_path('fake_lxc_attach.py')
        context = self.router.lxc(
            container='container_name',
            lxc_attach_path=lxc_attach_path,
        )

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEquals(argv[0], lxc_attach_path)
        self.assertTrue('--clear-env' in argv)
        self.assertTrue(has_subseq(argv, ['--name', 'container_name']))
示例#20
0
 def fake_ssh(self, FAKESSH_MODE=None, **kwargs):
     os.environ['FAKESSH_MODE'] = str(FAKESSH_MODE)
     try:
         return self.router.ssh(
             hostname='hostname',
             username='******',
             ssh_path=testlib.data_path('fakessh.py'),
             **kwargs
         )
     finally:
         del os.environ['FAKESSH_MODE']
示例#21
0
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
    kubectl_path = testlib.data_path('stubs/stub-kubectl.py')

    def test_okay(self):
        context = self.router.kubectl(pod='pod_name',
                                      kubectl_path=self.kubectl_path)

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEqual(argv[0], self.kubectl_path)
        self.assertEqual(argv[1], 'exec')
        self.assertEqual(argv[2], '-it')
        self.assertEqual(argv[3], 'pod_name')
示例#22
0
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
    stub_su_path = testlib.data_path('stubs/stub-su.py')

    def run_su(self, **kwargs):
        context = self.router.su(su_path=self.stub_su_path, **kwargs)
        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        return context, argv

    def test_basic(self):
        context, argv = self.run_su()
        self.assertEqual(argv[1], 'root')
        self.assertEqual(argv[2], '-c')
示例#23
0
 def test_list(self):
     context = self.router.local(
         python_path=[
             testlib.data_path('env_wrapper.sh'),
             "magic_first_arg",
             sys.executable
         ]
     )
     self.assertEquals(sys.executable, context.call(get_sys_executable))
     env = context.call(get_os_environ)
     self.assertEquals('magic_first_arg', env['ENV_WRAPPER_FIRST_ARG'])
     self.assertEquals('1', env['EXECUTED_VIA_ENV_WRAPPER'])
示例#24
0
    def test_okay(self):
        lxc_path = testlib.data_path('stubs/stub-lxc.py')
        context = self.router.lxd(
            container='container_name',
            lxc_path=lxc_path,
        )

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEquals(argv[0], lxc_path)
        self.assertEquals(argv[1], 'exec')
        self.assertEquals(argv[2], '--mode=noninteractive')
        self.assertEquals(argv[3], 'container_name')
示例#25
0
    def test_rsync_from_master(self):
        context = self.docker_ssh_any()

        if context.call(os.path.exists, '/tmp/data'):
            context.call(shutil.rmtree, '/tmp/data')

        return_code = mitogen.fakessh.run(context, self.router, [
            'rsync', '--progress', '-vvva',
            testlib.data_path('.'), 'target:/tmp/data'
        ])

        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'))
示例#26
0
    def test_okay(self):
        buildah_path = testlib.data_path('stubs/stub-buildah.py')
        context = self.router.buildah(
            container='container_name',
            buildah_path=buildah_path,
        )
        stream = self.router.stream_by_id(context.context_id)

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEquals(argv[0], buildah_path)
        self.assertEquals(argv[1], 'run')
        self.assertEquals(argv[2], '--')
        self.assertEquals(argv[3], 'container_name')
        self.assertEquals(argv[4], stream.python_path)
示例#27
0
 def test_connect_timeout(self):
     # Ensure the child process is reaped if the connection times out.
     stream = mitogen.parent.Stream(
         router=self.router,
         remote_id=1234,
         old_router=self.router,
         max_message_size=self.router.max_message_size,
         python_path=testlib.data_path('python_never_responds.sh'),
         connect_timeout=0.5,
     )
     self.assertRaises(mitogen.core.TimeoutError, lambda: stream.connect())
     wait_for_child(stream.pid)
     e = self.assertRaises(OSError, lambda: os.kill(stream.pid, 0))
     self.assertEquals(e.args[0], errno.ESRCH)
示例#28
0
    def test_rsync_from_master(self):
        context = self.docker_ssh_any()

        if context.call(os.path.exists, '/tmp/data'):
            context.call(shutil.rmtree, '/tmp/data')

        return_code = mitogen.fakessh.run(context, self.router, [
            'rsync', '--progress', '-vvva',
            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')
示例#29
0
    def test_okay(self):
        docker_path = testlib.data_path('stubs/stub-docker.py')
        context = self.router.docker(
            container='container_name',
            docker_path=docker_path,
        )
        stream = self.router.stream_by_id(context.context_id)

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEquals(argv[0], docker_path)
        self.assertEquals(argv[1], 'exec')
        self.assertEquals(argv[2], '--interactive')
        self.assertEquals(argv[3], 'container_name')
        self.assertEquals(argv[4], stream.conn.options.python_path)
示例#30
0
def run_fd_check(func, fd, mode, on_start=None):
    """
    Run ``tests/data/fd_check.py`` using `func`. The subprocess writes
    information about the `fd` it received to a temporary file.

    :param func:
        Function like `create_child()` used to start child.
    :param fd:
        FD child should read/write from, and report information about.
    :param mode:
        "read" or "write", depending on whether the FD is readable or writeable
        from the perspective of the child. If "read", `on_start()` should write
        "TEST" to it and the child reads "TEST" from it, otherwise `on_start()`
        should read "TEST" from it and the child writes "TEST" to it.
    :param on_start:
        Function invoked as `on_start(proc)`
    :returns:
        Tuple of `(proc, info, on_start_result)`, where:

            * `proc`: the :class:`mitogen.parent.Process` returned by `func`.
            * `info`: dict containing information returned by the child:
                * `buf`: "TEST" that was read in "read" mode
                * `flags`: :attr:`fcntl.F_GETFL` flags for `fd`
                * `st_mode`: st_mode field from :func:`os.fstat`
                * `st_dev`: st_dev field from :func:`os.fstat`
                * `st_ino`: st_ino field from :func:`os.fstat`
                * `ttyname`: :func:`os.ttyname` invoked on `fd`.
                * `controlling_tty`: :func:os.ttyname` invoked on ``/dev/tty``
                  from within the child.
    """
    tf = tempfile.NamedTemporaryFile()
    args = [
        sys.executable,
        testlib.data_path('fd_check.py'),
        tf.name,
        str(fd),
        mode,
    ]

    proc = func(args=args)
    os = None
    if on_start:
        os = on_start(proc)
    proc.proc.wait()
    try:
        return proc, eval(tf.read()), os
    finally:
        tf.close()
示例#31
0
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
    doas_path = testlib.data_path('stubs/stub-doas.py')

    def test_okay(self):
        context = self.router.doas(
            doas_path=self.doas_path,
            username='******',
        )
        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEquals(argv[:4], [
            self.doas_path,
            '-u',
            'someuser',
            '--',
        ])
        self.assertEquals('1', context.call(os.getenv, 'THIS_IS_STUB_DOAS'))
示例#32
0
    def test_connect_timeout(self):
        # Ensure the child process is reaped if the connection times out.
        options = mitogen.parent.Options(
            old_router=self.router,
            max_message_size=self.router.max_message_size,
            python_path=testlib.data_path('python_never_responds.py'),
            connect_timeout=0.5,
        )

        conn = mitogen.parent.Connection(options, router=self.router)
        self.assertRaises(
            mitogen.core.TimeoutError,
            lambda: conn.connect(context=mitogen.core.Context(None, 1234)))
        wait_for_child(conn.proc.pid)
        e = self.assertRaises(OSError, lambda: os.kill(conn.proc.pid, 0))
        self.assertEqual(e.args[0], errno.ESRCH)
示例#33
0
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
    jexec_path = testlib.data_path('stubs/stub-jexec.py')

    def test_okay(self):
        context = self.router.jail(
            jexec_path=self.jexec_path,
            container='somejail',
        )
        stream = self.router.stream_by_id(context.context_id)

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEquals(argv[:4], [
            self.jexec_path,
            'somejail',
            stream.conn.options.python_path,
            '-c',
        ])
        self.assertEquals('1', context.call(os.getenv, 'THIS_IS_STUB_JEXEC'))
示例#34
0
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
    chroot_exe = testlib.data_path('stubs/stub-chroot.py')

    def test_okay(self):
        context = self.router.chroot(
            chroot_exe=self.chroot_exe,
            container='somechroot',
        )
        stream = self.router.stream_by_id(context.context_id)

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEquals(argv[:4], [
            self.chroot_exe,
            'somechroot',
            stream.conn.options.python_path,
            '-c',
        ])
        self.assertEquals('1', context.call(os.getenv, 'THIS_IS_STUB_CHROOT'))
示例#35
0
def run_fd_check(func, fd, mode, on_start=None):
    tf = tempfile.NamedTemporaryFile()
    args = [
        sys.executable,
        testlib.data_path('fd_check.py'),
        tf.name,
        str(fd),
        mode,
    ]

    proc = func(args=args)
    os = None
    if on_start:
        os = on_start(proc)
    proc.proc.wait()
    try:
        return proc, eval(tf.read()), os
    finally:
        tf.close()
示例#36
0
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
    lxc_attach_path = testlib.data_path('stubs/stub-lxc-attach.py')

    def test_okay(self):
        context = self.router.lxc(
            container='container_name',
            lxc_attach_path=self.lxc_attach_path,
        )

        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        self.assertEquals(argv[0], self.lxc_attach_path)
        self.assertTrue('--clear-env' in argv)
        self.assertTrue(has_subseq(argv, ['--name', 'container_name']))

    def test_eof(self):
        e = self.assertRaises(
            mitogen.parent.EofError, lambda: self.router.lxc(
                container='container_name',
                lxc_attach_path='true',
            ))
        self.assertTrue(str(e).endswith(mitogen.lxc.Stream.eof_error_hint))
示例#37
0
 def test_empty_source_pkg(self):
     path, src, is_pkg = self.call('module_finder_testmod')
     self.assertEquals(path,
         testlib.data_path('module_finder_testmod/__init__.py'))
     self.assertEquals(mitogen.core.b(''), src)
     self.assertTrue(is_pkg)
示例#38
0
def read_sample(fname):
    sample_path = testlib.data_path('minimize_samples/' + fname)
    sample_file = open(sample_path)
    sample = sample_file.read()
    sample_file.close()
    return sample
示例#39
0
 def test_empty_source_module(self):
     path, src, is_pkg = self.call('module_finder_testmod.empty_mod')
     self.assertEquals(path,
         testlib.data_path('module_finder_testmod/empty_mod.py'))
     self.assertEquals(mitogen.core.b(''), src)
     self.assertFalse(is_pkg)