def test_altervm(self): """Test AlterVM""" radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("cpu.count", "=", 1), Feature("memory.size", "=", 512, "M"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure(str(radl), auth0) radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("cpu.count", "=", 2), Feature("memory.size", "=", 1024, "M"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) radl_info = IM.AlterVM(infId, "0", str(radl), auth0) parsed_radl_info = parse_radl(str(radl_info)) self.assertEqual(parsed_radl_info.systems[0].getValue("cpu.count"), 2) self.assertEqual( parsed_radl_info.systems[0].getFeature('memory.size').getValue( 'M'), 1024) IM.DestroyInfrastructure(infId, auth0)
def test_inf_cloud_order(self, suds_cli): """Test cloud selection in base of the auth data order.""" n0, n1 = 1, 1 # Machines to deploy radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("cpu.count", "=", 1), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", n0)) radl.add( system("s1", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("cpu.count", "=", 1), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s1", n1)) cloud0 = self.get_cloud_connector_mock("MyMock0") self.register_cloudconnector("Mock0", cloud0) cloud1 = self.get_cloud_connector_mock("MyMock1") self.register_cloudconnector("Mock1", cloud1) auth0 = self.getAuth([0], [0], [("Mock0", 0), ("Mock1", 1)]) infId = IM.CreateInfrastructure(str(radl), auth0) self.assertEqual(cloud0.launch.call_count, n0 + n1) IM.DestroyInfrastructure(infId, auth0)
def test_inf_addresources0(self): """Deploy single virtual machines and test reference.""" radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure("", auth0) vms = IM.AddResource(infId, str(radl), auth0) self.assertEqual(vms, [0]) # Test references radl = RADL() radl.add(system("s0", reference=True)) radl.add(deploy("s0", 1)) vms = IM.AddResource(infId, str(radl), auth0) self.assertEqual(vms, [1]) IM.DestroyInfrastructure(infId, auth0)
def test_create_disk_snapshot(self): """Test CreateDiskSnapshot """ radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) new_url = "mock0://linux.for.ev.er/test" cloud0 = self.get_cloud_connector_mock("MyMock0") cloud0.create_snapshot = Mock(return_value=(True, new_url)) self.register_cloudconnector("Mock0", cloud0) auth0 = self.getAuth([0], [], [("Mock0", 0)]) infId = IM.CreateInfrastructure(str(radl), auth0) InfrastructureList.infrastructure_list[infId].vm_list[ 0].cloud_connector = cloud0 res = IM.CreateDiskSnapshot(infId, 0, 0, "test", True, auth0) self.assertEqual(res, new_url) self.assertEqual(cloud0.create_snapshot.call_count, 1)
def test_inf_addresources_without_credentials(self): """Deploy single virtual machine without credentials to check that it raises the correct exception.""" radl = RADL() radl.add( system( "s0", [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")])) radl.add(deploy("s0", 1)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure("", auth0) vms = IM.AddResource(infId, str(radl), auth0) self.assertEqual(vms, [0]) res = IM.GetInfrastructureState(infId, auth0) self.assertEqual(res['state'], VirtualMachine.FAILED) res = IM.GetVMContMsg(infId, 0, auth0) self.assertEqual(res, ( "Error launching the VMs of type s0 to cloud ID cloud0 of type Dummy." " No username for deploy: s0\n")) IM.DestroyInfrastructure(infId, auth0)
def test_create_async(self): """Create Inf. async""" radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) cloud = type("MyMock0", (CloudConnector, object), {}) cloud.launch = Mock(side_effect=self.sleep_and_create_vm) cloud.finalize = Mock(return_value=(True, "")) self.register_cloudconnector("Mock", cloud) auth0 = self.getAuth([0], [], [("Mock", 0)]) before = int(time.time()) infId = IM.CreateInfrastructure(str(radl), auth0, True) delay = int(time.time()) - before self.assertLess(delay, 2) time.sleep(6) IM.DestroyInfrastructure(infId, auth0)
def test_inf_auth_with_token(self): im_auth = {"token": (self.gen_token())} im_auth['username'] = InfrastructureInfo.OPENID_USER_PREFIX + "micafer" im_auth['password'] = "******" # Check that a user/pass cred cannot access OpenID ones user_auth = Authentication([{ 'id': 'im', 'type': 'InfrastructureManager', 'username': im_auth['username'], 'password': im_auth['password'] }]) with self.assertRaises(Exception) as ex: IM.check_auth_data(user_auth) self.assertEqual( str(ex.exception), "Invalid username used for the InfrastructureManager.") inf = InfrastructureInfo() inf.id = "1" inf.auth = user_auth res = inf.is_authorized(user_auth) self.assertEqual(res, False) user_auth = Authentication([{ 'id': 'im', 'type': 'InfrastructureManager', 'username': im_auth['username'], 'password': im_auth['password'], 'token': im_auth['token'] }]) res = inf.is_authorized(user_auth) self.assertEqual(res, True)
def RESTGetInfrastructureProperty(id=None, prop=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: if prop == "contmsg": res = InfrastructureManager.GetInfrastructureContMsg(id, auth) elif prop == "radl": res = InfrastructureManager.GetInfrastructureRADL(id, auth) elif prop == "state": accept = get_media_type('Accept') if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept: return return_error(415, "Unsupported Accept Media Types: %s" % accept) bottle.response.content_type = "application/json" res = InfrastructureManager.GetInfrastructureState(id, auth) return format_output(res, default_type="application/json", field_name="state") else: return return_error(404, "Incorrect infrastructure property") return format_output(res, field_name=prop) except DeletedInfrastructureException as ex: return return_error(404, "Error Getting Inf. prop: " + str(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting Inf. prop: " + str(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Getting Inf. prop: " + str(ex)) except Exception as ex: logger.exception("Error Getting Inf. prop") return return_error(400, "Error Getting Inf. prop: " + str(ex))
def RESTGetVMProperty(infid=None, vmid=None, prop=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: if prop == 'contmsg': info = InfrastructureManager.GetVMContMsg(infid, vmid, auth) else: info = InfrastructureManager.GetVMProperty(infid, vmid, prop, auth) if info is None: return return_error( 404, "Incorrect property %s for VM ID %s" % (prop, vmid)) else: return format_output(info, field_name=prop) except DeletedInfrastructureException as ex: return return_error(404, "Error Getting VM. property: " + str(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting VM. property: " + str(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Getting VM. property: " + str(ex)) except DeletedVMException as ex: return return_error(404, "Error Getting VM. property: " + str(ex)) except IncorrectVMException as ex: return return_error(404, "Error Getting VM. property: " + str(ex)) except Exception as ex: logger.exception("Error Getting VM property") return return_error(400, "Error Getting VM property: " + str(ex))
def test_contextualize(self): """Test Contextualization process""" radl = """" network publica (outbound = 'yes') system front ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'publica' and net_interface.0.ip = '10.0.0.1' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.fstype='ext4' and disk.1.mount_path='/mnt/disk' and disk.0.applications contains (name = 'ansible.modules.micafer.hadoop') and disk.0.applications contains (name='gmetad') and disk.0.applications contains (name='wget') ) deploy front 1 """ auth0 = self.getAuth([0], [], [("Mock", 0)]) IM._reinit() Config.PLAYBOOK_RETRIES = 1 Config.CONTEXTUALIZATION_DIR = os.path.dirname(os.path.realpath(__file__)) + "/../../contextualization" Config.CONFMAMAGER_CHECK_STATE_INTERVAL = 0.01 Config.UPDATE_CTXT_LOG_INTERVAL = 1 Config.CHECK_CTXT_PROCESS_INTERVAL = 1 cloud0 = self.get_cloud_connector_mock("MyMock") self.register_cloudconnector("Mock", cloud0) infId = IM.CreateInfrastructure(str(radl), auth0) time.sleep(15) state = IM.GetInfrastructureState(infId, auth0) self.assertEqual(state["state"], "unconfigured") IM.infrastructure_list[infId].ansible_configured = True IM.infrastructure_list[infId].vm_list[0].get_ctxt_log = MagicMock() IM.infrastructure_list[infId].vm_list[0].get_ctxt_log.return_value = "OK" IM.Reconfigure(infId, "", auth0) time.sleep(5) state = IM.GetInfrastructureState(infId, auth0) self.assertEqual(state["state"], "running") contmsg = IM.GetInfrastructureContMsg(infId, auth0) self.assertGreater(len(contmsg), 150) IM.DestroyInfrastructure(infId, auth0)
def test_get_infrastructure_list(self): """Get infrastructure List.""" auth0 = self.getAuth([0]) infId = IM.CreateInfrastructure("", auth0) inf_ids = IM.GetInfrastructureList(auth0) self.assertEqual(inf_ids, [infId]) IM.DestroyInfrastructure(infId, auth0)
def test_get_vm_info(self): """ Test GetVMInfo and GetVMProperty and GetVMContMsg and GetInfrastructureRADL and GetInfrastructureContMsg and GetInfrastructureState. """ radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure(str(radl), auth0) radl_info = IM.GetVMInfo(infId, "0", auth0) parsed_radl_info = parse_radl(str(radl_info)) self.assertEqual(parsed_radl_info.systems[0].getValue("state"), "running") radl_info = IM.GetVMInfo(infId, "0", auth0, True) parsed_radl_info = parse_radl_json(radl_info) self.assertEqual(parsed_radl_info.systems[0].getValue("state"), "running") state = IM.GetVMProperty(infId, "0", "state", auth0) self.assertEqual(state, "running") contmsg = IM.GetVMContMsg(infId, "0", auth0) self.assertEqual(contmsg, "") InfrastructureList.infrastructure_list[infId].cont_out = "Header" InfrastructureList.infrastructure_list[infId].vm_list[ 0].cloud_connector = MagicMock() InfrastructureList.infrastructure_list[infId].vm_list[ 0].cloud_connector.error_messages = "TESTMSG" contmsg = IM.GetInfrastructureContMsg(infId, auth0) header_contmsg = IM.GetInfrastructureContMsg(infId, auth0, True) InfrastructureList.infrastructure_list[infId].vm_list[ 0].cloud_connector = None self.assertIn("TESTMSG", contmsg) self.assertNotIn("TESTMSG", header_contmsg) self.assertIn("Header", header_contmsg) state = IM.GetInfrastructureState(infId, auth0) self.assertEqual(state["state"], "running") self.assertEqual(state["vm_states"]["0"], "running") radl_info = IM.GetInfrastructureRADL(infId, auth0) parsed_radl_info = parse_radl(str(radl_info)) self.assertEqual( parsed_radl_info.systems[0].getValue( "disk.0.os.credentials.username"), "user") IM.DestroyInfrastructure(infId, auth0)
def launch_daemon(): """ Launch the IM daemon """ if os.path.isfile(Config.DATA_FILE) or Config.DATA_DB: InfrastructureManager.load_data() if Config.XMLRCP_SSL: # if specified launch the secure version import ssl from IM.request import AsyncSSLXMLRPCServer server = AsyncSSLXMLRPCServer(Config.XMLRCP_ADDRESS, Config.XMLRCP_PORT, Config.XMLRCP_SSL_KEYFILE, Config.XMLRCP_SSL_CERTFILE, Config.XMLRCP_SSL_CA_CERTS, cert_reqs=ssl.CERT_OPTIONAL) else: # otherwise the standard XML-RPC service server = AsyncXMLRPCServer((Config.XMLRCP_ADDRESS, Config.XMLRCP_PORT)) # Register the API functions server.register_function(CreateInfrastructure) server.register_function(DestroyInfrastructure) server.register_function(StartInfrastructure) server.register_function(StopInfrastructure) server.register_function(GetInfrastructureInfo) server.register_function(GetVMInfo) server.register_function(GetVMProperty) server.register_function(AlterVM) server.register_function(RemoveResource) server.register_function(AddResource) server.register_function(GetInfrastructureList) server.register_function(Reconfigure) server.register_function(ExportInfrastructure) server.register_function(ImportInfrastructure) server.register_function(GetInfrastructureRADL) server.register_function(GetInfrastructureContMsg) server.register_function(GetVMContMsg) server.register_function(StartVM) server.register_function(StopVM) server.register_function(GetInfrastructureState) server.register_function(GetVersion) InfrastructureManager.logger.info( '************ Start Infrastructure Manager daemon (v.%s) ************' % version) # Launch the API XMLRPC thread server.serve_forever_in_thread() if Config.ACTIVATE_REST: # If specified launch the REST server import IM.REST IM.REST.run_in_thread(host=Config.REST_ADDRESS, port=Config.REST_PORT) # Start the messages queue get_system_queue().timed_process_loop(None, 1, exit_callback=im_stop)
def RESTCreateInfrastructure(): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read().decode("utf-8") tosca_data = None async_call = False if "async" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("async").lower() if str_ctxt in ['yes', 'true', '1']: async_call = True elif str_ctxt in ['no', 'false', '0']: async_call = False else: return return_error(400, "Incorrect value in async parameter") if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) _, radl_data = tosca_data.to_radl() elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) inf_id = InfrastructureManager.CreateInfrastructure( radl_data, auth, async_call) # Store the TOSCA document if tosca_data: sel_inf = InfrastructureManager.get_infrastructure(inf_id, auth) sel_inf.extra_info['TOSCA'] = tosca_data bottle.response.headers['InfID'] = inf_id bottle.response.content_type = "text/uri-list" res = get_full_url('/infrastructures/%s' % inf_id) return format_output(res, "text/uri-list", "uri") except InvaliddUserException as ex: return return_error(401, "Error Getting Inf. info: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Creating Inf.") return return_error(400, "Error Creating Inf.: %s" % get_ex_error(ex))
def test_db(self, execute, select, table_exists, connect): table_exists.return_value = True select.return_value = [["1", "", read_file_as_string("../files/data.pkl")]] execute.return_value = True res = IM.get_data_from_db("mysql://*****:*****@server/db_name") self.assertEqual(len(res), 1) inf = InfrastructureInfo() inf.id = "1" success = IM.save_data_to_db("mysql://*****:*****@server/db_name", {"1": inf}) self.assertTrue(success)
def setUp(self): IM._reinit() # Patch save_data IM.save_data = staticmethod(lambda *args: None) ch = logging.StreamHandler(sys.stdout) log = logging.getLogger('InfrastructureManager') log.setLevel(logging.ERROR) log.propagate = 0 log.addHandler(ch) log = logging.getLogger('ConfManager') log.setLevel(logging.DEBUG) log.propagate = 0 log.addHandler(ch)
def setUp(self): Config.DATA_DB = "/tmp/inf.dat" InfrastructureList.load_data() IM._reinit() ch = logging.StreamHandler(sys.stdout) log = logging.getLogger('InfrastructureManager') log.setLevel(logging.ERROR) log.propagate = 0 log.addHandler(ch) log = logging.getLogger('ConfManager') log.setLevel(logging.DEBUG) log.propagate = 0 log.addHandler(ch)
def RESTGetInfrastructureList(): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: flt = None if "filter" in bottle.request.params.keys(): flt = bottle.request.params.get("filter") inf_ids = InfrastructureManager.GetInfrastructureList(auth, flt) res = [] for inf_id in inf_ids: res.append(get_full_url('/infrastructures/%s' % inf_id)) return format_output(res, "text/uri-list", "uri-list", "uri") except InvaliddUserException as ex: return return_error(401, "Error Getting Inf. List: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting Inf. List") return return_error(400, "Error Getting Inf. List: %s" % get_ex_error(ex))
def RESTReconfigureInfrastructure(id=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: vm_list = None if "vm_list" in bottle.request.params.keys(): str_vm_list = bottle.request.params.get("vm_list") try: vm_list = [int(vm_id) for vm_id in str_vm_list.split(",")] except: return return_error(400, "Incorrect vm_list format.") content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read() if radl_data: if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) else: radl_data = "" bottle.response.content_type = "text/plain" return InfrastructureManager.Reconfigure(id, radl_data, auth, vm_list) except DeletedInfrastructureException, ex: return return_error(404, "Error reconfiguring infrastructure: " + str(ex))
def RESTGetVMInfo(infid=None, vmid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: radl = InfrastructureManager.GetVMInfo(infid, vmid, auth) return format_output(radl, field_name="radl") except DeletedInfrastructureException as ex: return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Getting VM. info: %s" % get_ex_error(ex)) except DeletedVMException as ex: return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except IncorrectVMException as ex: return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting VM info") return return_error(400, "Error Getting VM info: %s" % get_ex_error(ex))
def test_inf_auth_with_userdb(self): """Test access im with user db""" Config.USER_DB = os.path.dirname( os.path.realpath(__file__)) + '/../files/users.txt' auth0 = self.getAuth([0]) infId0 = IM.CreateInfrastructure("", auth0) IM.DestroyInfrastructure(infId0, auth0) auth1 = self.getAuth([1]) with self.assertRaises(Exception) as ex: IM.CreateInfrastructure("", auth1) self.assertEqual(str(ex.exception), "Invalid InfrastructureManager credentials") Config.USER_DB = None
def RESTCreateInfrastructure(): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read() if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) inf_id = InfrastructureManager.CreateInfrastructure(radl_data, auth) bottle.response.headers['InfID'] = inf_id bottle.response.content_type = "text/uri-list" protocol = "http://" if Config.REST_SSL: protocol = "https://" res = protocol + \ bottle.request.environ['HTTP_HOST'] + \ "/infrastructures/" + str(inf_id) return format_output(res, "text/uri-list", "uri") except InvaliddUserException, ex: return return_error(401, "Error Getting Inf. info: " + str(ex))
def RESTRemoveResource(infid=None, vmid=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: context = True if "context" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("context").lower() if str_ctxt in ['yes', 'true', '1']: context = True elif str_ctxt in ['no', 'false', '0']: context = False else: return return_error(400, "Incorrect value in context parameter") InfrastructureManager.RemoveResource(infid, vmid, auth, context) bottle.response.content_type = "text/plain" return "" except DeletedInfrastructureException as ex: return return_error(404, "Error Removing resources: %s" % ex.args[0]) except IncorrectInfrastructureException as ex: return return_error(404, "Error Removing resources: %s" % ex.args[0]) except UnauthorizedUserException as ex: return return_error(403, "Error Removing resources: %s" % ex.args[0]) except DeletedVMException as ex: return return_error(404, "Error Removing resources: %s" % ex.args[0]) except IncorrectVMException as ex: return return_error(404, "Error Removing resources: %s" % ex.args[0]) except Exception as ex: logger.exception("Error Removing resources") return return_error(400, "Error Removing resources: %s" % ex.args[0])
def RESTGetInfrastructureInfo(infid=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: vm_ids = InfrastructureManager.GetInfrastructureInfo(infid, auth) res = [] for vm_id in vm_ids: res.append( get_full_url('/infrastructures/' + str(infid) + '/vms/' + str(vm_id))) return format_output(res, "text/uri-list", "uri-list", "uri") except DeletedInfrastructureException as ex: return return_error(404, "Error Getting Inf. info: %s" % ex.args[0]) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting Inf. info: %s" % ex.args[0]) except UnauthorizedUserException as ex: return return_error(403, "Error Getting Inf. info: %s" % ex.args[0]) except Exception as ex: logger.exception("Error Getting Inf. info") return return_error(400, "Error Getting Inf. info: %s" % ex.args[0])
def RESTImportInfrastructure(): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') data = bottle.request.body.read().decode("utf-8") if content_type: if "application/json" not in content_type: return return_error(415, "Unsupported Media Type %s" % content_type) new_id = InfrastructureManager.ImportInfrastructure(data, auth) bottle.response.content_type = "text/uri-list" res = get_full_url('/infrastructures/%s' % new_id) return format_output(res, "text/uri-list", "uri") except InvaliddUserException as ex: return return_error(401, "Error Impporting Inf.: %s" % ex.args[0]) except Exception as ex: logger.exception("Error Impporting Inf.") return return_error(400, "Error Impporting Inf.: %s" % ex.args[0])
def RESTAlterVM(infid=None, vmid=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read() if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) vm_info = InfrastructureManager.AlterVM(infid, vmid, radl_data, auth) return format_output(vm_info, field_name="radl") except DeletedInfrastructureException as ex: return return_error(404, "Error modifying resources: " + str(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error modifying resources: " + str(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error modifying resources: " + str(ex)) except DeletedVMException as ex: return return_error(404, "Error modifying resources: " + str(ex)) except IncorrectVMException as ex: return return_error(404, "Error modifying resources: " + str(ex)) except Exception as ex: logger.exception("Error modifying resources") return return_error(400, "Error modifying resources: " + str(ex))
def RESTGetInfrastructureInfo(id=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: vm_ids = InfrastructureManager.GetInfrastructureInfo(id, auth) res = [] protocol = "http://" if Config.REST_SSL: protocol = "https://" for vm_id in vm_ids: res.append(protocol + bottle.request.environ[ 'HTTP_HOST'] + '/infrastructures/' + str(id) + '/vms/' + str(vm_id)) return format_output(res, "text/uri-list", "uri-list", "uri") except DeletedInfrastructureException as ex: return return_error(404, "Error Getting Inf. info: " + str(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting Inf. info: " + str(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Getting Inf. info: " + str(ex)) except Exception as ex: logger.exception("Error Getting Inf. info") return return_error(400, "Error Getting Inf. info: " + str(ex))
def test_inf_creation2(self): """Create infrastructure with an incorrect RADL in two cloud providers and the first fails.""" radl = """" network publica (outbound = 'yes') network privada () system front ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'publica' and net_interface.1.connection = 'privada' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' ) system wn ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'privada' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' ) deploy front 1 deploy wn 1 """ cloud0 = self.get_cloud_connector_mock("MyMock0") self.register_cloudconnector("Mock0", cloud0) cloud1 = type("MyMock1", (CloudConnector, object), {}) cloud1.launch = Mock(return_value=[(False, "Error")]) self.register_cloudconnector("Mock1", cloud1) auth0 = self.getAuth([0], [], [("Mock1", 1), ("Mock0", 0)]) infId = IM.CreateInfrastructure(radl, auth0) self.assertEqual(cloud0.launch.call_count, 2) self.assertEqual(cloud1.launch.call_count, 1) for call, _ in cloud0.launch.call_args_list: self.assertEqual(call[3], 1) for call, _ in cloud1.launch.call_args_list: self.assertEqual(call[3], 1) IM.DestroyInfrastructure(infId, auth0)
def test_inf_creation_addition_clouds(self): """Add resources infrastructure with an incorrect RADL with 2 clouds.""" radl = """" network publica (outbound = 'yes') network privada () system front ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'publica' and net_interface.1.connection = 'privada' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' ) system wn ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'privada' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' ) deploy front 1 cloud0 deploy wn 1 """ auth0 = self.getAuth([0], [], [("Dummy", 0), ("Dummy", 1)]) infId = IM.CreateInfrastructure(radl, auth0) radl = """ network privada system wn deploy wn 1 cloud1 """ with self.assertRaises(Exception) as ex: _ = IM.AddResource(infId, radl, auth0) self.assertIn( "Two deployments that have to be launched in the same cloud provider" " are asked to be deployed in different cloud providers", str(ex.exception))
def test_check_oidc_valid_token(self, openidclient): im_auth = {"token": (self.gen_token())} user_info = json.loads( read_file_as_string('../files/iam_user_info.json')) openidclient.is_access_token_expired.return_value = False, "Valid Token for 100 seconds" openidclient.get_user_info_request.return_value = True, user_info Config.OIDC_ISSUERS = ["https://iam-test.indigo-datacloud.eu/"] Config.OIDC_AUDIENCE = None IM.check_oidc_token(im_auth) self.assertEqual(im_auth['username'], InfrastructureInfo.OPENID_USER_PREFIX + "micafer") self.assertEqual(im_auth['password'], "https://iam-test.indigo-datacloud.eu/sub")
def im_stop(): """ Function to safely stop the service """ try: # Assure that the IM data are correctly saved InfrastructureManager.logger.info('Stopping Infrastructure Manager daemon...') InfrastructureManager.stop() if Config.ACTIVATE_REST: # we have to stop the REST server import IM.REST IM.REST.stop() except: InfrastructureManager.logger.exception("Error stopping Infrastructure Manager daemon") InfrastructureManager.logger.info('************ Infrastructure Manager daemon stopped ************') sys.exit(0)
def test_export_import(self): """Test ExportInfrastructure and ImportInfrastructure operations""" radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure(str(radl), auth0) res = IM.ExportInfrastructure(infId, True, auth0) new_inf_id = IM.ImportInfrastructure(res, auth0) IM.DestroyInfrastructure(new_inf_id, auth0)
def test_inf_removeresources(self): """Deploy 4 VMs and remove 2""" radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 4)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure(str(radl), auth0) cont = IM.RemoveResource(infId, ['0', '1'], auth0) self.assertEqual(cont, 2) vms = IM.GetInfrastructureInfo(infId, auth0) self.assertEqual(sorted(vms), ['2', '3']) IM.DestroyInfrastructure(infId, auth0)
def test_check_iam_token(self, connection): im_auth = {"token": ("eyJraWQiOiJyc2ExIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJkYzVkNWFiNy02ZGI5LTQwNzktOTg1Yy04MGF" "jMDUwMTcwNjYiLCJpc3MiOiJodHRwczpcL1wvaWFtLXRlc3QuaW5kaWdvLWRhdGFjbG91ZC5ldVwvIiwiZXhwI" "joxNDY1NDcxMzU0LCJpYXQiOjE0NjU0Njc3NTUsImp0aSI6IjA3YjlkYmE4LTc3NWMtNGI5OS1iN2QzLTk4Njg" "5ODM1N2FiYSJ9.DwpZizVaYtvIj7fagQqDFpDh96szFupf6BNMIVLcopqQtZ9dBvwN9lgZ_w7Htvb3r-erho_hc" "me5mqDMVbSKwsA2GiHfiXSnh9jmNNVaVjcvSPNVGF8jkKNxeSSgoT3wED8xt4oU4s5MYiR075-RAkt6AcWqVbXU" "z5BzxBvANko")} user_info = read_file_as_string('../files/iam_user_info.json') conn = MagicMock() connection.return_value = conn resp = MagicMock() resp.status = 200 resp.read.return_value = user_info conn.getresponse.return_value = resp IM.check_iam_token(im_auth) self.assertEqual(im_auth['username'], "micafer") self.assertEqual(im_auth['password'], "https://iam-test.indigo-datacloud.eu/sub")
def tearDown(self): IM.stop()
def setUp(self): IM._reinit() # Patch save_data IM.save_data = staticmethod(lambda *args: None)