How to use updateExecutionMachine method in redwood

Best JavaScript code snippet using redwood

executionengine.js

Source:executionengine.js Github

copy

Full Screen

...906 callback();907 agentInstructions.threadID = foundMachine.threadID;908 updateExecutionTestCase({_id:executions[executionID].testcases[id]._id},{$set:{"status":"Running","result":"",error:"",trace:"",resultID:result._id,startdate:testcase.startDate,enddate:"",runtime:"",host:foundMachine.host,vncport:foundMachine.vncport}},foundMachine.host,foundMachine.vncport);909 if ((testcase.machines.length > 0) &&((testcase.machines[0].baseState))){910 updateExecutionMachine(executionID,testcase.machines[0]._id,"",result._id.toString());911 }912 executionsRoute.updateExecutionTotals(executionID);913 if (foundMachine.runBaseState === true){914 if (foundMachine.multiThreaded == true){915 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,30);916 }917 else{918 //make sure the files are actually there, in case of revert to snapshot or not persistent VMs files919 //could have changed while test case was running920 sendAgentCommand(foundMachine.host,foundMachine.port,{command:"files loaded",executionID:executionID},30,function(message){921 if (message.loaded == true){922 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,30);923 }924 else{925 runBaseState();926 }927 });928 }929 }930 else{931 runBaseState();932 }933 });934 })935 });936}937exports.logPost = function(req,res){938 var insertLogMessage = function(record){939 var executionID = record.executionID.replace(/-/g, '');940 delete record.command;941 delete record.executionID;942 db.collection('executionlogs'+executionID, function(err, collection) {943 collection.insert(record, {safe:true},function(err,returnData){944 });945 });946 };947 if(Array.isArray(req.body) == true){948 req.body.forEach(function(record){949 insertLogMessage(record);950 });951 realtime.emitMessage("AddExecutionLog",req.body);952 }953 else{954 insertLogMessage(req.body);955 realtime.emitMessage("AddExecutionLog",[req.body]);956 }957 res.contentType('json');958 res.set('Connection','Close');959 res.json({success:true});960};961exports.actionresultPost = function(req, res){962 res.contentType('json');963 res.set('Connection','Close');964 res.json({success:true});965 var execution = executions[req.body.executionID];966 if (!execution) return;967 var testcase = execution.currentTestCases[req.body.testcaseID];968 if (testcase == undefined) return;969 if (testcase.testcase.script){970 testcase.result.status = "Finished";971 testcase.result.result = req.body.result;972 if (req.body.error){973 testcase.result.error = req.body.error;974 }975 else{976 testcase.result.error = "";977 }978 if (req.body.trace){979 formatTrace(req.body.trace,execution.sourceCache,function(trace){980 testcase.result.trace = trace;981 testcase.trace = trace;982 if(trace == "") testcase.result.trace = req.body.trace;983 updateResult(testcase.result);984 });985 }986 else{987 updateResult(testcase.result);988 }989 finishTestCaseExecution(execution,req.body.executionID,execution.testcases[testcase.executionTestCaseID]._id,testcase);990 return;991 }992 var actionFlow = testcase.currentAction.dbAction.executionflow;993 testcase.currentAction.result.status = "Finished";994 testcase.currentAction.result.result = req.body.result;995 if (req.body.error && actionFlow != "Ignore Error Continue Test Case"){996 testcase.result.error = req.body.error;997 testcase.currentAction.result.error = req.body.error;998 }999 if(!testcase.result.error){1000 testcase.result.error = "";1001 }1002 if (req.body.trace && actionFlow != "Ignore Error Continue Test Case"){1003 testcase.result.trace = req.body.trace;1004 testcase.currentAction.result.trace = req.body.trace;1005 testcase.trace = req.body.trace;1006 }1007 if (req.body.screenshot){1008 testcase.result.screenshot = req.body.screenshot;1009 testcase.currentAction.result.screenshot = req.body.screenshot;1010 }1011 if ((req.body.returnValue)&&(testcase.currentAction.dbAction.returnvalue != "")){1012 if(!execution.returnVars[req.body.testcaseID]){1013 execution.returnVars[req.body.testcaseID] = {};1014 }1015 execution.returnVars[req.body.testcaseID][testcase.currentAction.dbAction.returnvalue] = req.body.returnValue;1016 //.[testcase.currentAction.dbAction.returnvalue] = req.body.returnValue;1017 //execution.variables[testcase.currentAction.dbAction.returnvalue] = req.body.returnValue;1018 }1019 if (req.body.result == "Failed"){1020 if (actionFlow == "Record Error Stop Test Case"){1021 testcase.result.status = "Finished";1022 testcase.result.result = "Failed";1023 testcase.runAfterState = true;1024 testcase.testcase.actions.forEach(function(action){1025 if(!action.dbAction.afterState == true){1026 action.runAction = false;1027 }1028 });1029 //updateExecutionTestCase({_id:execution.testcases[testcase.executionTestCaseID]._id},{$set:{error:testcase.result.error,trace:testcase.trace}});1030 /*1031 markFinishedResults(testcase.result.children,execution.sourceCache,function(){1032 updateResult(testcase.result);1033 });1034 finishTestCaseExecution(execution,req.body.executionID,execution.testcases[testcase.executionTestCaseID]._id,testcase);1035 return;1036 */1037 }1038 else if (actionFlow == "Record Error Continue Test Case"){1039 testcase.result.result = "Failed";1040 updateExecutionTestCase({_id:execution.testcases[testcase.executionTestCaseID]._id},{$set:{result:"Failed",trace:testcase.trace}});1041 }1042 else{1043 testcase.currentAction.result.result = "";1044 testcase.currentAction.result.trace = "";1045 testcase.currentAction.result.error = "";1046 //testcase.result.result = "";1047 //testcase.result.error = "";1048 }1049 }1050 var actionVariables = {};1051 for (var attrname in testcase.testcase.variables) { actionVariables[attrname] = testcase.testcase.variables[attrname]; }1052 for (var attrname in testcase.machineVars) { actionVariables[attrname] = testcase.machineVars[attrname]; }1053 if(execution.returnVars[testcase.executionTestCaseID]){1054 for (var attrname in execution.returnVars[testcase.executionTestCaseID]) { actionVariables[attrname] = execution.returnVars[testcase.executionTestCaseID][attrname]; }1055 }1056 findNextAction(testcase.testcase.actions,actionVariables,function(action){1057 if(action == null){1058 testcase.result.status = "Finished";1059 if(testcase.result.result != "Failed") testcase.result.result = "Passed";1060 markFinishedResults(testcase.result.children,execution.sourceCache,function(){1061 updateResult(testcase.result);1062 });1063 finishTestCaseExecution(execution,req.body.executionID,execution.testcases[testcase.executionTestCaseID]._id,testcase);1064 return;1065 }1066 else{1067 markFinishedResults(testcase.result.children,execution.sourceCache,function(){1068 updateResult(testcase.result);1069 });1070 }1071 var foundMachine = null;1072 var actionHost = "Default";1073 if(action.dbAction.host != "") actionHost = action.dbAction.host;1074 testcase.testcase.machines.forEach(function(machine){1075 if ((machine.roles.indexOf(actionHost) != -1)&&(foundMachine == null)){1076 foundMachine = machine;1077 }1078 });1079 updateExecutionTestCase({_id:execution.testcases[testcase.executionTestCaseID]._id},{$set:{"status":"Running","result":"",host:foundMachine.host,vncport:foundMachine.vncport}},foundMachine.host,foundMachine.vncport);1080 testcase.result.status = "Running";1081 var agentInstructions = {command:"run action",executionID:req.body.executionID,threadID:foundMachine.threadID,testcaseID:testcase.executionTestCaseID,variables:testcase.testcase.variables};1082 execution.currentTestCases[testcase.executionTestCaseID].currentAction = action;1083 agentInstructions.name = action.name;1084 agentInstructions.executionflow = action.dbAction.executionflow;1085 agentInstructions.executionID = req.body.executionID;1086 agentInstructions.ignoreScreenshots = executions[req.body.executionID].ignoreScreenshots;1087 agentInstructions.allScreenshots = executions[req.body.executionID].allScreenshots;1088 agentInstructions.returnValueName = action.dbAction.returnvalue;1089 agentInstructions.testcaseName = testcase.testcase.dbTestCase.name;1090 agentInstructions.script = action.script;1091 agentInstructions.scriptLang = action.scriptLang;1092 agentInstructions.resultID = testcase.result._id.toString();1093 agentInstructions.parameters = [];1094 action.dbAction.parameters.forEach(function(parameter){1095 agentInstructions.parameters.push({name:parameter.paramname,value:parameter.paramvalue});1096 });1097 var runBaseState = function(){1098 agentBaseState(execution.project+"/"+execution.username,req.body.executionID,foundMachine.host,foundMachine.port,foundMachine.threadID,function(err){1099 if (err){1100 testcase.result.error = err.error;1101 testcase.result.status = "Finished";1102 testcase.result.result = "Failed";1103 updateResult(testcase.result);1104 if (execution && testcase.dbTestCase){1105 execution.currentTestCases[testcase.executionTestCaseID].result = "Failed";1106 finishTestCaseExecution(execution,req.body.executionID,execution.testcases[id]._id,execution.currentTestCases[testcase.executionTestCaseID]);1107 }1108 }1109 else{1110 foundMachine.runBaseState = true;1111 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,3);1112 }1113 });1114 };1115 if (foundMachine.runBaseState === true){1116 if (foundMachine.multiThreaded == true){1117 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,3);1118 }1119 else{1120 //make sure the files are actually there, in case of revert to snapshot or not persistent VMs files1121 //could have changed while test case was running1122 sendAgentCommand(foundMachine.host,foundMachine.port,{command:"files loaded",executionID:req.body.executionID},3,function(message){1123 if (message.loaded == true){1124 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,3);1125 }1126 else{1127 runBaseState();1128 }1129 });1130 }1131 }1132 else{1133 runBaseState();1134 }1135 });1136};1137//last action or we are done with the TC1138//start next test case if there is one1139function finishTestCaseExecution(execution,executionID,testcaseId,testcase){1140 //updateExecutionTestCase({_id:execution.testcases[testcase.executionTestCaseID]._id},{$set:{"status":"Finished",result:testcase.result.result}});1141 var date = new Date();1142 var status = "Finished";1143 if((testcase.result.result != "Passed") && (testcase.result.result != "Failed")){1144 status = "Not Run";1145 }1146 var updateTC = function(){1147 updateExecutionTestCase({_id:testcaseId},{$set:{trace:testcase.trace,"status":status,resultID:testcase.result._id.toString(),result:testcase.result.result,error:testcase.result.error,enddate:date,runtime:date-testcase.testcase.startDate,host:"",vncport:""}});1148 executionsRoute.updateExecutionTotals(executionID);1149 };1150 //update machine base state result1151 if(execution.cachedTCs){1152 if (testcase.testcase.machines.length > 0){1153 updateExecutionMachine(executionID,testcase.testcase.machines[0]._id,testcase.result.result,testcase.result._id.toString(),function(){1154 updateTC();1155 });1156 }1157 else{1158 updateTC();1159 }1160 //updateExecutionMachine({testcase.testcase.machines[0]._id)},{$set:{result:testcase.result.result,resultID:testcase.result._id.__id}});1161 }1162 else{1163 updateTC();1164 }1165 var count = 0;1166 var retry = false;1167 if(testcase.result.result == "Failed"){1168 if (execution.testcases[testcase.executionTestCaseID].retryCount > 0){1169 retry = true;1170 execution.testcases[testcase.executionTestCaseID].retryCount--;1171 execution.testcases[testcase.executionTestCaseID].executing = false;1172 execution.returnVars[testcase.executionTestCaseID] = {};1173 }1174 if (execution.cachedTCs){1175 execution.baseStateFailed = true;1176 }1177 }1178 if(testcase.testcase.machines.length === 0){1179 if (retry == false){1180 //delete execution.testcases[testcase.executionTestCaseID];1181 execution.testcases[testcase.executionTestCaseID].finished = true;1182 execution.testcases[testcase.executionTestCaseID].executing = false;1183 }1184 delete execution.currentTestCases[testcase.executionTestCaseID];1185 executeTestCases(execution.testcases,executionID);1186 return;1187 }1188 testcase.testcase.machines.forEach(function(machine){1189 delete machine.runningTC;1190 count++;1191 if (count == testcase.testcase.machines.length){1192 if (retry == false){1193 //delete execution.testcases[testcase.executionTestCaseID];1194 execution.testcases[testcase.executionTestCaseID].finished = true;1195 execution.testcases[testcase.executionTestCaseID].executing = false;1196 }1197 delete execution.currentTestCases[testcase.executionTestCaseID];1198 //if(execution.executingTCs != true){1199 executeTestCases(execution.testcases,executionID);1200 //}1201 }1202 });1203}1204exports.formatTrace = function(trace,sourceCache,callback){formatTrace(trace,sourceCache,callback)};1205function formatTrace(trace,sourceCache,callback){1206 var newTrace = "";1207 var traceCount = 0;1208 var traces = trace.split(",");1209 traces.forEach(function(line){1210 traceCount++;1211 var fileName = line.substring(line.indexOf("(")+1,line.indexOf(":"));1212 var lineNumber = line.substring(line.indexOf(":")+1,line.indexOf(")"));1213 var location = line.substring(0,line.indexOf("("));1214 location = location.trim();1215 location = location.replace("[","");1216 var count = 0;1217 var found = false;1218 if((location.indexOf("org.codehaus.") != -1) || (location.indexOf("java.lang.") != -1) || (location.indexOf("groovy.lang.") != -1) || (location.indexOf("redwood.launcher.") != -1)|| (location.indexOf("sun.reflect.") != -1)){1219 if (line.indexOf("[") == 0){1220 newTrace += line;1221 }1222 else{1223 newTrace += ",\r\n"+line;1224 }1225 if (traceCount == traces.length){1226 callback(newTrace);1227 }1228 return;1229 }1230 if ((fileName.indexOf(".groovy") != -1) ||(fileName.indexOf(".java") != -1)){1231 sourceCache.forEach(function(file){1232 if (found == true) return;1233 if (file.indexOf("/"+fileName) != -1){1234 if(location.replace(/\./g, '/').indexOf(file.substring(0,file.lastIndexOf("/")).replace("src/", '')) != -1){1235 found = true;1236 var parsedLineNumber = (parseInt(lineNumber,10)-1).toString();1237 newTrace += ",\r\n<a style= 'color: blue;' href='javascript:openScript(&quot;"+ file +"&quot;,&quot;"+ parsedLineNumber +"&quot;)'>" + line +"</a>";1238 }1239 }1240 count++;1241 if (count == sourceCache.length){1242 if (found == false){1243 if (line.indexOf("[") == 0){1244 newTrace += line;1245 }1246 else{1247 newTrace += ",\r\n"+line;1248 }1249 }1250 if (traceCount == traces.length){1251 callback(newTrace);1252 }1253 }1254 });1255 }1256 else{1257 if (traceCount == traces.length){1258 callback(newTrace);1259 }1260 }1261 });1262}1263function markFinishedResults(results,sourceCache,callback){1264 var count = 0;1265 var result = "Passed";1266 var status = "Finished";1267 results.forEach(function(action){1268 if (action.status == "Not Run"){1269 if (action.children.length != 0){1270 markFinishedResults(action.children,sourceCache,function(childStatus,childResult,markFinished){1271 action.expanded = false;1272 if (markFinished === true){1273 action.status = "Finished";1274 action.result = "Failed";1275 action.expanded = true;1276 callback("Finished","Failed",true);1277 return;1278 }1279 count++;1280 if (childStatus == "Not Run"){1281 status = "Not Run";1282 result = "";1283 }1284 if((childStatus == "Finished") && (childResult == "Failed")){1285 result = "Failed";1286 action.expanded = true;1287 }1288 if(count == action.children.length){1289 action.result = result;1290 action.status = status;1291 if (status == "Finished"){1292 if ((action.executionflow == "Record Error Stop Test Case")&&(result == "Failed")){1293 callback(status,result,true)1294 }1295 else{1296 callback(status,result)1297 }1298 }1299 else{1300 callback(status,"")1301 }1302 }1303 })1304 }1305 else{1306 callback("Not Run","")1307 }1308 }1309 else{1310 var returnValue;1311 if ((action.executionflow == "Record Error Stop Test Case")&&(action.result == "Failed")){1312 returnValue = function(){callback("Finished","Failed",true)}1313 }1314 else{1315 returnValue = function(){callback("Finished",action.result)}1316 }1317 if(action.result == "Failed"){1318 action.expanded = true;1319 }1320 if(action.trace){1321 formatTrace(action.trace,sourceCache,function(trace){1322 if(trace != "") action.trace = trace;1323 returnValue();1324 })1325 }1326 else{1327 returnValue();1328 }1329 }1330 });1331}1332exports.agentBaseState = function(project,executionID,agentHost,port,threadID,callback){agentBaseState(project,executionID,agentHost,port,threadID,callback)};1333function agentBaseState(project,executionID,agentHost,port,threadID,callback){1334 sendAgentCommand(agentHost,port,{command:"cleanup",executionID:executionID},3,function(message){1335 if (message && message.error){1336 callback(message);1337 return;1338 }1339 if(executions[executionID]){1340 executions[executionID].fileReqs = [];1341 }1342 else if(executionID.indexOf("unittest") == -1){1343 return;1344 }1345 //os.tmpDir()+"/jar_"+executionID1346 syncFilesWithAgent(agentHost,port,path.join(__dirname, '../public/automationscripts/'+project+"/bin"),"executionfiles/"+executionID+"/bin",executionID,function(error){1347 if(error) {callback(error);return}1348 syncFilesWithAgent(agentHost,port,path.join(__dirname, '../launcher'),"executionfiles/"+executionID+"/launcher",executionID,function(error){1349 if(error) {callback(error);return}1350 syncFilesWithAgent(agentHost,port,path.join(__dirname, '../public/automationscripts/'+project+"/External Libraries"),"executionfiles/"+executionID+"/lib",executionID,function(error){1351 if(error) {callback(error);return}1352 syncFilesWithAgent(agentHost,port,os.tmpDir()+"/jar_"+executionID,"executionfiles/"+executionID+"/lib",executionID,function(error){1353 if(error) {callback(error);return}1354 sendAgentCommand(agentHost,port,{command:"start launcher",executionID:executionID,threadID:threadID},3,function(message){1355 if ((message) && (message.error)){1356 callback(message);1357 }1358 else{1359 callback();1360 }1361 });1362 })1363 })1364 })1365 });1366 });1367}1368function syncFilesWithAgent_old(agentHost,port,rootPath,destDir,callback){1369 var walker = walk.walkSync(rootPath);1370 var fileCount = 0;1371 walker.on("file", function (root, fileStats, next) {1372 fileCount++;1373 var path = root.replace(rootPath,"");1374 var dest = "";1375 if (path == ""){1376 dest = destDir +"/"+ fileStats.name;1377 }1378 else{1379 dest = destDir + path+"/"+fileStats.name1380 }1381 sendFileToAgent(root+"/"+fileStats.name,dest,agentHost,port,3,function(){1382 fileCount--;1383 if(fileCount == 0){1384 callback();1385 }1386 });1387 });1388 walker.on("end",function(){1389 if (fileCount == 0) callback();1390 });1391}1392function syncFilesWithAgent(agentHost,port,rootPath,destDir,executionID,callback){1393 var walker = walk.walkSync(rootPath);1394 var fileCount = 0;1395 var files = [];1396 var foundError = false;1397 var sendFiles = function(){1398 if(foundError) return;1399 fileCount++;1400 if (!files[fileCount-1]){1401 callback();1402 return;1403 }1404 sendFileToAgent(files[fileCount-1].file,files[fileCount-1].dest,agentHost,port,3,executionID,function(error){1405 if(error){1406 foundError = true;1407 callback(error);1408 return;1409 }1410 if(fileCount === files.length){1411 callback();1412 }1413 else{1414 sendFiles()1415 }1416 });1417 };1418 walker.on("file", function (root, fileStats, next) {1419 var path = root.replace(rootPath,"");1420 var dest = "";1421 if (path == ""){1422 dest = destDir +"/"+ fileStats.name;1423 }1424 else{1425 dest = destDir + path+"/"+fileStats.name1426 }1427 files.push({file:root+"/"+fileStats.name,dest:dest});1428 /*1429 sendFileToAgent(root+"/"+fileStats.name,dest,agentHost,port,3,executionID,function(error){1430 if(error){1431 foundError = true;1432 callback(error);1433 return;1434 }1435 fileCount++;1436 if(fileCount === files.length){1437 callback();1438 }1439 })1440 */1441 });1442 walker.on("end",function(){1443 if(files.length == 0) {1444 callback();1445 return;1446 }1447 sendFiles();1448 });1449}1450//if agent already got the file in cache don't copy1451function matchFileWithAgent(file,dest,agentHost,port,retryCount,callback){1452 //don't match pythonLibs or python sources1453 if (file.indexOf("pythonLibs.zip", file.length - "pythonLibs.zip".length) !== -1 || file.indexOf("pythonSources.zip", file.length - "pythonSources.zip".length) !== -1){1454 if(callback) callback({error:null,success:true,matched:false});1455 //if(callback) callback();1456 return;1457 }1458 var options = {1459 hostname: agentHost,1460 port: port,1461 path: '/matchfile',1462 method: 'POST',1463 headers: {1464 'Content-Type': 'application/json'1465 }1466 };1467 var req = http.request(options, function(res) {1468 res.setEncoding('utf8');1469 res.on('data', function (chunk) {1470 try{1471 var msg = JSON.parse(chunk);1472 }1473 catch(err){1474 if (callback) callback(err);1475 }1476 if((msg )&&(msg.error != null)){1477 if (callback) callback(msg.error);1478 }1479 else if (msg){1480 if(callback) callback(msg);1481 }1482 else{1483 if(callback) callback();1484 }1485 });1486 });1487 var retryMatch = function(message){1488 if(retryCount <= 0){1489 if (callback) callback("Unable to connect to machine: "+agentHost + " error: " + message);1490 common.logger.error('matchFileWithAgent problem with request: ' + message+ ' ');1491 }1492 else{1493 retryCount--;1494 setTimeout(matchFileWithAgent(file,dest,agentHost,port,retryCount,callback),1000)1495 }1496 };1497 req.on('error', function(e) {1498 retryMatch(e.message)1499 });1500 //fs.readFile(file, function(err, buf) {1501 // write data to request body1502 //req.write(JSON.stringify({dest:dest,file:file,md5:md5(buf)}));1503 var md5sum = require("crypto").createHash('md5');1504 var s = fs.ReadStream(file);1505 fileSync[file] = s;1506 s.on('data',function(d){1507 md5sum.update(d);1508 });1509 s.on('error',function(err){1510 s.destroy.bind(s);1511 retryMatch("Unable to read file:"+file)1512 });1513 s.on('close',function(){1514 var d = md5sum.digest('hex');1515 this.destroy();1516 req.write(JSON.stringify({dest:dest,file:file,md5:d.toString()}));1517 req.end();1518 //md5sum.end();1519 });1520 //});1521}1522function sendFileToAgent(file,dest,agentHost,port,retryCount,executionID,callback){1523 if (file in fileSync){1524 if(fileSync[file].close){1525 fileSync[file].destroy();1526 delete fileSync[file];1527 }1528 }1529 fileSync[file] = true;1530 matchFileWithAgent(file,dest,agentHost,port,0,function(response){1531 if(response && response.matched == true){1532 if(file in fileSync && fileSync[file].close){1533 fileSync[file].destroy();1534 }1535 delete fileSync[file];1536 if (callback) callback();1537 return;1538 }1539 try{1540 var stat = fs.statSync(file);1541 var readStream = fs.createReadStream(file);1542 }1543 catch(e){1544 if (callback) callback({error: "Can't read file: " + file + " " + e.message});1545 return;1546 }1547 fileSync[file] = readStream;1548 var boundary = '--------------------------';1549 for (var i = 0; i < 24; i++) {1550 boundary += Math.floor(Math.random() * 10).toString(16);1551 }1552 var message = '------' + boundary + '\r\n'1553 // use your file's mime type here, if known1554 + 'Content-Disposition: form-data; name="file"; filename="'+dest+'"\r\n'1555 + 'Content-Type: application/octet-stream\r\n'1556 // "name" is the name of the form field1557 // "filename" is the name of the original file1558 + 'Content-Transfer-Encoding: binary\r\n\r\n';1559 var options = {1560 hostname: agentHost,1561 port: port,1562 path: '/fileupload',1563 method: 'POST',1564 headers: {1565 //'Content-Type': 'text/plain'//,1566 'Content-Type': 'multipart/form-data; boundary=----'+boundary,1567 //'Content-Disposition': 'form-data; name="file"; filename="ProjectName.jar"',1568 //'Content-Length': 33601569 //'Content-Length': stat.size + message.length + 30 + boundary.length1570 'Content-Length': stat.size + message.length + boundary.length + 141571 }1572 };1573 var req = http.request(options, function(res) {1574 //res.setEncoding('utf8');1575 res.on('data', function (chunk) {1576 readStream.destroy();1577 if (file in fileSync){1578 if(fileSync[file].close){1579 fileSync[file].destroy();1580 delete fileSync[file];1581 }1582 }1583 if (callback) callback();1584 });1585 res.on('close', function(){1586 if(file in fileSync && fileSync[file].close){1587 fileSync[file].destroy();1588 }1589 delete fileSync[file];1590 readStream.destroy.bind(readStream);1591 });1592 });1593 if( executions[executionID] && executions[executionID].fileReqs){1594 executions[executionID].fileReqs.push(req);1595 }1596 var handleError = function(e){1597 readStream.destroy();1598 if(file in fileSync && fileSync[file].close){1599 fileSync[file].destroy();1600 }1601 delete fileSync[file];1602 if(retryCount <= 0){1603 if (callback) callback({error:'sendFileToAgent problem with request: ' + e.message+ ' file:'+file});1604 common.logger.error('sendFileToAgent problem with request: ' + e.message+ ' file:'+file);1605 }1606 else{1607 retryCount--;1608 setTimeout(sendFileToAgent(file,dest,agentHost,port,retryCount,executionID,callback),1000)1609 }1610 };1611 req.setTimeout(300000, function(){1612 handleError({message:"Unable to connect to machine: "+agentHost + " CONNECTION TIMEOUT"});1613 this.end();1614 });1615 req.on('error', function(e) {1616 handleError(e);1617 this.end();1618 });1619 req.write(message);1620 //readStream.pipe(req, { end: false });1621 readStream.on("error",function(e){1622 this.end();1623 handleError(e);1624 }).pipe(req, { end: false });1625 readStream.on("end", function(){1626 try{1627 req.end('\r\n------' + boundary + '--\r\n');1628 }1629 catch(e){1630 //req.end();1631 readStream.destroy();1632 }1633 });1634 readStream.on('close',function(){1635 if(file in fileSync && fileSync[file].close){1636 fileSync[file].destroy();1637 }1638 delete fileSync[file];1639 })1640 })1641}1642exports.sendAgentCommand = function(agentHost,port,command,retryCount,callback){sendAgentCommand(agentHost,port,command,retryCount,callback)};1643function sendAgentCommand(agentHost,port,command,retryCount,callback){1644 common.logger.info(command);1645 var options = {1646 hostname: agentHost,1647 port: port,1648 path: '/command',1649 method: 'POST',1650 headers: {1651 'Content-Type': 'application/json'1652 }1653 };1654 var req = http.request(options, function(res) {1655 res.setEncoding('utf8');1656 res.on('data', function (chunk) {1657 try{1658 var msg = JSON.parse(chunk);1659 }1660 catch(err){1661 if (callback) callback({error:err});1662 }1663 if((msg )&&(msg.error != null)){1664 if (callback) callback({error:msg.error});1665 }1666 else if (msg){1667 if(callback) callback(msg);1668 }1669 else{1670 if(callback) callback();1671 }1672 });1673 });1674 //req.setTimeout(50000, function(){1675 // if (callback) callback({error:"Unable to connect to machine: "+agentHost + " CONNECTION TIMEOUT"});1676 //});1677 req.on('error', function(e) {1678 retryCount = 0;1679 if(retryCount <= 0){1680 if (callback) callback({error:"Unable to connect to machine: "+agentHost + " error: " + e.message});1681 common.logger.error('sendAgentCommand problem with request: ' + e.message+ ' ');1682 }1683 else{1684 retryCount--;1685 setTimeout(sendAgentCommand(agentHost,port,command,retryCount,callback),3000)1686 }1687 //req.close();1688 });1689 // write data to request body1690 req.write(JSON.stringify(command));1691 req.end();1692}1693function resolveParamValue(value,variables){1694 var returnNULL = false;1695 var resolveVariable = function(stringValue){1696 return stringValue.replace(new RegExp("\\$\\{([\\s\\w_.-]+)\\}", "g" ),function(a,b){1697 if(b in variables){1698 if (variables[b] == "<NULL>"){1699 if (returnNULL == true){1700 return "<NULL>"1701 }1702 else{1703 return "";1704 }1705 }1706 else{1707 return variables[b];1708 }1709 }1710 else{1711 return a;1712 }1713 });1714 };1715 if(Object.prototype.toString.call(value) == '[object Array]'){1716 if((value.length == 1) && (value[0] === "<NULL>")){1717 return [];1718 }1719 else{1720 var returnValue = [];1721 value.forEach(function(arrayItem){1722 returnValue.push(resolveVariable(arrayItem))1723 });1724 return returnValue;1725 }1726 }1727 else if (typeof value != "string"){1728 return value;1729 }1730 if (value.length > 4){1731 if ((value.indexOf("$\{") == 0) &&(value.charAt(value.length -1) == "}") && (value.split("}").length > 1))1732 {1733 returnNULL = true;1734 }1735 }1736 return resolveVariable(value);1737}1738function createResult(result,callback){1739 db.collection('testcaseresults', function(err, collection) {1740 result._id = new ObjectID(result._id);1741 collection.insert(result, {safe:true},function(err,returnData){1742 callback(returnData);1743 });1744 });1745}1746function updateResult(result,callback){1747 db.collection('testcaseresults', function(err, collection) {1748 collection.save(result,{safe:true},function(err,data){1749 if(err) common.logger.error("ERROR updating results: "+err);1750 if (callback){1751 callback(err);1752 }1753 //realtime.emitMessage("UpdateResult",result);1754 realtime.emitMessage("UpdateResult"+result._id.toString(),result);1755 });1756 });1757}1758function updateExecutionTestCase(query,update,machineHost,vncport,callback){1759 db.collection('executiontestcases', function(err, collection) {1760 collection.findAndModify(query,{},update,{safe:true,new:true},function(err,data){1761 if(err) common.logger.error("ERROR updating results: "+err.message);1762 if (data == null){1763 if (callback){1764 callback(err);1765 }1766 return;1767 }1768 if (data && data.status == "Finished"){1769 elasticSearch.indexTCResult(data,"PUT");1770 }1771 if (machineHost){1772 data.host = machineHost;1773 data.vncport = vncport;1774 }1775 realtime.emitMessage("UpdateExecutionTestCase",data);1776 if (callback){1777 callback(err);1778 }1779 });1780 });1781}1782function updateExecution(query,update,finished,callback){1783 db.collection('executions', function(err, collection) {1784 collection.findAndModify(query,{},update,{safe:true,new:true},function(err,data){1785 try{1786 elasticSearch.indexExecution(data);1787 }1788 catch(e){1789 console.log(e);1790 }1791 if(err) {1792 common.logger.error("ERROR updating execution: "+err.message);1793 return;1794 }1795 realtime.emitMessage("UpdateExecutions",data);1796 if(finished === true){1797 realtime.emitMessage("FinishExecution",data);1798 }1799 if (callback){1800 callback(err);1801 }1802 });1803 });1804}1805function updateExecutionMachine(executionID,machineID,result,resultID,callback){1806 db.collection('executions', function(err, collection) {1807 collection.update({_id:executionID,machines:{$elemMatch:{_id:machineID}}},{$set:{'machines.$.result':result,'machines.$.resultID':resultID}},{safe:true,new:true},function(err,data){1808 //realtime.emitMessage("UpdateExecutions",data);1809 if (callback){1810 callback(err);1811 }1812 });1813 });1814}1815function cleanExecutionMachines(executionID,callback){1816 db.collection('executions', function(err, collection) {1817 collection.findOne({_id:executionID},function(err,data){1818 //realtime.emitMessage("UpdateExecutions",data);1819 if(data != null){1820 data.machines.forEach(function(machine){1821 machine.result = "";1822 machine.resultID = "";1823 });1824 collection.save(data,{safe:true},function(err){1825 //realtime.emitMessage("UpdateExecutions",data);1826 if (callback) callback();1827 });1828 }1829 });1830 });1831}1832function updateMachine(query,update,callback){1833 db.collection('machines', function(err, collection) {1834 collection.findAndModify(query,{},update,{safe:true,new:true},function(err,data){1835 if(data != null){1836 realtime.emitMessage("UpdateMachines",{_id:data._id,state:data.state,takenThreads:data.takenThreads});1837 }1838 if (callback) callback();1839 });1840 });1841}1842exports.verifyMachineState = function(machines,callback){verifyMachineState(machines,callback)};1843function verifyMachineState(machines,callback){1844 var machineCount = 0;1845 if(machines.length == 0 && callback) callback();1846 machines.forEach(function(machine){1847 db.collection('machines', function(err, collection) {1848 collection.findOne({_id:new ObjectID(machine._id)}, {}, function(err, dbMachine) {1849 if(dbMachine.maxThreads < dbMachine.takenThreads + machine.threads)1850 {1851 callback("Machine: "+ machine.host+" has reached thread limit.");1852 machineCount = machines.length+1;1853 return;1854 }1855 machineCount++;1856 if (machineCount == machines.length){1857 if(callback) callback();1858 }1859 });1860 });1861 });1862}1863function GetRunningExecution(callback){1864 db.collection('executions', function(err, collection) {1865 var executions = [];1866 collection.find({status:"Running"}, {}, function (err, cursor) {1867 cursor.each(function (err, execution) {1868 if (execution == null) {1869 callback(executions)1870 }1871 else{1872 executions.push(execution._id)1873 }1874 });1875 });1876 });1877}1878function VerifyCloudCapacity(template,callback){1879 if(template == null) {1880 callback({capacityAvailable:true});1881 return;1882 }1883 db.collection('hosts', function(err, collection) {1884 var hosts = [];1885 collection.find({}, {}, function(err, cursor) {1886 cursor.each(function(err, host) {1887 if(host == null) {1888 if(hosts.length == 0){1889 callback({err:"Error: No hosts are found."});1890 return1891 }1892 GetRunningExecution(function(runningExecutions){1893 var appDir = path.resolve(__dirname,"../")+"/";1894 var proc = spawn(appDir+"vendor/Java/bin/java",["-cp",appDir+'utils/lib/*;'+appDir+'vendor/groovy/*;'+appDir+'utils/*',"com.primatest.cloud.Main",JSON.stringify({operation:"capacityValidation",hosts:hosts,totalInstances:template.instances,templateName:template.name,runningExecutions:runningExecutions})]);1895 var cache = "";1896 proc.stdout.on('data', function (data) {1897 cache = cache + data.toString();1898 //console.log(data.toString());1899 });1900 proc.stderr.on('data', function (data) {1901 common.logger.error('Cloud stderr: ' + data.toString());1902 });1903 proc.on('close', function (code) {1904 var response = JSON.parse(cache);1905 callback(response);1906 });1907 })1908 }1909 hosts.push(host);1910 });1911 })1912 });1913}1914function StartCloudMachines(template,executionID,callback){1915 if(template == null) {1916 callback([]);1917 return;1918 }1919 db.collection('hosts', function(err, collection) {1920 var hosts = [];1921 collection.find({}, {}, function(err, cursor) {1922 cursor.each(function(err, host) {1923 if(host == null) {1924 if(hosts.length == 0){1925 callback({err:"Error: No hosts are found."});1926 return1927 }1928 var appDir = path.resolve(__dirname,"../")+"/";1929 //console.log(appDir+"vendor/Java/bin/java "+"-cp "+appDir+'utils/lib/*;'+appDir+'vendor/groovy/*;'+appDir+'utils/* '+"com.primatest.cloud.Main \""+JSON.stringify({operation:"capacityValidation",hosts:hosts,totalInstances:totalInstances}).replace(/"/g,'\\"')+'"');1930 //console.log({operation:"startCloudMachines",hosts:hosts,template:template});1931 var proc = spawn(appDir+"vendor/Java/bin/java",["-cp",appDir+'utils/lib/*;'+appDir+'vendor/groovy/*;'+appDir+'utils/*',"com.primatest.cloud.Main",JSON.stringify({operation:"startCloudMachines",hosts:hosts,template:template,executionID:executionID})]);1932 var cache = "";1933 proc.stdout.on('data', function (data) {1934 //console.log(data.toString());1935 cache = cache + data.toString();1936 });1937 proc.stderr.on('data', function (data) {1938 common.logger.error('Cloud stderr: ' + data.toString());1939 //callback({err:data.toString()})1940 });1941 /*1942 proc.on('exit', function (code) {1943 var response = JSON.parse(cache);1944 if(response.err){1945 callback(response);1946 }1947 });1948 */1949 proc.on('close', function (code) {1950 var response = JSON.parse(cache);1951 if(response.err){1952 callback(response);1953 return;1954 }1955 var cloudMachines = [];1956 response.forEach(function(machine,index){1957 cloudMachines.push({1958 host:machine.IP,1959 machineVars:[],1960 maxThreads:1000,1961 port:5009,1962 result:"",1963 roles:["Default"],1964 threads:template.threads,1965 vncport: 3006,1966 cloud:true,1967 vmName:machine.vmName,1968 vmHost:machine.host,1969 template:template.name1970 });1971 if(index == response.length-1){1972 callback(cloudMachines)1973 }1974 });1975 });1976 return;1977 }1978 else{1979 hosts.push(host);1980 }1981 });1982 })1983 });1984}1985function lockMachines(machines,executionID,callback){1986 var machineCount = 0;1987 if(machines.length == 0 && callback) callback();1988 machines.forEach(function(machine){1989 updateExecutionMachine(executionID,machine._id,"","");1990 db.collection('machines', function(err, collection) {1991 collection.findOne({_id:new ObjectID(machine._id)}, {}, function(err, dbMachine) {1992 if(dbMachine.maxThreads < dbMachine.takenThreads + machine.threads)1993 {1994 callback("Machine: "+ machine.host+" has reached thread limit.");1995 return;1996 }1997 if(dbMachine != null) {1998 var takenThreads = 1;1999 if (dbMachine.takenThreads){2000 takenThreads = dbMachine.takenThreads + machine.threads;2001 }2002 else{2003 takenThreads = machine.threads;...

Full Screen

Full Screen

executionengine original.js

Source:executionengine original.js Github

copy

Full Screen

...833 });834 agentInstructions.threadID = foundMachine.threadID;835 updateExecutionTestCase({_id:executions[executionID].testcases[id]._id},{$set:{"status":"Running","result":"",error:"",trace:"",resultID:result._id,startdate:testcase.startDate,enddate:"",runtime:"",host:foundMachine.host,vncport:foundMachine.vncport}},foundMachine.host,foundMachine.vncport);836 if ((testcase.machines.length > 0) &&((testcase.machines[0].baseState))){837 updateExecutionMachine(executionID,testcase.machines[0]._id,"",result._id.__id);838 }839 executionsRoute.updateExecutionTotals(executionID);840 if (foundMachine.runBaseState === true){841 if (foundMachine.multiThreaded == true){842 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,3);843 }844 else{845 //make sure the files are actually there, in case of revert to snapshot or not persistent VMs files846 //could have changed while test case was running847 sendAgentCommand(foundMachine.host,foundMachine.port,{command:"files loaded",executionID:executionID},3,function(message){848 if (message.loaded == true){849 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,3);850 }851 else{852 runBaseState();853 }854 });855 }856 }857 else{858 runBaseState();859 }860 });861 })862 });863}864exports.logPost = function(req,res){865 var record = req.body;866 var executionID = record.executionID.replace(/-/g, '');867 delete record.command;868 delete record.executionID;869 db.collection('executionlogs'+executionID, function(err, collection) {870 collection.insert(record, {safe:true},function(err,returnData){871 res.contentType('json');872 res.json({success:true});873 realtime.emitMessage("AddExecutionLog",record);874 });875 });876};877exports.actionresultPost = function(req, res){878 res.contentType('json');879 res.json({success:true});880 var execution = executions[req.body.executionID];881 if (!execution) return;882 var testcase = execution.currentTestCases[req.body.testcaseID];883 if (testcase == undefined) return;884 if (testcase.testcase.script){885 testcase.result.status = "Finished";886 testcase.result.result = req.body.result;887 if (req.body.error){888 testcase.result.error = req.body.error;889 }890 else{891 testcase.result.error = "";892 }893 if (req.body.trace){894 formatTrace(req.body.trace,execution.sourceCache,function(trace){895 testcase.result.trace = trace;896 if(trace == "") testcase.result.trace = req.body.trace;897 updateResult(testcase.result);898 });899 }900 else{901 updateResult(testcase.result);902 }903 finishTestCaseExecution(execution,req.body.executionID,execution.testcases[testcase.executionTestCaseID]._id,testcase);904 return;905 }906 testcase.currentAction.result.status = "Finished";907 testcase.currentAction.result.result = req.body.result;908 if(!testcase.result.error){909 testcase.result.error = "";910 }911 if (req.body.error){912 testcase.result.error = req.body.error;913 testcase.currentAction.result.error = req.body.error;914 }915 if (req.body.trace){916 testcase.result.trace = req.body.trace;917 testcase.currentAction.result.trace = req.body.trace;918 testcase.trace = req.body.trace;919 }920 if (req.body.screenshot){921 testcase.result.screenshot = req.body.screenshot;922 testcase.currentAction.result.screenshot = req.body.screenshot;923 }924 if ((req.body.returnValue)&&(testcase.currentAction.dbAction.returnvalue != "")){925 if(!execution.returnVars[req.body.testcaseID]){926 execution.returnVars[req.body.testcaseID] = {};927 }928 execution.returnVars[req.body.testcaseID][testcase.currentAction.dbAction.returnvalue] = req.body.returnValue;929 //.[testcase.currentAction.dbAction.returnvalue] = req.body.returnValue;930 //execution.variables[testcase.currentAction.dbAction.returnvalue] = req.body.returnValue;931 }932 var actionFlow = testcase.currentAction.dbAction.executionflow;933 if (req.body.result == "Failed"){934 if (actionFlow == "Record Error Stop Test Case"){935 testcase.result.status = "Finished";936 testcase.result.result = "Failed";937 testcase.runAfterState = true;938 testcase.testcase.actions.forEach(function(action){939 if(!action.dbAction.afterState == true){940 action.runAction = false;941 }942 });943 //updateExecutionTestCase({_id:execution.testcases[testcase.executionTestCaseID]._id},{$set:{error:testcase.result.error,trace:testcase.trace}});944 /*945 markFinishedResults(testcase.result.children,execution.sourceCache,function(){946 updateResult(testcase.result);947 });948 finishTestCaseExecution(execution,req.body.executionID,execution.testcases[testcase.executionTestCaseID]._id,testcase);949 return;950 */951 }952 else if (actionFlow == "Record Error Continue Test Case"){953 testcase.result.result = "Failed";954 updateExecutionTestCase({_id:execution.testcases[testcase.executionTestCaseID]._id},{$set:{result:"Failed",trace:testcase.trace}});955 }956 else{957 testcase.currentAction.result.result = "";958 testcase.currentAction.result.trace = "";959 testcase.currentAction.result.error = "";960 testcase.result.result = "";961 testcase.result.error = "";962 }963 }964 markFinishedResults(testcase.result.children,execution.sourceCache,function(){965 updateResult(testcase.result);966 });967 var actionVariables = {};968 for (var attrname in execution.variables) { actionVariables[attrname] = execution.variables[attrname]; }969 for (var attrname in testcase.machineVars) { actionVariables[attrname] = testcase.machineVars[attrname]; }970 if(execution.returnVars[testcase.executionTestCaseID]){971 for (var attrname in execution.returnVars[testcase.executionTestCaseID]) { actionVariables[attrname] = execution.returnVars[testcase.executionTestCaseID][attrname]; }972 }973 findNextAction(testcase.testcase.actions,actionVariables,function(action){974 if(action == null){975 testcase.result.status = "Finished";976 if(testcase.result.result != "Failed") testcase.result.result = "Passed";977 updateResult(testcase.result);978 finishTestCaseExecution(execution,req.body.executionID,execution.testcases[testcase.executionTestCaseID]._id,testcase);979 return;980 }981 var foundMachine = null;982 var actionHost = "Default";983 if(action.dbAction.host != "") actionHost = action.dbAction.host;984 testcase.testcase.machines.forEach(function(machine){985 if ((machine.roles.indexOf(actionHost) != -1)&&(foundMachine == null)){986 foundMachine = machine;987 }988 });989 updateExecutionTestCase({_id:execution.testcases[testcase.executionTestCaseID]._id},{$set:{"status":"Running","result":"",host:foundMachine.host,vncport:foundMachine.vncport}},foundMachine.host,foundMachine.vncport);990 testcase.result.status = "Running";991 var agentInstructions = {command:"run action",executionID:req.body.executionID,threadID:foundMachine.threadID,testcaseID:testcase.testcase.dbTestCase._id,variables:execution.variables};992 execution.currentTestCases[testcase.testcase.dbTestCase._id].currentAction = action;993 agentInstructions.name = action.name;994 agentInstructions.executionflow = action.dbAction.executionflow;995 agentInstructions.executionID = req.body.executionID;996 agentInstructions.ignoreScreenshots = executions[req.body.executionID].ignoreScreenshots;997 agentInstructions.allScreenshots = executions[req.body.executionID].allScreenshots;998 agentInstructions.returnValueName = action.dbAction.returnvalue;999 agentInstructions.testcaseName = testcase.testcase.dbTestCase.name;1000 agentInstructions.script = action.script;1001 agentInstructions.scriptLang = action.scriptLang;1002 agentInstructions.resultID = testcase.result._id.__id;1003 agentInstructions.parameters = [];1004 action.dbAction.parameters.forEach(function(parameter){1005 agentInstructions.parameters.push({name:parameter.paramname,value:parameter.paramvalue});1006 });1007 var runBaseState = function(){1008 agentBaseState(execution.project+"/"+execution.username,req.body.executionID,foundMachine.host,foundMachine.port,foundMachine.threadID,function(err){1009 if (err){1010 testcase.result.error = err.error;1011 testcase.result.status = "Finished";1012 testcase.result.result = "Failed";1013 updateResult(testcase.result);1014 if (execution){1015 execution.currentTestCases[testcase.dbTestCase._id].result = "Failed";1016 finishTestCaseExecution(execution,req.body.executionID,execution.testcases[id]._id,execution.currentTestCases[testcase.dbTestCase._id]);1017 }1018 }1019 else{1020 foundMachine.runBaseState = true;1021 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,3);1022 }1023 });1024 };1025 if (foundMachine.runBaseState === true){1026 if (foundMachine.multiThreaded == true){1027 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,3);1028 }1029 else{1030 //make sure the files are actually there, in case of revert to snapshot or not persistent VMs files1031 //could have changed while test case was running1032 sendAgentCommand(foundMachine.host,foundMachine.port,{command:"files loaded",executionID:req.body.executionID},3,function(message){1033 if (message.loaded == true){1034 sendAgentCommand(foundMachine.host,foundMachine.port,agentInstructions,3);1035 }1036 else{1037 runBaseState();1038 }1039 });1040 }1041 }1042 else{1043 runBaseState();1044 }1045 });1046};1047//last action or we are done with the TC1048//start next test case if there is one1049function finishTestCaseExecution(execution,executionID,testcaseId,testcase){1050 //updateExecutionTestCase({_id:execution.testcases[testcase.executionTestCaseID]._id},{$set:{"status":"Finished",result:testcase.result.result}});1051 var date = new Date();1052 var status = "Finished";1053 if((testcase.result.result != "Passed") && (testcase.result.result != "Failed")){1054 status = "Not Run";1055 }1056 var updateTC = function(){1057 updateExecutionTestCase({_id:testcaseId},{$set:{trace:testcase.trace,"status":status,resultID:testcase.result._id.__id,result:testcase.result.result,error:testcase.result.error,enddate:date,runtime:date-testcase.testcase.startDate,host:"",vncport:""}});1058 executionsRoute.updateExecutionTotals(executionID);1059 };1060 //update machine base state result1061 if(execution.cachedTCs){1062 if (testcase.testcase.machines.length > 0){1063 updateExecutionMachine(executionID,testcase.testcase.machines[0]._id,testcase.result.result,testcase.result._id.__id,function(){1064 updateTC();1065 });1066 }1067 else{1068 updateTC();1069 }1070 //updateExecutionMachine({testcase.testcase.machines[0]._id)},{$set:{result:testcase.result.result,resultID:testcase.result._id.__id}});1071 }1072 else{1073 updateTC();1074 }1075 var count = 0;1076 var retry = false;1077 if(testcase.result.result == "Failed"){1078 if (execution.testcases[testcase.executionTestCaseID].retryCount > 0){1079 retry = true;1080 execution.testcases[testcase.executionTestCaseID].retryCount--;1081 execution.testcases[testcase.executionTestCaseID].executing = false;1082 execution.returnVars[testcase.executionTestCaseID] = {};1083 }1084 if (execution.cachedTCs){1085 execution.baseStateFailed = true;1086 }1087 }1088 if(testcase.testcase.machines.length === 0){1089 if (retry == false){1090 //delete execution.testcases[testcase.executionTestCaseID];1091 execution.testcases[testcase.executionTestCaseID].finished = true;1092 execution.testcases[testcase.executionTestCaseID].executing = false;1093 }1094 delete execution.currentTestCases[testcase.executionTestCaseID];1095 executeTestCases(execution.testcases,executionID);1096 return;1097 }1098 testcase.testcase.machines.forEach(function(machine){1099 delete machine.runningTC;1100 count++;1101 if (count == testcase.testcase.machines.length){1102 if (retry == false){1103 //delete execution.testcases[testcase.executionTestCaseID];1104 execution.testcases[testcase.executionTestCaseID].finished = true;1105 execution.testcases[testcase.executionTestCaseID].executing = false;1106 }1107 delete execution.currentTestCases[testcase.executionTestCaseID];1108 if(execution.executingTCs != true){1109 executeTestCases(execution.testcases,executionID);1110 }1111 }1112 });1113}1114exports.formatTrace = function(trace,sourceCache,callback){formatTrace(trace,sourceCache,callback)};1115function formatTrace(trace,sourceCache,callback){1116 var newTrace = "";1117 var traceCount = 0;1118 var traces = trace.split(",");1119 traces.forEach(function(line){1120 traceCount++;1121 var fileName = line.substring(line.indexOf("(")+1,line.indexOf(":"));1122 var lineNumber = line.substring(line.indexOf(":")+1,line.indexOf(")"));1123 var location = line.substring(0,line.indexOf("("));1124 location = location.trim();1125 location = location.replace("[","");1126 var count = 0;1127 var found = false;1128 if((location.indexOf("org.codehaus.") != -1) || (location.indexOf("java.lang.") != -1) || (location.indexOf("groovy.lang.") != -1) || (location.indexOf("redwood.launcher.") != -1)|| (location.indexOf("sun.reflect.") != -1)){1129 if (line.indexOf("[") == 0){1130 newTrace += line;1131 }1132 else{1133 newTrace += ",\r\n"+line;1134 }1135 if (traceCount == traces.length){1136 callback(newTrace);1137 }1138 return;1139 }1140 if ((fileName.indexOf(".groovy") != -1) ||(fileName.indexOf(".java") != -1)){1141 sourceCache.forEach(function(file){1142 if (found == true) return;1143 if (file.indexOf("/"+fileName) != -1){1144 if(location.replace(/\./g, '/').indexOf(file.substring(0,file.lastIndexOf("/")).replace("src/", '')) != -1){1145 found = true;1146 var parsedLineNumber = (parseInt(lineNumber,10)-1).toString();1147 newTrace += ",\r\n<a style= 'color: blue;' href='javascript:openScript(&quot;"+ file +"&quot;,&quot;"+ parsedLineNumber +"&quot;)'>" + line +"</a>";1148 }1149 }1150 count++;1151 if (count == sourceCache.length){1152 if (found == false){1153 if (line.indexOf("[") == 0){1154 newTrace += line;1155 }1156 else{1157 newTrace += ",\r\n"+line;1158 }1159 }1160 if (traceCount == traces.length){1161 callback(newTrace);1162 }1163 }1164 });1165 }1166 else{1167 if (traceCount == traces.length){1168 callback(newTrace);1169 }1170 }1171 });1172}1173function markFinishedResults(results,sourceCache,callback){1174 var count = 0;1175 var result = "Passed";1176 var status = "Finished";1177 results.forEach(function(action){1178 if (action.status == "Not Run"){1179 if (action.children.length != 0){1180 markFinishedResults(action.children,sourceCache,function(childStatus,childResult,markFinished){1181 action.expanded = false;1182 if (markFinished === true){1183 action.status = "Finished";1184 action.result = "Failed";1185 action.expanded = true;1186 callback("Finished","Failed",true);1187 return;1188 }1189 count++;1190 if (childStatus == "Not Run"){1191 status = "Not Run";1192 result = "";1193 }1194 if((childStatus == "Finished") && (childResult == "Failed")){1195 result = "Failed";1196 action.expanded = true;1197 }1198 if(count == action.children.length){1199 action.result = result;1200 action.status = status;1201 if (status == "Finished"){1202 if ((action.executionflow == "Record Error Stop Test Case")&&(result == "Failed")){1203 callback(status,result,true)1204 }1205 else{1206 callback(status,result)1207 }1208 }1209 else{1210 callback(status,"")1211 }1212 }1213 })1214 }1215 else{1216 callback("Not Run","")1217 }1218 }1219 else{1220 var returnValue;1221 if ((action.executionflow == "Record Error Stop Test Case")&&(action.result == "Failed")){1222 returnValue = function(){callback("Finished","Failed",true)}1223 }1224 else{1225 returnValue = function(){callback("Finished",action.result)}1226 }1227 if(action.result == "Failed"){1228 action.expanded = true;1229 }1230 if(action.trace){1231 formatTrace(action.trace,sourceCache,function(trace){1232 if(trace != "") action.trace = trace;1233 returnValue();1234 })1235 }1236 else{1237 returnValue();1238 }1239 }1240 });1241}1242exports.agentBaseState = function(project,executionID,agentHost,port,threadID,callback){agentBaseState(project,executionID,agentHost,port,threadID,callback)};1243function agentBaseState(project,executionID,agentHost,port,threadID,callback){1244 sendAgentCommand(agentHost,port,{command:"cleanup",executionID:executionID},3,function(message){1245 if (message.error){1246 callback(message.error);1247 return;1248 }1249 syncFilesWithAgent(agentHost,port,path.join(__dirname, '../public/automationscripts/'+project+"/bin"),"executionfiles/"+executionID+"/bin",function(error){1250 if(error) {callback(error);return}1251 syncFilesWithAgent(agentHost,port,path.join(__dirname, '../launcher'),"executionfiles/"+executionID+"/launcher",function(){1252 syncFilesWithAgent(agentHost,port,path.join(__dirname, '../public/automationscripts/'+project+"/External Libraries"),"executionfiles/"+executionID+"/lib",function(){1253 //syncFilesWithAgent(agentHost,port,path.join(__dirname, '../public/automationscripts/'+project+"/build/jar_"+executionID),"executionfiles/"+executionID+"/lib",function(){1254 syncFilesWithAgent(agentHost,port,os.tmpDir()+"/jar_"+executionID,"executionfiles/"+executionID+"/lib",function(){1255 sendAgentCommand(agentHost,port,{command:"start launcher",executionID:executionID,threadID:threadID},3,function(message){1256 if ((message) && (message.error)){1257 callback(message.error);1258 }1259 else{1260 callback();1261 }1262 });1263 })1264 })1265 })1266 });1267 });1268}1269function syncFilesWithAgent_old(agentHost,port,rootPath,destDir,callback){1270 var walker = walk.walkSync(rootPath);1271 var fileCount = 0;1272 walker.on("file", function (root, fileStats, next) {1273 fileCount++;1274 var path = root.replace(rootPath,"");1275 var dest = "";1276 if (path == ""){1277 dest = destDir +"/"+ fileStats.name;1278 }1279 else{1280 dest = destDir + path+"/"+fileStats.name1281 }1282 sendFileToAgent(root+"/"+fileStats.name,dest,agentHost,port,3,function(){1283 fileCount--;1284 if(fileCount == 0){1285 callback();1286 }1287 });1288 });1289 walker.on("end",function(){1290 if (fileCount == 0) callback();1291 });1292}1293function syncFilesWithAgent(agentHost,port,rootPath,destDir,callback){1294 var walker = walk.walkSync(rootPath);1295 var fileCount = 0;1296 var files = [];1297 var foundError = false;1298 var sendFiles = function(){1299 if(foundError) return;1300 fileCount++;1301 if (!files[fileCount-1]){1302 callback();1303 return;1304 }1305 sendFileToAgent(files[fileCount-1].file,files[fileCount-1].dest,agentHost,port,3,function(error){1306 if(error){1307 foundError = true;1308 callback(error);1309 return;1310 }1311 if(fileCount === files.length){1312 callback();1313 }1314 else{1315 sendFiles()1316 }1317 });1318 };1319 walker.on("file", function (root, fileStats, next) {1320 var path = root.replace(rootPath,"");1321 var dest = "";1322 if (path == ""){1323 dest = destDir +"/"+ fileStats.name;1324 }1325 else{1326 dest = destDir + path+"/"+fileStats.name1327 }1328 files.push({file:root+"/"+fileStats.name,dest:dest});1329 });1330 walker.on("end",function(){1331 sendFiles()1332 });1333}1334function sendFileToAgent(file,dest,agentHost,port,retryCount,callback){1335 var stat = fs.statSync(file);1336 var readStream = fs.createReadStream(file);1337 var boundary = '--------------------------';1338 for (var i = 0; i < 24; i++) {1339 boundary += Math.floor(Math.random() * 10).toString(16);1340 }1341 var message = '------' + boundary + '\r\n'1342 // use your file's mime type here, if known1343 + 'Content-Disposition: form-data; name="file"; filename="'+dest+'"\r\n'1344 + 'Content-Type: application/octet-stream\r\n'1345 // "name" is the name of the form field1346 // "filename" is the name of the original file1347 + 'Content-Transfer-Encoding: binary\r\n\r\n';1348 var options = {1349 hostname: agentHost,1350 port: port,1351 path: '/fileupload',1352 method: 'POST',1353 headers: {1354 //'Content-Type': 'text/plain'//,1355 'Content-Type': 'multipart/form-data; boundary=----'+boundary,1356 //'Content-Disposition': 'form-data; name="file"; filename="ProjectName.jar"',1357 //'Content-Length': 33601358 //'Content-Length': stat.size + message.length + 30 + boundary.length1359 'Content-Length': stat.size + message.length + boundary.length + 141360 }1361 };1362 var req = http.request(options, function(res) {1363 //res.setEncoding('utf8');1364 res.on('data', function (chunk) {1365 if (callback) callback();1366 });1367 });1368 req.on('error', function(e) {1369 if(retryCount <= 0){1370 if (callback) callback({error:'sendFileToAgent problem with request: ' + e.message+ ' file:'+file});1371 common.logger.error('sendFileToAgent problem with request: ' + e.message+ ' file:'+file);1372 }1373 else{1374 retryCount--;1375 setTimeout(sendFileToAgent(file,dest,agentHost,port,retryCount,callback),1000)1376 }1377 });1378 req.write(message);1379 readStream.pipe(req, { end: false });1380 readStream.on("end", function(){1381 req.end('\r\n------' + boundary + '--\r\n');1382 });1383}1384exports.sendAgentCommand = function(agentHost,port,command,retryCount,callback){sendAgentCommand(agentHost,port,command,retryCount,callback)};1385function sendAgentCommand(agentHost,port,command,retryCount,callback){1386 common.logger.info(command);1387 var options = {1388 hostname: agentHost,1389 port: port,1390 path: '/command',1391 method: 'POST',1392 headers: {1393 'Content-Type': 'application/json'1394 }1395 };1396 var req = http.request(options, function(res) {1397 res.setEncoding('utf8');1398 res.on('data', function (chunk) {1399 try{1400 var msg = JSON.parse(chunk);1401 }1402 catch(err){1403 if (callback) callback(err);1404 }1405 if((msg )&&(msg.error != null)){1406 if (callback) callback(msg.error);1407 }1408 else if (msg){1409 if(callback) callback(msg);1410 }1411 else{1412 if(callback) callback();1413 }1414 });1415 });1416 req.setTimeout(50000, function(){1417 //when timeout, this callback will be called1418 });1419 req.on('error', function(e) {1420 retryCount = 0;1421 if(retryCount <= 0){1422 if (callback) callback("Unable to connect to machine: "+agentHost + " error: " + e.message);1423 common.logger.error('sendAgentCommand problem with request: ' + e.message+ ' ');1424 }1425 else{1426 retryCount--;1427 setTimeout(sendAgentCommand(agentHost,port,command,retryCount,callback),1000)1428 }1429 });1430 // write data to request body1431 req.write(JSON.stringify(command));1432 req.end();1433}1434function resolveParamValue(value,variables){1435 var returnNULL = false;1436 if(Object.prototype.toString.call(value) == '[object Array]'){1437 if((value.length == 1) && (value[0] === "<NULL>")){1438 return [];1439 }1440 else{1441 return value;1442 }1443 }1444 else if (typeof value != "string"){1445 return value;1446 }1447 if (value.length > 4){1448 if ((value.indexOf("$\{") == 0) &&(value.charAt(value.length -1) == "}") && (value.split("}").length > 1))1449 {1450 returnNULL = true;1451 }1452 }1453 //var result = value.replace(new RegExp("\\$\\{([\\w_.-\\s*]+)\\}", "g" ),function(a,b){1454 var result = value.replace(new RegExp("\\$\\{([\\s\\w_.-]+)\\}", "g" ),function(a,b){1455 if(b in variables){1456 if (variables[b] == "<NULL>"){1457 if (returnNULL == true){1458 return "<NULL>"1459 }1460 else{1461 return "";1462 }1463 }1464 else{1465 return variables[b];1466 }1467 }1468 else{1469 return a;1470 }1471 });1472 return result;1473}1474function createResult(result,callback){1475 db.collection('testcaseresults', function(err, collection) {1476 result._id = db.bson_serializer.ObjectID(result._id);1477 collection.insert(result, {safe:true},function(err,returnData){1478 callback(returnData);1479 });1480 });1481}1482function updateResult(result,callback){1483 db.collection('testcaseresults', function(err, collection) {1484 collection.save(result,{safe:true},function(err){1485 if (callback){1486 callback(err);1487 }1488 //realtime.emitMessage("UpdateResult",result);1489 realtime.emitMessage("UpdateResult"+result._id.__id,result);1490 });1491 });1492}1493function updateExecutionTestCase(query,update,machineHost,vncport,callback){1494 db.collection('executiontestcases', function(err, collection) {1495 collection.findAndModify(query,{},update,{safe:true,new:true},function(err,data){1496 if (data == null){1497 if (callback){1498 callback(err);1499 }1500 return;1501 }1502 if (machineHost){1503 data.host = machineHost;1504 data.vncport = vncport;1505 }1506 realtime.emitMessage("UpdateExecutionTestCase",data);1507 if (callback){1508 callback(err);1509 }1510 });1511 });1512}1513function updateExecution(query,update,finished,callback){1514 db.collection('executions', function(err, collection) {1515 collection.findAndModify(query,{},update,{safe:true,new:true},function(err,data){1516 if(err) common.logger.error("ERROR updating execution: "+err);1517 realtime.emitMessage("UpdateExecutions",data);1518 if(finished === true){1519 realtime.emitMessage("FinishExecution",data);1520 }1521 if (callback){1522 callback(err);1523 }1524 });1525 });1526}1527function updateExecutionMachine(executionID,machineID,result,resultID,callback){1528 db.collection('executions', function(err, collection) {1529 collection.update({_id:executionID,machines:{$elemMatch:{_id:machineID}}},{$set:{'machines.$.result':result,'machines.$.resultID':resultID}},{safe:true,new:true},function(err,data){1530 //realtime.emitMessage("UpdateExecutions",data);1531 if (callback){1532 callback(err);1533 }1534 });1535 });1536}1537function cleanExecutionMachines(executionID,callback){1538 db.collection('executions', function(err, collection) {1539 collection.findOne({_id:executionID},function(err,data){1540 //realtime.emitMessage("UpdateExecutions",data);1541 if(data != null){1542 data.machines.forEach(function(machine){1543 machine.result = "";1544 machine.resultID = "";1545 });1546 collection.save(data,{safe:true},function(err){1547 //realtime.emitMessage("UpdateExecutions",data);1548 if (callback) callback();1549 });1550 }1551 });1552 });1553}1554function updateMachine(query,update,callback){1555 db.collection('machines', function(err, collection) {1556 collection.findAndModify(query,{},update,{safe:true,new:true},function(err,data){1557 if(data != null){1558 realtime.emitMessage("UpdateMachines",{_id:data._id,state:data.state,takenThreads:data.takenThreads});1559 }1560 if (callback) callback();1561 });1562 });1563}1564exports.verifyMachineState = function(machines,callback){verifyMachineState(machines,callback)};1565function verifyMachineState(machines,callback){1566 var machineCount = 0;1567 if(machines.length == 0 && callback) callback();1568 machines.forEach(function(machine){1569 db.collection('machines', function(err, collection) {1570 collection.findOne({_id:db.bson_serializer.ObjectID(machine._id)}, {}, function(err, dbMachine) {1571 if(dbMachine.maxThreads < dbMachine.takenThreads + machine.threads)1572 {1573 callback("Machine: "+ machine.host+" has reached thread limit.");1574 machineCount = machines.length+1;1575 return;1576 }1577 machineCount++;1578 if (machineCount == machines.length){1579 if(callback) callback();1580 }1581 });1582 });1583 });1584}1585function GetRunningExecution(callback){1586 db.collection('executions', function(err, collection) {1587 var executions = [];1588 collection.find({status:"Running"}, {}, function (err, cursor) {1589 cursor.each(function (err, execution) {1590 if (execution == null) {1591 callback(executions)1592 }1593 else{1594 executions.push(execution._id)1595 }1596 });1597 });1598 });1599}1600function VerifyCloudCapacity(template,callback){1601 if(template == null) {1602 callback({capacityAvailable:true});1603 return;1604 }1605 db.collection('hosts', function(err, collection) {1606 var hosts = [];1607 collection.find({}, {}, function(err, cursor) {1608 cursor.each(function(err, host) {1609 if(host == null) {1610 if(hosts.length == 0){1611 callback({err:"Error: No hosts are found."});1612 return1613 }1614 GetRunningExecution(function(runningExecutions){1615 var appDir = path.resolve(__dirname,"../")+"/";1616 var proc = spawn(appDir+"vendor/Java/bin/java",["-cp",appDir+'utils/lib/*;'+appDir+'vendor/groovy/*;'+appDir+'utils/*',"com.primatest.cloud.Main",JSON.stringify({operation:"capacityValidation",hosts:hosts,totalInstances:template.instances,templateName:template.name,runningExecutions:runningExecutions})]);1617 var cache = "";1618 proc.stdout.on('data', function (data) {1619 cache = cache + data.toString();1620 //console.log(data.toString());1621 });1622 proc.stderr.on('data', function (data) {1623 common.logger.error('Cloud stderr: ' + data.toString());1624 });1625 proc.on('close', function (code) {1626 var response = JSON.parse(cache);1627 callback(response);1628 });1629 })1630 }1631 hosts.push(host);1632 });1633 })1634 });1635}1636function StartCloudMachines(template,executionID,callback){1637 if(template == null) {1638 callback([]);1639 return;1640 }1641 db.collection('hosts', function(err, collection) {1642 var hosts = [];1643 collection.find({}, {}, function(err, cursor) {1644 cursor.each(function(err, host) {1645 if(host == null) {1646 if(hosts.length == 0){1647 callback({err:"Error: No hosts are found."});1648 return1649 }1650 var appDir = path.resolve(__dirname,"../")+"/";1651 //console.log(appDir+"vendor/Java/bin/java "+"-cp "+appDir+'utils/lib/*;'+appDir+'vendor/groovy/*;'+appDir+'utils/* '+"com.primatest.cloud.Main \""+JSON.stringify({operation:"capacityValidation",hosts:hosts,totalInstances:totalInstances}).replace(/"/g,'\\"')+'"');1652 //console.log({operation:"startCloudMachines",hosts:hosts,template:template});1653 var proc = spawn(appDir+"vendor/Java/bin/java",["-cp",appDir+'utils/lib/*;'+appDir+'vendor/groovy/*;'+appDir+'utils/*',"com.primatest.cloud.Main",JSON.stringify({operation:"startCloudMachines",hosts:hosts,template:template,executionID:executionID})]);1654 var cache = "";1655 proc.stdout.on('data', function (data) {1656 //console.log(data.toString());1657 cache = cache + data.toString();1658 });1659 proc.stderr.on('data', function (data) {1660 common.logger.error('Cloud stderr: ' + data.toString());1661 //callback({err:data.toString()})1662 });1663 /*1664 proc.on('exit', function (code) {1665 var response = JSON.parse(cache);1666 if(response.err){1667 callback(response);1668 }1669 });1670 */1671 proc.on('close', function (code) {1672 var response = JSON.parse(cache);1673 if(response.err){1674 callback(response);1675 return;1676 }1677 var cloudMachines = [];1678 response.forEach(function(machine,index){1679 cloudMachines.push({1680 host:machine.IP,1681 machineVars:[],1682 maxThreads:1000,1683 port:5009,1684 result:"",1685 roles:["Default"],1686 threads:template.threads,1687 vncport: 3006,1688 cloud:true,1689 vmName:machine.vmName,1690 vmHost:machine.host,1691 template:template.name1692 });1693 if(index == response.length-1){1694 callback(cloudMachines)1695 }1696 });1697 });1698 return;1699 }1700 else{1701 hosts.push(host);1702 }1703 });1704 })1705 });1706}1707function lockMachines(machines,executionID,callback){1708 var machineCount = 0;1709 if(machines.length == 0 && callback) callback();1710 machines.forEach(function(machine){1711 updateExecutionMachine(executionID,machine._id,"","");1712 db.collection('machines', function(err, collection) {1713 collection.findOne({_id:db.bson_serializer.ObjectID(machine._id)}, {}, function(err, dbMachine) {1714 if(dbMachine.maxThreads < dbMachine.takenThreads + machine.threads)1715 {1716 callback("Machine: "+ machine.host+" has reached thread limit.");1717 return;1718 }1719 if(dbMachine != null) {1720 var takenThreads = 1;1721 if (dbMachine.takenThreads){1722 takenThreads = dbMachine.takenThreads + machine.threads;1723 }1724 else{1725 takenThreads = machine.threads;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { Redwood } = require('@redwood.dev/client');2const redwood = new Redwood();3const updateExecutionMachine = async () => {4 try {5 const res = await redwood.updateExecutionMachine({6 executionMachine: {

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var config = require('./config.json');3var redwoodClient = new redwood(config);4redwoodClient.updateExecutionMachine('machine1', {status: 'active'}, function(err, res) {5 if (err) {6 console.log('error: ' + err);7 } else {8 console.log('result: ' + JSON.stringify(res));9 }10});11{12}13var redwood = require('redwood');14var redwoodClient = new redwood(config);15redwoodClient.getExecutionMachines(callback)16redwoodClient.getExecutionMachine(machineId, callback)17redwoodClient.updateExecutionMachine(machineId, data, callback)18redwoodClient.getExecutionMachineLogs(machineId, callback)19redwoodClient.getExecutionMachineLog(machineId, logId, callback)

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run redwood automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful