示例#1
0
    def test_shell(self):
        logging.debug('')
        logging.debug('test_shell')

        testdir = 'test_shell'
        if os.path.exists(testdir):
            shutil.rmtree(testdir, onerror=onerror)
        os.mkdir(testdir)
        os.chdir(testdir)

        try:
            # Create a server.
            server = ObjServer(allow_shell=True)

            # Execute a command.
            if sys.platform == 'win32':
                cmd = 'cmd'
                args = ['/c', 'dir']
            else:
                cmd = 'ls'
                args = []
            rdesc = {
                'remote_command': cmd,
                'args': args,
                'output_path': 'cmd.out'
            }
            return_code, error_msg = server.execute_command(rdesc)
            self.assertEqual(return_code, 0)

            # Bad command, specify lots of resources.
            with open('stdin1', 'w') as out:
                out.write('z')
            rdesc = {
                'remote_command': 'no-such-command',
                'args': ['a', 'b', 'c'],
                'input_path': 'stdin1',
                'error_path': 'stderr1',
                'wallclock_time': 10
            }
            if sys.platform == 'win32':
                msg = '[Error 2] The system cannot find the file specified'
            else:
                msg = '[Errno 2] No such file or directory'
            assert_raises(self, 'server.execute_command(rdesc)', globals(),
                          locals(), OSError, msg)

            # Load a model.
            exec_comp = server.create('openmdao.test.execcomp.ExecComp')
            exec_comp.run()
            egg_info = exec_comp.save_to_egg('exec_comp', '0')
            obj = server.load_model(egg_info[0])
            obj.run()

            assert_raises(self, "server.load_model('no-such-egg')", globals(),
                          locals(), ValueError, "'no-such-egg' not found.")
        finally:
            SimulationRoot.chroot('..')
            shutil.rmtree(testdir, onerror=onerror)
    def test_shell(self):
        logging.debug('')
        logging.debug('test_shell')

        testdir = 'test_shell'
        if os.path.exists(testdir):
            shutil.rmtree(testdir, onerror=onerror)
        os.mkdir(testdir)
        os.chdir(testdir)

        try:
            # Create a server.
            server = ObjServer(allow_shell=True)

            # Execute a command.
            if sys.platform == 'win32':
                cmd = 'cmd'
                args = ['/c', 'dir']
            else:
                cmd = 'ls'
                args = []
            rdesc = {'remote_command': cmd,
                     'args': args,
                     'output_path': 'cmd.out'}
            return_code, error_msg = server.execute_command(rdesc)
            self.assertEqual(return_code, 0)

            # Bad command, specify lots of resources.
            with open('stdin1', 'w') as out:
                out.write('z')
            rdesc = {'remote_command': 'no-such-command',
                     'args': ['a', 'b', 'c'],
                     'input_path': 'stdin1',
                     'error_path': 'stderr1',
                     'wallclock_time': 10}
            if sys.platform == 'win32':
                msg = '[Error 2] The system cannot find the file specified'
            else:
                msg = '[Errno 2] No such file or directory'
            assert_raises(self, 'server.execute_command(rdesc)',
                          globals(), locals(), OSError, msg)

            # Load a model.
            exec_comp = server.create('openmdao.test.execcomp.ExecComp')
            exec_comp.run()
            egg_info = exec_comp.save_to_egg('exec_comp', '0')
            obj = server.load_model(egg_info[0])
            obj.run()

            assert_raises(self, "server.load_model('no-such-egg')",
                          globals(), locals(), ValueError,
                          "'no-such-egg' not found.")
        finally:
            SimulationRoot.chroot('..')
            shutil.rmtree(testdir, onerror=onerror)
    def test_shell(self):
        logging.debug("")
        logging.debug("test_shell")

        testdir = "test_shell"
        if os.path.exists(testdir):
            shutil.rmtree(testdir)
        os.mkdir(testdir)
        os.chdir(testdir)

        try:
            # Create a server.
            server = ObjServer(allow_shell=True)

            # Execute a command.
            cmd = "dir" if sys.platform == "win32" else "ls"
            rdesc = {"remote_command": cmd, "output_path": "cmd.out", "hard_runtime_limit": 10}
            return_code, error_msg = server.execute_command(rdesc)
            self.assertEqual(return_code, 0)

            # Non-zero return code.
            rdesc = {
                "remote_command": "no-such-command",
                "output_path": "stdout1",
                "error_path": "stderr1",
                "hard_runtime_limit": 10,
            }
            return_code, error_msg = server.execute_command(rdesc)
            self.assertNotEqual(return_code, 0)

            # Load a model.
            exec_comp = server.create("openmdao.test.execcomp.ExecComp")
            exec_comp.run()
            egg_info = exec_comp.save_to_egg("exec_comp", "0")
            obj = server.load_model(egg_info[0])
            obj.run()

            assert_raises(
                self, "server.load_model('no-such-egg')", globals(), locals(), ValueError, "'no-such-egg' not found."
            )
        finally:
            SimulationRoot.chroot("..")
            shutil.rmtree(testdir)
    def test_shell(self):
        logging.debug('')
        logging.debug('test_shell')
        
        testdir = 'test_shell'
        if os.path.exists(testdir):
            shutil.rmtree(testdir)
        os.mkdir(testdir)
        os.chdir(testdir)

        try:
            # Create a server.
            server = ObjServer(allow_shell=True)

            # Execute a command.
            cmd = 'dir' if sys.platform == 'win32' else 'ls'
            return_code, error_msg = \
                server.execute_command(cmd, None, 'cmd.out', None, None, 0, 10)
            self.assertEqual(return_code, 0)

            # Non-zero return code.
            return_code, error_msg = \
                server.execute_command('no-such-command',
                                       None, 'stdout1', 'stderr1', None, 0, 10)
            self.assertNotEqual(return_code, 0)

            # Exception creating process.
# FIXME: despite the files being closed, Windows thinks they're in use :-(
            if sys.platform != 'win32':
                try:
                    server.execute_command(['no-such-command'],
                                           None, 'stdout2', 'stderr2', None, 0, 10)
                except OSError as exc:
                    msg = '[Errno 2] No such file or directory'
                    self.assertEqual(str(exc), msg)
                else:
                    self.fail('Expected OSError')

            # Load a model.
            exec_comp = server.create('openmdao.test.execcomp.ExecComp')
            exec_comp.run()
            egg_info = exec_comp.save_to_egg('exec_comp', '0')
            obj = server.load_model(egg_info[0])
            obj.run()

            assert_raises(self, "server.load_model('no-such-egg')",
                          globals(), locals(), ValueError,
                          "'no-such-egg' not found.")
        finally:
            os.chdir('..')
            shutil.rmtree(testdir)
    def test_shell(self):
        logging.debug('')
        logging.debug('test_shell')

        testdir = 'test_shell'
        if os.path.exists(testdir):
            shutil.rmtree(testdir)
        os.mkdir(testdir)
        os.chdir(testdir)

        try:
            # Create a server.
            server = ObjServer(allow_shell=True)

            # Execute a command.
            cmd = 'dir' if sys.platform == 'win32' else 'ls'
            rdesc = {
                'remote_command': cmd,
                'output_path': 'cmd.out',
                'hard_runtime_limit': 10
            }
            return_code, error_msg = server.execute_command(rdesc)
            self.assertEqual(return_code, 0)

            # Non-zero return code.
            rdesc = {
                'remote_command': 'no-such-command',
                'output_path': 'stdout1',
                'error_path': 'stderr1',
                'hard_runtime_limit': 10
            }
            return_code, error_msg = server.execute_command(rdesc)
            self.assertNotEqual(return_code, 0)

            # Load a model.
            exec_comp = server.create('openmdao.test.execcomp.ExecComp')
            exec_comp.run()
            egg_info = exec_comp.save_to_egg('exec_comp', '0')
            obj = server.load_model(egg_info[0])
            obj.run()

            assert_raises(self, "server.load_model('no-such-egg')", globals(),
                          locals(), ValueError, "'no-such-egg' not found.")
        finally:
            SimulationRoot.chroot('..')
            shutil.rmtree(testdir)
    def test_server(self):
        logging.debug('')
        logging.debug('test_server')

        testdir = 'test_server'
        if os.path.exists(testdir):
            shutil.rmtree(testdir)
        os.mkdir(testdir)
        os.chdir(testdir)

        try:
            # Create a server.
            server = ObjServer()

            # Create a component.
            exec_comp = server.create('openmdao.test.execcomp.ExecComp')
            exec_comp.run()
            egg_info = exec_comp.save_to_egg('exec_comp', '0')

            # Echo some arguments.
            args = server.echo('Hello', 'world!')
            self.assertEqual(args[0], 'Hello')
            self.assertEqual(args[1], 'world!')

            # Try to execute a command.
            cmd = 'dir' if sys.platform == 'win32' else 'ls'
            rdesc = {
                'remote_command': cmd,
                'output_path': 'cmd.out',
                'hard_runtime_limit': 10
            }
            code = 'server.execute_command(rdesc)'
            assert_raises(self, code, globals(), locals(), RuntimeError,
                          'shell access is not allowed by this server')

            # Try to load a model.
            assert_raises(self, 'server.load_model(egg_info[0])', globals(),
                          locals(), RuntimeError,
                          'shell access is not allowed by this server')

            # Bogus file accesses.
            assert_raises(self, "server.open('../xyzzy', 'r')", globals(),
                          locals(), RuntimeError,
                          "Can't open '../xyzzy', not within root ")
            assert_raises(self, "server.open('xyzzy', 'r')", globals(),
                          locals(), IOError,
                          "[Errno 2] No such file or directory: 'xyzzy'")

            # Create a file using context.
            with server.open('xyzzy', 'w') as out:
                out.write('Hello world!\n')

            # Create another file using file proxy.
            out = server.open('fred', 'w')
            out.write('Hello fred!\n')
            out.flush()  # Not really necessary, just for coverage.
            out.close()

            # Zip it.
            server.pack_zipfile(['xyzzy', 'fred'], 'zipped')

            # Make it read-only.
            server.chmod('zipped', 0400)

            if sys.platform == 'win32':
                msg = '[Error 2] The system cannot find the file specified'
            else:
                msg = "[Errno 2] No such file or directory: 'no-such-file'"
            assert_raises(self, "server.chmod('no-such-file', 0400)",
                          globals(), locals(), OSError, msg)

            # Get stats.
            info = server.stat('zipped')

            assert_raises(self, "server.stat('no-such-file')", globals(),
                          locals(), OSError, msg)

            # Remove zipped contents.
            server.remove('xyzzy')
            server.remove('fred')

            if sys.platform == 'win32':
                msg = '[Error 2] The system cannot find the file specified'
            else:
                msg = "[Errno 2] No such file or directory: 'xyzzy'"
            assert_raises(self, "server.remove('xyzzy')", globals(), locals(),
                          OSError, msg)

            # Unpack.
            server.unpack_zipfile('zipped')
            server.chmod('zipped', 0600)  # Ensure we can remove under Windows.

            # Verify contents.
            with server.open('xyzzy', 'r') as inp:
                data = inp.read()
                self.assertEqual(data, 'Hello world!\n')

            inp = server.open('fred', 'r')
            try:
                data = inp.read(1000)
                self.assertEqual(data, 'Hello fred!\n')
            finally:
                inp.close()

            # Try to create a process.
            args = 'dir' if sys.platform == 'win32' else 'ls'
            try:
                proc = server.create('subprocess.Popen', args=args, shell=True)
                proc.wait()
            except TypeError as exc:
                msg = "'subprocess.Popen' is not an allowed type"
                self.assertEqual(str(exc), msg)
            else:
                self.fail('Expected TypeError')

        finally:
            SimulationRoot.chroot('..')
            shutil.rmtree(testdir)
    def test_server(self):
        logging.debug('')
        logging.debug('test_server')

        testdir = 'test_server'
        if os.path.exists(testdir):
            shutil.rmtree(testdir, onerror=onerror)
        os.mkdir(testdir)
        os.chdir(testdir)

        try:
            # Create a server.
            server = ObjServer()

            # Create a component.
            exec_comp = server.create('openmdao.test.execcomp.ExecComp')
            exec_comp.run()
            egg_info = exec_comp.save_to_egg('exec_comp', '0')

            # Echo some arguments.
            args = server.echo('Hello', 'world!')
            self.assertEqual(args[0], 'Hello')
            self.assertEqual(args[1], 'world!')

            # Try to execute a command.
            cmd = 'dir' if sys.platform == 'win32' else 'ls'
            rdesc = {'remote_command': cmd,
                     'output_path': 'cmd.out',
                     'hard_runtime_limit': 10}
            code = 'server.execute_command(rdesc)'
            assert_raises(self, code, globals(), locals(), RuntimeError,
                          'shell access is not allowed by this server')

            # Try to load a model.
            assert_raises(self, 'server.load_model(egg_info[0])',
                          globals(), locals(), RuntimeError,
                          'shell access is not allowed by this server')

            # Bogus file accesses.
            assert_raises(self, "server.open('../xyzzy', 'r')", globals(), locals(),
                          RuntimeError, "Can't open '../xyzzy', not within root ")
            assert_raises(self, "server.open('xyzzy', 'r')", globals(), locals(),
                          IOError, "[Errno 2] No such file or directory: 'xyzzy'")

            # Create a file using context.
            with server.open('xyzzy', 'w') as out:
                out.write('Hello world!\n')

            # Create another file using file proxy.
            out = server.open('fred', 'w')
            out.write('Hello fred!\n')
            out.flush()  # Not really necessary, just for coverage.
            out.close()

            # Zip it.
            server.pack_zipfile(['xyzzy', 'fred'], 'zipped')

            # Make it read-only.
            server.chmod('zipped', 0400)

            if sys.platform == 'win32':
                msg = '[Error 2] The system cannot find the file specified'
            else:
                msg = "[Errno 2] No such file or directory: 'no-such-file'"
            assert_raises(self, "server.chmod('no-such-file', 0400)",
                          globals(), locals(), OSError, msg)

            # Get stats.
            info = server.stat('zipped')

            assert_raises(self, "server.stat('no-such-file')",
                          globals(), locals(), OSError, msg)

            # Remove zipped contents.
            server.remove('xyzzy')
            server.remove('fred')

            if sys.platform == 'win32':
                msg = '[Error 2] The system cannot find the file specified'
            else:
                msg = "[Errno 2] No such file or directory: 'xyzzy'"
            assert_raises(self, "server.remove('xyzzy')",
                          globals(), locals(), OSError, msg)

            # Unpack.
            server.unpack_zipfile('zipped')
            server.chmod('zipped', 0600)  # Ensure we can remove under Windows.

            # Verify contents.
            with server.open('xyzzy', 'r') as inp:
                data = inp.read()
                self.assertEqual(data, 'Hello world!\n')

            inp = server.open('fred', 'r')
            try:
                data = inp.read(1000)
                self.assertEqual(data, 'Hello fred!\n')
            finally:
                inp.close()

            # Try to create a process.
            args = 'dir' if sys.platform == 'win32' else 'ls'
            try:
                proc = server.create('subprocess.Popen', args=args, shell=True)
                proc.wait()
            except TypeError as exc:
                msg = "'subprocess.Popen' is not an allowed type"
                self.assertEqual(str(exc), msg)
            else:
                self.fail('Expected TypeError')

            # isdir().
            self.assertTrue(server.isdir('.'))

            # listdir().
            self.assertEqual(sorted(server.listdir('.')),
                             [egg_info[0], 'fred', 'xyzzy', 'zipped'])
            if sys.platform == 'win32':
                msg = "[Error 3] The system cannot find the path specified: '42/*.*'"
            else:
                msg = "[Errno 2] No such file or directory: '42'"
            assert_raises(self, "server.listdir('42')",
                          globals(), locals(), OSError, msg)
        finally:
            SimulationRoot.chroot('..')
            shutil.rmtree(testdir, onerror=onerror)
    def test_server(self):
        logging.debug("")
        logging.debug("test_server")

        testdir = "test_server"
        if os.path.exists(testdir):
            shutil.rmtree(testdir)
        os.mkdir(testdir)
        os.chdir(testdir)

        try:
            # Create a server.
            server = ObjServer()

            # Create a component.
            exec_comp = server.create("openmdao.test.execcomp.ExecComp")
            exec_comp.run()
            egg_info = exec_comp.save_to_egg("exec_comp", "0")

            # Echo some arguments.
            args = server.echo("Hello", "world!")
            self.assertEqual(args[0], "Hello")
            self.assertEqual(args[1], "world!")

            # Try to execute a command.
            cmd = "dir" if sys.platform == "win32" else "ls"
            rdesc = {"remote_command": cmd, "output_path": "cmd.out", "hard_runtime_limit": 10}
            code = "server.execute_command(rdesc)"
            assert_raises(self, code, globals(), locals(), RuntimeError, "shell access is not allowed by this server")

            # Try to load a model.
            assert_raises(
                self,
                "server.load_model(egg_info[0])",
                globals(),
                locals(),
                RuntimeError,
                "shell access is not allowed by this server",
            )

            # Bogus file accesses.
            assert_raises(
                self,
                "server.open('../xyzzy', 'r')",
                globals(),
                locals(),
                RuntimeError,
                "Can't open '../xyzzy', not within root ",
            )
            assert_raises(
                self,
                "server.open('xyzzy', 'r')",
                globals(),
                locals(),
                IOError,
                "[Errno 2] No such file or directory: 'xyzzy'",
            )

            # Create a file using context.
            with server.open("xyzzy", "w") as out:
                out.write("Hello world!\n")

            # Create another file using file proxy.
            out = server.open("fred", "w")
            out.write("Hello fred!\n")
            out.flush()  # Not really necessary, just for coverage.
            out.close()

            # Zip it.
            server.pack_zipfile(["xyzzy", "fred"], "zipped")

            # Make it read-only.
            server.chmod("zipped", 0400)

            if sys.platform == "win32":
                msg = "[Error 2] The system cannot find the file specified"
            else:
                msg = "[Errno 2] No such file or directory: 'no-such-file'"
            assert_raises(self, "server.chmod('no-such-file', 0400)", globals(), locals(), OSError, msg)

            # Get stats.
            info = server.stat("zipped")

            assert_raises(self, "server.stat('no-such-file')", globals(), locals(), OSError, msg)

            # Remove zipped contents.
            server.remove("xyzzy")
            server.remove("fred")

            if sys.platform == "win32":
                msg = "[Error 2] The system cannot find the file specified"
            else:
                msg = "[Errno 2] No such file or directory: 'xyzzy'"
            assert_raises(self, "server.remove('xyzzy')", globals(), locals(), OSError, msg)

            # Unpack.
            server.unpack_zipfile("zipped")
            server.chmod("zipped", 0600)  # Ensure we can remove under Windows.

            # Verify contents.
            with server.open("xyzzy", "r") as inp:
                data = inp.read()
                self.assertEqual(data, "Hello world!\n")

            inp = server.open("fred", "r")
            try:
                data = inp.read(1000)
                self.assertEqual(data, "Hello fred!\n")
            finally:
                inp.close()

            # Try to create a process.
            args = "dir" if sys.platform == "win32" else "ls"
            try:
                proc = server.create("subprocess.Popen", args=args, shell=True)
                proc.wait()
            except TypeError as exc:
                msg = "'subprocess.Popen' is not an allowed type"
                self.assertEqual(str(exc), msg)
            else:
                self.fail("Expected TypeError")

        finally:
            SimulationRoot.chroot("..")
            shutil.rmtree(testdir)