

def test_configure(self):
    logging.debug('')
    logging.debug('test_configure')
    with open('resources.cfg', 'w') as out:
        out.write('\n[LocalHost]\nmax_load: 100\n')
    local = RAM.get_allocator('LocalHost')
    max_load = local.max_load
    try:
        self.assertTrue((max_load < 100))
        RAM.configure('resources.cfg')
        self.assertEqual(local.max_load, 100)
        local.max_load = max_load
    finally:
        os.remove('resources.cfg')
    with open('resources.cfg', 'w') as out:
        out.write('\n[Local2]\nclassname: openmdao.main.resource.LocalAllocator\nauthkey: PublicKey\nallow_shell: False\ntotal_cpus: 42\nmax_load: 200\n')
    try:
        RAM.configure('resources.cfg')
        local2 = RAM.get_allocator('Local2')
        self.assertEqual(local2.factory._authkey, 'PublicKey')
        self.assertEqual(local2.factory._allow_shell, False)
        self.assertEqual(local2.total_cpus, 42)
        self.assertEqual(local2.max_load, 200)
        self.assertEqual(local2.host, socket.gethostname())
        self.assertTrue((local2.pid > 0))
        RAM.remove_allocator('Local2')
    finally:
        os.remove('resources.cfg')
    with open('resources.cfg', 'w') as out:
        out.write('\n[Local2]\nclassname: openmdao.main.resource.LocalAllocator\ntotal_cpus: 0\n')
    try:
        assert_raises(self, "RAM.configure('resources.cfg')", globals(), locals(), ValueError, 'Local2: total_cpus must be > 0, got 0')
    finally:
        os.remove('resources.cfg')
    with open('resources.cfg', 'w') as out:
        out.write('\n[Local2]\nclassname: openmdao.main.resource.LocalAllocator\nmax_load: 0\n')
    try:
        assert_raises(self, "RAM.configure('resources.cfg')", globals(), locals(), ValueError, 'Local2: max_load must be > 0, got 0')
    finally:
        os.remove('resources.cfg')
    with open('resources.cfg', 'w') as out:
        out.write('\n[BadModule]\nclassname: no-such-module.Allocator\nmax_load: 100\n')
    try:
        assert_raises(self, "RAM.configure('resources.cfg')", globals(), locals(), RuntimeError, "RAM configure BadModule: can't import 'no-such-module'")
    finally:
        os.remove('resources.cfg')
    with open('resources.cfg', 'w') as out:
        out.write('\n[BadClass]\nclassname: openmdao.main.resource.NoSuchAllocator\nmax_load: 100\n')
    try:
        assert_raises(self, "RAM.configure('resources.cfg')", globals(), locals(), RuntimeError, "RAM configure BadClass: no class 'NoSuchAllocator' in openmdao.main.resource")
    finally:
        os.remove('resources.cfg')
    local3 = LocalAllocator('Local3')
    local4 = LocalAllocator('Local4', total_cpus=4)
    RAM.add_allocator(local3)
    try:
        allocator_names = [allocator.name for allocator in RAM.list_allocators()]
        self.assertEqual(allocator_names, ['LocalHost', 'Local3'])
        self.assertTrue((RAM.get_allocator('Local3') is local3))
        self.assertTrue((RAM.get_allocator(1) is local3))
        RAM.insert_allocator(0, local4)
        try:
            allocator_names = [allocator.name for allocator in RAM.list_allocators()]
            self.assertEqual(allocator_names, ['Local4', 'LocalHost', 'Local3'])
        finally:
            RAM.remove_allocator('Local4')
    finally:
        RAM.remove_allocator(1)
    assert_raises(self, "RAM.get_allocator('Local3')", globals(), locals(), ValueError, "allocator 'Local3' not found")
    assert_raises(self, "RAM.remove_allocator('Local3')", globals(), locals(), ValueError, "allocator 'Local3' not found")
    assert_raises(self, "LocalAllocator('BadLoad', max_load=-2)", globals(), locals(), ValueError, 'BadLoad: max_load must be > 0, got -2')

dataset/ETHPy150Open OpenMDAO/OpenMDAO-Framework/openmdao.main/src/openmdao/main/test/test_resource.py TestCase.test_configure/original