def get_cluster(self, cloud_provider=None, config=None, nodes=None): if not cloud_provider: cloud_provider = BotoCloudProvider("https://hobbes.gc3.uzh.ch/", "nova", "a-key", "s-key") if not config: config = Configuration().get_config(self.path) setup = Mock() configurator = Configurator(config) conf_login = configurator.cluster_conf['mycluster']['login'] repository = PickleRepository(self.storage_path) cluster = Cluster( name="mycluster", cloud_provider=cloud_provider, setup_provider=setup, repository=repository, user_key_name=conf_login['user_key_name'], user_key_public=conf_login['user_key_public'], user_key_private=conf_login['user_key_private'], ) if not nodes: nodes = {"compute": 2, "frontend": 1} for kind, num in nodes.iteritems(): conf_kind = configurator.cluster_conf['mycluster']['nodes'][kind] cluster.add_nodes(kind, num, conf_kind['image_id'], conf_login['image_user'], conf_kind['flavor'], conf_kind['security_group']) return cluster
def test_get_frontend_node(self): """ Get frontend node """ config = Configuration().get_config(self.path) ssh_to = "frontend" config["mycluster"]["cluster"]["ssh_to"] = ssh_to cluster = self.get_cluster(config=config) cluster.ssh_to = ssh_to frontend = cluster.get_frontend_node() self.assertEqual(cluster.nodes['frontend'][0], frontend)
def test_dict_mixin(self): """Check that the node class can be seen as dictionary""" config = Configuration().get_config(self.path) ssh_to = "frontend" config["mycluster"]["cluster"]["ssh_to"] = ssh_to cluster = self.get_cluster(config=config) cluster.ssh_to = ssh_to frontend = cluster.get_frontend_node() dcluster = dict(cluster) self.assertEqual(dcluster['ssh_to'], ssh_to) self.assertEqual(dcluster['nodes'].keys(), cluster.nodes.keys()) self.failUnlessRaises(KeyError, lambda x: x['_cloud_provider'], dcluster) self.assertEqual(cluster['_cloud_provider'], cluster._cloud_provider)
def setUp(self): file, path = tempfile.mkstemp() self.path = path self.config = Configuration().get_config(self.path)
class TestConfigurator(unittest.TestCase): def setUp(self): file, path = tempfile.mkstemp() self.path = path self.config = Configuration().get_config(self.path) def tearDown(self): os.unlink(self.path) def test_create_cloud_provider(self): configurator = Configurator(self.config) provider = configurator.create_cloud_provider("mycluster") url = self.config['mycluster']['cloud']['ec2_url'] self.assertEqual(provider._url, url) access_key = self.config['mycluster']['cloud']['ec2_access_key'] self.assertEqual(provider._access_key, access_key) secret_key = self.config['mycluster']['cloud']['ec2_secret_key'] self.assertEqual(provider._secret_key, secret_key) region = self.config['mycluster']['cloud']['ec2_region'] self.assertEqual(provider._region_name, region) def test_create_cluster(self): configurator = Configurator(self.config) cluster = configurator.create_cluster("mycluster") self.assertEqual(cluster.name, "mycluster") self.assertTrue(type(cluster._cloud_provider) is BotoCloudProvider) self.assertTrue(type(cluster._setup_provider) is AnsibleSetupProvider) self.assertTrue("compute" in cluster.nodes) self.assertTrue("frontend" in cluster.nodes) self.assertTrue(len(cluster.nodes["compute"]) == 2) self.assertTrue(len(cluster.nodes["frontend"]) == 1) def test_create_cluster_with_nodes_min(self): cfg = self.config.copy() cfg['mycluster']['cluster']['compute_nodes_min'] = 1 configurator = Configurator(cfg) cconf = configurator.cluster_conf['mycluster']['cluster'] self.assertEqual(cconf['compute_nodes_min'], 1) def test_load_cluster(self): # test without storage file storage_path = tempfile.mkdtemp() configurator = Configurator(self.config, storage_path=storage_path) self.assertRaises(ClusterNotFound, configurator.load_cluster, "mycluster") shutil.rmtree(storage_path) # TODO: test with storage file; the problem is to give a fixed # directory as a parameter to configurator, since it should work # anywhere def test_create_setup_provider(self): configurator = Configurator(self.config) provider = configurator.create_setup_provider("mycluster") self.assertTrue(type(provider) is AnsibleSetupProvider) conf = self.config['mycluster']['setup'] groups = dict((k[:-7], v.split(',')) for k, v in conf.items() if k.endswith('_groups')) self.assertEqual(groups, provider.groups) playbook_path = resource_filename('elasticluster', 'share/playbooks/site.yml') self.assertEqual(playbook_path, provider._playbook_path) storage_path = configurator.general_conf['storage_path'] self.assertEqual(provider._storage_path, storage_path) usr_sudo = self.config['mycluster']['login']['image_user_sudo'] self.assertEqual(provider._sudo_user, usr_sudo) sudo = self.config['mycluster']['login']['image_sudo'] self.assertEqual(provider._sudo, sudo) def test_setup_provider_using_environment(self): config = copy.deepcopy(self.config) configurator = Configurator(config) # Save current variable, modify it and check if it's correctly read SAVEDUSERNAME=os.getenv('OS_USERNAME') os.environ['OS_USERNAME'] = '******' provider = configurator.create_cloud_provider("os-cluster") try: self.assertEqual(provider._os_username, 'newusername') except: if SAVEDUSERNAME: os.environ['OS_USERNAME'] = SAVEDUSERNAME else: del os.environ['OS_USERNAME'] raise def test_storage_type(self): configurator = Configurator(self.config) repo = configurator.create_repository()