Commit 2b8d088e authored by Renato Figueiro Maia's avatar Renato Figueiro Maia
Browse files

Inclusão do eslint+prettier para validação do código JavaScript.

[SOMA-7005][SOMA-6957]
parent ea3b8943
{
"env": {
"node": true,
"jasmine": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
},
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
}
}
......@@ -18,9 +18,17 @@
"author": "Renato Maia",
"license": "ISC",
"devDependencies": {
"eslint": "^7.15.0",
"eslint-config-prettier": "^7.0.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.0",
"eslint-plugin-promise": "^4.2.1",
"express": "^4.17.1",
"frisby": "^2.1.3",
"jasmine": "^3.6.3",
"jasmine-expect": "^5.0.0"
"jasmine-expect": "^5.0.0",
"prettier": "2.2.1"
}
}
"use strict";
const events = require('events');
const events = require("events");
const frisby = require("frisby");
const utils = require('./utils');
const utils = require("./utils");
describe("Started SGA", function() {
describe("Started SGA", function () {
afterEach(utils.clearAllResources);
it('should terminate on shutdown after registration', async () => {
it("should terminate on shutdown after registration", async () => {
const config = utils.fillSgaConfig();
const mock = new utils.RegistryService(config);
const sga = new utils.SgaDaemon(config);
......@@ -23,10 +23,13 @@ describe("Started SGA", function() {
expect(exitVal).toEqual(0);
});
it('should retry to register when server is down', async () => {
const mockPort = 55555
it("should retry to register when server is down", async () => {
const mockPort = 55555;
const mockBaseUri = `http://localhost:${mockPort}`;
const config = utils.fillSgaConfig({csbase_server: mockBaseUri});
const config = utils.fillSgaConfig({
csbase_server: mockBaseUri,
register_retry_s: 0.1,
});
const mock = new utils.RegistryService(config, undefined, mockPort);
const sga = new utils.SgaDaemon(config);
......
"use strict";
const child_process = require('child_process');
const events = require('events');
const express = require('express');
const fs = require('fs');
const os = require('os');
const path = require('path');
const util = require('util');
const child_process = require("child_process");
const events = require("events");
const express = require("express");
const fs = require("fs");
const os = require("os");
const path = require("path");
const testDir = fs.mkdtempSync(path.join(os.tmpdir(), "sga_test-"));
const sgaConfig = {
......@@ -15,24 +14,20 @@ const sgaConfig = {
sgad_host: "localhost",
sgad_port: 65432,
sga_name: "SgaForUnitTest",
status_interval_s: .1,
exec_polling_interval_s: .1,
register_retry_s: .1,
status_interval_s: 0.1,
exec_polling_interval_s: 0.1,
register_retry_s: 0.1,
project_root_dir: path.join(testDir, "projects"),
algorithm_root_dir: path.join(testDir, "algorithms"),
runtime_data_dir: path.join(testDir, "runtime"),
sandbox_root_dir: path.join(testDir, "sandbox"),
driver: "sga.driver.posix",
resources: [
"some resource",
"another resource",
"yet another resource"
],
resources: ["some resource", "another resource", "yet another resource"],
extra_config: {
some_config: "some configuration",
another_config: "another configuration",
yet_another_config: "yet another configuration"
}
yet_another_config: "yet another configuration",
},
};
function fillSgaConfig(values) {
......@@ -46,30 +41,36 @@ function fillSgaConfig(values) {
function writeAsLua(stream, value, prefix = "") {
switch (typeof value) {
case "number":
stream.write(value.toString());
{
stream.write(value.toString());
}
break;
case "string":
stream.write(JSON.stringify(value));
{
stream.write(JSON.stringify(value));
}
break;
case "object":
const newPrefix = prefix + " ";
stream.write("{\n");
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
stream.write(newPrefix);
writeAsLua(stream, value[i], newPrefix);
stream.write(",\n");
}
} else {
for (let property in value) {
stream.write(newPrefix);
stream.write(`${property} = `);
writeAsLua(stream, value[property], newPrefix);
stream.write(",\n");
{
const newPrefix = prefix + " ";
stream.write("{\n");
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
stream.write(newPrefix);
writeAsLua(stream, value[i], newPrefix);
stream.write(",\n");
}
} else {
for (let property in value) {
stream.write(newPrefix);
stream.write(`${property} = `);
writeAsLua(stream, value[property], newPrefix);
stream.write(",\n");
}
}
stream.write(prefix);
stream.write("}");
}
stream.write(prefix);
stream.write("}");
break;
default:
throw new TypeError("unable to write as Lua value " + value);
......@@ -99,23 +100,26 @@ class SgaDaemon {
await events.once(configFile, "finish");
configFile.close();
const sgaOutput = fs.createWriteStream(path.join(testDir, "sgad.out"),
{ flags: "a" });
const sgaOutput = fs.createWriteStream(path.join(testDir, "sgad.out"), {
flags: "a",
});
await events.once(sgaOutput, "open");
// TODO: find a better way to find the SGA deploy to be tested.
const sgaHome = "/home/maia/.luaenv/versions/sga"
const sgaLuaVer = "5.2"
const sgaLuaBin = `${sgaHome}/bin/lua`
const sgaLuaPath = `${sgaHome}/share/lua/${sgaLuaVer}/?.lua;${sgaHome}/share/lua/${sgaLuaVer}/?/init.lua`
const sgaCPath = `${sgaHome}/lib/lua/${sgaLuaVer}/?.so`
const sgaHome = "/home/maia/.luaenv/versions/sga";
const sgaLuaVer = "5.2";
const sgaLuaBin = `${sgaHome}/bin/lua`;
const sgaLuaPath = `${sgaHome}/share/lua/${sgaLuaVer}/?.lua;${sgaHome}/share/lua/${sgaLuaVer}/?/init.lua`;
const sgaCPath = `${sgaHome}/lib/lua/${sgaLuaVer}/?.so`;
const sgaRocksSetup = `package.path = '../?.lua;${sgaLuaPath};'..package.path
package.cpath = '${sgaCPath};'..package.cpath`
package.cpath = '${sgaCPath};'..package.cpath`;
this.process = child_process.spawn(sgaLuaBin,
this.process = child_process.spawn(
sgaLuaBin,
["-e", sgaRocksSetup, "../sgad.lua", configPath],
{ stdio: [ "ignore", sgaOutput, sgaOutput ] });
this.process.on('exit', (exitVal, signalName) => {
{ stdio: ["ignore", sgaOutput, sgaOutput] }
);
this.process.on("exit", (exitVal, signalName) => {
sgaDaemons.delete(this);
this.process = undefined;
this.config = undefined;
......@@ -139,7 +143,7 @@ class SgaDaemon {
if (this.process) {
return events.once(this.process, "exit");
}
return new Promise(res => res([this.exitVal, this.signalName]));
return new Promise((res) => res([this.exitVal, this.signalName]));
}
}
......@@ -149,27 +153,27 @@ const registryEndpoints = [
{
httpMethod: "post",
endpoint: "/v1/sga",
funcName: "register"
funcName: "register",
},
{
httpMethod: "post",
endpoint: "/mock/completion",
funcName: "completion"
funcName: "completion",
},
{
httpMethod: "post",
endpoint: "/mock/status",
funcName: "status"
funcName: "status",
},
{
httpMethod: "get",
endpoint: "/mock/heartbeat",
funcName: "heartbeat"
}
funcName: "heartbeat",
},
];
class RegistryService {
constructor(sgaConfig, heartbeatInteval = .1, port = 0) {
constructor(sgaConfig, heartbeatInteval = 0.1, port = 0) {
this.sgaConfig = sgaConfig;
this.listenPort = port;
this.regReply = { actions: {} };
......@@ -184,7 +188,7 @@ class RegistryService {
this.events.emit(funcName, req, res);
const method = this[funcName];
return method.call(this, req, res);
})
});
this.regReply.actions[funcName] = { uri: spec.endpoint };
}
this.regReply.actions.heartbeat.interval_s = heartbeatInteval;
......@@ -219,7 +223,7 @@ class RegistryService {
if (this.timestamps[name]) {
const delay = now - this.timestamps[name];
expect(delay).toBeGreaterThan(value * NS_PER_SEC);
expect(delay).toBeLessThan((value + .5) * NS_PER_SEC);
expect(delay).toBeLessThan((value + 0.5) * NS_PER_SEC);
}
this.timestamps[name] = now;
}
......@@ -236,8 +240,12 @@ class RegistryService {
expect(body.actions.paths).toBeString();
expect(body.actions.job).toBeString();
expect(body.actions.shutdown).toBeString();
expect(body.resources).toEqual(jasmine.objectContaining(this.sgaConfig.resources));
expect(body.extra_config).toEqual(jasmine.objectContaining(this.sgaConfig.extra_config));
expect(body.resources).toEqual(
jasmine.objectContaining(this.sgaConfig.resources)
);
expect(body.extra_config).toEqual(
jasmine.objectContaining(this.sgaConfig.extra_config)
);
this.timestamps.heartbeat = process.hrtime.bigint();
res.status(201).json(this.regReply);
......@@ -245,7 +253,10 @@ class RegistryService {
heartbeat(req, res) {
expect(this.timestamps.heartbeat).toBeDefined();
this.expectInterval("heartbeat", this.regReply.actions.heartbeat.interval_s);
this.expectInterval(
"heartbeat",
this.regReply.actions.heartbeat.interval_s
);
res.status(200).end();
}
......@@ -270,9 +281,10 @@ async function clearAllResources() {
}
}
const waitMsec = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
const waitMsec = (delay) =>
new Promise((resolve) => setTimeout(resolve, delay));
exports.waitMsec = waitMsec
exports.waitMsec = waitMsec;
exports.fillSgaConfig = fillSgaConfig;
exports.SgaDaemon = SgaDaemon;
exports.RegistryService = RegistryService;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment