CiscoTool_v1/netmiko_Api/utils/manualTest/manualTest.js

344 lines
11 KiB
JavaScript

const fs = require("fs");
const Device = require("../../models/DeviceModel");
const { detectFaulty } = require("../autoTest/detectFaulty");
const { getInfoLog } = require("../autoTest/getInfoLog");
const { spawn } = require("child_process");
const { sendNoteToERP } = require("./addNoteToERP");
const manual_test = async (
client, date, time, body
) => {
try {
const { infoStation, listLine, stream, topic, valueConfirmPhysical } = body;
let checkTest = false;
const manualTest = spawn(
"python3",
[
"./utils/manualTest/manualTest.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
listLine.line_port,
],
{ timeout: 120000 }
);
manualTest.stdout.on("data", async (data) => {
data =
// 'NAME: "Virtual Private Network (VPN) Module on Slot 0", DESCR: "Encryption AIM Element"\nPID: AIM-VPN/SSL-3 , VID: V01, SN: FOC13293U2B\n'+
data.toString();
// +"\ninternal power supply poe -48v voltage status: fail";// console.log(data)
checkTest = true;
let PID = data
.split("\n")
.filter(
(i) =>
i.search("PID:") !== -1 &&
i.search("VID:") !== -1 &&
i.search("SN:") !== -1
)[0]
?.split("VID:")[0]
?.split(":")[1]
.replace(/,/g, "")
?.trim();
let VID = data
.split("\n")
.filter(
(i) =>
i.search("PID:") !== -1 &&
i.search("VID:") !== -1 &&
i.search("SN:") !== -1
)[0]
?.split("VID:")[1]
?.split("SN:")[0]
.replace(",", "")
.trim();
let SN = data
.split("\n")
.filter(
(i) =>
i.search("PID:") !== -1 &&
i.search("VID:") !== -1 &&
i.search("SN:") !== -1
)[0]
?.split("SN:")[1]
?.trim();
fs.writeFile(time, data, async function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
if (data.search("xxError") !== -1) {
//test error
let params = {
type: "stream",
to: stream,
topic: topic,
content:
":red_circle: Line " +
listLine.line_number +
" manual test error. Please check!",
};
const result = await client.messages.send(params);
fs.writeFile(time, data, async function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
date,
"manual",
"",
""
);
if (
valueConfirmPhysical !== "" &&
valueConfirmPhysical.option !== null
) {
setTimeout(async () => {
let res = await Device.getLogByCreatedAt(date);
if (res.pData[0] !== undefined) {
if (valueConfirmPhysical.option === "Issue") {
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option +
": " +
valueConfirmPhysical.value
);
} else {
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option
);
}
}
}, 3000);
}
} else {
//send message to zulip
if (data.length > 30) {
// console.log(data)
await Device.addLog(
listLine.id_line,
time,
"DPELP",
date,
"manual",
PID !== undefined ? PID : "",
SN !== undefined ? SN : ""
);
if (
valueConfirmPhysical !== "" &&
valueConfirmPhysical.option !== null
) {
let testLog = "";
setTimeout(async () => {
let res = await Device.getLogByCreatedAt(date);
if (res.pData[0] !== undefined) {
if (valueConfirmPhysical.option === "Issue") {
testLog = testLog + "Issue:\n" + valueConfirmPhysical.value;
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option +
": " +
valueConfirmPhysical.value
);
} else {
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option
);
testLog = testLog + valueConfirmPhysical.option;
}
}
await sendNoteToERP(
PID,
VID,
SN,
testLog,
infoStation.sta_name.search("AU") !== -1 ? "AU" : "US"
);
}, 3000);
}
const faulty = await detectFaulty(data); //faulty.fail.length > 20 ---> ton tai loi
const fail = faulty.fail.length > 20 ? faulty.fail : "not found";
const RAM_FLASH = await getInfoLog(data);
const itemExtra =
faulty.modelCheck !== ""
? "\n- " + faulty.PIDSpecial.join("\n- ")
: "not found";
let shortLog = [];
let shortConfig = data
.split("\n")
?.slice(
data
.split("\n")
?.indexOf(
data
.split("\n")
?.filter(
(i) =>
i.search("bytes of") !== -1 && i.search("memory") !== -1
)[0]
),
data
.split("\n")
?.indexOf(
data
.split("\n")
?.filter(
(i) =>
i
.toLocaleLowerCase()
.search("configuration register is") !== -1
)[0]
) + 1
);
data.split("\n")?.map((u, index) => {
if (
(u.search("PID:") !== -1 &&
u.search("VID:") !== -1 &&
u.search("SN:") !== -1) ||
(u.toLocaleLowerCase().search("descr:") !== -1 &&
u.toLocaleLowerCase().search("name:") !== -1) ||
u.toLocaleLowerCase().search("system image file") !== -1
) {
shortLog.push(u);
}
});
const title =
valueConfirmPhysical === ""
? "**Line " + listLine.line_number + ": Test Report Summary**\n**"
: valueConfirmPhysical.option === "Issue"
? "**Line " +
listLine.line_number +
": Test Report Summary**\n\n**Physical test: :warning: ISSUE:** \n" +
valueConfirmPhysical.value +
"\n\n**"
: "**Line " +
listLine.line_number +
": Test Report Summary**\n\n**Physical test: :check: " +
valueConfirmPhysical.option +
"**\n\n**";
const content =
title +
data
.split("\n")
.filter(
(i) =>
i.search("PID:") !== -1 &&
i.search("VID:") !== -1 &&
i.search("SN:") !== -1
)[0] +
"**\n\n" +
RAM_FLASH.split("\n").filter((i) => i.search("RAM: ") !== -1)[0] +
" - " +
RAM_FLASH.split("\n")?.filter(
(i) => i.search("Flash: ") !== -1
)[0] +
"\n\nExtra item: " +
itemExtra +
"\n\nAuto detect issue: \n```\n" +
fail +
"\n```\n\nShorten log: - [View all](" +
process.env.SOCKET_SERVER +
"/api/log?name=" +
time.split("/")[time.split("/").length - 1] +
")\n````\n" +
shortLog.join("\n") +
"\n" +
shortConfig.join("\n") +
"\n````";
let params = {
type: "stream",
to: stream,
topic: topic,
content: content,
};
const result = await client.messages.send(params);
}
}
});
manualTest.stderr.on("data", async (data) => {
//test error
data = data.toString();
console.log(data);
checkTest = true;
let params = {
type: "stream",
to: stream,
topic: topic,
content:
":red_circle: Line " +
listLine.line_number +
" manual test error. Please check!",
};
const result = await client.messages.send(params);
fs.writeFile(time, data, async function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
date,
"manual",
"",
""
);
if (valueConfirmPhysical !== "" && valueConfirmPhysical.option !== null) {
setTimeout(async () => {
let res = await Device.getLogByCreatedAt(date);
if (res.pData[0] !== undefined) {
if (valueConfirmPhysical.option === "Issue") {
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option + ": " + valueConfirmPhysical.value
);
} else {
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option
);
}
}
}, 3000);
}
});
setTimeout(async () => {
if (checkTest === false) {
//check timeout
let params = {
type: "stream",
to: stream,
topic: topic,
content:
listLine.line_number + ". Manual test time out. Please check!",
};
const result = await client.messages.send(params);
}
}, 150000);
} catch (error) {
console.log(error);
}
};
module.exports.manual_test = manual_test;