def test_service_load_wrong_template(self): Node = self.load_template('node') Vm = self.load_template('vm') srv = tcol.instantiate_service(Node, 'testnode') serialized = _serialize_service(srv) with self.assertRaises(BadTemplateError): # Try to load with the wrong template class scol.load(Vm, serialized)
def test_service_load_wrong_name(self): Node = self.load_template('node') srv = tcol.instantiate_service(Node, 'testnode') path = srv.save() # rename the folder where the service have been saved new_path = os.path.join(os.path.dirname(path), 'other') os.rename(path, new_path) with self.assertRaises(BadTemplateError): scol.load(Node, new_path)
def test_service_load(self): Node = self.load_template('node') srv = tcol.instantiate_service(Node, 'testnode') # unload service from memory scol.drop_all() loaded = scol.load(Node, _serialize_service(srv)) self.assertEqual(srv.name, loaded.name, "name of the loaded service should be %s" % srv.name) self.assertEqual( srv.template_name, loaded.template_name, "template_name of the loaded service should be %s" % srv.template_name) self.assertEqual(srv.guid, loaded.guid, "guid of the loaded service should be %s" % srv.guid) self.assertEqual( srv.version, loaded.version, "version of the loaded service should be %s" % srv.version) self.assertIsNotNone(srv.data, "loaded service data should not be None") self.assertIsNotNone(srv.state, "loaded service state should not be None") self.assertIsNotNone(srv.task_list, "loaded service task_list should not be None")
def load_services(config): for service_details in storage.list(): tmpl_uid = TemplateUID.parse(service_details['service']['template']) try: tmplClass = tcol.get(str(tmpl_uid)) except tcol.TemplateNotFoundError: # template of the service not found, could be we have the template but not the same version # try to get the template without specifiying version tmplClasses = tcol.find(host=tmpl_uid.host, account=tmpl_uid.account, repo=tmpl_uid.repo, name=tmpl_uid.name) size = len(tmplClasses) if size > 1: raise RuntimeError("more then one template version found, this should never happens") elif size < 1: # if the template is not found, try to add the repo using the info of the service template uid url = "http://%s/%s/%s" % (tmpl_uid.host, tmpl_uid.account, tmpl_uid.repo) tcol.add_repo(url) tmplClass = tcol.get(service_details['service']['template']) else: # template of another version found, use newer version to load the service tmplClass = tmplClasses[0] scol.load(tmplClass, service_details) loading_failed = [] logger = j.logger.get('zerorobot') for service in scol.list_services(): try: service.validate() except Exception as err: logger.error("fail to load %s: %s" % (service.guid, str(err))) # the service is not going to process its task list until it can # execute validate() without problem service.gl_mgr.stop('executor') loading_failed.append(service) if len(loading_failed) > 0: gevent.spawn(_try_load_service, loading_failed)
def load_services(data_dir): if not os.path.exists(data_dir): os.makedirs(data_dir) for srv_dir in j.sal.fs.listDirsInDir(data_dir, recursive=True): info_path = os.path.join(srv_dir, 'service.yaml') if not os.path.exists(info_path): continue service_info = j.data.serializer.yaml.load(info_path) tmpl_uid = TemplateUID.parse(service_info['template']) try: tmplClass = tcol.get(str(tmpl_uid)) except tcol.TemplateNotFoundError: # template of the service not found, could be we have the template but not the same version # try to get the template without specifiying version tmplClasses = tcol.find(host=tmpl_uid.host, account=tmpl_uid.account, repo=tmpl_uid.repo, name=tmpl_uid.name) size = len(tmplClasses) if size > 1: raise RuntimeError("more then one template version found, this should never happens") elif size < 1: # if the template is not found, try to add the repo using the info of the service template uid url = "http://%s/%s/%s" % (tmpl_uid.host, tmpl_uid.account, tmpl_uid.repo) tcol.add_repo(url) tmplClass = tcol.get(service_info['template']) else: # template of another version found, use newer version to load the service tmplClass = tmplClasses[0] srv = scol.load(tmplClass, srv_dir) loading_failed = [] for service in scol.list_services(): try: service.validate() except Exception as err: logger.error("fail to load %s: %s" % (service.guid, str(err))) # the service is not going to process its task list until it can # execute validate() without problem service.gl_mgr.stop('executor') loading_failed.append(service) if len(loading_failed) > 0: gevent.spawn(_try_load_service, loading_failed)
def test_service_load_dir_not_exists(self): Node = self.load_template('node') srv = tcol.instantiate_service(Node, 'testnode') path = srv.save() with self.assertRaises(FileNotFoundError): scol.load(Node, '/tmp/zerorobot-test-not-exists')