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)