def test_load(self):
        """
        Verify that a plugin can be loaded successfully through a pkg_resources
        entry point.
        """
        logging.debug('')
        logging.debug('test_load')

        # make sure we're looking in the right spot for the plugins whether
        # we're in a develop egg or in the released version
        dist = working_set.find(Requirement.parse('openmdao.test'))
        fact = PkgResourcesFactory(['openmdao.component'],
                                   [os.path.join(dist.location,
                                                 'openmdao',
                                                 'test','plugins')],
                                   )
        
        comp = fact.create('testplugins.components.dumb.DumbComponent')
        logging.debug('    loaders:')
        for key, value in fact._loaders.items():
            logging.debug('        %s:', key)
            for val in value:
                logging.debug('                name: %s', val.name)
                logging.debug('               group: %s', val.group)
                logging.debug('                dist: %s', val.dist)
                logging.debug('            entry_pt: %s', val.entry_pt)
                logging.debug('                ctor: %s', val.ctor)
                logging.debug('')

        self.assertEqual(comp.svar,'abcdefg')
        comp.run()
        self.assertEqual(comp.svar,'gfedcba')
    def test_get_loaders(self):
        """test retrieval of loaders"""
        # Get a list of entry point loaders for the openmdao.dumbplugins 
        # group.       
        dist = working_set.find(Requirement.parse('openmdao.test'))
        fact = PkgResourcesFactory(['openmdao.dumbplugins'],
                                   [os.path.join(dist.location,
                                                 'openmdao',
                                                 'test','plugins')])
        # first, look for active loaders. list should be empty
        dumb_loaders = fact.get_loaders('openmdao.dumbplugins')
        self.assertEqual(len(dumb_loaders), 0)
        
        # now, get all of the loaders, including inactive ones
        dumb_loaders = fact.get_loaders('openmdao.dumbplugins', active=False)
        self.assertEqual(len(dumb_loaders), 6)
        self.assertEqual(dumb_loaders[0].name, 'bar.Comp1Plugin')
        self.assertEqual(dumb_loaders[0].dist.version, '1.0')
        self.assertEqual(dumb_loaders[0].dist.project_name, 'bar')
        self.assertEqual(dumb_loaders[0].ctor, None)

        # now, create a plugin object, which will make its loader active
        comp = fact.create('bar.Comp1Plugin')
        self.assertEqual(comp.version, '1.0')
        
        # now there should be one active loader (its ctor should not be None)
        dumb_loaders = fact.get_loaders('openmdao.dumbplugins')
        self.assertEqual(len(dumb_loaders), 1)
        self.assertEqual(dumb_loaders[0].name, 'bar.Comp1Plugin')
        self.assertEqual(dumb_loaders[0].dist.version, '1.0')
        self.assertEqual(dumb_loaders[0].dist.project_name, 'bar')
        mybar = dumb_loaders[0].create(None)
        self.assertEqual(mybar.version, '1.0')
 def test_load_version(self):
     """load a specific version, then try to load a conflicting version"""
     
     dist = working_set.find(Requirement.parse('openmdao.test'))
     fact = PkgResourcesFactory(['openmdao.dumbplugins'],
                                [os.path.join(dist.location,
                                              'openmdao','test',
                                              'plugins')])
     foo = fact.create('foo.Comp1Plugin', version='1.0')
     self.assertEqual(foo.version, '1.0')
     
     # now try to create an object that requires a conflicting version of foo
     self.assertRaises(VersionConflict,
                       fact.create,'foo.Comp1Plugin',
                       version='1.4')
     
     # now request a non-existent version of foo
     foo10 = fact.create('foo.Comp1Plugin', version='10.5')
     self.assertEqual(foo10, None)
    def test_load(self):
        # make sure we're looking in the right spot for the plugins whether
        # we're in a develop egg or in the released version
        dist = working_set.find(Requirement.parse('openmdao.test'))
        fact = PkgResourcesFactory(['openmdao.component'], None)

        comp = fact.create('openmdao.test.execcomp.ExecComp',
                           exprs=['x = a+1', 'y=b-2', 'z=x*2'])
        comp.a = 4
        comp.b = 2
        comp.run()
        self.assertEqual(comp.x, 5)
        self.assertEqual(comp.y, 0)
        self.assertEqual(comp.z, 10)
 def test_load(self):
     # make sure we're looking in the right spot for the plugins whether
     # we're in a develop egg or in the released version
     dist = working_set.find(Requirement.parse('openmdao.test'))
     fact = PkgResourcesFactory(['openmdao.component'], None)
     
     comp = fact.create('openmdao.test.execcomp.ExecComp', 
                        exprs=['x = a+1','y=b-2','z=x*2'])
     comp.a = 4
     comp.b = 2
     comp.run()
     self.assertEqual(comp.x, 5)
     self.assertEqual(comp.y, 0)
     self.assertEqual(comp.z, 10)
Exemplo n.º 6
0
    def test_pkg_resources_factory(self):
        # NOTE: this test fails if run standalone:
        #       ImportError: No module named test_egg_save
        # Probably need Egg_TestModel.test_egg_save, or adjusted sys.path.
        if MODULE_NAME == '__main__':
            return

        logging.debug('')
        logging.debug('test_pkg_resources_factory')

        # Write to egg.
        egg_info = self.model.save_to_egg(self.model.name, next_egg(),
                                          py_dir=PY_DIR,
                                          child_objs=self.child_objs)
        self.egg_name = egg_info[0]

        # Create factory.
        factory = PkgResourcesFactory(['openmdao.component',
                                       'openmdao.container'],
                                      [os.getcwd()])

        # Create and move to test directory.
        orig_dir = os.getcwd()
        test_dir = 'EggTest'
        if os.path.exists(test_dir):
            shutil.rmtree(test_dir, onerror=onerror)
        os.mkdir(test_dir)
        os.chdir(test_dir)
        try:
            # Check multiple model instances.
            self.create_and_check_model(factory, 'test_model_1',
                                        'Hello world!\n')
            self.create_and_check_model(factory, 'test_model_2',
                                        'Hello world!\n')

            # Check that reloading doesn't clobber existing files.
            file_data = 'New and interesting stuff\n'
            path = os.path.join('test_model_2', 'Source', EXTERNAL_FILES[2])
            out = open(path, 'w')
            out.write(file_data)
            out.close()
            logging.debug('updated %s', path)
            self.create_and_check_model(factory, 'test_model_2', file_data)

            # Check observations.
            global OBSERVATIONS
            OBSERVATIONS = []
            model = factory.create('Egg_TestModel', name='observed',
                                   observer=observer)
            if model is None:
                self.fail("Create of 'observed' failed.")
            expected = [
                ('copy', 'Source/xyzzy'),
                ('copy', 'sub/data2'),
                ('copy', 'Source/hello'),
                ('copy', 'sub/data4'),
                ('complete', 'observed'),
            ]
            self.assertEqual(len(OBSERVATIONS), len(expected))
            for i, observation in enumerate(OBSERVATIONS):
                state, string, file_fraction, byte_fraction = observation
                self.assertEqual(state, expected[i][0])
                self.assertEqual(string, expected[i][1])
                self.assertEqual(file_fraction, float(i) / float(len(expected) - 1))

            # Create a component.
            comp = factory.create('Egg_TestModel.Oddball', name='test_comp',
                                  observer=observer)
            if comp is None:
                self.fail('Create of test_comp failed.')
            self.assertEqual(comp.executions, 0)
            comp.run()
            self.assertEqual(comp.executions, 3)
            self.assertEqual(comp.get_pathname(), 'test_comp')

            # Create a (sub)component.
            sub = factory.create('Egg_TestModel.Oddball.oddcomp',
                                 name='test_sub')
            if sub is None:
                self.fail('Create of test_sub failed.')
            self.assertEqual(sub.get_pathname(), 'test_sub')

            # Create a (sub)container.
            sub = factory.create('Egg_TestModel.Oddball.oddcont',
                                 name='test_sub')
            if sub is None:
                self.fail('Create of test_sub failed.')
            self.assertEqual(sub.get_pathname(), 'test_sub')

            # Try a non-existent entry point.
            obj = factory.create('no-such-entry', name='xyzzy')
            self.assertEqual(obj, None)

        finally:
            os.chdir(orig_dir)
            shutil.rmtree(test_dir, onerror=onerror)
Exemplo n.º 7
0
    def test_pkg_resources_factory(self):
        # NOTE: this test fails if run standalone:
        #       ImportError: No module named test_egg_save
        # Probably need Egg_TestModel.test_egg_save, or adjusted sys.path.
        if MODULE_NAME == '__main__':
            return

        logging.debug('')
        logging.debug('test_pkg_resources_factory')

        # Write to egg.
        egg_info = self.model.save_to_egg(self.model.name, next_egg(),
                                          py_dir=PY_DIR,
                                          child_objs=self.child_objs)
        self.egg_name = egg_info[0]

        # Create factory.
        factory = PkgResourcesFactory(['openmdao.component',
                                       'openmdao.container'],
                                      [os.getcwd()])

        # Create and move to test directory.
        orig_dir = os.getcwd()
        test_dir = 'EggTest'
        if os.path.exists(test_dir):
            shutil.rmtree(test_dir, onerror=onerror)
        os.mkdir(test_dir)
        os.chdir(test_dir)
        try:
            # Check multiple model instances.
            self.create_and_check_model(factory, 'test_model_1',
                                        'Hello world!\n')
            self.create_and_check_model(factory, 'test_model_2',
                                        'Hello world!\n')

            # Check that reloading doesn't clobber existing files.
            file_data = 'New and interesting stuff\n'
            path = os.path.join('test_model_2', 'Source', EXTERNAL_FILES[2])
            out = open(path, 'w')
            out.write(file_data)
            out.close()
            logging.debug('updated %s', path)
            self.create_and_check_model(factory, 'test_model_2', file_data)

            # Check observations.
            global OBSERVATIONS
            OBSERVATIONS = []
            model = factory.create('Egg_TestModel', name='observed',
                                   observer=observer)
            if model is None:
                self.fail("Create of 'observed' failed.")
            expected = [
                ('copy', 'Source/xyzzy'),
                ('copy', 'sub/data2'),
                ('copy', 'Source/hello'),
                ('copy', 'sub/data4'),
                ('complete', 'observed'),
            ]
            self.assertEqual(len(OBSERVATIONS), len(expected))
            for i, observation in enumerate(OBSERVATIONS):
                state, string, file_fraction, byte_fraction = observation
                self.assertEqual(state,  expected[i][0])
                self.assertEqual(string, expected[i][1])
                self.assertEqual(file_fraction, float(i)/float(len(expected)-1))

            # Create a component.
            comp = factory.create('Egg_TestModel.Oddball', name='test_comp',
                                  observer=observer)
            if comp is None:
                self.fail('Create of test_comp failed.')
            self.assertEqual(comp.executions, 0)
            comp.run()
            self.assertEqual(comp.executions, 3)
            self.assertEqual(comp.get_pathname(), 'test_comp')

            # Create a (sub)component.
            sub = factory.create('Egg_TestModel.Oddball.oddcomp',
                                 name='test_sub')
            if sub is None:
                self.fail('Create of test_sub failed.')
            self.assertEqual(sub.get_pathname(), 'test_sub')

            # Create a (sub)container.
            sub = factory.create('Egg_TestModel.Oddball.oddcont',
                                 name='test_sub')
            if sub is None:
                self.fail('Create of test_sub failed.')
            self.assertEqual(sub.get_pathname(), 'test_sub')

            # Try a non-existent entry point.
            obj = factory.create('no-such-entry', name='xyzzy')
            self.assertEqual(obj, None)

        finally:
            os.chdir(orig_dir)
            shutil.rmtree(test_dir, onerror=onerror)