예제 #1
0
    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)
예제 #2
0
class WriteFile(Component):
    x = Float(1.0, iotype='in')
    y = Float(2.0, iotype='in')
    file_out = File(FileRef('x.in'), iotype='out')

    def execute(self):
        with open(self.file_out.path, 'w') as f:
            f.write('{:7.3f}\n'.format(self.x))
            f.write('{:7.3f}\n'.format(self.y))
예제 #3
0
    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)
예제 #4
0
    def test_component(self):
        logging.debug('')
        logging.debug('test_component')

        # We should get version 0.2 (with directory 'floyd').
        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()

        exe_dir = comp.get('exe_dir')
        if sys.platform == 'win32':
            expected = r'floyd\\ASTestComp'  # Odd that backslash is repeated.
        else:
            expected = os.path.join('floyd', 'ASTestComp')
        print 'exe_dir', exe_dir
        print 'expected', expected
        self.assertTrue(exe_dir.endswith(expected))

        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()
예제 #5
0
class Model(Assembly):
    """ Run multiple `Unique` component instances. """

    infile = File(iotype='in', local_path='input')
    outfile = File(FileRef('output'), iotype='out')

    def configure(self):
        self.add('a', Unique())
        self.add('b', Unique())
        self.driver.workflow.add(['a', 'b'])
        self.connect('infile', 'a.infile')
        self.connect('a.outfile', 'b.infile')
        self.connect('b.outfile', 'outfile')
예제 #6
0
class Source(Component):
    """ Source data for test component. """

    x = Float(iotype='out')
    y = Float(iotype='out')
    tof = Float(iotype='out')
    sof = Float(iotype='out')
    output = File(FileRef('output'), iotype='out')

    def execute(self):
        self.x = 6
        self.y = 7
        self.tof = 2.781828
        self.sof = 3.14159
        with open(self.output.path, 'w') as out:
            out.write('Hello world!')
예제 #7
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()
예제 #8
0
class Sleeper(ExternalCode):
    """ Used to test external code functionality. """

    delay = Int(1, units='s', iotype='in')
    env_filename = Str(iotype='in')
    infile = File(iotype='in', local_path='input')
    outfile = File(FileRef('output'), iotype='out')

    def __init__(self):
        super(Sleeper, self).__init__()
        self.directory = DIRECTORY
        self.external_files = [
            FileMetadata(path='sleep.py', input=True, constant=True),
        ]

    def execute(self):
        """ Runs code and sets `outfile`. """
        self.command = ['python', 'sleep.py', str(self.delay)]
        if self.env_filename:
            self.command.append(self.env_filename)
        super(Sleeper, self).execute()
예제 #9
0
    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'")

        # 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'")