From f559b653f227dd21ff01aa958612d2f4a7517fd3 Mon Sep 17 00:00:00 2001 From: Dunemask Date: Thu, 7 Jul 2022 21:45:38 +0000 Subject: [PATCH] Added dev reporting --- dev/suite/failing.js | 4 +++ dev/suite/primary.js | 4 +++ dev/suite/runner.js | 47 ++++++++++++++++++++++++++++++++ dev/suite/secondary.js | 4 +++ dev/suite/single.js | 4 +++ lib/core/JobManager.js | 4 ++- lib/core/job-builder.js | 59 +++++++++++++++++++++++++++++++++++++++++ lib/core/server.js | 3 ++- lib/routes/dev-route.js | 14 ++++++++++ tests/index.js | 14 +++------- 10 files changed, 145 insertions(+), 12 deletions(-) create mode 100644 dev/suite/failing.js create mode 100644 dev/suite/primary.js create mode 100644 dev/suite/runner.js create mode 100644 dev/suite/secondary.js create mode 100644 dev/suite/single.js create mode 100644 lib/core/job-builder.js create mode 100644 lib/routes/dev-route.js diff --git a/dev/suite/failing.js b/dev/suite/failing.js new file mode 100644 index 0000000..99fb0f3 --- /dev/null +++ b/dev/suite/failing.js @@ -0,0 +1,4 @@ +export default function failingTest(){ + console.log("This came from a failing test!"); + return {status:1}; +} \ No newline at end of file diff --git a/dev/suite/primary.js b/dev/suite/primary.js new file mode 100644 index 0000000..d0cc4a4 --- /dev/null +++ b/dev/suite/primary.js @@ -0,0 +1,4 @@ +export default function primaryTest(){ + console.log("This came from a primary test!"); + return {status:0, pipelineData:"SomeData"}; +} \ No newline at end of file diff --git a/dev/suite/runner.js b/dev/suite/runner.js new file mode 100644 index 0000000..17168ab --- /dev/null +++ b/dev/suite/runner.js @@ -0,0 +1,47 @@ +import axios from "axios"; +import primary from "./primary.js"; +import secondary from "./secondary.js"; +import single from "./single.js"; +import failing from "./failing.js"; + +// Constants +const endLiveCount = 2; +const reportingUrl = `${process.env.QUALITEER_URL}/api/dev/rabbit/TestResults`; +// Pull args +const args = process.argv.slice(2); +const test = (args.find((v)=>v.includes("test=")) ?? "").replace("test=",""); +const pipelineData = (args.find((v)=>v.includes("pipelineData=")) ?? "").replace("pipelineData=",""); +const pipelineLife = parseInt((args.find((v)=>v.includes("pipelineLife=")) ?? "0").replace("pipelineLife=","")); +const pipelineDashboardSocket = (args.find((v)=>v.includes("pipelineDashboardSocket=")) ?? "").replace("pipelineDashboardSocket=","") || undefined; + +const logNow = () => console.log(Date.now()); +const liveIndicator = () => { + for (var i = 0; i < endLiveCount; i++) setTimeout(logNow, i * 1000); +} + +const runTests = () => { + switch (test) { + case "primary": + return primary(); + case "secondary": + return secondary(pipelineData); + case "single": + return single(); + case "failing": + return failing(); + default: + return single(); + } +} + +// Run +liveIndicator(); +setTimeout(()=>{ +const status = runTests(); +const testResult = {...status, name:test, pipelineLife, pipelineDashboardSocket} +axios.post(reportingUrl, {testResult}).catch((e)=>{console.log(e.response.status)}); +},endLiveCount * 1000); + + + + diff --git a/dev/suite/secondary.js b/dev/suite/secondary.js new file mode 100644 index 0000000..b83cd69 --- /dev/null +++ b/dev/suite/secondary.js @@ -0,0 +1,4 @@ +export default function secondaryTest(pipelineData){ + console.log("This came from a secondary test!"); + return {status: + (pipelineData !== "SomeData")}; +} \ No newline at end of file diff --git a/dev/suite/single.js b/dev/suite/single.js new file mode 100644 index 0000000..3d05a6c --- /dev/null +++ b/dev/suite/single.js @@ -0,0 +1,4 @@ +export default function singleTest(){ + console.log("This came from a single test!"); + return {status:0}; +} \ No newline at end of file diff --git a/lib/core/JobManager.js b/lib/core/JobManager.js index 84857fc..579e2a7 100644 --- a/lib/core/JobManager.js +++ b/lib/core/JobManager.js @@ -1,5 +1,6 @@ import { v4 } from "uuid"; import applyJob from "./kubernetes.js"; +import buildJob from "./job-builder.js"; const maxJobs = process.env.MAX_JOBS ? parseInt(process.env.MAX_JOBS) : 3; @@ -41,7 +42,8 @@ class JobManager { this.clientMaxJobs ) throw Error("Client's Active Jobs Exceeded!"); - const job = { ...jobRequest }; + + const job = buildJob(jobRequest, id); job.id = v4(); job.log = []; this.clients[id].jobs.push(job); diff --git a/lib/core/job-builder.js b/lib/core/job-builder.js new file mode 100644 index 0000000..7ac1233 --- /dev/null +++ b/lib/core/job-builder.js @@ -0,0 +1,59 @@ +const baseCommand = "node"; +const suiteEntry = "dev/suite/runner.js"; +const pipelineMapping = [ + { + id: 0, + pipeline: [{ name: "primary" }, { name: "secondary", delay: 5000 }], + }, +]; + +const buildCommon = (jobRequest) => { + const { testName } = jobRequest; + if (!testName) throw Error("'testName' must be provided!"); + const command = [baseCommand, suiteEntry, `test=${testName}`]; + + // Apply Common Flags + command.push("isRetry=false"); + + // Return new request + return { ...jobRequest, command }; +}; + +const buildSingle = (jobReq) => jobReq; + +const buildMarker = (jobReq) => {}; + +const buildProject = (jobReq) => {}; + +const pipelineMaxLife = (testName) => { + const pipelines = pipelineMapping + .filter((m) => m.pipeline.find((t) => t.name === testName)) + .map((m) => m.pipeline); + return Math.max(pipelines.map((p) => p.length)) + 1; +}; + +const buildCompound = (jobReq, socketId) => { + const { testName, command } = jobReq; + const pipelineLife = jobReq.pipelineLife ?? pipelineMaxLife(testName); + command.push(`pipelineLife=${pipelineLife}`); + command.push(`pipelineDashboardSocket=${socketId}`); + return { ...jobReq, command }; +}; + +const nextCompound = (previousTest) => {}; + +export default function jobBuilder(jobRequest, id) { + const jobReq = buildCommon(jobRequest, id); + switch (jobRequest.type) { + case "single": + return buildSingle(jobReq); + case "marker": + return buildMarker(jobReq); + case "project": + return buildProject(jobReq); + case "compound": + return buildCompound(jobReq, id); + default: + throw Error("No Job Request Type Specified!"); + } +} diff --git a/lib/core/server.js b/lib/core/server.js index de78cf1..32b01bb 100644 --- a/lib/core/server.js +++ b/lib/core/server.js @@ -9,11 +9,12 @@ import catalog from "../routes/catalog-route.js"; import jobs from "../routes/jobs-route.js"; import mock from "../routes/mock-route.js"; - +import dev from "../routes/dev-route.js"; const app = express(); // Special Routes app.all("/", (req, res) => res.redirect("/qualiteer")); if (process.env.MOCK_ROUTES === "true") app.use(mock); +if(process.env.USE_DEV_ROUTER === "true") app.use("/api/dev",dev); // Middlewares diff --git a/lib/routes/dev-route.js b/lib/routes/dev-route.js new file mode 100644 index 0000000..68d1358 --- /dev/null +++ b/lib/routes/dev-route.js @@ -0,0 +1,14 @@ +import { Router, json as jsonMiddleware } from "express"; +import TestResultsWorker from "../rabbit/workers/TestResultsWorker.js"; + +const testResultsHandler = new TestResultsWorker(); + +const router = Router(); +router.use(jsonMiddleware()); +router.post("/rabbit/TestResults", (req, res) => { + const { testResult } = req.body; + testResultsHandler.onMessage(testResult); + res.sendStatus(200); +}); + +export default router; diff --git a/tests/index.js b/tests/index.js index ba875dc..d554929 100644 --- a/tests/index.js +++ b/tests/index.js @@ -7,22 +7,16 @@ import { Initiator, Executor } from "qualiteer/clients"; const qltr = new Qualiteer(); await qltr.start(); -const url = "https://Qualiteer.elijahparker3.repl.co"; +const url = process.env.QUALITEER_URL; // Create an initiator and make a job request const primary = new Initiator(url); const job = { - command: ["node", "dev/other.js"], + type: "compound", + testName: "primary", + pipelineLife: 1, name: "testing", image: "node", }; await primary.newJob(job, null, () => console.log("Primary Job Concluded")); -/*const { clients } = qltr.jobs; -const skId = Object.keys(clients)[0]; -const { jobs } = clients[skId]; -const serverJob = jobs[0]; - -const exec = new Executor(url, serverJob); -exec.runJob(); -*/