UNPKG

jchaos

Version:

module to access chaos resources

406 lines (341 loc) 12.3 kB
var assert = require('assert'); if (typeof btoa === 'undefined') { global.btoa = function (str) { return new Buffer(str, 'binary').toString('base64'); }; } if (typeof atob === 'undefined') { global.atob = function (b64Encoded) { return new Buffer(b64Encoded, 'base64').toString('binary'); }; } var jchaos = require('jchaos'); options = {'timeout':30000}; var clock = new Date(); var npush = 500 var uid=""; var wave_uid=""; process.argv.forEach(function (val, index, array) { if (val == "uri") { options.uri = array[index + 1]; console.log(val + "=" + array[index + 1]); } if (val == "async") { options.async = (array[index + 1] == "true"); console.log(val + "=" + array[index + 1]); } if (val == "npush") { npush = (array[index + 1]); console.log(val + "=" + array[index + 1]); } }); var root_program = "#include <driver/misc/models/cernRoot/rootUtil.h>\nint simpleRootCounter(int max){\n int cnt;for(cnt=0;cnt<max;cnt++){std::cout<<\"counter:\"<<cnt<<std::endl;sleep(1);} std::cout<<\"END COUNTER:\"<<cnt<<std::endl;exit(0);}"; jchaos.setOptions(options); var start_test = Date.now(); var best_agent=""; var best_server=""; var rest_port=8071; describe("CHAOS AGENT ROOT TEST", function () { this.timeout(1200000); it('Find best agent', function (done) { jchaos.findBestServer(function (bs,ba) { best_agent=ba; best_server=bs; if((best_agent!="")&&(best_server!="")){ console.log("\tbest server:"+best_server); console.log("\tbest agent:"+best_agent); done(0); }else { console.log("\t## query on "+jchaos.options.uri+" NO valid server retrieved b_agent : "+bs+ " b_server:"+best_server); done(1); } },(error)=>{ console.log("\t## Error "+JSON.stringify(error)); }); }); it('Find rest port', function (done) { jchaos.node(best_agent, "desc", "agent", function (data) { if(data.hasOwnProperty('ndk_rest_port')){ rest_port=data.ndk_rest_port; done(0) } else { console.log("\t## not found ndk_rest_port in :"+JSON.stringify(data)); done(1); } },(bad)=>{ console.log("\t## error performing desc query on "+best_agent+ " err:"+JSON.stringify(err)); done(1); } ); }); it('Upload script simpleRootCounter', function (done) { var script = {}; script['name'] = "simpleRootCounter"; script['script_name'] = "simpleRootCounter.C"; script['eudk_script_content'] = btoa(root_program); script['eudk_script_language'] = "CPP"; script['script_description'] = "CHAOS AGENT ROOT TEST"; script['default_argument'] = "(15)"; script['workingdir'] = ""; console.log("saving script:" + JSON.stringify(script)); jchaos.saveScript(script, function (data) { done(0); },function(err){ console.log("\t## error Uploading script err:"+JSON.stringify(err)); done(1); }); }); /* it('Upload script SinWave', function (done) { var script = {}; script['name'] = "sineWave.C"; script['script_name'] = "sineWave.C"; script['eudk_script_content'] = btoa(sinwave_program); script['eudk_script_language'] = "CPP"; script['script_description'] = "CHAOS AGENT ROOT sineWave.C TEST"; script['default_argument'] = "(1.2,3.4)"; script['workingdir'] = ""; console.log("saving script:" + JSON.stringify(script)); jchaos.saveScript(script, function (data) { done(0); },function(err){ done(1); }); }); */ it('Load back and Execute', function (done) { jchaos.search("simpleRootCounter.C", "script", false, function (l) { var list_algo = l['found_script_list']; list_algo.forEach(function (elem) { console.log(" found " + elem.script_name + " id:" + elem.seq) }); // take the last jchaos.loadScript(list_algo[0].script_name, list_algo[list_algo.length-1].seq, function (data) { var agent_server = "localhost"; var launch_arg = ""; if (process.env.hasOwnProperty('AGENT_SERVER')) { agent_server = process.env['AGENT_SERVER']; } var name = data['script_name']; var language = data['eudk_script_language']; var defargs = data['default_argument']; var workingdir = data['workingdir']; // console.log("loadScript of :" + JSON.stringify(data)); //data['eudk_script_content']=atob(data['eudk_script_content']); jchaos.rmtUploadScript(agent_server + ":"+rest_port, data, function (p) { //console.log("rmtUploadScript:" + JSON.stringify(p)); jchaos.rmtGetEnvironment(agent_server + ":"+rest_port, "CHAOS_PREFIX", function (r) { var chaos_prefix = r.data.value; var path = p.data.path; // console.log("rmtGetEnvironment:" + JSON.stringify(p)); launch_arg = chaos_prefix + "/bin/chaosRoot --conf-file " + chaos_prefix + "/etc/chaos_root.cfg --rootopt \"-q " + path + defargs + "\""; jchaos.rmtCreateProcess(agent_server + ":"+rest_port, name, launch_arg, language, workingdir, function (r) { pid=r.info.pid; uid=r.data.uid; // console.log("\tScript running onto:" + agent_server + " UID:"+uid+" :" + JSON.stringify(r)); done(0); }, function (bad) { console.log("Some error getting loading script:" + JSON.stringify(bad)); done(1); }); }); },function(err){ console.log("##cannot upload err:"+JSON.stringify(err)); done(1); }); }); }); }); it('Test Status', function (done) { /* var agent_server = "localhost"; var launch_arg = ""; if (process.env.hasOwnProperty('AGENT_SERVER')) { agent_server = process.env['AGENT_SERVER']; }*/ jchaos.checkPeriodiocally("\tCheck Tests ends", 120, 2000, function () { var stat = jchaos.rmtListProcess(best_server, null); if (stat.data.hasOwnProperty("processes")) { if (stat.data.processes instanceof Array) { for(var i in stat.data.processes){ // console.log("process info:" + JSON.stringify(stat.data.processes[i])); var cuid=stat.data.processes[i].uid; var cmsg=stat.data.processes[i].msg; if((cuid==uid)&&(cmsg== "ENDED")){ console.log("\tEND uid "+cuid+" "+cmsg); return true; } else { console.log("\tuid "+cuid+" "+cmsg); } } } } return false; }, function () { done(0); }, function () { done(1); }); }); it('Test Console', function (done) { /*var agent_server = "localhost"; if (process.env.hasOwnProperty('AGENT_SERVER')) { agent_server = process.env['AGENT_SERVER']; }*/ jchaos.rmtGetConsole(best_server, uid, 0, -1, function (r) { console.log(atob(r.data.console)); done(0); }, function (bad) { console.log("Some error getting console occur:" + JSON.stringify(bad)); done(1); }); }); it('Test Associate Script sineWave', function (done) { jchaos.associateNode(best_agent,"ALGO/WAVE","","sineWave.C",true,false,false,function(ok){ done(0); },function(err){ console.log("\t## Association error:"+JSON.stringify(err)); done(1); }); }); it('Test Start ALGO/WAVE', function (done) { jchaos.node("ALGO/WAVE", "start", "us", function () { // agent will start the algo done(0); }, function (err) { console.log("## Starting error:"+JSON.stringify(err)); done(1); }); }); it('Check go in Start', function (done) { var cu_status = []; jchaos.checkLive('\tCheck Start',["ALGO/WAVE"], 20, 5000, function (ds) { return (ds!=null)&&ds.hasOwnProperty("health")&&ds.health.hasOwnProperty("nh_status")&&(ds.health.nh_status == "Start"); }, function () { done(0); }, function () { done(1) }); }); it('Test if lives', function (done) { jchaos.checkLive('\tCheck in Process',["ALGO/WAVE"], 60, 2000, function (ds) { var ret=false; var stat = jchaos.rmtListProcess(best_server , null); if (stat.data.hasOwnProperty("processes")) { if (stat.data.processes instanceof Array) { for(var i in stat.data.processes){ var cuid=stat.data.processes[i].uid; var cmsg=stat.data.processes[i].msg; var pname=stat.data.processes[i].pname; if(pname=="ALGO/WAVE"){ wave_uid=cuid; } console.log("\tserver:"+best_server+" process name:"+pname+ " uid:" + cuid+" state:"+cmsg); } } } // console.log("syslen:"+JSON.stringify(ds.system).length+ " healt len:"+JSON.stringify(ds.health).length+" outlen:"+JSON.stringify(ds.output).length); if(typeof ds.health ==='undefined'){ if(ds.hasOwnProperty('system')&&ds.system.hasOwnProperty('ndk_uid')){ console.log(ds.system.ndk_uid,": HEALTH Undefined"); } else { console.log("HEALTH Undefined"); } } if(typeof ds.output ==='undefined'){ if(ds.hasOwnProperty('health')&&ds.health.hasOwnProperty('ndk_uid')){ console.log(ds.health.ndk_uid,": OUTPUT Undefined"); } else { console.log("OUTPUT and HEALTH Undefined"); } } if(typeof ds.input ==='undefined'){ if(ds.hasOwnProperty('health')&&ds.health.hasOwnProperty('ndk_uid')){ console.log(ds.health.ndk_uid,": INPUT Undefined"); } else { console.log("INPUT and HEALTH Undefined"); } } try{ ret=((JSON.stringify(ds.health).length >= 2) && (wave_uid!="")&& (JSON.stringify(ds.output).length >= 2)); } catch(err){ } return ret; }, function () { done(0); }, function () { done(1); }); }); it('Test Console WAVE', function (done) { // the last ten lines jchaos.checkPeriodiocally("\tTests Wave console", 120, 2000, function () { var r=jchaos.rmtGetConsole(best_server, wave_uid, -20, -1, null); if(r.hasOwnProperty("data")&&r.data.hasOwnProperty("console")){ console.log(atob(r.data.console)); return true; } else { return false; } }, function () { done(0); }, function () { done(1); }); }); it('Test STOP sineWave', function (done) { jchaos.node("ALGO/WAVE", "stop", "cu", function () { done(0); }, function (err) { console.log("## stop error:"+JSON.stringify(err)); done(1); }); }); it('check go in Stop', function (done) { var cu_status = []; jchaos.checkLive('\tCheck Stop',["ALGO/WAVE"], 20, 5000, function (ds) { return (ds!=null)&&ds.hasOwnProperty("health")&&ds.health.hasOwnProperty("nh_status")&&(ds.health.nh_status == "Stop"); }, function () { done(0); }, function () { done(1) }); }); it('Test START sineWave', function (done) { jchaos.node("ALGO/WAVE", "start", "cu", function () { done(0); }, function (err) { console.log("## start error:"+JSON.stringify(err)); done(1); }); }); it('check go in Start', function (done) { var cu_status = []; jchaos.checkLive('\tCheck Start',["ALGO/WAVE"], 20, 5000, function (ds) { return (ds!=null)&&ds.hasOwnProperty("health")&&ds.health.hasOwnProperty("nh_status")&&(ds.health.nh_status == "Start"); }, function () { done(0); }, function () { done(1) }); }); it('Test STOP ALGO/WAVE', function (done) { jchaos.node("ALGO/WAVE", "stop", "cu", function () { done(0); }, function (err) { console.log("## Stopping error:"+JSON.stringify(err)); done(1); }); }); it('Test deinit sineWave', function (done) { jchaos.node("ALGO/WAVE", "deinit", "cu", function () { done(0); }, function (err) { console.log("## deinit error:"+JSON.stringify(err)); done(1); }); }); it('check go in Deinit', function (done) { var cu_status = []; jchaos.checkLive('\tCheck Deinit',["ALGO/WAVE"], 20, 5000, function (ds) { return (ds!=null)&&ds.hasOwnProperty("health")&&ds.health.hasOwnProperty("nh_status")&&(ds.health.nh_status == "Deinit"); }, function () { done(0); }, function () { done(1) }); }); it('Test unload sineWave', function (done) { jchaos.node("ALGO/WAVE", "unload", "cu", function () { done(0); }, function (err) { console.log("## unload error:"+JSON.stringify(err)); done(1); }); }); it('Remove Node ALGO/WAVE', function (done) { jchaos.node("ALGO/WAVE", "deletenode", "root", function () { done(0); }, function (err) { console.log("## remove ALGO/WAVE error:"+JSON.stringify(err)); done(1); }); }); it('Remove Association ALGO/WAVE', function (done) { jchaos.node(best_agent, "del", "agent","ALGO/WAVE",null, function () { done(0); }, function (err) { console.log("## deleting association error:"+JSON.stringify(err)); done(1); }); }); });