def setUp(self): # SimulationRoot is static and so some junk can be left # over from other tests when running under nose, so # set it to cwd here just to be safe SimulationRoot.chroot(os.getcwd()) self.asm = set_as_top(Assembly()) obj = self.asm.add('scomp1', SimpleComp()) self.asm.add('scomp2', SimpleComp()) self.asm.driver.workflow.add(['scomp1', 'scomp2']) with self.asm.dir_context: filename = 'top.data.vt' with open(filename, 'w') as out: out.write('vt data\n') obj.cont_in.data = FileRef(filename, self.asm) filename = 'top.data.vt2' with open(filename, 'w') as out: out.write('vt2 data\n') obj.cont_in.vt2.data = FileRef(filename, self.asm) filename = 'top.data.vt3' with open(filename, 'w') as out: out.write('vt3 data\n') obj.cont_in.vt2.vt3.data = FileRef(filename, self.asm)
def execute(self): self.cont_out.v1 = self.cont_in.v1 + 1.0 self.cont_out.v2 = self.cont_in.v2 + 1.0 self.cont_out.vt2.x = self.cont_in.vt2.x + 1.0 self.cont_out.vt2.y = self.cont_in.vt2.y + 1.0 self.cont_out.vt2.vt3.a = self.cont_in.vt2.vt3.a self.cont_out.vt2.vt3.b = self.cont_in.vt2.vt3.b if self.cont_in.data is not None: with self.cont_in.data.open() as inp: filename = '%s.data.vt' % self.name with open(filename, 'w') as out: out.write(inp.read()) self.cont_out.data = FileRef(filename, self) if self.cont_in.vt2.data is not None: with self.cont_in.vt2.data.open() as inp: filename = '%s.data.vt2' % self.name with open(filename, 'w') as out: out.write(inp.read()) self.cont_out.vt2.data = FileRef(filename, self) if self.cont_in.vt2.vt3.data is not None: with self.cont_in.vt2.vt3.data.open() as inp: filename = '%s.data.vt3' % self.name with open(filename, 'w') as out: out.write(inp.read()) self.cont_out.vt2.vt3.data = FileRef(filename, self)
def test_no_owner(self): logging.debug("") logging.debug("test_no_owner") # Absolute FileRef. path = os.path.join(os.sep, "xyzzy") ref = FileRef(path) try: ref.open() except ValueError, exc: msg = "Path '%s' is absolute and no path checker is available." % path self.assertEqual(str(exc), msg)
def test_no_owner(self): logging.debug('') logging.debug('test_no_owner') # Absolute FileRef. path = os.path.join(os.sep, 'xyzzy') ref = FileRef(path) try: ref.open() except ValueError, exc: msg = "Path '%s' is absolute and no path checker is available." \ % path self.assertEqual(str(exc), msg)
def test_unique(self): logging.debug('') logging.debug('test_unique') model = Model() ## This part isn't valid any more because Assemblies now do not configure ## themselves unless they're part of a rooted hierarchy. That means in this ## case that model.a and model.b won't exist until set_as_top is called on model #for comp in (model.a, model.b): #self.assertEqual(comp.create_instance_dir, True) #self.assertNotEqual(model.a.directory, 'a') #self.assertNotEqual(model.b.directory, 'b') set_as_top(model) for comp in (model.a, model.b): self.assertEqual(comp.create_instance_dir, False) self.assertEqual(comp.return_code, 0) self.assertEqual(comp.timed_out, False) self.assertEqual(model.a.directory, 'a') self.assertEqual(model.b.directory, 'b') model.infile = FileRef(INP_FILE, model, input=True) model.run() for comp in (model.a, model.b): self.assertEqual(comp.return_code, 0) self.assertEqual(comp.timed_out, False) with model.outfile.open() as inp: result = inp.read() self.assertEqual(result, INP_DATA)
def test_save_load(self): logging.debug('') logging.debug('test_save_load') sleeper = set_as_top(Sleeper()) sleeper.name = 'Sleepy' sleeper.infile = FileRef(INP_FILE, sleeper, input=True) # Exercise check_save_load(). retcode = check_save_load(sleeper) self.assertEqual(retcode, 0)
def test_remote(self): logging.debug('') logging.debug('test_remote') init_cluster(allow_shell=True) sleeper = set_as_top(Sleeper()) sleeper.env_filename = ENV_FILE sleeper.env_vars = {'SLEEP_DATA': 'Hello world!'} sleeper.external_files.append(FileMetadata(path=ENV_FILE, output=True)) sleeper.infile = FileRef(INP_FILE, sleeper, input=True) sleeper.timeout = 5 sleeper.resources = {'min_cpus': 1} sleeper.run() self.assertEqual(sleeper.return_code, 0) self.assertEqual(sleeper.timed_out, False) self.assertEqual(os.path.exists(ENV_FILE), True) with open(ENV_FILE, 'r') as inp: data = inp.readline().rstrip() self.assertEqual(data, sleeper.env_vars['SLEEP_DATA']) with sleeper.outfile.open() as inp: result = inp.read() self.assertEqual(result, INP_DATA) # Null input file. sleeper.stdin = '' assert_raises( self, 'sleeper.run()', globals(), locals(), ValueError, ": Remote execution requires stdin of DEV_NULL or" " filename, got ''") # Specified stdin, stdout, and join stderr. with open('sleep.in', 'w') as out: out.write('froboz is a pig!\n') sleeper.stdin = 'sleep.in' sleeper.stdout = 'sleep.out' sleeper.stderr = ExternalCode.STDOUT sleeper.run() # Null stderr. sleeper.stderr = None sleeper.run()
def test_component(self): logging.debug('') logging.debug('test_component') comp = set_as_top(self.factory.create('ASTestComp')) comp.set('x', 6) comp.set('y', 7) comp.set('obj_input.tof', 2.781828) comp.set('obj_input.subobj.sof', 3.14159) path = 'output' with comp.dir_context: with open(path, 'w') as out: out.write('Hello world!') comp.set('in_file', FileRef(path, comp)) with comp.dir_context: os.remove(path) comp.run() self.assertEqual(comp.get('z'), 42.) self.assertEqual(comp.get('obj_output.tof'), 2.781828) self.assertEqual(comp.get('obj_output.subobj.sof'), 3.14159) with comp.get('out_file').open() as inp: data = inp.read() self.assertEqual(data, 'Hello world!') result_b = comp.float_method() before = self.get_state(comp, 'the_obj', comp._client) state_file = 'state.pickle' try: comp.save(state_file) restored = Component.load(state_file) result_a = restored.float_method() after = self.get_state(restored, 'the_obj', restored._client) restored.pre_delete() self.assertEqual(result_a, result_b) self.compare_states(before, after) finally: os.remove(state_file) os.remove('AS-the_obj.in_file.dat') os.remove('AS-the_obj.out_file.dat') comp.pre_delete()
def get(self, obj, name): """ Get remote value and write to local file. obj: object Containing object, ignored. name: string Name in `obj`, ignored. """ if self._component._call_tree_rooted: return None # Not initialized. if self._client is None: # Happens during component.__setstate__ return None binary = self._client.get(self._rpath + '.isBinary') == 'true' valstr = self.rget() mode = 'wb' if binary else 'w' with self._component.dir_context: with open(self._path, mode) as out: out.write(valstr) return FileRef(self._path, self._component, binary=binary)
# Absolute FileRef. path = os.path.join(os.sep, 'xyzzy') ref = FileRef(path) try: ref.open() except ValueError, exc: msg = "Path '%s' is absolute and no path checker is available." \ % path self.assertEqual(str(exc), msg) else: self.fail('Expected ValueError') # Relative FileRef. path = 'xyzzy' ref = FileRef(path) try: ref.open() except ValueError, exc: msg = "Path '%s' is relative and no absolute directory is available." \ % path self.assertEqual(str(exc), msg) else: self.fail('Expected ValueError') def test_bad_trait(self): logging.debug('') logging.debug('test_bad_trait') try: File(42)
i1d = Array(dtype=int, iotype='in', desc='1D int array', default_value=[1, 2, 3, 4, 5, 6, 7, 8, 9]) s1d = List(Str, iotype='in', desc='1D string array', value=['Hello', 'from', 'TestComponent.SubGroup']) flst = List(Float, iotype='in', desc='List of floats') ilst = List(Int, iotype='in', desc='List of ints') class Bogus(object): """ To test instantiation. """ def __init__(self, need_one_argument): self._arg = need_one_argument if __name__ == '__main__': top = Assembly() comp = top.add('comp', TestComponent()) comp.in_file = FileRef(path='ASTestComp-0.1.cfg', owner=top) comp.run() for path in ('x', 'y', 'z', 'exe_count', 'sub_group.b', 'sub_group.f', 'sub_group.i', 'sub_group.s', 'sub_group.fe', 'sub_group.ie', 'sub_group.se', 'sub_group.f1d', 'sub_group.i1d', 'sub_group.s1d'): print '%s: %s' % (path, comp.get(path))
def test_normal(self): logging.debug('') logging.debug('test_normal') sleeper = set_as_top(Sleeper()) sleeper.env_filename = ENV_FILE sleeper.env_vars = {'SLEEP_DATA': 'Hello world!'} sleeper.external_files.append(FileMetadata(path=ENV_FILE, output=True)) sleeper.infile = FileRef(INP_FILE, sleeper, input=True) sleeper.stderr = None sleeper.run() self.assertEqual(sleeper.return_code, 0) self.assertEqual(sleeper.timed_out, False) self.assertEqual(os.path.exists(ENV_FILE), True) with open(ENV_FILE, 'rU') as inp: data = inp.readline().rstrip() self.assertEqual(data, sleeper.env_vars['SLEEP_DATA']) with sleeper.outfile.open() as inp: result = inp.read() self.assertEqual(result, INP_DATA) # Force an error. sleeper.stderr = 'sleep.err' sleeper.delay = -1 assert_raises(self, 'sleeper.run()', globals(), locals(), RuntimeError, ': return_code = 1') sleeper.delay = 1 # Redirect stdout & stderr. sleeper.env_vars = {'SLEEP_ECHO': '1'} sleeper.stdin = 'sleep.in' sleeper.stdout = 'sleep.out' sleeper.stderr = 'sleep.err' with open('sleep.in', 'w') as out: out.write('Hello World!\n') sleeper.run() with open('sleep.out', 'r') as inp: self.assertEqual(inp.read(), 'stdin echo to stdout\n' 'Hello World!\n') with open('sleep.err', 'r') as inp: self.assertEqual(inp.read(), 'stdin echo to stderr\n' 'Hello World!\n') # Exercise check_files() errors. os.remove('input') assert_raises(self, 'sleeper.check_files(inputs=True)', globals(), locals(), RuntimeError, ": missing 'in' file 'input'") os.remove('output') assert_raises(self, 'sleeper.check_files(inputs=False)', globals(), locals(), RuntimeError, ": missing 'out' file 'output'") os.remove('sleep.in') assert_raises(self, 'sleeper.check_files(inputs=True)', globals(), locals(), RuntimeError, ": missing stdin file 'sleep.in'") os.remove('sleep.err') assert_raises(self, 'sleeper.check_files(inputs=False)', globals(), locals(), RuntimeError, ": missing stderr file 'sleep.err'") os.remove('sleep.out') assert_raises(self, 'sleeper.check_files(inputs=False)', globals(), locals(), RuntimeError, ": missing stdout file 'sleep.out'") # Now show that existing outputs are removed before execution. with open('input', 'w') as out: out.write(INP_DATA) sleeper.stdin = None sleeper.stdout = None sleeper.stderr = None sleeper.env_vars = {} sleeper.env_filename = '' assert_raises(self, 'sleeper.run()', globals(), locals(), RuntimeError, ": missing output file 'env-data'") # Show that non-existent expected files are detected. sleeper.external_files.append( FileMetadata(path='missing-input', input=True)) assert_raises(self, 'sleeper.run()', globals(), locals(), RuntimeError, ": missing input file 'missing-input'")