decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 9.34 kB
JavaScript
var request=require("request"),fs=require("fs"),Promise=require("bluebird"),path=require("path"),_=require("underscore"),qs=require("querystring");const Joi=require("@hapi/joi"),Lab=require("lab"),lab=exports.lab=Lab.script();var clusterpost=require("clusterpost-lib"),clustermodel=require("clusterpost-model");const getConfigFile=function(e,t){try{return require(t+"/conf.my."+e+".json")}catch(s){return require(t+"/conf."+e+".json")}};var env=process.env.NODE_ENV;if(!env)throw"Please set NODE_ENV variable.";var conf=getConfigFile(env,"./");conf.uri&&conf.couchdb||(console.error("The configuration for the test is wrong. It must contain fields 'uri' and 'couchdb', example:"),console.error(JSON.stringify({uri:"http://localhost:8180",couchdb:"http://localhost:5984/clusterjobstest"},null,2)),process.exit(1)),console.log("Using the following configuration for test:",JSON.stringify(conf,null,2));var agentOptions={};conf.tls&&conf.tls.cert&&(agentOptions.ca=fs.readFileSync(conf.tls.cert)),clusterpost.setClusterPostServer(conf.uri),clusterpost.setAgentOptions(agentOptions);var jobid,token,tokenraw,inputs=["./data/gravitational-waves-simulation.jpg"],updateJobStatusRec=function(e,t){var s=Joi.object().keys({jobid:Joi.number(),status:Joi.string().valid(t),downloadstatus:Joi.array().items(Joi.object().keys({path:Joi.string(),status:Joi.boolean().valid(!0)})),uploadstatus:Joi.array().items(Joi.object())});return new Promise(function(e,t){setTimeout(e,7e4)}).then(function(){return clusterpost.updateJobStatus(e).then(function(e){return Joi.assert(e,s),e}).catch(function(t){return updateJobStatusRec(e)})})},job={executable:"cksum",parameters:[{flag:"",name:"gravitational-waves-simulation.jpg"}],inputs:[{name:"gravitational-waves-simulation.jpg"}],outputs:[{type:"directory",name:"./"},{type:"tar.gz",name:"./"},{type:"file",name:"stdout.out"},{type:"file",name:"stderr.err"}],type:"job",userEmail:"algiedi85@gmail.com"},user={email:"algiedi85@gmail.com",name:"Alpha Capricorni",password:"Some808Password!"},joiokres=Joi.object().keys({ok:Joi.boolean().valid(!0),id:Joi.string(),rev:Joi.string()}),job2={executable:"python",parameters:[{flag:"-c",name:"while True: print '.'"}],outputs:[{type:"file",name:"stdout.out"},{type:"file",name:"stderr.err"}],type:"job",userEmail:"algiedi85@gmail.com"};lab.experiment("Test clusterpost",function(){lab.test("returns true when new user is created.",function(){return clusterpost.createUser(user).then(function(e){Joi.assert(e.token,Joi.string().required())})}),lab.test("returns true when user is logged in.",function(){return clusterpost.userLogin({email:"algiedi85@gmail.com",password:"Some808Password!"}).then(function(e){Joi.assert(e.token,Joi.string().required()),tokenraw=e.token,token="Bearer "+e.token,console.log(e.token)})}),lab.test('returns true when executionservers are fetched due to insufficient scope, the scope "clusterpost" is also added manually',function(){return clusterpost.getExecutionServers().then(function(e){return Joi.assert(e.statusCode,403),new Promise(function(e,t){var s={uri:conf.couchdb+"/_design/user/_view/info?"+qs.stringify({key:'"algiedi85@gmail.com"',include_docs:!0}),method:"GET"};request(s,function(s,r,o){var n=_.pluck(JSON.parse(o).rows,"doc")[0];n.scope.push("clusterpost");var i={uri:conf.couchdb+"/_bulk_docs",method:"POST",json:{docs:[n]}};request(i,function(s,r,o){s?t(s):o.error?t(o.error):e(o)})})})})}),lab.test("returns true when executionservers are fetched with valid scope",function(){return clusterpost.getExecutionServers().then(function(e){job.executionserver=e[0].name,job2.executionserver=e[0].name})}),lab.test("returns true when document is created",function(){return clusterpost.createDocument(job).then(function(e){Joi.assert(e,joiokres),jobid=e.id,console.info("JOBID:",jobid)})}),lab.test("returns true when document is fetched",function(){return clusterpost.getDocument(jobid).then(function(e){Joi.assert(e,clustermodel.job),Joi.assert(e.jobstatus,Joi.object().keys({status:Joi.string().valid("CREATE")}))})}),lab.test("returns true when attachment is added",function(){return clusterpost.uploadFiles(jobid,inputs).then(function(e){var t=Joi.array().items(joiokres).min(1);Joi.assert(e,t)})}),lab.test("returns true when job is executed",function(){return clusterpost.executeJob(jobid).then(function(e){Joi.assert(e.status,Joi.string().valid("QUEUE"))})}),lab.test("returns true when jobstatus is RUN",function(){return updateJobStatusRec(jobid,"RUN").then(function(e){Joi.assert(e.status,Joi.string().valid("RUN"))})}),lab.test("returns true until jobstatus is DONE",function(){return updateJobStatusRec(jobid,"DONE").then(function(e){Joi.assert(e.status,Joi.string().valid("DONE"))})}),lab.test("returns true if get attachment output stream is valid",function(){return clusterpost.getDocumentAttachment(jobid,"stdout.out").then(function(e){var t="774035995 70572 gravitational-waves-simulation.jpg";if(-1===e.indexOf(t))throw"Output validation not found: "+t})}),lab.test("returns true if attachment not found",function(){return clusterpost.getDocumentAttachment(jobid,"stdout.err").then(function(e){Joi.assert(e.statusCode,404)})}),lab.test("returns true if get attachment output stream is valid using a download token",function(e){clusterpost.getDownloadToken(jobid,"stdout.out").then(function(e){return Joi.assert(e.token,Joi.string()),clusterpost.downloadAttachment(e.token)}).then(function(e){var t="774035995 70572 gravitational-waves-simulation.jpg";if(-1===e.indexOf(t))throw"Output validation not found: "+t})}),lab.test("returns true if the document is deleted",function(){return clusterpost.deleteJob(jobid).then(function(e){Joi.assert(e.status,Joi.string().valid("DELETE"))})}),lab.test("returns true when second job is created",function(){return clusterpost.createDocument(job2).then(function(e){Joi.assert(e,joiokres),jobid=e.id,console.info("JOBID:",jobid)})}),lab.test("returns true when second job is executed",function(){return clusterpost.executeJob(jobid).then(function(e){Joi.assert(e.status,Joi.string().valid("QUEUE"))})}),lab.test("returns true when second jobstatus is RUN",function(){return updateJobStatusRec(jobid,"RUN").then(function(e){Joi.assert(e.status,Joi.string().valid("RUN"))})}),lab.test("returns true when second job is killed",function(){return clusterpost.killJob(jobid).then(function(e){Joi.assert(e,clustermodel.jobstatus),Joi.assert(e.status,Joi.string().valid("KILL"))})}),lab.test("returns true when second job is deleted",function(){return clusterpost.deleteJob(jobid).then(function(e){Joi.assert(e.status,Joi.string().valid("DELETE"))})}),lab.test("returns true if tokens are not fetch as regular user",function(){return clusterpost.getExecutionServerToken().then(function(e){Joi.assert(e.statusCode,403)})}),lab.test("returns true when get all users is denied due to insufficient scope, updates scope manually to admin",function(){return clusterpost.getUsers().then(function(e){return Joi.assert(e.statusCode,403),new Promise(function(e,t){var s={uri:conf.couchdb+"/_design/user/_view/info?"+qs.stringify({key:'"algiedi85@gmail.com"',include_docs:!0}),method:"GET"};request(s,function(s,r,o){var n=_.pluck(JSON.parse(o).rows,"doc")[0];n.scope.push("admin");var i={uri:conf.couchdb+"/_bulk_docs",method:"POST",json:{docs:[n]}};request(i,function(s,r,o){s?t(s):o.error?t(o.error):e(o)})})})})}),lab.test("returns true if tokens are fetch as admin user",function(){return clusterpost.getExecutionServerToken().then(function(e){return Joi.assert(e,Joi.array().items(clustermodel.executionservertoken)),Promise.map(e,function(e){return delete e.token,clusterpost.getExecutionServers(e)}).then(function(e){Joi.assert(_.flatten(e),Joi.array().items(clustermodel.executionservertoken))})})}),lab.test("returns true when a user is created, then all users are fetched, the scope of the new user is updated and the new user is deleted",function(){var e={email:"someemail@gmail.com",name:"Test user",password:"Some88Password!"};return clusterpost.createUser(e).bind({}).then(function(e){return clusterpost.getUsers()}).then(function(t){var s=JSON.parse(t);Joi.assert(s,Joi.array().items(Joi.object()));var r=_.find(s,function(t){return t.email==e.email});return r.scope.push("clusterpost"),clusterpost.updateUser(r)}).then(function(e){return Joi.assert(e,Joi.object().keys({ok:Joi.boolean(),id:Joi.string(),rev:Joi.string()})),clusterpost.getUser()}).then(function(t){var s=_.isObject(t)?t:JSON.parse(t);s.scope=["default"];var r={email:e.email,password:e.password};return clusterpost.userLogin(r).then(function(e){return clusterpost.updateUser(s)})}).then(function(e){return Joi.assert(e.statusCode,401),clusterpost.userLogin({email:"algiedi85@gmail.com",password:"Some808Password!"}).then(function(e){return clusterpost.getUsers()})}).then(function(t){var s=_.find(JSON.parse(t),function(t){return t.email===e.email});return Joi.assert(s.scope,Joi.array().items(Joi.string().valid("default","clusterpost"))),s}).then(function(e){return clusterpost.deleteUsers(e).then(function(e){Joi.assert(e,Joi.object().keys({ok:Joi.boolean(),id:Joi.string(),rev:Joi.string()}))})})}),lab.test("returns true when valid user deletes itself.",function(){return clusterpost.deleteUser().then(function(e){Joi.assert(e,Joi.object().keys({ok:Joi.boolean(),id:Joi.string(),rev:Joi.string()}))})})});